Schaff Trend Cycle
#1
Hello everybody,

Have any of you played with the Schaff Trend Cycle at all? I think it could be really great.
https://www.investopedia.com/articles/fo...icator.asp

I have managed to put together a strategy in "pine script," the language used over at tradingview:
https://www.tradingview.com/chart/lEaTj9yp/

That strategy looks like it beats the market by more than 40% when backtested against the last couple months. The source code (in pine script) is here: https://bpaste.net/show/937e9df5278a

Mike was kind enough to link me to this script:
https://www.prorealcode.com/prorealtime-...nd-cycle2/

I am a novice, and haven't really done hardly any coding before, but I've managed to get gekko running, and I have been struggling to figure out how to code the STC into an indicator for gekko, so that I can try to translate that pine script strategy into a gekko strategy, and run it with some real money, or at least backtest it with gekko and see if it looks like it might work.

I would be willing to pay somebody but don't have much to offer, unfortunately, likely nowhere near enough to make it worth your while. I'm hoping that somebody will be interested in helping port this over to gekko and make it available open-source out of the goodness in their hearts. -- EDIT: Will consider any offers, willing to pay a bounty, name a price and we'll see if I can afford it!


Attached Files
.png   stc_chart.png (Size: 85.33 KB / Downloads: 82)
.png   STC_stats.png (Size: 39.62 KB / Downloads: 64)
  Reply
#2
If coding the STC as an indicator into gekko is too challenging for anybody to tackle for free, please, let me know what kind of bounty I would need to pay to make it worth your while. Maybe I can find a way to afford it!
  Reply
#3
Interesting indicator, I've had a go at it today, adapting it from here rather than from pine script. For some reason I always struggle to read pine script... and write it for that matter.

I'd be very grateful if somebody could check my logic. I think I've understood the maths correctly but another pair of eyes would be good.

Put it through a genetic algo backtest for a while... running it by itself it can return pretty good profits but with some rather large drawdowns.

Just done more testing. Mine is complete and utter junk! Use ZSchoros!
Removed the links until I get it fixed.

Backtest Screenshot: https://i.imgur.com/XdAyThx.png

Thoughts?

Edits as first strategy I posted was pretty flawed, fixed now.
  Reply
#4
Looks like Gryphon beat me to it, but I also wrote this up:

https://github.com/zschro/GekkoSchaffTrendCycle


I will need to compare our implementations.

Thanks,
Zack
  Reply
#5
I am so excited! Thank you so much for digging into this for me! I really can't put into words how happy this has made me -- I've been looking at the STC and trying to figure out how to get a bot trading with it for over a year now.

I'm trying to backtest it on my gekko instance right now, and I'm getting some errors -- I haven't really even began to try to figure it out, but maybe a glance at the errors I'm getting will make it obvious for you what I'm doing wrong: https://bpaste.net/show/57461e03508a

If not no worries, you have done so much for me already! I'm sure I'll figure out what mistake I'm making.

--- EDIT ---

I figured it out, I had to restart gekko and I had put one of the .js files in the wrong place.

z.schro , I will try playing with yours next! Thanks you guys! this is awesome!
  Reply
#6
Good work Gryphon, did you run this through GekkoGA yet?
  Reply
#7
Just graphed mine against ZSchoro's... Turns out I should have done some more sanity checking before I posted mine - it doesn't even stick between 0-100. Rather embarrassing really. Looking at the graph I have no idea how it ever returned a profit in any backtests.

So... mine is utter junk, ignore it until I get it fixed. ZSchoro's looks pretty good so far.

Here is a quick chart of Zschoro's:
[Image: hFS8MY8.png]
  Reply
#8
Hi all, I would like to thank Mike and the community for all the wonderful work on Gekko!
Even if I'm not a javascript expert I tried to give my small contribution writing the code as a native indicator (STC.js):


Code:
// SCHAFF TREND CYCLE INDICATOR
// ported to Gekko from ProRealCode.com Indicator: https://www.prorealcode.com/prorealtime-indicators/schaff-trend-cycle2/
// 2018 rob2112

var _ = require('lodash');
// required indicators
var EMA = require('./EMA.js');

