# 定義初始倉位 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): # 取得時間、價格欄位 Time = datetime.datetime.strptime(row[0], '%Y/%m/%d %H:%M:%S.%f') Price = float(row[2]) # 判斷移動停損 if Price > AfterOrder: AfterOrder = Price elif Price <= AfterOrder - StopLoss: # 空單出場
if OrderRecord.GetOpenInterest()==0 : # 在9點15 判斷當日高低點 if KBar['time'][n].strftime('%H%M') <= "0916" : # 新的一天則新增一組key if Date not in CeilPrice.keys(): CeilPrice[Date] = KBar['high'][n] FloorPrice[Date] = KBar['low'][n] Spread[Date]=(CeilPrice[Date]-FloorPrice[Date])*0.2 else: CeilPrice[Date] = max(CeilPrice[Date],KBar['high'][n]) FloorPrice[Date] = min(FloorPrice[Date],KBar['low'][n]) Spread[Date]=(CeilPrice[Date]-FloorPrice[Date])*0.2 # 在9:16以後 並且當日無進場,則開始判斷當日進場 elif KBar['time'][n].strftime('%H%M') > "0916" and KBar['time'][n].strftime('%Y%m%d') != LastOrderDay : if KBar['close'][n] > CeilPrice[Date] + Spread[Date] : OrderRecord.Order('Buy', KBar['product'][n+1],KBar['time'][n+1],KBar['open'][n+1],1) OrderPrice = KBar['open'][n+1] StopLossPoint = OrderPrice * (1-MoveStopLoss) LastOrderDay = KBar['time'][n].strftime('%Y%m%d') elif KBar['close'][n] < FloorPrice[Date] - Spread[Date] : OrderRecord.Order('Sell', KBar['product'][n+1],KBar['time'][n+1],KBar['open'][n+1],1) OrderPrice = KBar['open'][n+1] StopLossPoint = OrderPrice * (1+MoveStopLoss) LastOrderDay = KBar['time'][n].strftime('%Y%m%d') # 如果有多單部位,則在1330點以後立即平倉 elif OrderRecord.GetOpenInterest()==1 : # 逐筆更新移動停損價位 if KBar['close'][n] * (1-MoveStopLoss) > StopLossPoint : StopLossPoint = KBar['close'][n] * (1-MoveStopLoss) # 判斷到期出場
EndDate = sys.argv[2] # 停損停利點數設置 StopLoss = float(sys.argv[3]) TakeProfit = float(sys.argv[4]) LastOrderDay = '' # 回測取報價物件 KBar = GOrder.GetTAKBar(StartDate, EndDate, 'TXF', 'Future', '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) OrderPrice = KBar['open'][n + 1] StopLossPoint = OrderPrice - StopLoss TakeProfitPoint = OrderPrice + TakeProfit LastOrderDay = KBar['time'][n].strftime('%Y%m%d') # 如果有未平倉部位,則在11點以後立即平倉 elif OrderRecord.GetOpenInterest() == 1: # 如果最新的K線時間超過 11點 if KBar['time'][n].strftime('%H%M') >= "1100": OrderRecord.Cover('Sell', KBar['product'][n], KBar['time'][n], KBar['open'][n], 1) # 如果上一根K的收盤價觸及停利價位,則在最新時間出場 elif KBar['close'][n] > TakeProfitPoint: OrderRecord.Cover('Sell', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) # 如果上一根K的收盤價觸及停損價位,則在最新時間出場