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

Multiple Exits with Iorders

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

    Multiple Exits with Iorders

    Hi,

    I have a strategy with 2 exits options in OnBarUpdate as defined as:
    1. ExitShort/Long after 10 bars from entry
    2. ExitShort/Long on Friday, 2pm.

    The problem is both actions can be triggered if i have 10 bars after my entry on Friday at 2pm.
    NT7 will trigger 2 Exits instructions resulting on an Entry once prior exit has been filled.

    So to force only 1 Exit action, shall i use OnOrderUpdate or OnExecution ?
    The following logic doesnt seem to be working.


    in OnBarUpdate:

    Code:
    if(BarsSinceEntry >= 10)                 
                {    
                    if (Position.MarketPosition == MarketPosition.Long
                        && (myEmgSellStop == null))
                    {
                        mySellStop     = ExitLong("ExitLong","LongEntry");
                    }
                    if (Position.MarketPosition == MarketPosition.Short
                        && (myEmgBuyStop == null))
                    {
                        myBuyStop     = ExitShort("ExitShort","ShortEntry");
                    }
                }
    
    if ((ToTime(Time[0]) >= 140000) && (Time[0].DayOfWeek == DayOfWeek.Friday))        
                {
                    if (Position.MarketPosition == MarketPosition.Long
                        && (mySellStop == null))
                    {
                        myEmgSellStop    =  ExitLong("ExitLongWE","LongEntry");
                    }
                    if (Position.MarketPosition == MarketPosition.Short
                        && (myBuyStop == null))
                    {
                        myEmgBuyStop    = ExitShort("ExitShortWE","ShortEntry");
                    }
                }
    So in OnOrderUpdate:

    Code:
    if ((myBuyStop != null && myBuyStop == order)
        || (myEmgBuyStop != null && myEmgBuyStop == order)
                {
                    if (order.OrderState == OrderState.Filled)
                    {
                        myEmgBuyStop     = null;
                        myBuyStop         = null;
                    }
                }
                if     ((mySellStop != null && mySellStop == order)
                       || (myEmgSellStop != null && myEmgSellStop == order)
                {
                    if (order.OrderState == OrderState.Filled)
                    {
                        myEmgSellStop     = null;
                        mySellStop         = null;
                    }
                }

    #2
    Hello cbadr,

    Thank you for the post.

    To make sure that only one set of logic is executed at that time, you would just need to make your logic more specific. You wouldn't necessarily need to move your logic to a different override as that changes your script overall.

    Right now you are using two separate if conditions which could become true within the same period of time. To avoid that, you would need to make it so only one of these items can happen at once. One way to do that would be to use an else statement.

    Code:
    if(...)
    {
    
    }
    else if(...)
    {
    
    }
    You could also do other things such as using variables to control this, it would really be up to you and how you want this to occur to support your goals. The main takeaway would be that only one of these groups of logic should occur at once if you are trying to avoid the duplication of exit. This part of your logic would need to be what is changed.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by kevinenergy, 02-17-2023, 12:42 PM
    118 responses
    2,777 views
    1 like
    Last Post kevinenergy  
    Started by briansaul, Today, 05:31 AM
    0 responses
    6 views
    0 likes
    Last Post briansaul  
    Started by fwendolynlpxz, Today, 05:19 AM
    0 responses
    4 views
    0 likes
    Last Post fwendolynlpxz  
    Started by traderqz, Yesterday, 12:06 AM
    11 responses
    28 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by PaulMohn, Today, 03:49 AM
    0 responses
    8 views
    0 likes
    Last Post PaulMohn  
    Working...
    X