跳到主要內容

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

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

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

鍵盤按鍵壞了怎麼辦?用 Sharpkeys 換一個鍵來用就好啦 (重新設定 / 配置鍵盤按鍵 / Key Mapping)

我有一個壞習慣就是很常按 Alt+Tab,有時候即使沒要跳畫面還是偶爾會手癢按個幾下,尤其 Alt 又是大拇指按的,可能我的大拇指特別有力所以最近把 Alt 真的按壞了,我是用青軸的機械式鍵盤,所以每個軸都是獨立的應該是可以更換,但如果是用薄膜式鍵盤呢?那可能只能整把換掉了, 或是交換一下鍵位拿一個平常沒在用的鍵頂替一下,也就是今天要介紹的「Sharpkeys」。 Sharpkeys 官方網站 :  http://www.randyrants.com/category/sharpkeys GitHub :  https://github.com/randyrants/sharpkeys 使用 Sharpkeys 會需要 .NET Framework 4.0,如果還沒有安裝的可以到 微軟官方 下載。 開啟 Sharpkeys 後點選 「Add」 ,左方是選擇 「被重新分配的鍵位」 ,右方是 「替代的鍵位」 ,你也可以按 Type Key 直接用打的讓系統偵測。 像我就做了兩個 Key Mapping,其實就是 Win 鍵和 Alt 鍵互換啦,設定完成後就按「Write to Registry」寫入註冊表, 然後重開機或重新登入就可以生效囉。