Announcement

Collapse
No announcement yet.

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.

    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.

        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
                }
            }

            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.

                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:

                    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.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        567 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        568 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X