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

Check condtions on three consecutive bars

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

    Check condtions on three consecutive bars

    Hi,

    I am trying to code the following strategy:

    If conditions are met on three consecutive bars I want to enter trade on next bar. I have attempted to code this but I will be extremely grateful for any code examples because I am struggling with this one!




    private int good1
    private int good2
    private int good3

    if(condition1)
    good1 = CurrentBar; // if conditions are met go to next bar

    if
    (CurrentBar = good1 + 1) // if conditions are met go to next bar
    && (condition2)
    CurrentBar = good2;

    if
    (CurrentBar = good2 + 1) // if condtions are met go to next bar
    && (condition3)
    CurrentBar = good3;

    EnterShort([0] + 1) //Enter short on next bar

    #2
    Hello Rawheritage,

    Thank you for the post.

    I am reviewing your inquiry and will be back with a reply shortly.

    I look forward to assisting further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello Rawheritage,

      Thank you for your patience.

      You will use the Close[] array to access previous bars from the instrument.

      Code:
      protected override void OnBarUpdate() {
      
              //Ensure we have at least 3 bars to work with
               if(CurrentBar<2)
                      return;
      
              //Close[0] will be the most recent bar. So we will use the 3 previous bars. 
              if(Close[1] == condition && Close[2] == condition && Close[3] == condition) {
      
                      EnterShort();
      
              }
              
      
      }
      You do not have to use the Close[] array, this is just an example of how to access historical bars. You may use any of the bar data arrays such as Open[], High[], or Low[].

      If there is anything else I may assist with please let me know.
      Chris L.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by junkone, Today, 11:37 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by frankthearm, Yesterday, 09:08 AM
      11 responses
      41 views
      0 likes
      Last Post frankthearm  
      Started by quantismo, 04-17-2024, 05:13 PM
      5 responses
      35 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by proptrade13, Today, 11:06 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by love2code2trade, 04-17-2024, 01:45 PM
      4 responses
      34 views
      0 likes
      Last Post love2code2trade  
      Working...
      X