# 載入必要模組 from haohaninfo import GOrder from order import Record import sys # 登入帳號密碼(讀者須修正該帳號密碼為自己的,否則無法執行策略) GOrder.Login('TestAccount','TestPasswd') # 建立部位管理物件 OrderRecord=Record() # 取得回測參數 StartDate=sys.argv[1] EndDate=sys.argv[2] # 每日進場標記、高低點判斷 LastOrderDay='' CeilPrice={} FloorPrice={} Spread={} MoveStopLoss=0.02 # 回測取報價物件 KBar=GOrder.GetTAKBar(StartDate,EndDate,'3008','Stock','1','1') # 開始回測 for n in range(0,len(KBar['time'])): # 將日期取出 Date=KBar['time'][n].strftime('%Y%m%d') # 如果無未平倉部位 並時間為指定的進場時間 則進場 if OrderRecord.GetOpenInterest()==0 : # 在9點15 判斷當日高低點 if KBar['time'][n].strftime('%H%M') <= "0916" :
# -*- coding: utf-8 -*- import sys, haohaninfo, datetime from order import Record # 定義交易商品 Product = sys.argv[1] # 定義券商 Broker = 'Simulator' # 定義初始倉位 OrderRecord = Record() # 移動停損點數 StopLoss = 10 # 訂閱報價 GO = haohaninfo.GOrder.GOQuote() # 進場判斷 for row in GO.Subscribe(Broker, 'match', Product): # 取得時間、價格欄位 Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') Price = float(row[2]) # 無條件進場多單 OrderRecord.Order('B', Product, Time, Price, 1) # 進場後判斷最高價變數 AfterOrder = Price print(Time, '價格', Price, '無條件進場多單') GO.EndSubscribe() # 出場判斷 for row in GO.Subscribe(Broker, 'match', Product):
# 載入必要模組 from haohaninfo import GOrder from order import Record import sys # 登入帳號密碼(讀者須修正該帳號密碼為自己的,否則無法執行策略) GOrder.Login('TestAccount', 'TestPasswd') # 建立部位管理物件 OrderRecord = Record() # 取得回測參數 StartDate = sys.argv[1] EndDate = sys.argv[2] # 每日進場標記、高低點判斷 LastOrderDay = '' CeilPrice = {} FloorPrice = {} Spread = {} StopLoss = 30 TakeProfit = 70 # 回測取報價物件 KBar = GOrder.GetTAKBar(StartDate, EndDate, 'TXF', 'Future', '1', '1') # 開始回測 for n in range(0, len(KBar['time'])): # 將日期取出 Date = KBar['time'][n].strftime('%Y%m%d') # 如果無未平倉部位 並時間為指定的進場時間 則進場 if OrderRecord.GetOpenInterest() == 0: # 在9點15 判斷當日高低點
# -*- coding: utf-8 -*- import sys, haohaninfo, datetime from order import Record from indicator import KBar # 定義交易商品 Product = sys.argv[1] # 定義券商 # Broker='Masterlink_Future' Broker = 'Simulator' # 定義K棒物件 Today = datetime.datetime.now().strftime('%Y%m%d') KBar1M = KBar(Today, 1) # 定義初始倉位 OrderRecord = Record() # 定義突破n分鐘的高低點 BeforeTime = 30 # 定義持倉最久時間(180分鐘) HoldTime = datetime.timedelta(minutes=180) # 移動停損點數 StopLoss = 30 # 訂閱報價 GO = haohaninfo.GOrder.GOQuote() # 進場判斷 for row in GO.Subscribe(Broker, 'match', Product): # 取得時間、價格、量欄位 Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') Price = float(row[2]) Qty = float(row[3]) # 將資料填入K棒
# 匯入模組 from indicator import GetHistoryDataByPeriod, KBar from order import Record import datetime, sys # 指定日期 DataPath = sys.argv[1] # 'C:/Data' Broker = sys.argv[2] # 'simulator' Product = sys.argv[3] # 'TXFI9' Start = sys.argv[4] # '20190830' End = sys.argv[5] # '20190911' # 定義初始倉位 OrderRecord = Record() # 定義K棒物件(判斷區間) MinuteKbar = KBar(Start, 1) # 定義MA、RSI週期 MAPeriod = 10 RSIPeriod = 30 # 策略迴圈 (GetHistoryDataByPeriod 改為取得即時報價即可轉為實單交易) for row in GetHistoryDataByPeriod(DataPath, Broker, Product, 'Match', Start, End): Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') Price = float(row[2]) Qty = int(row[3]) # 餵資料進K線物件 ChangeFlag = MinuteKbar.AddPrice(Time, Price, Qty) # 如果有換K棒才會取得MA以及RSI if ChangeFlag == 1:
# -*- coding: utf-8 -*- import sys, haohaninfo, datetime, numpy from order import Record from indicator import KBar # 定義交易商品 Product = sys.argv[1] # 定義券商 Broker = 'Simulator' # 定義初始倉位 OrderRecord = Record() # 定義K棒物件 Today = datetime.datetime.now().strftime('%Y%m%d') KBar1M = KBar(Today, 1) # 定義布林通道週期 Period = 10 # 訂閱報價 GO = haohaninfo.GOrder.GOQuote() # 進場判斷 for row in GO.Subscribe(Broker, 'match', Product): # 取得時間、價格欄位 Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') Price = float(row[2]) Qty = float(row[3]) ChangeKFlag = KBar1M.AddPrice(Time, Price, Qty) # 每分鐘判斷一次 if ChangeKFlag == 1: # 取得布林通道上下界 Upper, Middle, Lower = KBar1M.GetBBands(Period)
def orderPaser(order_line): ''' param: one string of every single order return: one Order object containing record and all info ''' use_line = order_line.split(' ', 4) #取订单,切分 #print (use_line[4]) #去除无用字符,仅保留4 p1 = r"(?<=is\":\[).+?(?=\],\"inf)" searchObj = re.compile(p1).search(use_line[4]) if searchObj: record_set = re.compile(p1).search(use_line[4]).group(0) #提取一个订单中的所有记录 else: return None p_order = r"(?<=\"info\":\{).+?(?=\}\},\"type\":)" order = re.compile(p_order).search(use_line[4]).group(0) #提取一个订单中的信息 #print (order) #提取并转换订单中的时间 p_time = re.compile(r"20[0-9][0-9]\..+?:[0-9][0-9]:[0-9][0-9]") time_str = p_time.findall(order) order = p_time.sub(' ', order) #删除字符串中的时间,方便后续对字符串处理 time_set = [] for time_s in time_str: time_set.append(time.strptime(time_s, "%Y.%m.%d %H:%M:%S")) #提取并转换订单中的 carTypeNo p_carTypeNo = re.compile(r"(?<=carTypeNo\":).+?(?=,\")") carTypeNo = p_carTypeNo.findall(order)[0].replace('\"', '') #print(carTypeNo) carTypeNo = carTypeNo order = p_carTypeNo.sub('" "', order) #print(order) #将订单信息转换为字典 order_peice = order.replace('\"', '').split(',') order_dict = {} #print(order_peice) for order_info in order_peice: order_info = order_info.split(':') #print(order_info) order_dict[order_info[0]] = order_info[1] #格式化时间 if time_set[0] < time_set[1]: order_dict['timeStart'] = time_set[0] order_dict['timeEnd'] = time_set[1] else: order_dict['timeStart'] = time_set[1] order_dict['timeEnd'] = time_set[0] order_dict['cts'] = order_dict['cts'] order_dict['ctl'] = order_dict['ctl'] #插入carTypeNo order_dict['carTypeNo'] = carTypeNo #print(order_dict) #生成order对象 order_temp = Order(order_dict) ''' 下面为记录的处理 ''' p2 = r"\[.+?\]" matcher2 = re.compile(p2).findall(record_set) #将记录集转为List record_idx = 0 #取第一条记录 #print (matcher2[record_idx]) pattern3 = re.compile(r"(?<=\[).+?(?=\])") record_list = [] #将一条记录分片 for record in matcher2: peice = pattern3.search(record).group(0).replace('\"', '').split(',') #print (peice) #将一条记录保存为Record对象 r_temp = Record(float(peice[0])*0.001,float(peice[1]),float(peice[2]),\ float(peice[3]),float(peice[7])) #print(r_temp.soc) record_list.append(r_temp) #print(record_list) order_temp.record_list = record_list return order_temp
# 載入必要模組 from haohaninfo import GOrder from order import Record import sys # 取得回測參數 StartDate = sys.argv[1] EndDate = sys.argv[2] # 登入帳號密碼(讀者須修正該帳號密碼為自己的,否則無法執行策略) GOrder.Login('TestAccount', 'TestPasswd') # 建立部位管理物件 OrderRecord = Record() # 回測取報價物件 KBar = GOrder.GetTAKBar(StartDate, EndDate, '3008', 'Stock', '1', '1') # 開始回測 for n in range(0, len(KBar['time'])): # 如果無未平倉部位 並時間為指定的進場時間 則進場 if OrderRecord.GetOpenInterest() == 0: # 如果最新的K線時間在 10點至11點內買進 if KBar['time'][n].strftime('%H%M') >= "1000" and KBar['time'][ n].strftime('%H%M') < "1100": OrderRecord.Order('Buy', KBar['product'][n], KBar['time'][n], KBar['open'][n], 1) # 如果有未平倉部位,則在11點以後立即平倉 elif OrderRecord.GetOpenInterest() == 1: # 如果最新的K線時間超過 11點 if KBar['time'][n].strftime('%H%M') >= "1100": OrderRecord.Cover('Sell', KBar['product'][n], KBar['time'][n],