东方财富 自动止损程序

自动止损,加一个5秒执行的循环语句,自动止损。下周跑跑看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# encoding: utf-8
 
#@author: 东哥加油!!!
#@file: dfcf.py
#@time: 2019/1/1 15:47
 
 
 
import requests
import requests.packages.urllib3.exceptions
import json
import http
import time
import urllib.parse
from com.stock_df import stock
import logging
import random
from PIL import Image
import pytesseract
import re
 
from lxml import etree
 
 
class stock():
    def __init__(self,Zqdm,Zqmc,Zqsl,Kysl,Cbjg,Zxjg,Ykbl):
 
        self.Zqdm = Zqdm             #证券编码
        self.Zqmc = Zqmc             #证券名称
        self.Zqsl = int(Zqsl)        #持仓数量
        self.Kysl = int(Kysl)        #可用数量
        self.Cbjg = float(Cbjg)      #成本价
        self.Zxjg = float(Zxjg)      #当前价
        self.Ykbl = float(Ykbl)      #盈亏比例
 
 
 
class dfcf():
    def __init__(self):
        self.sess = requests.session()
        requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
        self.headers = {
            'Host': 'jy.xzsec.com',
            'Connection': 'keep - alive',
            'Accept': 'application/json, text/plain, */*',
            'Origin': 'https://jy.xzsec.com',
             'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Referer': 'https://jy.xzsec.com/Search/Position',
        }
        self.validatekey=''
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(level=logging.INFO)
        handler = logging.FileHandler("stock.log")
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter('%(asctime)s:%(message)s')
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
 
        #止损比
        self.zqbl = -0.02
        #止盈比
        self.zybl = 0.04
 
    #登录并获取cookie
    def dfcf_login(self):
 
        randNumber=random.random()-0.00000000000000009
        headers = {
            'Host': 'jy.xzsec.com',
            'Connection': 'keep - alive',
            'Accept': 'application/json, text/plain, */*',
            'Origin': 'https://jy.xzsec.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Referer': 'https://jy.xzsec.com/',
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        url = 'https://jy.xzsec.com/Login/YZM?randNum='+str(randNumber)
        response = self.sess.get(url,headers=headers,verify=False)
        with  open('dfyzm.png', 'wb') as file:
            file.write(response.content)
        image = Image.open('dfyzm.png')
 
        identifyCode = input("输入验证码: ")
        url = 'https://jy.xzsec.com/Login/Authentication?validatekey='
        headers = {
            'Host': 'jy.xzsec.com',
            'Connection': 'keep - alive',
            'Accept': 'application/json, text/plain, */*',
            'Origin': 'https://jy.xzsec.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Referer': 'https://jy.xzsec.com/Login?el=1&clear=1',
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        data={
              'userId':'账号',
              'password':'密码',
              'randNumber':randNumber,
              'identifyCode':identifyCode,
              'duration':1800,
              'authCode':'',
              'type':'Z'
              }
 
        response = self.sess.post(url, data=data, headers=headers, verify=False)
 
        c = requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取
        c.set('cookie-name', 'cookie-value')
        self.sess.cookies.update(c)
 
        headers = {
            'Host': 'jy.xzsec.com',
            'Connection': 'keep - alive',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Referer': 'https://jy.xzsec.com/Login?el=1&clear=1',
            'Upgrade-Insecure-Requests': '1',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
        }
 
        url = 'https://jy.xzsec.com/Trade/Buy'
        response = self.sess.get(url,headers=headers, verify=False)
        html = etree.HTML(response.text)
        em_validatekey = html.xpath('//input[@id="em_validatekey"]')[0].get('value')
        print(em_validatekey)
        self.validatekey = em_validatekey
 
 
    def get_stocklist(self):
        url = 'https://jy.xzsec.com/Search/GetStockList'
        data = {'validatekey':self.validatekey}
 
        response = self.sess.post(url,data=data,headers=self.headers,verify=False)
        print(self.sess.cookies)
        rjson = json.loads(response.text)
        t_list = rjson['Data']
        stock_list = []
        for i in t_list:
            si = stock(i['Zqdm'],i['Zqmc'],i['Zqsl'],i['Kysl'],i['Cbjg'],i['Zxjg'],i['Ykbl'])
            stock_list.append(si)
 
        for si in stock_list:
            #止损比 self.zqbl = -0.02
            if si.Ykbl<self.zqbl:
                self.sell_stock(si,si.Zxjg,100)
                self.logger.info('挂单卖出'+ si.Zqdm+' '+si.Zqmc+' 数量:'+str(100)+' 价格:'+str(si.Zxjg))
            #止盈比self.zybl = 0.04
            elif si.Ykbl>self.zybl:
                # 增加卖出操作
                pass
            else:
                #持仓
                pass
 
    def get_yzm(self):
        pass
 
    #卖出操作
    def sell_stock(self,stock,price,amount):
        data = {'stockCode':stock.Zqdm,'price':price,'amount':amount,'tradeType':'S','zqmc':stock.Zqmc}
        d = urllib.parse.urlencode(data)
        print(data)
        url = 'https://jy.xzsec.com/Trade/SubmitTrade?validatekey='+self.validatekey
        response = self.sess.post(url, data=data, headers=self.headers, verify=False)
        print(response.text)
 
    #撤单
    def revoke_stock(self):
        url='https://jy.xzsec.com/Trade/GetRevokeList'
        data={'validatekey':self.validatekey}
        response = self.sess.get(url,data=data,headers=self.headers,verify=False)
        rjson = json.loads(response.text)
        s_list = rjson['Data']
        for si in s_list:
            print(si)
            #revokes=20190110_315084
            data = {'revokes':si['Wtrq']+'_'+si['Wtbh']}
            url = 'https://jy.xzsec.com/Trade/RevokeOrders?validatekey='+self.validatekey
            response = self.sess.post(url, data=data, headers=self.headers, verify=False)
            self.logger.info('卖出撤单 ' + si['Zqdm'] + ' ' + si['Zqmc'] + ' 数量:' + si['Wtsl'] + ' 价格:' + si['Wtjg']+' 服务器响应内容'+response.text)
            print(response.text)
 
 
    #买入操作
    def buy_stock(self,zqdm,price,amount,zqmc):
        url = 'https://jy.xzsec.com/Trade/SubmitTrade?validatekey='+self.validatekey
        data = {
                'stockCode':zqdm,
                'price':price,
                'amount':amount,
                'tradeType':'B',
                'zqmc':zqmc}
        print(self.sess.cookies)
        response = self.sess.post(url,data=data,headers=self.headers,verify=False)
        print(response.text)
if __name__ == '__main__':
    al = dfcf()
    al.dfcf_login()
    #al.get_stocklist()
    #al.buy_stock('000727',1.5,100,'华东科技')
    #al.revoke_stock()

  

posted @   东哥加油!!!  阅读(3395)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示