zoukankan      html  css  js  c++  java
  • BotVS趋势交易策略-RSI

    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);
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/bitquant/p/botvs-strategy-rsi.html
Copyright © 2011-2022 走看看