This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all tablib indicators to a promise and replace calculation usage…
… in strategies as this function does not handle async behavior (#1383)
- Loading branch information
Showing
8 changed files
with
153 additions
and
122 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
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
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 |
---|---|---|
@@ -1,49 +1,59 @@ | ||
var talib = require('talib') | ||
|
||
module.exports = function ta_ema (s, key, length) { | ||
//create object for talib. only close is used for now but rest might come in handy | ||
if (!s.marketData) { | ||
s.marketData = { open: [], close: [], high: [], low: [], volume: [] } | ||
} | ||
if (s.lookback.length > s.marketData.close.length) { | ||
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) { | ||
//console.log('add data') | ||
s.marketData.close.push(s.lookback[i].close) | ||
module.exports = function ta_ema (s, length) { | ||
return new Promise(function(resolve, reject) { | ||
// create object for talib. only close is used for now but rest might come in handy | ||
if (!s.marketData) { | ||
s.marketData = { open: [], close: [], high: [], low: [], volume: [] } | ||
} | ||
//dont calculate until we have enough data | ||
if (s.marketData.close.length >= length) { | ||
//fillup marketData for talib. | ||
//this might need improvment for performance. | ||
//for (var i = 0; i < length; i++) { | ||
// s.marketData.close.push(s.lookback[i].close); | ||
//} | ||
//fillup marketData for talib. | ||
var tmpMarket = JSON.parse(JSON.stringify(s.marketData.close)) | ||
//add current period | ||
tmpMarket.push(s.period.close) | ||
|
||
//doublecheck length. | ||
if (tmpMarket.length >= length) { | ||
talib.execute({ | ||
name: 'EMA', | ||
startIdx: 0, | ||
endIdx: tmpMarket.length -1, | ||
inReal: tmpMarket, | ||
optInTimePeriod: length | ||
}, function (err, result) { | ||
if (err) { | ||
console.log(err) | ||
return | ||
} | ||
//Result format: (note: outReal can have multiple items in the array) | ||
// { | ||
// begIndex: 8, | ||
// nbElement: 1, | ||
// result: { outReal: [ 1820.8621111111108 ] } | ||
// } | ||
s.period[key] = result.result.outReal[(result.nbElement - 1)] | ||
}) | ||
if (s.lookback.length > s.marketData.close.length) { | ||
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) { | ||
s.marketData.close.push(s.lookback[i].close) | ||
} | ||
|
||
//dont calculate until we have enough data | ||
if (s.marketData.close.length >= length) { | ||
//fillup marketData for talib. | ||
//this might need improvment for performance. | ||
//for (var i = 0; i < length; i++) { | ||
// s.marketData.close.push(s.lookback[i].close); | ||
//} | ||
//fillup marketData for talib. | ||
let tmpMarket = s.marketData.close.slice() | ||
|
||
//add current period | ||
tmpMarket.push(s.period.close) | ||
|
||
//doublecheck length. | ||
if (tmpMarket.length >= length) { | ||
talib.execute({ | ||
name: 'EMA', | ||
startIdx: 0, | ||
endIdx: tmpMarket.length -1, | ||
inReal: tmpMarket, | ||
optInTimePeriod: length | ||
}, function (err, result) { | ||
if (err) { | ||
console.log(err) | ||
reject(err, result) | ||
return | ||
} | ||
|
||
//Result format: (note: outReal can have multiple items in the array) | ||
// { | ||
// begIndex: 8, | ||
// nbElement: 1, | ||
// result: { outReal: [ 1820.8621111111108 ] } | ||
// } | ||
resolve({ | ||
'outReal': result.result.outReal[(result.nbElement - 1)], | ||
}) | ||
}) | ||
} | ||
} else { | ||
resolve() | ||
} | ||
} | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,55 +1,61 @@ | ||
var talib = require('talib') | ||
|
||
module.exports = function macd (s, macd_key,hist_key,sig_key, slow_period,fast_period,signal_period) { | ||
//check parameters | ||
// if (fast_period > slow_period) { | ||
// console.log('incorrect parameters MACD. (fast_period < slow_period || signal_period > fast_period)') | ||
// return; | ||
// } | ||
//create object for talib. only close is used for now but rest might come in handy | ||
if (!s.marketData) { | ||
s.marketData = { open: [], close: [], high: [], low: [], volume: [] } | ||
} | ||
if (s.lookback.length > s.marketData.close.length) { | ||
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) { | ||
//console.log('add data') | ||
s.marketData.close.push(s.lookback[i].close) | ||
} | ||
} | ||
var periods_necessary = slow_period + signal_period - 1 | ||
//dont calculate until we have enough data | ||
if (s.marketData.close.length >= periods_necessary) { | ||
//fillup marketData for talib. | ||
var tmpMarket = JSON.parse(JSON.stringify(s.marketData.close)) | ||
//add current period | ||
tmpMarket.push(s.period.close) | ||
module.exports = function macd (s, slow_period, fast_period, signal_period) { | ||
return new Promise(function(resolve, reject) { | ||
// check parameters | ||
// if (fast_period > slow_period) { | ||
// console.log('incorrect parameters MACD. (fast_period < slow_period || signal_period > fast_period)') | ||
// return; | ||
// } | ||
|
||
talib.execute({ | ||
name: 'MACD', | ||
startIdx: 0, | ||
endIdx: tmpMarket.length -1, | ||
inReal: tmpMarket, | ||
optInFastPeriod: fast_period, | ||
optInSlowPeriod: slow_period, | ||
optInSignalPeriod: signal_period | ||
}, function (err, result) { | ||
if (err) { | ||
console.log(err) | ||
return | ||
//create object for talib. only close is used for now but rest might come in handy | ||
if (!s.marketData) { | ||
s.marketData = { open: [], close: [], high: [], low: [], volume: [] } | ||
} | ||
if (s.lookback.length > s.marketData.close.length) { | ||
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) { | ||
s.marketData.close.push(s.lookback[i].close) | ||
} | ||
//Result format: (note: outReal can have multiple items in the array) | ||
// { | ||
// begIndex: 8, | ||
// nbElement: 1, | ||
// result: { outReal: [ 1820.8621111111108 ] } | ||
// } | ||
// console.log(JSON.stringify(marketData)) | ||
// console.log(JSON.stringify(result.result)) | ||
s.period[macd_key] = result.result.outMACD[(result.nbElement - 1)] | ||
s.period[hist_key] = result.result.outMACDHist[(result.nbElement - 1)] | ||
s.period[sig_key] = result.result.outMACDSignal[(result.nbElement - 1)] | ||
}) | ||
} | ||
|
||
} | ||
} | ||
let periods_necessary = slow_period + signal_period - 1 | ||
//dont calculate until we have enough data | ||
|
||
if (s.marketData.close.length >= periods_necessary) { | ||
// fillup marketData for talib. | ||
let tmpMarket = s.marketData.close.slice() | ||
|
||
// add current period | ||
tmpMarket.push(s.period.close) | ||
|
||
talib.execute({ | ||
name: 'MACD', | ||
startIdx: 0, | ||
endIdx: tmpMarket.length - 1, | ||
inReal: tmpMarket, | ||
optInFastPeriod: fast_period, | ||
optInSlowPeriod: slow_period, | ||
optInSignalPeriod: signal_period | ||
}, function (err, result) { | ||
if (err) { | ||
reject(err) | ||
console.log(err) | ||
return | ||
} | ||
//Result format: (note: outReal can have multiple items in the array) | ||
// { | ||
// begIndex: 8, | ||
// nbElement: 1, | ||
// result: { outReal: [ 1820.8621111111108 ] } | ||
// } | ||
resolve({ | ||
'macd': result.result.outMACD[(result.nbElement - 1)], | ||
'macd_histogram': result.result.outMACDHist[(result.nbElement - 1)], | ||
'macd_signal': result.result.outMACDSignal[(result.nbElement - 1)], | ||
}) | ||
}) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
} |
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
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
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
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