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 NullPointStrategies, Today, 05:17 AM
      0 responses
      51 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      129 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X