Gekko Forum
Schaff Trend Cycle - Printable Version

+- Gekko Forum (
+-- Forum: Gekko (
+--- Forum: Strategy Development (
+--- Thread: Schaff Trend Cycle (/thread-56716.html)

Pages: 1 2 3

Schaff Trend Cycle - Henry151 - 04-10-2018

Hello everybody,

Have any of you played with the Schaff Trend Cycle at all? I think it could be really great.

I have managed to put together a strategy in "pine script," the language used over at tradingview:

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:

Mike was kind enough to link me to this script:

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!

RE: Schaff Trend Cycle - Henry151 - 04-12-2018

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!

RE: Schaff Trend Cycle [BOUNTY] - Gryphon - 04-12-2018

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:


Edits as first strategy I posted was pretty flawed, fixed now.

RE: Schaff Trend Cycle [BOUNTY] - z.schro - 04-12-2018

Looks like Gryphon beat me to it, but I also wrote this up:

I will need to compare our implementations.


RE: Schaff Trend Cycle [BOUNTY] - Henry151 - 04-12-2018

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:

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!

RE: Schaff Trend Cycle [BOUNTY] - Kris191 - 04-13-2018

Good work Gryphon, did you run this through GekkoGA yet?

RE: Schaff Trend Cycle [BOUNTY] - Gryphon - 04-13-2018

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]

RE: Schaff Trend Cycle [BOUNTY] - rob2112 - 04-13-2018

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):

// ported to Gekko from Indicator:
// 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.XMAC = this.MA1.result-this.MA2.result;    

 // macd stochastic

 if(_.size(this.XMAChistory) > this.TCLen)
        // remove oldest XMAC value
 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

 if(_.size(this.PFhistory) > this.TCLen)
        // remove oldest PF value
 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):

// This is a basic example strategy for Gekko.
// For more information on everything please refer
// to this document:

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) { =;    


// For debugging purposes.
strat.log = function() {

// 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( > 20 && this.prevstc < 20) this.advice('long');
 if( < 80 && this.prevstc > 80) this.advice('short');
 this.prevstc =;


module.exports = strat;

and finally this is the code for testSTC.toml:

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.

RE: Schaff Trend Cycle [BOUNTY] - Gryphon - 04-13-2018

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...

RE: Schaff Trend Cycle [BOUNTY] - Henry151 - 04-13-2018

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