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

How to express the statement

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

    How to express the statement

    I want to add a logic where it says enter only if price hasnt touched EMA in the last x number of bars back.?

    #2
    Hello tkaboris,

    You would need to use a int variable and increment it to count bars where your condition is not true. You would need a second condition to check if that int variable is greater than X and if so do your action and reset the int variable to 0 so it can repeat the process.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello tkaboris,

      You would need to use a int variable and increment it to count bars where your condition is not true. You would need a second condition to check if that int variable is greater than X and if so do your action and reset the int variable to 0 so it can repeat the process.
      Do you have an example of it? maybe somewhere in the forum..

      Comment


        #4
        Hello tkaboris,

        Not that I am aware of that is very specific.

        Are you manually coding or using the strategy builder? I can likely link to some releated concept samples but would need to know how you will be making the script.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello tkaboris,

          Not that I am aware of that is very specific.

          Are you manually coding or using the strategy builder? I can likely link to some releated concept samples but would need to know how you will be making the script.
          I am making custom strategy, no builder.

          Comment


            #6
            Hello tkaboris,

            In that case you can use a simple variable like the following for incrementing on each bar.

            Code:
            private int myIntVariable;
            
            protected override void OnBarUpdate()
            {
            
                if(ConditionToCheckIfPriceTocuhedEma)
                {
                    myIntVariable = 0; // reset to 0 when the price tocuhes
                }
                else 
                {
                     myIntVariable++; // increment the variable each bar where the condition is true. 
                }
            
                if(myIntVariable > 5) 
                {
                     //enter
                }
            }
            JesseNinjaTrader Customer Service

            Comment


              #7
              I put this logic to open short at 89EMA only if last time price hasnt reached 89EMA in the last 20 bars back. Its not working...

              else if (Trade89B

              && (Close[1] < Open[1] && High[1] > Open[1])// previous bar bullish
              && (Close[0] < Open[0] && High[0] > Open[0]) // current bar bearish with wick
              && High[0] > High[1] // current wick higher
              && High[0] > cEMA89[0] && Close[0] < cEMA89[0] // piercing through 89 ema

              && slowEMA[0] < cEMA89[0] - DForDW89 * TickSize) // enough distance to 34 ema, not overlapping

              {
              int count = 0;
              for (int i=0; i < Bars.Count - 1; i++)
              {
              if (Close[i] <= cEMA89[i])
              {
              count++;
              }
              }
              if (count >= 20)
              {
              entryOrder = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, TradeSize, 0, 0, "", "89B");
              }
              return;
              }​

              Comment


                #8
                Hello tkaboris,

                You would need to use a print to see what's not working. I had only provided a suggestion on how to increment a variable, you would still need to till use Print to debug and make sure that works for your use case. You are also using a loop over the bars which is not what I had intended with that example, you would normally process bars by just allowing the OnBarUpdate to be called and not using a loop.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  its not opening short trades at all. I am not as familiar with how you suggested. is there a way you can provide little more guidance?

                  Comment


                    #10
                    Hello tkaboris,

                    You would need to debug the script if its not doing what you intended.

                    You can use prints to identify how your logic is working:

                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Would this be the right syntax ? Also Do i need to state property as bool for ConditionToCheckIfPriceTocuhedEma down there at the end so I dont get "... doesnt exist in context" error?
                      Code:
                      else if (Trade89B
                      
                      && (Close[1] < Open[1] && High[1] > Open[1]) // previous bar bullish
                      && (Close[0] < Open[0] && High[0] > Open[0]) // current bar bearish with wick
                      && High[0] > High[1] // current wick higher
                      && High[0] > cEMA89[0] && Close[0] < cEMA89[0] // piercing through 89 ema
                      && slowEMA[0] < cEMA89[0] - DForDW89 * TickSize) // enough distance to 34 ema, not overlapping
                      
                      {
                      
                      if(ConditionToCheckIfPriceTocuhedEma)
                      {
                      myIntVariable = 0; // reset to 0 when the price tocuhes
                      }
                      else
                      {
                      myIntVariable++; // increment the variable each bar where the condition is true.
                      }
                      
                      if(myIntVariable > 5)
                      {
                      entryOrder = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, TradeSize, 0, 0, "", "89B");
                      }
                      // entryOrder = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, TradeSize, 0, 0, "", "89B");
                      
                      return;
                      }​

                      Comment


                        #12
                        Hello tkaboris,

                        ConditionToCheckIfPriceTocuhedEma isn't something that is real that's just a placeholder to say you need to use your own conditions there.

                        The code I provided is not something you can copy/paste into a script, that's only showing how to increment a variable but otherwise contains no valid conditions.

                        The general concept that I described is just to increment a variable based on your conditions from OnBarUpdate. You could also make price conditions similar to what you posted in post 7.

                        To know if something is working or not you will need to compile and test it, most likely you will also need to use prints to debug it.
                        JesseNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by cocoescala, 10-12-2018, 11:02 PM
                        6 responses
                        939 views
                        0 likes
                        Last Post Jquiroz1975  
                        Started by gbourque, Today, 06:39 AM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by cmtjoancolmenero, Yesterday, 03:58 PM
                        1 response
                        17 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by benmarkal, Yesterday, 12:52 PM
                        3 responses
                        23 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by helpwanted, Today, 03:06 AM
                        1 response
                        20 views
                        0 likes
                        Last Post sarafuenonly123  
                        Working...
                        X