Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

stop order management

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

    stop order management

    I have a question about stop order management. I have one trading system, if it=1, then go long, if it=-1, then go short. if allowing reverse position, from long to short or via verse. I want to place a stop order to the exchange immediately after there is a position.The issue is when go from short to long or via verse, the buy to cover stop order doesn't get cancelled, so I got a error message as follows

    4/14/2025 10:43:00 AM,Default,Strategy 'RKV8StopOrder/340960992' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests attempted to close the position and terminated itself.,
    4/14/2025 10:43:00 AM,Order,xxx Buy order stop price must be above last trade price[Order parameters are invalid] affected Order: BuyToCover 2 StopMarket @ 18868.5,
    4/14/2025 10:43:00 AM,Order,Order='8734009144/xxxxx' Name='StopShort' New state='Rejected' Instrument='MNQ JUN25' Action='Buy to cover' Limit price=0 Stop price=18868.5 Quantity=2 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Order rejected' Native error='Buy order stop price must be above last trade price[Order parameters are invalid]',​


    Here is my code.

    // Manage position and stops
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Reset counters when flat
    barsSinceEntry = 0;
    isFirstBarAfterEntry = false;

    // Cancel all stop orders when flat
    ExitLongStopMarket(0, true, 0, 0, "StopLong", "LongEntry");
    ExitShortStopMarket(0, true, 0, 0, "StopShort", "ShortEntry");
    }
    else
    {
    // Increment bars since entry
    barsSinceEntry++;

    // Check if we need to transition from initial stop to trailing stop
    if (barsSinceEntry > 1 && isFirstBarAfterEntry)
    {
    isFirstBarAfterEntry = false;

    if (Position.MarketPosition == MarketPosition.Long)
    {
    // Replace initial stop with trailing stop
    double stopPrice = RK1.TrailingStop[0] - 20;
    ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "StopLong", "LongEntry");
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Replace initial stop with trailing stop
    double stopPrice = RK1.TrailingStop[0] + 20;
    ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "StopShort", "ShortEntry");
    }
    }
    // On subsequent bars, continue updating the trailing stop
    else if (barsSinceEntry > 1 && !isFirstBarAfterEntry)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    double stopPrice = RK1.TrailingStop[0] - 20;
    ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "StopLong", "LongEntry");
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    double stopPrice = RK1.TrailingStop[0] + 20;
    ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "StopShort", "ShortEntry");
    }
    }
    }
    }

    // Handle order execution events to set the initial stop
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    // Call the base method first
    base.OnExecutionUpdate(execution, executionId, price, quantity, marketPosition, orderId, time);

    // Set initial stop based on entry price
    if (marketPosition == MarketPosition.Long && execution.Name == "LongEntry")
    {
    double initialStopPrice = price - 65;
    ExitLongStopMarket(0, true, Position.Quantity, initialStopPrice, "StopLong", "LongEntry");

    // Reset the counter and set the flag to transition to trailing stop on next bar
    barsSinceEntry = 0;
    isFirstBarAfterEntry = true;
    }
    else if (marketPosition == MarketPosition.Short && execution.Name == "ShortEntry")
    {
    double initialStopPrice = price + 65;
    ExitShortStopMarket(0, true, Position.Quantity, initialStopPrice, "StopShort", "ShortEntry");

    // Reset the counter and set the flag to transition to trailing stop on next bar
    barsSinceEntry = 0;
    isFirstBarAfterEntry = true;
    }
    }

    #2
    Hello Playdc,

    From this snippet of code it seems like you are trying to make dynamic exits but that does not match the description you provided. If you want to reverse the position you simply need to call the opposite entry method. For example if you were short you just call EnterLong to reverse the position. The rejection you got is because of an invalid price, if yu want to keep using the orders as you have them you need to make sure the price is valid before submitting the order.

    Comment


      #3
      Hi Jesse,

      My question is suppose I use ExitLongStopMarket to set up a dynamic stop order for long position, before I enter a short position, how to cancel the previous stop order?
      Last edited by Playdc; 04-16-2025, 10:41 AM.

      Comment


        #4
        Hello Playdc,

        In that use case you would generally need to either wait at least 1 bar until the order expires assuming the orders condition is no longer true or cancel the order using CancelOrder and then submit a new order once cancelled.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        43 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 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