Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

flat_or_notflat

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

    flat_or_notflat

    Hello,

    in the attached screenshot you can see when a trade is closed with white background coloring. The next blue upbar (with blue blackground) there should be a new trade referring conditions but there is no entry. The reason is the marketpositionflat condition. Only the next bar would be the entry (as I know from testing).

    Can I change the "Position.MarketPosition == MarketPosition.Flat" (with COBC=true) so that entry is done already next bar after exit if conditions are filled? Do I have to change maybe somehow from onBarUpdate to OnExecution?

    Thanks
    Tony
    Attached Files

    #2
    Hello,

    Position.MarketPosition would only be evaluated the next time the bar closes on OnBarUpdate().

    If you would like to improve the timing of this, you can either use another method such as OnExecution() to check your market position, or you can also try adding a higher resolution time period to your strategy and evaluating your position on that BarsArray.

    For the second option, please take a look at our refernece sample on Entering on one time frame and exiting on another:

    MatthewNinjaTrader Product Management

    Comment


      #3
      Hello Matthew,

      thank you for your reply. I work with BarsArray. Maybe I didn´t translate correctly my question before. How can I do in OnExecution? Would this be different to onbarupdate ("flat") so that the entry is done the next bar after exits of the trade before - if conditions filled?

      Thanks
      Tony



      Originally posted by NinjaTrader_Matthew View Post
      Hello,

      Position.MarketPosition would only be evaluated the next time the bar closes on OnBarUpdate().

      If you would like to improve the timing of this, you can either use another method such as OnExecution() to check your market position, or you can also try adding a higher resolution time period to your strategy and evaluating your position on that BarsArray.

      For the second option, please take a look at our refernece sample on Entering on one time frame and exiting on another:

      http://www.ninjatrader.com/support/f...ead.php?t=5787

      Comment


        #4
        Hello,

        Yes, you can check your position in OnExecution() which will update faster than OnBarUpdate().

        This means if your entry condition is true, but is not executing because the MarketPosition is flagged in OnBarUpdate, you can instead put this check in OnExecution which should return as Flat as soon as the previous order has exited.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Thank you for your reply. Sorry for my bad translation to english maybe. I meant how this can be done in onexecution-section so that the entry referring conditions in onbarupdate-section is done?

          Thank you
          Tony



          Originally posted by NinjaTrader_Matthew View Post
          Hello,

          Yes, you can check your position in OnExecution() which will update faster than OnBarUpdate().

          This means if your entry condition is true, but is not executing because the MarketPosition is flagged in OnBarUpdate, you can instead put this check in OnExecution which should return as Flat as soon as the previous order has exited.

          Comment


            #6
            Originally posted by tonynt View Post
            Thank you for your reply. Sorry for my bad translation to english maybe. I meant how this can be done in onexecution-section so that the entry referring conditions in onbarupdate-section is done?

            Thank you
            Tony
            I think that maybe the answer to your question is the same one as this: http://www.ninjatrader.com/support/f...ad.php?t=48849

            Comment


              #7
              Koganam,

              thank you very much for your help!

              But as you write the first 2 methods are more cpu-intensive, therefore I use the COBC=true.

              With the multidataseries I know I have 5 add(...) I would understand but this is not how I have to do it.

              Do you know how one can do in the script so that the "flat-condition" is in OnExecution but this work "together" with OnBarUpdate-conditions? Maybe to put all conditions to OnExecution? Or how to refer from one to other? "Conditions in onbarupdate and flat in onexecution, then do...."

              Thank you
              Tony

              Originally posted by koganam View Post
              I think that maybe the answer to your question is the same one as this: http://www.ninjatrader.com/support/f...ad.php?t=48849

              Comment


                #8
                Hello NinjaSupport,

                as I have a lifetime-license on NT I think it should be possible to give a short answer to the question how one can do the conditions in onbarupdate-section but doing the marketposition.flat condtion in onexecution-section. How can the conditions each section refer to the other section?

                I could not found on your support sites.

                Thanks
                Tony

                Comment


                  #9
                  Tony, unfortuantely I think our problem here is still one of understand and being clear exactly what you wish to achieve - OnExecution() will fire everytime you receive an execution, if that's the execution taking you flat > then this would be seen earlier in OnExecution() as compared to the position update that will follow then in OnBarUpdate() and OnPositionUpdate(). Those are simply different event timings seen as the event progresses through the NT framework - the question remaining would, what are you looking to do with the earlier known execution event compared to the positon one showing you're flat? Sending a reversal order out?
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hello,

                    no, I dont want to send a reversal order out. The entry would be at this reversal bar in the screenshot (but no matter if reversal bar or not, this is to explain in this screenshot where the new entry should have been only)

                    When I work with COBC=true then the entry is not done at a bar when exit of strategy has been the bar before. Please see the attached screenshot. Strategy doesn´t enter the next blue upbar not because its an upbar, but maybe 3 conditions are true there. One of the condition is "&&Position.MarketPosition == MarketPosition.Flat"

                    I found out that the "flat-condition" is the problem and when I remove then the entry is done this bar (where there is flat!). So I asked in the forum if it could be done to check the flat in onexecution section. This was confirmed, but I don´t know how one can work with the conditions in both sections, the onbarupdate (eg "MACD, SMA....") and checking if flat in onexecution.

                    I simply want to have eg "if Rising(MACD(BarsArray[3],12,26,9).Avg) == true && "....flat..." but checking flat in onexecution then Enterlong (but checking in onexecution as there is no enterlong when the exit has been the bar before and checking flat is in onbarupdate with COBCtrue)


                    I think this is more to explain than to code and you know now what I mean

                    Thank you
                    Tony


                    Originally posted by NinjaTrader_Bertrand View Post
                    Tony, unfortuantely I think our problem here is still one of understand and being clear exactly what you wish to achieve - OnExecution() will fire everytime you receive an execution, if that's the execution taking you flat > then this would be seen earlier in OnExecution() as compared to the position update that will follow then in OnBarUpdate() and OnPositionUpdate(). Those are simply different event timings seen as the event progresses through the NT framework - the question remaining would, what are you looking to do with the earlier known execution event compared to the positon one showing you're flat? Sending a reversal order out?

                    Comment


                      #11
                      Tony,

                      Here is a basic example of how you can set a bool flag in OnExeuction to be evaluated in OnBarUpdate

                      First, we need to create a global boolean variable that can be accessed from both OnBarUpdate and OnExecution
                      Code:
                      [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]#region[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Variables[/SIZE][/FONT][/FONT][/COLOR]
                      [FONT=Courier New][SIZE=2][COLOR=#008000]// create isFlat bool[/COLOR][/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#0000ff]#endregion[/COLOR][/SIZE][/FONT][/LEFT]
                      Next, we will examine the current market position in OnExeuction. As soon as an exeuction is received, it will check your market position and if this is returned as anything but flat, it will set the isFlat to false, indicating that you are in position. Otherwise, the isFlat flag will return to true indicating your market position is flat

                      Code:
                        [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnExecution(IExecution execution)[/SIZE][/FONT][/FONT][/COLOR]
                      [FONT=Courier New][SIZE=2]        {[/SIZE][/FONT]
                      
                      [FONT=Courier New][SIZE=2][COLOR=#008000]// Checks if the current strategy position is not flat[/COLOR][/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Position.MarketPosition != MarketPosition.Flat)[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                  {[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#008000]// sets isFlat bool to false (market position is NOT flat)  This will dynamically update in OnBarUpdate[/COLOR][/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                        isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                  }[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#008000]// otherwise set the flag to true (market position is flat)[/COLOR][/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]            }[/SIZE][/FONT][/LEFT]
                      Finally, we can workin OnBarUpdate which will return the current market position flag that we set in OnExeuction

                      Code:
                           [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT][/FONT][/COLOR]
                      [FONT=Courier New][SIZE=2]        {[/SIZE][/FONT]
                      
                      [FONT=Courier New][SIZE=2][COLOR=#008000]//Checks for a condition and returns the current state of the isFlat bool set in OnExecution[/COLOR][/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Rising(MACD(BarsArray[3],12,26,9).Avg) == true &&[/SIZE][/FONT][FONT=Courier New][SIZE=2] && isFlat == [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                  {[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                        EnterLong([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
                      [FONT=Courier New][SIZE=2]                  }[/SIZE][/FONT]
                      
                      [FONT=Courier New][SIZE=2]        }[/SIZE][/FONT]
                      [/LEFT]
                      Please let me know if you have any questions on this.
                      Last edited by NinjaTrader_Matthew; 04-03-2012, 05:21 PM.
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        Finally we got it!

                        THANK YOU!!!



                        Originally posted by NinjaTrader_Matthew View Post
                        Tony,

                        Here is a basic example of how you can set a bool flag in OnExeuction to be evaluated in OnBarUpdate

                        First, we need to create a global boolean variable that can be accessed from both OnBarUpdate and OnExecution
                        Code:
                        [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]#region[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Variables[/SIZE][/FONT][/FONT][/COLOR]
                        [FONT=Courier New][SIZE=2][COLOR=#008000]// create isFlat bool[/COLOR][/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#0000ff]#endregion[/COLOR][/SIZE][/FONT][/LEFT]
                        Next, we will examine the current market position in OnExeuction. As soon as an exeuction is received, it will check your market position and if this is returned as anything but flat, it will set the isFlat to false, indicating that you are in position. Otherwise, the isFlat flag will return to true indicating your market position is flat

                        Code:
                          [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnExecution(IExecution execution)[/SIZE][/FONT][/FONT][/COLOR]
                        [FONT=Courier New][SIZE=2]        {[/SIZE][/FONT]
                        
                        [FONT=Courier New][SIZE=2][COLOR=#008000]// Checks if the current strategy position is not flat[/COLOR][/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Position.MarketPosition != MarketPosition.Flat)[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                  {[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#008000]// sets isFlat bool to false (market position is NOT flat)  This will dynamically update in OnBarUpdate[/COLOR][/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                        isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                  }[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#008000]// otherwise set the flag to true (market position is flat)[/COLOR][/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] isFlat = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]            }[/SIZE][/FONT][/LEFT]
                        Finally, we can workin OnBarUpdate which will return the current market position flag that we set in OnExeuction

                        Code:
                             [LEFT][COLOR=#000000][FONT=Tahoma][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT][/FONT][/COLOR]
                        [FONT=Courier New][SIZE=2]        {[/SIZE][/FONT]
                        
                        [FONT=Courier New][SIZE=2][COLOR=#008000]//Checks for a condition and returns the current state of the isFlat bool set in OnExecution[/COLOR][/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Rising(MACD(BarsArray[3],12,26,9).Avg) == true &&[/SIZE][/FONT][FONT=Courier New][SIZE=2] && isFlat == [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                  {[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                        EnterLong([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
                        [FONT=Courier New][SIZE=2]                  }[/SIZE][/FONT]
                        
                        [FONT=Courier New][SIZE=2]        }[/SIZE][/FONT]
                        [/LEFT]
                        Please let me know if you have any questions on this.

                        Comment


                          #13
                          IExecution.MarketPosition on Sim account

                          If I first buy (position = Long) and then sell (position = Flat) on Sim account and inspect inside OnExecution() the values of IExecution.MarketPosition, I find that after the execution of the entry order it returns (correctly) "Long", but after the execution of the exit order it returns (erroneously) "Short". Order qty is in both cases 1, and IPosition.MarketPosition confirms "Flat". Using the latest NT release. Haven't yet tested this in live trading, the other properties have been working fine so far. Sim-specific thing?!?!

                          Comment


                            #14
                            Hello,

                            IExecution.MarketPosition only tells you what type of execution occurred - whether it was a buy or a sell. There is not a flat object returned in this function.

                            IExecution method and properties

                            To check whether the strategy is currently long, short or flat, please use position.MarketPosition.

                            This would be the case in live and simulation.
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              Ok, understand. Perhaps a bit misleading name for that property, since the same information can be retrieved from IExecution.Order.OrderAction.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Irukandji, Today, 09:34 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by RubenCazorla, Today, 09:07 AM
                              1 response
                              5 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by TraderBCL, Today, 04:38 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              11 responses
                              1,423 views
                              0 likes
                              Last Post jculp
                              by jculp
                               
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              29 views
                              1 like
                              Last Post BarzTrading  
                              Working...
                              X