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.
* experimental sar strat * fix sar description * default 1m period for sar * sar indicator grey * less noisy "vs our price" debug msgs
- Loading branch information
Showing
2 changed files
with
92 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,6 @@ | ||
module.exports = { | ||
_ns: 'zenbot', | ||
|
||
'strategies.sar': require('./strategy'), | ||
'strategies.list[]': '#strategies.sar' | ||
} |
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,86 @@ | ||
var z = require('zero-fill') | ||
, n = require('numbro') | ||
|
||
module.exports = function container (get, set, clear) { | ||
return { | ||
name: 'sar', | ||
description: 'Parabolic SAR', | ||
|
||
getOptions: function () { | ||
this.option('period', 'period length', String, '1m') | ||
this.option('min_periods', 'min. number of history periods', Number, 52) | ||
this.option('sar_af', 'acceleration factor for parabolic SAR', Number, 0.025) | ||
this.option('sar_max_af', 'max acceleration factor for parabolic SAR', Number, 0.55) | ||
}, | ||
|
||
calculate: function (s) { | ||
if (s.lookback.length >= s.options.min_periods) { | ||
if (!s.trend) { | ||
if (s.period.high > s.lookback[s.lookback.length - 1].high) { | ||
// start with uptrend | ||
s.trend = 'up' | ||
s.sar = Math.min(s.lookback[1].low, s.lookback[0].low) | ||
s.sar_ep = s.period.high | ||
s.sar_af = s.options.sar_af | ||
for (var idx = 0; idx < s.lookback.length; idx++) { | ||
s.sar_ep = Math.max(s.sar_ep, s.lookback[idx].high) | ||
} | ||
} | ||
else { | ||
s.trend = 'down' | ||
s.sar = Math.max(s.lookback[1].high, s.lookback[0].high) | ||
s.sar_ep = s.period.low | ||
s.sar_af = s.options.sar_af | ||
for (var idx = 0; idx < s.lookback.length; idx++) { | ||
s.sar_ep = Math.min(s.sar_ep, s.lookback[idx].low) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
onPeriod: function (s, cb) { | ||
if (typeof s.sar === 'number') { | ||
if (s.trend === 'up') { | ||
s.sar = Math.min(s.lookback[1].low, s.lookback[0].low, s.sar + (s.sar_af * (s.sar_ep - s.sar))) | ||
} | ||
else { | ||
s.sar = Math.max(s.lookback[1].high, s.lookback[0].high, s.sar - (s.sar_af * (s.sar - s.sar_ep))) | ||
} | ||
if (s.trend === 'down') { | ||
if (s.period.high >= s.sar) { | ||
s.trend = 'up' | ||
s.signal = 'buy' | ||
s.sar_ep = s.period.low | ||
s.sar_af = s.options.sar_af | ||
} | ||
else if (s.period.low < s.sar_ep && s.sar_af < s.options.sar_max_af) { | ||
s.sar_ep = s.period.low | ||
s.sar_af += s.options.sar_af | ||
} | ||
} | ||
else if (s.trend === 'up') { | ||
if (s.period.low <= s.sar) { | ||
s.trend = 'down' | ||
s.signal = 'sell' | ||
s.sar_ep = s.period.high | ||
s.sar_af = s.options.sar_af | ||
} | ||
else if (s.period.high > s.sar_ep && s.sar_af < s.options.sar_max_af) { | ||
s.sar_ep = s.period.high | ||
s.sar_af += s.options.sar_af | ||
} | ||
} | ||
} | ||
cb() | ||
}, | ||
|
||
onReport: function (s) { | ||
var cols = [] | ||
if (typeof s.sar === 'number') { | ||
cols.push(z(8, n(s.sar).subtract(s.period.close).divide(s.period.close).format('0.00%'), ' ').grey) | ||
} | ||
return cols | ||
} | ||
} | ||
} |