• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 waldoalvarez, Today, 04:59 PM
      0 responses
      2 views
      0 likes
      Last Post waldoalvarez  
      Started by habibalex, Today, 09:45 AM
      2 responses
      13 views
      0 likes
      Last Post habibalex  
      Started by junkone, Today, 03:42 PM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by peterslane1, Today, 09:47 AM
      2 responses
      17 views
      0 likes
      Last Post aviat72
      by aviat72
       
      Started by SlickRick, Today, 02:47 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Working...
      X