• 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

how to enter a trade when price cross an indicator

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

    how to enter a trade when price cross an indicator

    hello
    how can i code that if price cross an indicator to open a position right there before the close of that bar. for example i want to open a long position when price cross 5 EMA and not wait for the close of the candle. any help is greatly appreciated.

    #2
    Hello babouin77,

    Thanks for your post and welcome to the NinjaTrader forums.

    To have your code react to an immediate cross of the moving average and place an immediate order, on live or playback>market replay data, your code would need to run with Calculate.OnPriceChange (or Calculate.OnEachTick).

    Please note that in these calculation modes it is entirely possible that the price may cross and uncross many times within the bar because of the price movement. As a result, your code may issue entry orders many times and you will need to add further logic to prevent multiple orders. It is also possible that even though on one price change the moving average may be crossed and your strategy would place an order, subsequent price changes in the opposite direction may make it appear, historically, that a cross never occurred.

    The advantage of using Calculate.OnBarClose is that your code is only executed once per bar and the cross conditions would not have changed, the disadvantage is that you would have to wait for the bar to close for execution and placement of the entry order.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply. If I do the price change thing, would I put in the code when price close cross over the EMA for example or something else? because I tried to put price change in strategy builder on step 2 then later in the condition i put that price close cross above ... what would the condition be and how would i use the price change in the condition?

      Comment


        #4
        Originally posted by NinjaTrader_PaulH View Post
        Hello babouin77,

        Thanks for your post and welcome to the NinjaTrader forums.

        To have your code react to an immediate cross of the moving average and place an immediate order, on live or playback>market replay data, your code would need to run with Calculate.OnPriceChange (or Calculate.OnEachTick).

        Please note that in these calculation modes it is entirely possible that the price may cross and uncross many times within the bar because of the price movement. As a result, your code may issue entry orders many times and you will need to add further logic to prevent multiple orders. It is also possible that even though on one price change the moving average may be crossed and your strategy would place an order, subsequent price changes in the opposite direction may make it appear, historically, that a cross never occurred.

        The advantage of using Calculate.OnBarClose is that your code is only executed once per bar and the cross conditions would not have changed, the disadvantage is that you would have to wait for the bar to close for execution and placement of the entry order.
        One other workaround (for NT7) could be this : Initially the code has COBC = false; Just after the first cross set a bool var to true which, then, automatically switch the code to COBC=true, thus preventing possible multiple crosses until the end of the bar ! This method needs, of cource, some extra code to be accomplished. Thanks - George

        Comment


          #5
          but how would you define the first close since in the condition it is saying when we click price to choose from, there is no option other than high low close to choose under price. i do not see current price for example

          Comment


            #6
            Hello babouin77,

            Thanks for your replies.

            When you select Calculate.OnPriceChange or Calculate.OnEachTick, the price of Close[0] will be the current price. For the condition of a cross above, I would suggest checking that the previous bar High[1] was less than the previous bar EMA(5)[1] and that the current price Close[0] is greater than the EMA(5)[0].

            Forum member Gland1 (Welcome to the NinjaTrader forums!) has offered a solution based on NT7. To clarify a Bool does not actually change the Calculate setting but with the additional coding would permit the action to occur once per bar.

            A simpler approach, in the NinjaTrader8 strategy builder, to have a once per bar action when using Calculate.OnEachTick or Calculate.OnPriceChange, is to create an int type variable, for example call it "SavedBar" and to use the system bar counter CurrentBar. In the entry set you would add an entry condition User variables>SavedBar, NOT equal, Misc>Current bar. In the action section (where you place the order) add the action Misc> Set SavedBar and in the SavedBar field click "set" then select Misc>current Bar. The logic here is that as long as SavedBar is not equal to the current bar you can place the entry and once you place the entry to set the savedBar to be equal to the current bar which means the entry condition can not longer be true until the next bar which by then your conditions will have changed.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by babouin77 View Post
              but how would you define the first close since in the condition it is saying when we click price to choose from, there is no option other than high low close to choose under price. i do not see current price for example
              Hello,

              if a "cross" is included in your exit conditions, set a bool var after the Exit() command (for ex --> executedlong = true . This var verifies the Exit and, then, may trigger another var which (eventually) may turn to COBC = true equivalent.

              Another workaround may be to write :

              if(FirstTickOfBar)
              { counter1 = 0; } // counter1 = int var
              counter1++;

              in your entry conditions. Then limit the counter to 1 .for ex :
              if ( executedlong && counter1 < 2 ) { EnterLong(), etc}.

              You may, alternately, add if (Close[0] > Open[0] + x*TickSize

              as an additional entry condition (x= int var, close to bar's brick size (if you use Renko type Bars)), something which will certainly decrease the possibility of muliple entries due to "fluctuations" of the driving indicator(s).
              All above ideas, are just ideas, and I have not tested them. You may check them.
              Thanks - George.
              Last edited by Gland1; 06-04-2020, 11:36 AM.

              Comment


                #8
                thanks for the tip. I did that where I put price change and then price close cross below ema and it is still opening a long at close and not cross of the candle.did i miss something

                Comment


                  #9
                  Hello Gland1,

                  Thanks for your contributions and effort to help.

                  Please note that the thread OP is using the NinjaTrader8 strategy builder which would be the equivalent of the NinjaTrader7 strategy wizard so your coding help, while appreciated, is likely adding confusion to the OP who may or may not have coding knowledge.

                  You are welcome of course to continue your contrbutions but they would better serve the thread OP if they were specific to NinjaTrader8 Strategy Builder.

                  Thanks for your understanding and again welcome to the foums!

                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello babouin77,

                    Thanks for your reply.

                    Please note that when using Calculate.OnEachTick or Calculate.OnPriceChange what we have talked about will only work on Live data. If you apply to a chart and look at historical trades only these will only be based on Calculate.OnBarClose. It is only when the strategy is working on real time data (or use Playback with market replay data) that you would see entries made immediately at the cross.

                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      oh that explained it... ill look at it real time . thanks for the tip

                      Comment


                        #12
                        one more thing... let us say i am in a long entry and I want to place a stop loss 2 ticks below the low of the long entry... vice versa for the short entry... when i do at the attached it is not working and getting me out... is that will show in real time or it should show backtested?

                        Comment


                          #13
                          or lets say 4 ticks behind the entry price...

                          Comment


                            #14
                            Hello babouin77,

                            Thanks for your replies.

                            In the strategy builder, the stops and targets are to be fixed values only, for example select calculationMode of ticks and a value of 10. You cannot assign a dynamically changing value such as an indicator or a price series. This is a limitation of working with the strategy builder.

                            You have two alternatives:

                            1) unlock the strategy builder strategy and work directly in Ninjascript where you can then dynamically assign the profit/stops as wish,

                            2) In the strategy builder do not use the Stops and Targets and instead create additional sets where you submit the orders using other order methods, for example for a short entry you would use "Exit short position by a limit order" (profit target) and "exit short position by a stop order" (stop loss). I would suggest using signal names to tie the orders together for OCO type functionality (you would give the entry order a signal name and use that same name for the other two orders).



                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              ok i will try that... thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rafaelabe1977, Today, 03:51 AM
                              1 response
                              8 views
                              0 likes
                              Last Post sledge
                              by sledge
                               
                              Started by jenifeery79, Today, 05:03 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post jenifeery79  
                              Started by rexmie888, Today, 04:00 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post rexmie888  
                              Started by terminatorT1000, Yesterday, 06:40 PM
                              1 response
                              16 views
                              0 likes
                              Last Post NT-Roland  
                              Started by cmarkb, Today, 02:15 AM
                              4 responses
                              16 views
                              0 likes
                              Last Post cmarkb
                              by cmarkb
                               
                              Working...
                              X