@@ -5,9 +5,11 @@ const IndicatorBuilder = require('./dict/indicator_builder');
5
5
const IndicatorPeriod = require ( './dict/indicator_period' ) ;
6
6
const ta = require ( '../../utils/technical_analysis' ) ;
7
7
const Resample = require ( '../../utils/resample' ) ;
8
+ const CommonUtil = require ( '../../utils/common_util' ) ;
8
9
const StrategyContext = require ( '../../dict/strategy_context' ) ;
9
10
const Ticker = require ( '../../dict/ticker' ) ;
10
11
const SignalResult = require ( './dict/signal_result' ) ;
12
+ const Position = require ( '../../dict/position' ) ;
11
13
12
14
module . exports = class StrategyManager {
13
15
constructor ( technicalAnalysisValidator , exchangeCandleCombine , logger , projectDir ) {
@@ -94,16 +96,37 @@ module.exports = class StrategyManager {
94
96
* @param symbol
95
97
* @param options
96
98
* @param lastSignal
99
+ * @param lastSignalEntry
97
100
* @returns {Promise<array> }
98
101
*/
99
- async executeStrategyBacktest ( strategyName , exchange , symbol , options , lastSignal ) {
102
+ async executeStrategyBacktest ( strategyName , exchange , symbol , options , lastSignal , lastSignalEntry ) {
100
103
const results = await this . getTaResult ( strategyName , exchange , symbol , options ) ;
101
104
if ( ! results || Object . keys ( results ) . length === 0 ) {
102
105
return { } ;
103
106
}
104
107
105
108
const price = results . _candle ? results . _candle . close : undefined ;
106
- const context = StrategyContext . create ( new Ticker ( exchange , symbol , undefined , price , price ) ) ;
109
+
110
+ let context ;
111
+ if ( lastSignal && lastSignalEntry && price ) {
112
+ // provide a suitable value; its just backtesting
113
+ const amount = lastSignal === 'short' ? - 1 : 1 ;
114
+
115
+ context = StrategyContext . createFromPosition (
116
+ new Ticker ( exchange , symbol , undefined , price , price ) ,
117
+ new Position (
118
+ symbol ,
119
+ lastSignal ,
120
+ amount ,
121
+ CommonUtil . getProfitAsPercent ( lastSignal , price , lastSignalEntry ) ,
122
+ undefined ,
123
+ lastSignalEntry
124
+ )
125
+ ) ;
126
+ } else {
127
+ context = StrategyContext . create ( new Ticker ( exchange , symbol , undefined , price , price ) ) ;
128
+ }
129
+
107
130
context . lastSignal = lastSignal ;
108
131
109
132
const indicatorPeriod = new IndicatorPeriod ( context , results ) ;
0 commit comments