var Indicator = function(config) {
 this.input = 'price'
 this.result = false;
 this.TCLen = config.TCLen;
 this.Factor = config.Factor;
 this.MA1Len = config.MA1Len;
 this.MA2Len = config.MA2Len;
 this.MA1 = new EMA(this.MA1Len);
 this.MA2 = new EMA(this.MA2Len);
 this.XMAChistory = [];
 this.PFhistory = [];
 this.PF = 0;
 this.PFF = 0;    
 this.prevPF = 0;
 this.prevPFF = 0;
}

Indicator.prototype.update = function (candle)
{
 // macd
 this.MA1.update(candle);
 this.MA2.update(candle);
 this.XMAC = this.MA1.result-this.MA2.result;    

 // macd stochastic
 this.XMAChistory.push(this.XMAC);

 if(_.size(this.XMAChistory) > this.TCLen)
        // remove oldest XMAC value
        this.XMAChistory.shift();
 this.Value1 = _.min(this.XMAChistory);
 this.Value2 = _.max(this.XMAChistory) - this.Value1;
 
 // %Fast K of macd
 if (this.Value2 > 0) this.Frac1 = ((this.XMAC -this.Value1)/this.Value2) * 100;
 else this.Value2 = this. prevValue2;
 
 // smoothed %Fast D of macd
 this.PF = this.prevPF + this.Factor*(this.Frac1-this.prevPF);    
 
 // %Fast D stochastic
 this.PFhistory.push(this.PF);

 if(_.size(this.PFhistory) > this.TCLen)
        // remove oldest PF value
        this.PFhistory.shift();
 this.Value3 = _.min(this.PFhistory);
 this.Value4 = _.max(this.PFhistory) - this.Value3;

 // %Fast K of PF
 if (this.Value4 > 0) this.Frac2 = ((this.PF -this.Value3)/this.Value4)* 100;
 else this.Value4 = this. prevValue4;

 // smoothed %Fast D of PF
 this.PFF = this.prevPFF + this.Factor*(this.Frac2-this.prevPFF);
    
 this.result = this.PFF;

 if (this.Value2) this.prevValue2 = this.Value2;
 if (this.Value4) this.prevValue4 = this.Value4;
 if (this.PF) this.prevPF = this.PF;
 if (this.PFF) this.prevPFF = this.PFF;

}

module.exports = Indicator;
 
this is a test strategy which buys and sells at oversold/overbought crossing (testSTC.js):

Code:
// This is a basic example strategy for Gekko.
// For more information on everything please refer
// to this document:
//
// https://gekko.wizb.it/docs/strategies/creating_a_strategy.html
//

var log = require('../core/log');

// Let's create our own strat
var strat = {};

// Prepare everything our method needs
strat.init = function() {
 this.input = 'candle';
 this.currentTrend = 'long';
 this.requiredHistory = 0;
 this.STC = this.addIndicator('stc', 'STC', this.settings);
}

// What happens on every new candle?
strat.update = function(candle) {

 this.stc = this.indicators.stc.result;    

}

// For debugging purposes.
strat.log = function() {
 console.log('STC:'+this.stc.toFixed(3));
 //log.debug('\t', this.stc.toFixed(3));
    
}

// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function() {

 // Test advice buying when exiting oversold condition and selling when exiting overbought condition
 if(this.stc > 20 && this.prevstc < 20) this.advice('long');
 if(this.stc < 80 && this.prevstc > 80) this.advice('short');
 
 this.prevstc = this.stc;

 
}

module.exports = strat;

and finally this is the code for testSTC.toml:

Code:
TCLen = 10
Factor = 0.5
MA1Len = 23
MA2Len = 50

I couldn't make any serious test, just briefly compared with plotted indicator values, however I compared my result with Zschoro's implementation and found some different values.
Maybe somebody can have a look at the code or has some thoughts about how to test it against known correct results.
  Reply
#9
This is like waiting for a bus... Nothing for ages then three come at once! Cheers Rob Smile

For those interested:
[Image: 2d2StiO.png]
Zschoro's seems to give earlier buy signals, sell signals are pretty mixed, but I haven't got time to play with backtesting them right now...
  Reply
#10
This is above and beyond anything I had hoped for when I made this post. Thank you guys so much. Gekko is great. Mike is awesome. You all rock. <3<3<3
  Reply


Forum Jump:


Users browsing this thread: