When the first profit target is hit, I want to move the stop loss to the entry price and then move the second profit target to the current price plus 10 points.
Whatever I try though it doesn't work, it doesn't set the initial stop loss and profit targets.
Using different coding I was able to set them initially, but then I could not move them them through code
Please could you help?
Thank you
[#region Using declarations
using System;
using NinjaTrader.Cbi;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.Data;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class DynamicProfitTargetExample : Strategy
{
private double entryPrice;
private bool isBreakEvenSet;
private bool targetsMoved;
private Order stopLossOrder;
private Order profitTargetOrder1;
private Order profitTargetOrder2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Strategy example to set a break-even stop loss and move it 4 points higher.";
Name = "DynamicProfitTargetExample";
Calculate = Calculate.OnEachTick;
IsInstantiatedOnEachOptimizationIteration = false;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
StopTargetHandling = StopTargetHandling.ByStrategyPosition;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
}
else if (State == State.Configure)
{
// Configure your strategy here
}
}
protected override void OnBarUpdate()
{
// Ensure we have enough bars to proceed
if (CurrentBar < BarsRequiredToTrade)
return;
// Example entry condition (you would replace this with your own logic)
if (CrossAbove(Close, SMA(14), 1))
{
// Enter a long position
EnterLong(2);
// Set the entry price
entryPrice = Close[0];
// Set the initial profit targets
profitTargetOrder1 = ExitLongLimit(0, true, Position.Quantity / 2, entryPrice + 7, "Profit target 1", "Entry");
profitTargetOrder2 = ExitLongLimit(0, true, Position.Quantity / 2, entryPrice + 15, "Profit target 2", "Entry");
// Set the stop loss
stopLossOrder = ExitLongStopMarket(0, true, Position.Quantity, entryPrice - 2, "Stop loss", "Entry");
// Reset the flags
isBreakEvenSet = false;
targetsMoved = false;
}
// Adjust stop loss profit targets if price moves 5 points above the entry price
if (Position.MarketPosition == MarketPosition.Long && Close[0] >= entryPrice + 5 && !targetsMoved)
{
stopLossOrder = ExitLongStopMarket(0, true, Position.Quantity, entryPrice, "Stop loss", "Entry");
profitTargetOrder1 = ExitLongLimit(0, true, Position.Quantity / 2, entryPrice + 10, "Profit target 1", "Entry");
profitTargetOrder2 = ExitLongLimit(0, true, Position.Quantity / 2, entryPrice + 20, "Profit target 2", "Entry");
targetsMoved = true;
}
// Determine if the current position is in profit or loss
if (Position.MarketPosition == MarketPosition.Long)
{
if (Close[0] > entryPrice)
{
Print("The position is in profit.");
}
else
{
Print("The position is in loss.");
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (Close[0] < entryPrice)
{
Print("The position is in profit.");
}
else
{
Print("The position is in loss.");
}
}
}
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order.OrderState == OrderState.Filled)
{
// Check if the execution was for a profit target or stop loss
if (execution.Order.Name == "Profit target 1" || execution.Order.Name == "Profit target 2" || execution.Order.Name == "Stop loss")
{
CancelStopLossAndProfitTarget();
}
}
}
private void CancelStopLossAndProfitTarget()
{
// Cancel the stop loss order
if (stopLossOrder != null)
{
CancelOrder(stopLossOrder);
stopLossOrder = null;
}
// Cancel the profit target orders
if (profitTargetOrder1 != null)
{
CancelOrder(profitTargetOrder1);
profitTargetOrder1 = null;
}
if (profitTargetOrder2 != null)
{
CancelOrder(profitTargetOrder2);
profitTargetOrder2 = null;
}
}
}
}

Comment