Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// no required indicators | ||
// Bollinger Bands - Okibcn implementation 2018-01-02 | ||
/* | ||
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; | ||
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 | ||
this.diffs[this.age] = (price - this.middle); | ||
this.sumsq += this.diffs[this.age] ** 2 - diffsTail ** 2; | ||
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; | ||
*/ | ||
|
||
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// no required indicators | ||
// Bollinger Bands - Okibcn implementation 2018-01-02 | ||
/* | ||
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; | ||
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 | ||
this.diffs[this.age] = (price - this.middle); | ||
this.sumsq += this.diffs[this.age] ** 2 - diffsTail ** 2; | ||
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; | ||
*/ | ||
|
||
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// no required indicators | ||
// Bollinger Bands - Okibcn implementation 2018-01-02 | ||
/* | ||
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; | ||
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 | ||
this.diffs[this.age] = (price - this.middle); | ||
this.sumsq += this.diffs[this.age] ** 2 - diffsTail ** 2; | ||
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; | ||
*/ | ||
|
||
// 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; |