跳到主要內容

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

吃光硬碟讀寫的大怪獸 - Superfetch 是什麼?可以關嗎?

Superfetch 是 Windows 內建的硬碟快取功能,它會預先將檔案讀取起來到 Ram 裡方便稍後做使用,所以常常在工作管理員內都可以看到 Superfetch 往往占掉了不少的硬碟使用量,那這樣的情形是好的嗎?其實理論上並不會有太大影響,畢竟 Superfetch 只是將等會要讀取的內容先預置在 Ram 中,如果不趁硬碟負載小的時候優先讀取,之後反而可能忙不過來呢。當然如果你是使用 SSD 的話可能會為使用壽命擔心, 高頻率的讀取可能加速固態硬碟老化,那在讀取速度原本就很快的 SSD 當然就相對的沒有必要開啟 Superfetch 了。 Superfetch 在電腦待機時吃掉大部分硬碟使用量是很常有的事。 要關閉也很簡單, Win+R > services.msc 打開「服務」,找到 Superfetch。 右鍵 > 內容,將它更改為「 已停用 」後確定即可。

RealVNC - 支援多系統的 VNC Viewer,免費遠端桌面客戶端 (Windows / Linux (Ubuntu) / Raspberry Pi /...)

對於不熟悉 SSH 介面的使用者來說可能第一個想法都會是使用遠端桌面,但要知道大多數的 VPS 主機都是提供 Linux 系統 (Ubuntu / RedHat / Debian...等),它們架好的 VNC Server 多半是沒辦法直接以 Windows 系統內建的「遠端桌面連線」來操作的,就好像兩端的通道是開了起來但卻仍說著不同的語言,所以就需要專用的 VNC Viewer 來連線, 而 RealVNC 支援非常多不同的作業系統環境,包括 Windows、Linux、Raspberry Pi、Android、iOS...等,幾乎可以說是一應具全啦。 RealVNC 官方網站:  https://www.realvnc.com/en 下載頁面:  https://www.realvnc.com/en/connect/download/viewer 只要從 「File > new connection」 就可以新增一個連線,爾後只要點選介面中已設定好的連線就可以隨時進入遠端主機,而主機端 (VPS) 也記得要先架設好 VNC Server 和新增防火牆例外,避免被拒絕的連線。