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

Single entry, multiple exit logic

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

    Single entry, multiple exit logic

    My Strat buys in once, then will scale out in 3 steps depending on price movement.

    I've tagged the entry with a signal name 'b1'.
    Exits are tagged: 't1', 't2', 't3'

    I created boolean vars associated with each exit, each turned off (set to false):
    T1_xit
    T2_xit
    T3_xit

    After each exit, I turn the associated var on (set to true).

    I coded the Set logic to not execute the actions for the exit if it's associated boolean var is true.

    PROBLEM: Though the exits are executing, I'm sometimes getting multiple t1 exits, even though the 1st exit should have set T1_xit to true. (I assume when the condition is true twice before the position is completely exited).

    If you can find any logic error in my code, I'd appreciate it.

    I've included the relevant info in the code snippet below (minus the actual condition logic).

    Code:
           protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Calculate                                    = Calculate.OnPriceChange;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.UniqueEntries;
    
                    ...
    
                    StopLoss                = 1;
                    T1_xit                    = false;
                    T2_xit                    = false;
                    T3_xit                    = false;
                }
                else if (State == State.DataLoaded)
                {                
                    ...
    
                    SetStopLoss(@"b1", CalculationMode.Currency, StopLoss, false);
                }
    
            }
    
    
            protected override void OnBarUpdate()
            {
                 // Set 1
                 // ENTRY, full position
                if ( <set 1 condition> )
                {
                    EnterLong(Convert.ToInt32(300), @"b1");
                    T1_xit = false;
                    T2_xit = false;
                    T3_xit = false;
                }
    
                 // Set 2
                 // T1 Exit, 1/3 position
                if ( <set 2 condition> )
                     && (T1_xit == false)
                     && (T2_xit == false)
                     && (T3_xit == false))
                {
                    ExitLong(Convert.ToInt32(100), @"t1", @"b1");
                    T1_xit = true;
                }
    
                 // Set 3
                 // T2 Exit, 1/3 position
                if ( <set 3 condition> )
                     && (T2_xit == false)
                     && (T1_xit == true)
                     && (T3_xit == false))
                {
                    ExitLong(Convert.ToInt32(100), @"t2", @"b1");
                    T2_xit = true;
                }
    
                 // Set 4
                 // T3 Exit, 1/3 position
                if ( <set 4 condition> )
                     && (T3_xit == false)
                     && (T1_xit == true)
                     && (T2_xit == true))
                {
                    ExitLong(Convert.ToInt32(100), @"t3", @"b1");
                    T3_xit = true;
                }                
            }

    #2
    Hello Wick Wrangler,

    Thank you for your note.

    What's occurring is that your logic is resetting the T1_xit, T2_xit, and T3_xit to false when it tries to submit an order after the inital entry has been filled. Any time your conditions for entry are true, NinjaTrader will loop through and try to submit an order. If there's a position already active, it will check your Entries Per Direction setting and make sure that this entry would make the number of entries in the same direction equal or lesser to the value of that setting. If it's greater, the order will be ignored and you won't notice anything obvious to tell you that it tried to do that unless you have TraceOrders = true within the SetDefaults section of OnStateChange - if you do turn that on, a log entry will be added noting that the order was ignored and why.

    However, it will still also reset the bools to false since it's going through that particular block of code.

    If you're only using one entry at a time, then waiting to re-enter when you're in a flat position, you can fix this by adding the following to your entry conditions:

    && Position.MarketPosition == MarketPosition.Flat

    This will then not run through that code block until you are again in a flat position and ready for re-entry. This way you avoid accidentally resetting those bools before you meant to do so.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post

      What's occurring is that your logic is resetting the T1_xit, T2_xit, and T3_xit to false when it tries to submit an order after the inital entry has been filled.
      .
      Thanks. I suspected these vars were getting reset somehow.

      It's working properly now.




      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bmartz, 03-12-2024, 06:12 AM
      4 responses
      31 views
      0 likes
      Last Post bmartz
      by bmartz
       
      Started by Aviram Y, Today, 05:29 AM
      4 responses
      12 views
      0 likes
      Last Post Aviram Y  
      Started by algospoke, 04-17-2024, 06:40 PM
      3 responses
      28 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by gentlebenthebear, Today, 01:30 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by cls71, Today, 04:45 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X