BotVS趋势交易策略-RSI, 基于Python实现。
RSI简单买卖测试, 默认 70-100卖出,0-30买入
参数
代码
import math def adjustFloat(v): return math.floor(v*1000)/1000; # 取消挂起的订单 def cancelPendingOrders(): while True: orders = exchange.GetOrders(); if (not orders): if (len(orders) == 0): break; for j in range(len(records)): exchange.CancelOrder(orders[j].Id); Sleep(Interval); else: Sleep(Interval); STATE_WAIT_BUY = 0; STATE_WAIT_SELL = 1; STATE_BUY = 2; STATE_SELL = 3; STATE_WAIT_SELL_ALL = 4; State = STATE_WAIT_BUY; def onTick(exchange): global State records = exchange.GetRecords(); if (not records or len(records) < (RSIPeriod + 5)): return; rsi = TA.RSI(records, RSIPeriod); rsiValue = rsi[len(records) - 1]; #取最后一期的值为当前值 if (State == STATE_WAIT_BUY and rsiValue >= RSIBuyL and rsiValue <= RSIBuyH): # 买入点区间 State = STATE_BUY; elif (State == STATE_WAIT_SELL and rsiValue >= RSISellL and rsiValue <= RSISellH): # 卖出点区间 State = STATE_SELL; elif (State != STATE_WAIT_SELL_ALL): return; # Buy or Sell, Cancel pending orders first cancelPendingOrders(); account = exchange.GetAccount(); ticker = exchange.GetTicker(); if (not account or not ticker): return; # 买入 if (State == STATE_BUY): price = ticker.Last + SlidePrice; amount = adjustFloat(account.Balance / price); if (amount >= exchange.GetMinStock()): if (exchange.Buy(price, amount)): State = STATE_WAIT_SELL; # 卖出 else: if (account.Stocks > exchange.GetMinStock()): # STATE_WAIT_SELL or STATE_WAIT_SELL_ALL State = STATE_WAIT_SELL_ALL; exchange.Sell(ticker.Last - SlidePrice, account.Stocks); else: # No stocks, wait buy and log profit LogProfit(account.Balance); Log(account); State = STATE_WAIT_BUY; def main(): InitAccount = exchange.GetAccount(); Log(InitAccount); if (InitAccount.Stocks > 0): raise Exception("必须空仓介入") while True: onTick(exchange); Sleep(30000);