Skip to content

Commit

Permalink
only create trigger after init trade is completed
Browse files Browse the repository at this point in the history
ref correct trailValue prop
  • Loading branch information
askmike committed Aug 27, 2018
1 parent 7a26284 commit a63d927
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion plugins/eventLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _.each(subscriptions, sub => {
}

EventLogger.prototype[sub.handler] = (event, next) => {
log.debug(`\t\t\t\t[EVENT ${sub.event}]\n`, event);
log.info(`\t\t\t\t[EVENT ${sub.event}]\n`, event);
if(_.isFunction(next))
next();
}
Expand Down
59 changes: 31 additions & 28 deletions plugins/trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,34 +181,6 @@ Trader.prototype.processAdvice = function(advice) {
});
}

const trigger = advice.trigger;

if(trigger && trigger.type === 'trailingStop') {
const triggerId = 'trigger-' + (++this.propogatedTriggers);

this.deferredEmit('triggerCreated', {
id: triggerId,
at: advice.date,
initialPrice: this.price,
type: 'trialingStop',
proprties: {
trail: trigger.trailValue
}
});

this.activeStopTrigger = {
id: triggerId,
adviceId: advice.id,
instance: this.broker.createTrigger({
type: trigger.type,
onTrigger: this.onStopTrigger,
props: {
trail: trigger.trailValue,
}
})
}
}

amount = this.portfolio.currency / this.price * 0.95;

log.info(
Expand Down Expand Up @@ -354,6 +326,37 @@ Trader.prototype.createOrder = function(side, amount, advice, id) {
feePercent: summary.feePercent,
effectivePrice
});

if(
side === 'buy' &&
advice.trigger &&
advice.trigger.type === 'trailingStop'
) {
const trigger = advice.trigger;
const triggerId = 'trigger-' + (++this.propogatedTriggers);

this.deferredEmit('triggerCreated', {
id: triggerId,
at: advice.date,
initialPrice: this.price,
type: 'trialingStop',
properties: {
trail: trigger.trailValue
}
});

this.activeStopTrigger = {
id: triggerId,
adviceId: advice.id,
instance: this.broker.createTrigger({
type: 'trailingStop',
onTrigger: this.onStopTrigger,
props: {
trail: trigger.trailValue,
}
})
}
}
});
})
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/tradingAdvisor/baseTradingMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Base.prototype.advice = function(newDirection) {
trigger = newDirection.trigger;

if(trigger.trailPercentage && !trigger.trailValue) {
trigger.trailValue = (1 - (trigger.trailPercentage / 100)) * this.candle.close;
trigger.trailValue = trigger.trailPercentage / 100 * this.candle.close;
log.info('[StratRunner] Trailing stop trail value specified as percentage, setting to:', trigger.trailValue);
}
}
Expand Down

0 comments on commit a63d927

Please sign in to comment.