您好,登錄后才能下訂單哦!
哈咯!想用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。