Gekko 比特幣、加密貨幣交易機器人 Trading Bot - 簡易版 RSI 自動判定 [牛 / 熊] 市、短線對沖交易策略

這是一個基於 MA (移動平均) 線來自動改變 RSI 參數的短線交易策略,目的在於配合不同市場情況適合不同的應對,才能把可能收益最大化。由於這是一個自訂策略 (Custom Strategy) 所以必須要建立獨立的 .js 檔。

JS CODE :

/*
    RSI Bull and Bear
    Use different RSI-strategies depending on a longer trend
 
    (CC-BY-SA 4.0) Tommie Hansen
    https://creativecommons.org/licenses/by-sa/4.0/
*/

var _ = require ('lodash');
var log = require ('../core/log.js');

// Configuration
var config = require ('../core/util.js').getConfig();
var async = require ('async');

// Let's create our own method
var method = {};


// Prepare everything our method needs
method.init = function () {

   this.name = 'RSI Bull and Bear';

   // Keep state about stuff
   this.trend = {
       direction: 'none',
       duration: 0,
       persisted: false,
       adviced: false
   };

   // How many candles do we need as a base before start giving advice
   this.requiredHistory = config.tradingAdvisor.historySize;
 
    // add indicators
    this.addTulipIndicator('maSlow', 'sma', { optInTimePeriod: this.settings.SMA_long });
    this.addTulipIndicator('maFast', 'sma', { optInTimePeriod: this.settings.SMA_short });
 
    this.addTulipIndicator('BULL_RSI', 'rsi', { optInTimePeriod: this.settings.BULL_RSI });
    this.addTulipIndicator('BEAR_RSI', 'rsi', { optInTimePeriod: this.settings.BEAR_RSI });

}

// What happens on every new candle?
method.update = function(candle) {} // do nothing
method.log = function() {} // do nothing

method.check = function (candle)
{
   if( candle.close.length < this.requiredHistory ) { return; } // TODO: still needed?!
 
    // get all indicators
    let ind = this.tulipIndicators;
 
    let maSlow = ind.maSlow.result.result,
        maFast = ind.maFast.result.result,
        rsi;
 
 
    // define rules
    let goLong = false,
        goShort = false;
     
    // BEAR TREND
    if( maFast < maSlow )
    {
        log.debug('BEAR Trend');
        rsi = ind.BEAR_RSI.result.result;
        if( rsi > this.settings.BEAR_RSI_high ) goShort = true;
        if( rsi < this.settings.BEAR_RSI_low )  goLong = true;
    }
 
    // BULL TREND
    else
    {
        log.debug('BULL Trend');
        rsi = ind.BULL_RSI.result.result;
        if( rsi > this.settings.BULL_RSI_high ) goShort = true;
        if( rsi < this.settings.BULL_RSI_low )  goLong = true;
    }

    // LONG
    if( goLong )
    {
     
        // new trend? (only act on new trends)
        if (this.trend.direction !== 'up')
        {
         
            // reset the state for the new trend
            this.trend = {
                duration: 0,
                persisted: false,
                direction: 'up',
                adviced: false
            };
         
         
            if( !this.trend.adviced )
            {
                this.trend.adviced = true;
                this.advice('long');
            }
            else {
                this.advice();
            }
         
        }

        this.trend.duration ++;
        log.debug ('Positive since ', this.trend.duration, 'candle (s)');
     
    }
 
    // SHORT
    else if( goShort )
    {
     
        // new trend? (else do things)
        if( this.trend.direction !== 'down' )
        {
         
            // reset state
            this.trend = {
                duration: 0,
                persisted: false,
                direction: 'down',
                adviced: false
            };

            if( !this.trend.adviced )
            {
                this.trend.adviced = true;
                this.advice ('short');
            }
            else {
                this.advice();
            }
     
        }
     
        this.trend.duration ++;
        log.debug ('Negative since ', this.trend.duration, 'candle (s)');
     
    }
 
    // default
    else
    {
        //log.debug('No trend');
        this.advice();
    }
 
} // method.check()

module.exports = method;

將以上的 Code 存成 .js 檔再放到 gekko-develop\strategies 底下。



TOML-file :
# SMA Trends
SMA_long = 800
SMA_short = 40

# BULL
BULL_RSI = 10
BULL_RSI_high = 80
BULL_RSI_low = 50

# BEAR
BEAR_RSI = 15
BEAR_RSI_high = 50
BEAR_RSI_low = 25


# BULL/BEAR is defined by the longer SMA trends
# if SHORT over LONG = BULL
# if SHORT under LONG = BEAR

以上存成同檔名的 .toml 檔放到 gekko-develop\config\strategies 底下。


在 Gekko 的策略裡面就會看到剛剛新增的 RSI_BULL_BEAR 了。

實際跑 2017/11/2 ~ 2018/2/2 數據的結果,這段時間剛好經歷牛、熊市,可以看到策略轉換的不錯,收益甚至來到 67.9%,原作者也有提到這個策略適合短線對沖,最好把 Candle Size 設為 15 分鐘或較短的時間才能達到效果。

翻譯改寫自 : https://forum.gekko.wizb.it/thread-100.html

Gekko 教學 - 免費的比特幣 (Bitcoin)、加密貨幣自動交易機器人 (Trading Bot),支援幣安 Binance、Poloniex、Bitfinex...等交易所
Gekko 中文社群 :

留言

這個網誌中的熱門文章

CPU 加密貨幣挖礦入門首選匿名幣 - Monero(XMR)、Electroneum(ETN)、IntenseCoin(ITNS),到底該挖哪個?(CryptoNight 收益計算機)

格式化 (Format) 教學,重灌基本功|配置單位大小|NTFS、FAT32、exFAT 怎麼選?

Ubuntu 打不出中文?(Chinese input / Type Chinese) 安裝 gcin 注音輸入法輕鬆打出繁體中文