I'm a bit stuck with my own NT8 stratagy realization. I have a Tradingivew version, which I'm trying to replicate into NT8.
Sadly, NT8 doesn't have inbuilt logic called "Profit Trailing" (trailing stop is enabling only after reaching the specific profit target instead, not from the initial entry), so I'm working on my own version of this code.
Basically, I'm tracking the amont of ticks in profit (ES/NQ futures in my case) using global variable, and when the needed point is reached, I'm calling my function each tick (Strategy.OnEachTick + Tick Replay enabled), checking how much I'm in profit, and trying to move stoploss with a little offset (1-2 ticks. based on input)
I'm also using Output to check each tick calculation results and can see that all my variables are correct, but, when I'm trying to update stoploss using SetStopLoss with the my price level (SetStopLoss(CalculationMode.Price, newStopAbsolutePrice) - the whole trade is closed at unexpected place - at the OPEN of current bar (for all trades that were profitable enough and triggered my logic), for longs and shorts the situation is the same.
TF: 1m, and I checked on 1s TF later to be sure that price wasn't even so low/high to be executed at this level
TLDR:
I'm calling SetStopLoss(CalculationMode.Price, newStopAbsolutePrice); with 15375.25 value
But for some reason (based on logs from OnExecutionUpdate method) it receives the OPEN[0] as value, which is incorrect
Thanks in advance!
I know that it could be hard to understand what I'm talking about, so will try to add as much as possible info and screenshots about this behaviour:
- NQ 1m TF
- Strategy.OnEachTick + Tick Replay enabled
Code:
void updateTrailingStop(double currentPrice) { // only when we're already in some trade if (Position.MarketPosition != MarketPosition.Flat) { Print("currentPrice: " + currentPrice + " Position.MarketPosition: " + Position.MarketPosition + " ToTime(Time[0]) " + Time[0] ); // If no double argument is provided in the call, the current (real-time) Last price will be substituted in. // But for back-testing a double price to compare against should be provided. double currentProfitTicks = Position.GetUnrealizedProfitLoss(PerformanceUnit.Ticks, currentPrice); Print("currentProfitTicks " + currentProfitTicks + " highestProfitTicks " + highestProfitTicks); // Convert profit target and trailing stop loss to ticks // TickSize for ES/NQ Futures is 0.25, so 1 offest tick = 0.25 points double trailingStopLossPoints = trailingStopLossTicks * TickSize; // when we're reached the point of target profit, we should enable our logic for custom trail if (currentProfitTicks >= profitTargetTicks) { // update trail via SetStopLoss ONLY if current profit is more than was already reached if (currentProfitTicks > highestProfitTicks) { // update the highest reached profit highestProfitTicks = currentProfitTicks; // calculate new absolute price for stoploss newStopAbsolutePrice = Position.MarketPosition == MarketPosition.Long ? (currentPrice - 0.5) : (currentPrice + 0.5); Print("YEY! SET STOP LOSS: highestProfitTicks " + highestProfitTicks + " newStopAbsolutePrice " + newStopAbsolutePrice + " "); // set new stoploss, every other type (ticks, points, etc) works only as an offset from ENTRY, sadly SetStopLoss(CalculationMode.Price, newStopAbsolutePrice); } } } else { highestProfitTicks = 0; } } protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { Print("Execution Order Name " + execution.Order.Name + " and Size " + quantity + " and Price " + price); }
1st image - NT8 Output from backtest, as you can see, currentPrice is 15375.75, newStopAbsolutePrice is 15375.25 (2 ticks lower), but in OnExecutionUpdate it's somehow become 15370
2nd image - part of code (already attached)
3rd image - how it went on NT8 backtest
4th image - how it should be (Tradingview backtest)
Comment