跳到主要內容

Gekko 交易機器人 - TEMA 策略 (Triple EMA strategy),附帶有安全網機制 (SafetyNet) 來避免在熊市被套牢



這是一個以 EMA (指數移動平均) 決定做多做空的交易策略,但當短期趨勢超過了 EMA 線則會在上方使用一個更長時間的 SMA (簡單移動平均) 作為整個交易系統的安全網 (SafetyNet),在短期的 SMA 超越這個安全網前會避免任何可能被套牢的交易。所以簡單理解的概念就是在市場看多的時候進行交易,而市場下跌時則避免摻入其中,但實際上的運作狀況仍然有待測試。

JS Code :
/*
TEMA
Triple EMA strategy with 'safety net'
---
Uses 1x TEMA to go long/short if short trend is over TEMA.
On top of this it uses a longer SMA as a safety net and simple
stays out of the market if short SMA is under that SMA.
---
The general idea is to buy in good market conditions and simply not
be a part of longer downwards trends.
*/

// req's
var log = require ('../core/log.js');
var config = require ('../core/util.js').getConfig();

// strategy
var strat = {


/* INIT */
init: function()
{
// base
this.name = 'TEMA';
this.requiredHistory = config.tradingAdvisor.historySize;
this.debug = false; // outputs messages, set to false to increase performance

// add indicators and reset trend
this.resetTrend();
this.addTulipIndicator('maSlow', 'tema', { optInTimePeriod: this.settings.long });
this.addTulipIndicator('maFast', 'sma', { optInTimePeriod: this.settings.short });

// check if long SMA is to be used
if( this.settings.SMA_long > 0 )
{
this.useSafety = true;
this.addTulipIndicator('maSlowest', 'sma', { optInTimePeriod: this.settings.SMA_long });
}

// set startTime to measure execution time @ end()
this.startTime = new Date();

}, // init()



/* RESET TREND */
resetTrend: function()
{
var trend = {
duration: 0,
direction: 'none',
longPos: false,
};

this.trend = trend;

}, // resetTrend()



/* CHECK */
check: function()
{
// do nothing if we don't got enough history
if( this.candle.close.length < this.requiredHistory ) return;

// fetch indicators
let ti = this.tulipIndicators;
let maFast = ti.maFast.result.result,
maSlow = ti.maSlow.result.result;


// check if safety option > 0
if( this.useSafety )
{
let maSlowest = ti.maSlowest.result.result;
if( maSlow < maSlowest )
{
this.short();
return; // quit
}
}

// other rules
if( maFast > maSlow ) { this.long(); }
else if( maFast < maSlow ) { this.short(); }

}, // check()



/* LONG */
long: function()
{
if( this.trend.direction !== 'up' )
{
this.resetTrend();
this.trend.direction = 'up';
this.advice('long');
}

if( this.debug )
{
this.trend.duration++;
log.debug ('Positive since', this.trend.duration, 'candle(s)');
}
},



/* SHORT */
short: function()
{
if( this.trend.direction !== 'down' )
{
this.resetTrend();
this.trend.direction = 'down';
this.advice('short');
}

if( this.debug )
{
this.trend.duration++;
log.debug ('Negative since', this.trend.duration, 'candle(s)');
}
},


/* END */
end: function()
{
let seconds = ((new Date()- this.startTime)/1000),
minutes = seconds/60,
str;

minutes < 1 ? str = seconds + ' seconds' : str = minutes + ' minutes';

log.debug('Finished in ' + str);
}

}; // strat{}



/* EXPORT */
module.exports = strat;

Toml Code : 
# Short / Long (EMA/TEMA)
short = 10
long = 80

# Safety (SMA, 0 = disable)
SMA_long = 200


其實安全網本身是可以關掉的,可以測試看看不同的參數和開關的差異。


留言



這個網誌中的熱門文章

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」後 重新登入 就可以打中文啦。