• 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);
  • 相关阅读:
    es组合多个条件进行查询
    Sidecar模式:下一代微服务架构的关键
    排查python内存泄露中几个工具的使用
    Consul的反熵
    我的Linux操作系统的发行版是什么?版本号是什么?
    如何确定一台linux主机是Linux (i386/i686)还是Linux (x86_64)
    如何确定Isilon cluster的网卡类型是40GbE的还是10GbE的
    如何确定Isilon里的磁盘是多大的?
    用SSH登录远程的机器,在远程机器上执行本地机器上的脚本
    关于EOF的使用的好文章
  • 原文地址:https://www.cnblogs.com/bitquant/p/botvs-strategy-rsi.html
Copyright © 2020-2023  润新知