# 如果有多單部位,則在1330點以後立即平倉 elif OrderRecord.GetOpenInterest()==1 : # 逐筆更新移動停損價位 if KBar['close'][n] * (1-MoveStopLoss) > StopLossPoint : StopLossPoint = KBar['close'][n] * (1-MoveStopLoss) # 判斷到期出場 if KBar['time'][n].strftime('%H%M') >= "1330" : OrderRecord.Cover('Sell', KBar['product'][n],KBar['time'][n],KBar['open'][n],1) # 如果上一根K的收盤價觸及停損價位,則在最新時間出場 elif KBar['close'][n] < StopLossPoint : OrderRecord.Cover('Sell', KBar['product'][n+1],KBar['time'][n+1],KBar['open'][n+1],1) # 如果有空單部位,則在1330點以後立即平倉 elif OrderRecord.GetOpenInterest()==-1 : # 逐筆更新移動停損價位 if KBar['close'][n] * (1+MoveStopLoss) < StopLossPoint : StopLossPoint = KBar['close'][n] * (1+MoveStopLoss) # 判斷到期出場 if KBar['time'][n].strftime('%H%M') >= "1330" : OrderRecord.Cover('Buy', KBar['product'][n],KBar['time'][n],KBar['open'][n],1) # 如果上一根K的收盤價觸及停損價位,則在最新時間出場 elif KBar['close'][n] > StopLossPoint : OrderRecord.Cover('Buy', KBar['product'][n+1],KBar['time'][n+1],KBar['open'][n+1],1) # 將回測紀錄寫至MicroTest中 OrderRecord.StockMicroTestRecord('4-5-2-S',0.5) # 繪製走勢圖以及下單點位 from chart import ChartOrder ChartOrder(KBar,OrderRecord.GetTradeRecord())
# 訂閱報價 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: # 空單出場 OrderRecord.Cover('S', Product, Time, Price, 1) print(Time, '價格', Price, '進場後最高價', AfterOrder, '移動停損出場') GO.EndSubscribe() print('全部交易紀錄', OrderRecord.GetTradeRecord())
OrderRecord.Cover('Sell', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 如果有空單部位 elif OrderRecord.GetOpenInterest() == -1: # 結算平倉 if KBar['product'][n + 1] != KBar['product'][n]: OrderRecord.Cover('Buy', KBar['product'][n], KBar['time'][n], KBar['close'][n], 1) continue # 逐筆更新移動停損價位 if KBar['close'][n] + MoveStopLoss < StopLossPoint: StopLossPoint = KBar['close'][n] + MoveStopLoss # 如果上一根K的收盤價觸及停損價位,則在最新時間出場 elif KBar['close'][n] > StopLossPoint: OrderRecord.Cover('Buy', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 若小於 布林通道下界 則停利出場 if KBar['close'][n] <= KBar['Lower'][n]: OrderRecord.Cover('Buy', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 將回測紀錄寫至MicroTest中 OrderRecord.FutureMicroTestRecord('5-4-4-F', 200, 50) # 繪製走勢圖加上MA以及下單點位 from chart import ChartOrder_BBANDS ChartOrder_BBANDS(KBar, OrderRecord.GetTradeRecord())
KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 如果有空單部位 elif OrderRecord.GetOpenInterest() == -1: # 結算平倉 if KBar['product'][n + 1] != KBar['product'][n]: OrderRecord.Cover('Buy', KBar['product'][n], KBar['time'][n], KBar['close'][n], 1) continue # 逐筆更新移動停損價位 if KBar['close'][n] + MoveStopLoss < StopLossPoint: StopLossPoint = KBar['close'][n] + MoveStopLoss # 如果上一根K的收盤價觸及停損價位,則在最新時間出場 elif KBar['close'][n] > StopLossPoint: OrderRecord.Cover('Buy', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 若 RSI 低於 超賣界線 則停利出場 if KBar['RSI'][n] < KBar['Floor'][n]: OrderRecord.Cover('Buy', KBar['product'][n + 1], KBar['time'][n + 1], KBar['open'][n + 1], 1) continue # 將回測紀錄寫至MicroTest中 OrderRecord.FutureMicroTestRecord('5-3-5-F', 200, 50) # 繪製走勢圖加上MA以及下單點位 from chart import ChartOrder_RSI_2 ChartOrder_RSI_2(KBar, OrderRecord.GetTradeRecord())