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

Saving a numeric value in a boolean

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

    Saving a numeric value in a boolean

    Hello!

    I am struggling to fix a problem in my code.
    The idea is to save a close value of a bar after a condition is met. I have this:
    HTML Code:
    {
      bool firstCondition = false;
      double barClosed1 = 0; // this is the close value of the bar when the first condition is met.
      double barClosed1 = 0; // this is the close value of the bar in the second piece of my code.
    
      if(Close[0] < EMA(Close, 5))
      {
        firstCondition = true;
        barClosed1 = Close[0];
      }
     ​
    }
    Bars continue to be printed on the chart in the down trend and after a certain period of time, I have a bar closed above the bar of the first condition (in the reversal movement).
    I have this code:
    HTML Code:
    if(firstCondition && Close[0] > barClosed1)
      {
        barClosed2 = Close[0];
        print(barClosed2);
        firstCondition = false
    
        barClosed1 = 0;
        barClosed2 = 0;
      }    ​
    I am not having anything printed on the output window.
    The first condition is set to false at the end of the second code to be used again.
    Also barClosed1 and barClosed2 are set to zero.

    Could someone please points out where I am mistaking?

    Best regards,

    #2
    Hello Stanfillirenfro,

    If the intention is to save a value and check this across the span of multiple bars you need to move the variable to outside of OnBarUpdate. The variable how you have them now will reset for each new bar.

    Code:
    private bool firstCondition = false;
    private double barClosed1 = 0; // this is the close value of the bar when the first condition is met.
    private double barClosed2 = 0; // this is the close value of the bar in the second piece of my code.​
    protected override void OnBarUpdate()
    {
        if(Close[0] < EMA(Close, 5))
       {
          firstCondition = true;
          barClosed1 = Close[0];
       }
       ​if(firstCondition && Close[0] > barClosed1)
       {
          barClosed2 = Close[0];
          print(barClosed2);
          firstCondition = false
    
          barClosed1 = 0;
          barClosed2 = 0;
       } ​​
    }​
    JesseNinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_Jesse for your reply.

      I am moving forward with the code, but I am still facing some problems.

      I am now able to save the values of the corresponding bar correctly.
      The idea of this code if as followed: Let imagine we are in an UPTREND. After having the values of the corresponding bar, I want the prices to close FIRST below the bar of interest.
      SO I have the following code:
      HTML Code:
      if(firstCondition && Close[0] < barClosed1) // barClosed1 ist the Low of the bar of interest.
      {
       secondCondition = true;
      }
      ​
      After collecting the values of the bar of interest, I set the
      HTML Code:
      firstCondition = true
      as above (privious post). Now, I have the first condition fullfilled and also the second condition, making sure that at least one bar has closed below my bar of interest (This is set up by the secondCondition).
      Finally, I have:
      HTML Code:
      if(secondCondition && Close[0] > barClosed2) // barClosed2 is the High of the bar of interest.
      {
        Buys[0] = Low[0] - 3*TickSize;
      
        firstCondition = false;
        secondCondition = false;
        barClosed1 = 0;
        barClosed2 = 0;​
      }  ​
      With this code, I have my signal right below the bar formed directly after my bar of interest without having any bar closing below it before. It is not what I want.

      Could you please guide me here by pointing out where I am mistaking?

      Many thanks in advance.

      Comment


        #4
        Hello Stanfillirenfro,

        You would need to use prints here to check how your conditions are evaluating. I wouldn't be able to say what's happening when its running just by looking at the code. If your condition is happening late you would need to print out the values you used for that condition to get a better idea of why its late based on the values you are using.

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        837 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,293 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X