同花顺自动交易研究使用

上级
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 18 13:05:36 2018
@author: daveqing
@qq : 1029329095
"""
from lib import Winauto
#软件启动
class Auto(object):
#软件初始化
def __init__(self):
self.Winauto = Winauto();
#判断软件是否启动成功
def IsWindow(self):
if self.Winauto.IsWindow():
return 1
else:
return 0
#软件启动
def startApp(self,filePath,var):
self.Winauto.appStart(filePath,var)
#购买
def buy(self,stock,price,num):
self.Winauto.Buy(stock,price,num)
#出售
def sell(self,stock,price,num):
self.Winauto.Sell(stock,price,num)
#查看所有资产
def getAllInfo(self):
return self.Winauto.getAllAsset();
#窗口置顶
def topWindow(self):
self.Winauto.topWindow()
#查询目前持仓
\ No newline at end of file
文件已添加
# -*- mode: python -*-
block_cipher = None
a = Analysis(['auto.py'],
pathex=['E:\\dev\\python\\python-dem\\python-dem\\stock'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='auto',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='auto')
# -*- coding: utf-8 -*-
import sys
sys.path.append('../')
from common import Software
from common import DBMange
import time
class Winauto(object):
def __init__(self):
self.flush = Software(u'同花顺(v8.60.83) - A股技术分析')
self.flush.topWindow()
self.db = DBMange()
def seeK(self,stock):
self.flush.keyStr(stock)
self.flush.Key_event('enter')
def getData(self,stype):
if stype==1:
sql = "SELECT hy_zystock FROM hy \
WHERE newtime = '%s' " % ('2018-08-13')
if stype==2:
sql = "SELECT stkCode FROM stocklhrank \
WHERE newtime = '%s' " % ('2018-08-13')
data = self.db.select(sql)
stock = []
for row in data:
fname = row[0]
# 打印结果
stock.append(fname)
return stock
auto = Winauto()
stockList =auto.getData(1)
for i in range(len(stockList)):
auto.seeK(stockList[i][0:6])
#print stockList[i][0:6]
time.sleep(5)
# -*- coding: utf-8 -*-
import datetime
import time
from data import StockData
from auto import Auto
stock = '000735'
proce = '0'
num = '3000'
def doSth():
auto = Auto()
data = StockData(stock)
stockData = data.getTodayData()
print stockData[1] #开
print stockData[3] #最新
#
s = round((float(stockData[3])-44.22)/44.22,4)*100
#if s<1:
auto.buy(stock,proce,num)
time.sleep(30)
print '运行成功'
time.sleep(60)
def main(h=0, m=0):
workTime = range(5) #make work time [0,1,2,3,4]
d=datetime.datetime.now() #get now time
weekday = d.weekday() #get week
while True:
# determine whether the set time ,such as 0:0
if weekday in workTime:
while True:
now = datetime.datetime.now()
# The time up does the thing
if now.hour==h and now.minute==m:
break
# no time ,sleep 30s
'''
time.sleep(10)
data = StockData(stock)
stockData = data.getTodayData()
ope = stockData[1]
new = stockData[3]
d = round((float(new)-float(ope))/float(ope),4)*100
print ope
print new
print d
if d>4:
auto = Auto()
auto.sell(stock,proce,num)
break
'''
# do thing ,one day one to make
doSth()
main(h=20,m=44)
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import pymysql
import time
class DBMange(object):
def __init__(self):
self.db = pymysql.connect("localhost","root","root","stock",charset="utf8")
self.cursor = self.db.cursor()
def select(self,sql):
self.cursor.execute(sql)
# 获取所有记录列表
self.results = self.cursor.fetchall()
return self.results
\ No newline at end of file
文件已添加
# -*- coding: utf-8 -*-
from VK_CODE import VK_CODE
#from pywinauto import application
#import win32clipboard as wc
from pykeyboard import PyKeyboard
import win32gui, win32api, win32con
import time
class Software(object):
def __init__(self,keyword):
self.k = PyKeyboard() #get pykeyboard
self.software=keyword
self.hWndChildList=0
self.para_hld=win32gui.FindWindow(None,self.software)
if self.IsWindow():
self.hWndChildList=self.demo_child_windows(self.para_hld) #get hld
attr=self.show_window_attr(self.para_hld)
win32api.SetCursorPos([attr['post'][0],attr['post'][1]]) #Mover to post(x,y)
'''
判定窗口是否激活
'''
def IsWindow(self):
if self.para_hld>0:
return 1;
else:
return 0;
'''
窗口置顶
'''
def topWindow(self):
self.Key_event('ctrl')
win32gui.SetForegroundWindow(self.para_hld) #show window
'''
输入键盘事件
'''
def keyStr(self,st):
self.k.type_string(st)
'''
输入键盘事件
'''
def Key_event(self,key):
win32api.keybd_event(VK_CODE[key],0,0,0) #
win32api.keybd_event(VK_CODE[key],0,win32con.KEYEVENTF_KEYUP,0) #
'''
ldList:
'post:?§|left, top, right, bottom
'''
def GetHld(self,post):
for hld in self.hWndChildList:
left, top, right, bottom =win32gui.GetWindowRect(hld) #get hld post left top right bottom
#if (post[0]==left) & (post[1]==top) & (post[2]==right) & (post[3]==right):
if (post[0]==left) & (post[1]==top): #get hld by (x,y) left top
return hld
return 0
'''
通过字符找到窗口
'''
def GetHldWord(self,word):
for hld in self.hWndChildList:
title = win32gui.GetWindowText(hld)
title=title.decode('gbk')
if title==word:
return hld
return 0
'''
获取窗口下的所有子窗口
'''
def demo_child_windows(self,parent):
if not parent:
return
hWndChildList = []
win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd), hWndChildList)
return hWndChildList
#get all hld attr
def show_window_attr(self,hWnd):
if not hWnd:
return
title = win32gui.GetWindowText(hWnd)
clsname = win32gui.GetClassName(hWnd)
left, top, right, bottom =win32gui.GetWindowRect(hWnd) #
attr = {'hWnd':hWnd,'title': title, 'clsname':clsname, 'post':[left,top,right,bottom]}
return attr
文件已添加
# -*- coding: gb2312 -*-
#
VK_CODE = {
'backspace':0x08,
'tab':0x09,
'clear':0x0C,
'enter':0x0D,
'shift':0x10,
'ctrl':0x11,
'alt':0x12,
'pause':0x13,
'caps_lock':0x14,
'esc':0x1B,
'spacebar':0x20,
'page_up':0x21,
'page_down':0x22,
'end':0x23,
'home':0x24,
'left_arrow':0x25,
'up_arrow':0x26,
'right_arrow':0x27,
'down_arrow':0x28,
'select':0x29,
'print':0x2A,
'execute':0x2B,
'print_screen':0x2C,
'ins':0x2D,
'del':0x2E,
'help':0x2F,
'0':0x30,
'1':0x31,
'2':0x32,
'3':0x33,
'4':0x34,
'5':0x35,
'6':0x36,
'7':0x37,
'8':0x38,
'9':0x39,
'a':0x41,
'b':0x42,
'c':0x43,
'd':0x44,
'e':0x45,
'f':0x46,
'g':0x47,
'h':0x48,
'i':0x49,
'j':0x4A,
'k':0x4B,
'l':0x4C,
'm':0x4D,
'n':0x4E,
'o':0x4F,
'p':0x50,
'q':0x51,
'r':0x52,
's':0x53,
't':0x54,
'u':0x55,
'v':0x56,
'w':0x57,
'x':0x58,
'y':0x59,
'z':0x5A,
'numpad_0':0x60,
'numpad_1':0x61,
'numpad_2':0x62,
'numpad_3':0x63,
'numpad_4':0x64,
'numpad_5':0x65,
'numpad_6':0x66,
'numpad_7':0x67,
'numpad_8':0x68,
'numpad_9':0x69,
'multiply_key':0x6A,
'add_key':0x6B,
'separator_key':0x6C,
'subtract_key':0x6D,
'decimal_key':0x6E,
'divide_key':0x6F,
'F1':0x70,
'F2':0x71,
'F3':0x72,
'F4':0x73,
'F5':0x74,
'F6':0x75,
'F7':0x76,
'F8':0x77,
'F9':0x78,
'F10':0x79,
'F11':0x7A,
'F12':0x7B,
'F13':0x7C,
'F14':0x7D,
'F15':0x7E,
'F16':0x7F,
'F17':0x80,
'F18':0x81,
'F19':0x82,
'F20':0x83,
'F21':0x84,
'F22':0x85,
'F23':0x86,
'F24':0x87,
'num_lock':0x90,
'scroll_lock':0x91,
'left_shift':0xA0,
'right_shift ':0xA1,
'left_control':0xA2,
'right_control':0xA3,
'left_menu':0xA4,
'right_menu':0xA5,
'browser_back':0xA6,
'browser_forward':0xA7,
'browser_refresh':0xA8,
'browser_stop':0xA9,
'browser_search':0xAA,
'browser_favorites':0xAB,
'browser_start_and_home':0xAC,
'volume_mute':0xAD,
'volume_Down':0xAE,
'volume_up':0xAF,
'next_track':0xB0,
'previous_track':0xB1,
'stop_media':0xB2,
'play/pause_media':0xB3,
'start_mail':0xB4,
'select_media':0xB5,
'start_application_1':0xB6,
'start_application_2':0xB7,
'attn_key':0xF6,
'crsel_key':0xF7,
'exsel_key':0xF8,
'play_key':0xFA,
'zoom_key':0xFB,
'clear_key':0xFE,
'+':0xBB,
',':0xBC,
'-':0xBD,
'.':0xBE,
'/':0xBF,
'`':0xC0,
';':0xBA,
'[':0xDB,
'\\':0xDC,
']':0xDD,
"'":0xDE,
'`':0xC0}
\ No newline at end of file
文件已添加
# -*- mode: python -*-
block_cipher = None
a = Analysis(['VK_CODE.py'],
pathex=['E:\\dev\\python\\python-dem\\python-dem\\stock\\lib'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='VK_CODE',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='VK_CODE')
# -*- coding: utf-8 -*-
from .VK_CODE import VK_CODE
from .pykeyboard import PyKeyboard
from .Software import Software
from .DBMange import DBMange
\ No newline at end of file
文件已添加
#Copyright 2013 Paul Barton
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
The goal of PyMouse is to have a cross-platform way to control the mouse.
PyMouse should work on Windows, Mac and any Unix that has xlib.
PyKeyboard is a part of PyUserInput, along with PyMouse, for more information
about this project, see:
http://github.com/SavinaRoja/PyUserInput
"""
import sys
if sys.platform.startswith('java'):
from .java_ import PyKeyboard
elif sys.platform == 'darwin':
from .mac import PyKeyboard, PyKeyboardEvent
elif sys.platform == 'win32':
from .windows import PyKeyboard, PyKeyboardEvent
else:
from .x11 import PyKeyboard, PyKeyboardEvent
#Copyright 2013 Paul Barton
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
As the base file, this provides a rough operational model along with the
framework to be extended by each platform.
"""
import time
from threading import Thread
class PyKeyboardMeta(object):
"""
The base class for PyKeyboard. Represents basic operational model.
"""
#: We add this named character for convenience
space = ' '
def press_key(self, character=''):
"""Press a given character key."""
raise NotImplementedError
def release_key(self, character=''):
"""Release a given character key."""
raise NotImplementedError
def tap_key(self, character='', n=1, interval=0):
"""Press and release a given character key n times."""
for i in range(n):
self.press_key(character)
self.release_key(character)
time.sleep(interval)
def press_keys(self,characters=[]):
"""Press a given character key."""
for character in characters:
self.press_key(character)
for character in characters:
self.release_key(character)
def type_string(self, char_string, interval=0):
"""
A convenience method for typing longer strings of characters. Generates
as few Shift events as possible."""
shift = False
for char in char_string:
if self.is_char_shifted(char):
if not shift: # Only press Shift as needed
time.sleep(interval)
self.press_key(self.shift_key)
shift = True
#In order to avoid tap_key pressing Shift, we need to pass the