跳到主要內容

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 中文社群 :



留言



這個網誌中的熱門文章

Linux (Ubuntu) 查詢硬碟容量、剩餘大小指令

在 Ubuntu Desktop 版本或有安裝 GUI 像 Xfce 的 Server 版本當然可以直接從圖形介面查看硬碟容量和已經使用的大小, 但如果是在純 CLI 版本或使用 SSH 連線時呢?那就需要用到指令了。 顯示硬碟容量、已使用、可用大小。 df -h 查詢資料夾所占硬碟的大小。 du -h 查詢檔案大小 ls -l

TLS / SSL 金鑰轉檔,「.crt / .key」如何轉成「.pem」?( OpenSSL 教學)

不管是哪個 Certificate Authority (CA) 發的憑證多半金鑰檔都是給「.crt / .key」格式的檔案,像是「ca_bundle.crt」、「 certificate.crt」和「.private.key」這種檔案, 但是在實際使用 (Nginx、Apache...等伺服器) 卻會需要的是「.pem」檔 ,那要怎麼轉換呢?其實可以直接使用 OpenSSL 這個軟體和幾行指令來達成。 OpenSSL 官方網站 :  https://www.openssl.org 下載頁面 :  https://www.openssl.org/source sudo apt-get install openssl 以 Linux (Ubuntu / Debian ...等) 為例,可以直接使用 apt-get 指令下載安裝 OpenSSL,如果是其他系統可以參考官方網站的說明。 openssl rsa -in private.key -text > private.pem openssl x509 -inform PEM -in certificate.crt > certificate.pem openssl x509 -inform PEM -in ca_bundle.crt > ca_bundle.pem 以前述所提到的「ca_bundle.crt」、「 certificate.crt」和「.private.key」三個檔案為例,就可以用上方的指令完成轉檔,其實同副檔名的轉檔指令都相同,所以兩個「.crt」的指令其實是重複的,也要記得套換成自己的檔案名稱。 如果需要申請免費的 SSL 憑證也可以參考之前介紹過的 SSL For Free ,它是使用「Let's Encrypt」核發的憑證,以及如果覺得申請憑證和定期更新太麻煩,也可以考慮自動化的 Caddy Server 能自動幫網站升級 HTTPS。

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

如果第一次使用 Ubuntu 一定會有這個疑問,就是鍵盤怎麼按就是打不出中文字,其實不是你的 Ubuntu 壞了而是本來就需要安裝專用的中文輸入法才能用注音打中文,解法也很簡單,只要到 Ubuntu 軟體內找尋 gcin 這個中文輸入法就可以了。 在左上角 「尋找你的電腦」找到 Ubuntu 軟體 ,這就像是 Windows 軟體市集一樣,在這裡安裝小工具都不需要任何的指令,只要點一點就可以簡單的完成。 查詢 「gcin」 這個中文輸入法。 安裝時會需要管理員身份,只要輸入 Ubuntu 的密碼就可以了。 接著打開 設定 > 語言支援 。 將鍵盤輸入法系統改為「gcin」後 重新登入 就可以打中文啦。