Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Create BB strategy #1623

Closed
wants to merge 3 commits into from
Closed

Create BB strategy #1623

wants to merge 3 commits into from

Conversation

okibcn
Copy link

@okibcn okibcn commented Jan 3, 2018

This is a basic BB strategy:
It advice long when candle close price enter into the band from a lower value. On the other side it advice a short position when the candle close price enter into the band from a top value.

  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

A new feature

  • What is the current behavior? (You can also link to an open issue here)

  • What is the new behavior (if this is a feature change)?

  • Other information:

This is a basic BB strategy:
It advice long when candle close price enter into the band from a lower value. On the other side it advice a short position when the candle close price enter into the band from a top value.
Copy link

@billymcintosh billymcintosh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability recommendation. Multiple statements could be added to one if block or a switch statement.

else {
// There is a zone change
log.debug('Leaving zone: ',this.trend.zone)
if (this.trend.zone == 'top') this.advice('short');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than two different if statements where the zone equals the same, why not?

if (this.trend.zone === 'top') {
  log.debug('>>>>>   SIGNALING ADVICE SHORT   <<<<<<<<<<<<');
  this.advice('short');
}

Perhaps a switch statement would be more readable?

@hand0722
Copy link

hand0722 commented Jan 9, 2018

Thanks a lot for your excellent work!

Here are some customizations to your strategy indicator "bb.js"

Formula: http://vtadwiki.vtad.de/images/math/c/cd/cdbe5ac0c968565ba093b8add6766bd2.png
which I found @ http://vtadwiki.vtad.de/index.php/Bollinger_B%C3%A4nder

// no required indicators
// Bollinger Bands - Okibcn implementation 2018-01-02
// hand0722 customized 2019-01-09

var Indicator = function(BBSettings) {
this.input = 'price';
this.settings = BBSettings;
// Settings:
// TimePeriod: The amount of samples used for the average.
// NbDevUp: The distance in stdev of the upper band from the SMA.
// NbDevDn: The distance in stdev of the lower band from the SMA.
this.prices = [];
this.diffs = [];
this.age = 0; // age = Warm Up Period
this.sum = 0;
this.sumsq = 0;
this.upper = 0;
this.middle = 0;
this.lower = 0;
}

Indicator.prototype.update = function(price) {

var tail = this.prices[this.age] || 0; // oldest price in window
var diffsTail = this.diffs[this.age] || 0; // oldest average in window

this.prices[this.age] = price;
this.sum += price - tail;
this.middle = this.sum / this.prices.length; // SMA value

// your code:
// this.diffs[this.age] = (price - this.middle);
// customized code (see formula), we have to build a math.pow:
this.diffs[this.age] = Math.pow((price - this.middle), 2);

// your code:
// this.sumsq += this.diffs[this.age] ** 2 - diffsTail ** 2;
// customized code:
this.sumsq += this.diffs[this.age] - diffsTail;

// your code:
// var stdev = Math.sqrt(this.sumsq) / this.prices.length;
// customized code (see formula), we have to build a math.sqrt over the whole expression:
var stdev = Math.sqrt(this.sumsq / this.prices.length);

this.upper = this.middle + this.settings.NbDevUp * stdev;
this.lower = this.middle - this.settings.NbDevDn * stdev;

this.age = (this.age + 1) % this.settings.TimePeriod;

}
module.exports = Indicator;

@gvdhoven
Copy link

@okibcn @hand0722 why not use the Tulind indicator: BBands? Besides the fact that writing your own indicators is fun, what is the profit of using yours above the Tulind indicator?

@hand0722
Copy link

hand0722 commented Jan 26, 2018 via email

@mactac
Copy link

mactac commented Jan 28, 2018

Forgive my noob-ish-ness, but What exactly (steps) do I need to do to add this to Gekko? And can I access it via the html UI?

I added the3 files above in their respective folders, but the strategy does not appear in the web UI.

Is there anything else I need to do to make it work?
And what do I need to do to add it to the web UI?

Thanks!

@mactac
Copy link

mactac commented Jan 28, 2018

OK, update - I got it to appear in the UI, but it won't seem to run. When I try to do a backtest, I get:

2018-01-27 17:42:43 (INFO): Setting up Gekko in backtest mode
2018-01-27 17:42:43 (INFO):
2018-01-27 17:42:43 (INFO): Setting up:
2018-01-27 17:42:43 (INFO): Trading Advisor
2018-01-27 17:42:43 (INFO): Calculate trading advice
2018-01-27 17:42:43 (INFO): Using the strategy: BB
/home/gekko/strategies/indicators/BB.js:30
this.sumsq += this.diffs[this.age] ** 2 - diffsTail ** 2;
^

((note: the carat symbol is underneath the second "*" - it won't show properly in comments))

SyntaxError: Unexpected token *
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/gekko/strategies/BB.js:10:10)
xxx POST /api/backtest 500 585ms -

Error: non-error thrown: Child process has died.
at Object.onerror (/home/gekko/node_modules/koa/lib/context.js:105:40)
at process._tickCallback (internal/process/next_tick.js:109:7)

@hand0722
Copy link

hand0722 commented Jan 29, 2018 via email

@askmike
Copy link
Owner

askmike commented Jan 30, 2018

@okibcn hey! Apologies for the delay.

This looks awesome and I've love to include this. Would you mind adding a test case like this one for the MACD indicator?

@mohdalsaqqal
Copy link

man i using this strategy and its always work
using the BB indicator , LTC\USTD , 1 min chart .
i choose the last price that the candle touch the lower BB line , and open buy order and wait the price to come down to it , then when the buy already done , i see where BB upper line where and open sell order and wait while the price go up .

your method work as soon as the candle hit the upprBB you will sell , but sometimes the candle hit the upperBB with lower price than the buying price . so you have to compare prices ,
im sorry about my bad english , but this strategy always work for me , the problem is i dont know how to automate it .

@okibcn

untitled

@mactac
Copy link

mactac commented Feb 19, 2018

Sorry for a stupid question, Is anyone able to explain to me precisely what the variables mean?

For example, what does nbdevup compare against exactly? Is it the amount that the last candle went above thetop BB? is it $? is is %?

@Gab0
Copy link
Contributor

Gab0 commented Mar 16, 2018

A test case for BB:

test_BB.js ##########
var method = {};
method.init = function() {

    this.name = "TALib-port-tester";
var BollingerSettings = {

       TimePeriod: 20,
        NbDevUp: 2,
        NbDevDn: 2,
        optInMAType: 2
}
var TABollingerSettings = {

       optInTimePeriod: 20,
        optInNbDevUp: 2,
        optInNbDevDn: 2,
        optInMAType: 2,
optInNbStdDevs: 2

}
    this.addIndicator('ind', 'BB', BollingerSettings);
    this.addTalibIndicator("tind", "bbands", TABollingerSettings);
    this.addTulipIndicator("tuind", "bbands", TABollingerSettings);
};

method.check = function() {};
method.update= function(candle) {

var bbands = this.indicators.ind;

	console.log('L ' + bbands.lower);
	console.log('M ' + bbands.middle);
	console.log('U ' + bbands.upper);
        console.log(this.tulipIndicators.tuind.result);
        console.log(this.talibIndicators.tind.result);

};
method.log = function() {};

module.exports = method;

@stale
Copy link

stale bot commented Oct 24, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you feel this is very a important issue please reach out the maintainer of this project directly via e-mail: gekko at mvr dot me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants