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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X