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 TraderBCL, Today, 04:38 AM
      3 responses
      23 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by WeyldFalcon, 08-07-2020, 06:13 AM
      11 responses
      1,423 views
      0 likes
      Last Post jculp
      by jculp
       
      Started by RubenCazorla, Today, 09:07 AM
      0 responses
      4 views
      0 likes
      Last Post RubenCazorla  
      Started by BarzTrading, Today, 07:25 AM
      2 responses
      29 views
      1 like
      Last Post BarzTrading  
      Started by devatechnologies, 04-14-2024, 02:58 PM
      3 responses
      21 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X