Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

ExitStopMarket() Not working as expected

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

    ExitStopMarket() Not working as expected

    Hello:

    I am using ExitLongStopMarket() to move the StopLoss in a trade. I am facing an issue when I move the StopLoss. The original StopLoss order stays put and doesn't cancel. I am modifying the order object. This happens only If I modify the Stop Signal name. If I keep the same Signal name in the order it works fine. I need to use a different signal name to make tracking my orders better. Is there any way to fix the issue.

    When I change the signal name, Is it not modifying the same order object? It appears If I change the Quantity/Price and keep the Signal name and FromEntrySignal the order object is modified but If I change the StopLoss signal name then the original order object stays the same and a new StopLoss order is created.

    See below the log:



    ===========
    Now the interesting thing is If I do not change the name Name but keep it the same and update the StopLoss price.
    Then it is working as expected. The StopLoss is moved correctly
    ===========



    ===========
    Now the interesting thing is If I do not change the name Name but keep it the same and update the StopLoss price.
    Then it is working as expected. The StopLoss is moved correctly
    ===========


    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    =====
    I modified the StopLoss to give the trade little bit of breathing room
    Here I changed the StopLoss name to "StopShort STB1"

    Now the effect is , The Original StopLoss as well as the New StopLoss are there in Working State" I am expecting the Original Stop Loss to be replaced with the New StopLoss Order

    ==========

    NinjaScript strategy 'Ninja8STRX/131611074' submitting order
    Order='636340689/ACCOUNT' Name='TargetShort' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340689/ACCOUNT' Name='TargetShort' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340689/ACCOUNT' Name='TargetShort' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340685/ACCOUNT' Name='StopShort Init' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340685/ACCOUNT' Name='StopShort Init' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340685/ACCOUNT' Name='StopShort Init' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    =====
    My Initial StopLoss Order is called = "StopShort Init"
    ====

    Instrument='NZDUSD' Account='ACCOUNT' Average price=0.69865 Quantity=14,000 Market position=Short Operation=Operation_Add
    NinjaScript strategy 'Ninja8STRX/131611074' submitting order
    Execution='636340684' Instrument='NZDUSD' Account='ACCOUNT' Exchange=Default Price=0.69865 Quantity=14,000 Market position=Short Operation=Operation_Add Order='636340684' Time='6/1/2018 01:30'
    NinjaScript strategy 'Ninja8STRX/131611074' submitting order
    Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Filled' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=14000 Fill price=0.69865 Error='No error' Native error=''
    Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Working' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Submitted' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Accepted' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    NinjaScript strategy 'Ninja8STRX/131611074' submitting order
    Enabling NinjaScript strategy 'Ninja8STRX/131611074' : On starting a real-time strategy - StartBehavior=AdoptAccountPosition AccountPosition=NZDUSD 14000L, NZDUSD 14000L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes


    =============

    Character limit doesn't allow me to input the code snippet into this message.
    I will add it as the replay to this thread.

    #2
    Code Snippet
    ============


    Here are the relevant code details:

    Entry Order:

    EnterLong(Convert.ToInt32(QuantityBig), @"LongBig");

    StopLoss Order in OnExecutionUpdate:

    protected override void OnExecutionUpdate(......))

    StopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - StopLossInitBig, "StopLong Init", @"LongBig");

    TargetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + TakeProfitBig, "TargetLong", @"LongBig");

    Trail Stop and Update Quantity and some time StopLoss Name change in OnBarUpdate():

    protected override void OnBarUpdate()

    if ((Stop Loss Condition))
    {
    StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity, (Close[0] - STB1), "StopLong STB1", @"LongBig");
    StopLongPrice = (Close[0] - STB1);
    }
    else if ((Stop Loss Condition & Profit Condition))
    {
    ExitLong(0, BigTL2, "TakeProfit STB2", @"LongBig");
    TargetOrder = ExitLongLimit(0, true, StopOrderQuantity - BigTL2, TargetLongPrice, "TargetLong", @"LongBig");
    StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity - BigTL2, (Close[0] - STB2), "StopLong STB2", @"LongBig");
    StopLongPrice = (Close[0] - STB2);
    StopOrderQuantity = (StopOrderQuantity - BigTL2);
    }
    else
    {
    StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity, (Close[0] - STB2), "StopLong STB2", @"LongBig");
    StopLongPrice = (Close[0] - STB2);
    }

    As you can see from the above, I change the name inside the StopLoss Order object when the trade passes thru certain targets levels which I want to track. The problem is when I change the StopLoss name in the same order object, it does not seem to take it instead, it is keeping the old and creating a new order and what I end is with two duplicate working orders.

    Can you suggest a method to avoid the issue and keep the same StopLoss order and to avoid the duplications?

    Comment


      #3
      Hello ajsdnyyrr1234DDf,

      Thank you for your post.

      You would either need to avoid using two signal names for essentially the same order or use two Order objects and cancel the one you are switching from.

      Please let me know if you have any questions.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Irukandji, Yesterday, 02:53 AM
      2 responses
      17 views
      0 likes
      Last Post Irukandji  
      Started by adeelshahzad, Today, 03:54 AM
      0 responses
      3 views
      0 likes
      Last Post adeelshahzad  
      Started by CortexZenUSA, Today, 12:53 AM
      0 responses
      3 views
      0 likes
      Last Post CortexZenUSA  
      Started by CortexZenUSA, Today, 12:46 AM
      0 responses
      1 view
      0 likes
      Last Post CortexZenUSA  
      Started by usazencortex, Today, 12:43 AM
      0 responses
      5 views
      0 likes
      Last Post usazencortex  
      Working...
      X