Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss and SetProfitTarget

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SetStopLoss and SetProfitTarget

    When using EnterLong / EnterShort the market orders are often different from the previous price. Will this code guarantee that the stop loss and profit targets will always be 5 ticks from the entry price. Or am I over thinking this? Or is there a better way?
    PHP Code:
    TargetTicks       = 5;
    StopTicks         = 5;
    BreakevenTicks = 3;
    
    private override void OnBarUpdate()
    {
       EnterLong(1, "MyEntry");
    
       TargetPrice           = Position.AveragePrice + (TargetTicks * TickSize);
       CurrentStopPrice      = Position.AveragePrice - (StopTicks * TickSize);
       BreakevenTriggerPrice = Position.AveragePrice + (BreakevenTicks * TickSize);
       BreakevenPrice        = Instrument.MasterInstrument.RoundDownToTickSize(Position.AveragePrice);
    
    
       SetStopLoss("MyEntry", CalculationMode.Price, false);
       SetProfitTarget("MyEntry", CalculationMode.Price, false);
    
       StopMovedToBreakEven = false;
    } 
    
    If there is a better way could you please point me in the right direction.
    Thanks.

    #2
    Hello TAJTrades,

    Thank you for the post.

    For a market order, this would only guarantee the price at the time when you submit the order but would not confirm its fill price.

    If you want to submit target orders based on the market orders fill price, you should instead use OnExecutionUpdate to track the entry fill. Upon filling you could check its average price to position your targets at that point.

    We have a sample that demonstrates this concept here:


    This does not specifically use SetStopLoss/SetProfitTarget but it could easily be modified to use those instead of the limit orders it has. The main point of this sample is to show how to track the entry order and get its execution.Order.AverageFillPrice. This can be used to position the targets at a specified amount of ticks from the actual fill price.

    I look forward to being of further assistance.

    Comment


      #3
      Thank you. I totally missed that sample code. It is exactly what I was looking for.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      81 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      41 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      64 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      66 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X