跳到主要內容

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

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 和新增防火牆例外,避免被拒絕的連線。

如何將 ISO 檔上傳到 VMware ESXi 6.5 並且用虛擬光碟開機?適用於安裝 Linux 或其它作業系統

在將實體機器使用  VMware ESXi 6.5 虛擬化之後就可以在它所提供的網頁管理介面操作每個虛擬個體,但要怎麼把用來安裝系統的 ISO 檔傳到虛擬機呢?而這些 ISO 檔會需要做成開機隨身碟的模式才能用嗎? 其實 ESXi 6.5 可以直接建立一個虛擬光碟機來驅動安裝系統用的 ISO 檔 ,所以安裝起來甚至會比實體機更簡單方便。 首先進到 ESXi 的管理介面 > 儲存區 ,點選 資料存放區瀏覽器 。 然後就能上傳事先下載好的 ISO 檔,不一定要存在哪一層路徑或特定位址,但建議新開一個資料夾方便整理,之後也可以直接指定這個路徑內的 ISO 檔。 然後在 虛擬機電源關閉 的情況下進入編輯設定。 新增一台 CD/DVD 光碟機。 選擇資料存放區 ISO 檔案。 瀏覽到剛剛上傳 ISO 檔的位址。 並且在虛擬機器選項的地方強制在 下次開機時強制進入 BIOS 設定畫面。 然後啟動虛擬機電源並開機後 到 Boot 的地方將 CD-ROM Drive 順位提高 ,就能以剛剛掛載的虛擬光碟機開機了。