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 jxs_xrj, 01-12-2020, 09:49 AM
      6 responses
      3,290 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      9 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      11 responses
      62 views
      0 likes
      Last Post halgo_boulder  
      Started by Option Whisperer, Today, 09:55 AM
      0 responses
      8 views
      0 likes
      Last Post Option Whisperer  
      Started by halgo_boulder, 04-20-2024, 08:44 AM
      2 responses
      25 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X