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 Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      4 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      41 views
      0 likes
      Last Post alifarahani  
      Started by Waxavi, Today, 02:10 AM
      1 response
      20 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Working...
      X