哈咯!想用python设计一个简单的基于做市商的股票高频交易策略,之前的主要class都已经写好了,现在在主程序里面实现策略,但是始终想不出一个合理的代码结构来实现,有没有哪位大神敢尝试啊哈哈:
先给出模仿的论文策略图:
下面是我自己能写出的主程序部分:(如果需要我写的class可以在下面评论)
#Firstly, I load the first day's data from table BHP_data,
#then create two lists for storing the mid price and time,
#and then use for loop to read each events imformation, and store the mid price and time data into list.
#afterwards, plot the first day data and time in a figure.
import mysql.connector
import pyodbc
connection = mysql.connector.connect(user= 'root', password = '51jingtI', host = '127.0.0.1', database = 'HF2')
cursor = connection.cursor()
cursor.execute("select * from BHP_data where date='2007-07-02'")
events_data = cursor.fetchall()
#create two lists for storing the mid price and time,
money = 100
mid_price_list = []
best_ask_price_list = []
best_bid_price_list = []
time_list = []
book = OrderBook()
book_list_bid = []
book_list_ask = []
profit_list = []
for data in events_data:
S_price = float(book.best_ask_price())+0.001
S_PM_price = float(book.best_ask_price())-0.01
B_PM_price = float(book.best_bid_price())+0.01
B_price = float(book.best_bid_price())-0.001
lo = LimitOrder(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11],data[12])
lo_b = LimitOrder(data[0],data[1],data[2],'ENTER',B_price,1,'000','','B',0,0,0,data[12])
lo_s = LimitOrder(data[0],data[1],data[2],'ENTER',S_price,1,'','000','A',0,0,0,data[12])
book.add(lo)
if book.mid_price():
mid_price_list.append(float(book.mid_price()))
if len(mid_price_list) > 10:
if ((float(mid_price_list[len(mid_price_list)-1]) - float(mid_price_list[len(mid_price_list)-2]))>0
and
(float(mid_price_list[len(mid_price_list)-1] - mid_price_list[len(mid_price_list)-2])-
float(mid_price_list[len(mid_price_list)-2] - mid_price_list[len(mid_price_list)-1]))> 0
and
len(book_list_bid) == 0):
book.add(lo_b)
book_list_bid.append(lo_b.get_id())
elif((float(mid_price_list[len(mid_price_list)-1]) - float(mid_price_list[len(mid_price_list)-2])) < 0
and
(float(mid_price_list[len(mid_price_list)-1] - mid_price_list[len(mid_price_list)-2])-
float(mid_price_list[len(mid_price_list)-2] - mid_price_list[len(mid_price_list)-1])) < 0
and
len(book_list_ask) == 0):
book.add(lo_b)
book_list_ask.append(lo_b.get_id())
if len(mid_price_list) > 50:
if not (lo_b.get_id() in book.event_dict):
lo_b_pm = LimitOrder(data[0],data[1],data[2],'ENTER',B_PM_price,1,'','test_ask_new',data[8],1,1,1,1)
book.add(lo_b_pm)
if not (lo_s.get_id() in book.event_dict):
lo_s_pm = LimitOrder(data[0],data[1],data[2],'ENTER',S_PM_price,1,'test_bid_new','',data[8],1,1,1,1)
book.add(lo_s_pm)
#instrument, date, time, record_type, price, volume,bid_id, ask_id, direction, sameoffset, oppoffset, midoffset, timestamp):
plt.figure(figsize=(20,8))
plt.xlabel('volume')
plt.ylabel('Mid price')
plt.title('Mid price of BHP')
plt.plot(mid_price_list)
plt.show()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。