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

simple calculation

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

    simple calculation

    Hi,

    Could anybody help me, I'm stuck in simple code and simple math
    I want to calculate how many up close and down close bars appeared in current session. And then I want to have one number like a somekind of coef, how many up close bars were in this session compared to all bars in this session.

    so far what I get in output window is correct calculations for aboveS and belowS, but i get zeros for double sessionClose. So the code is not working.

    In my deffence I'm total beginner in programming so any help or suggestions are wellcome. And maybe there's a more effective ways do that kinds of calculations.

    Thank you all in advance.


    private int numberOfBars = 0;
    private int aboveS = 0;
    private int belowS = 0;
    private double sessionClose = 0.00;

    protected override void OnBarUpdate()
    {


    if (Bars.FirstBarOfSession)
    {
    numberOfBars = 0;
    private int aboveS = 0;
    private int belowS = 0;
    sessionClose = 0.00;
    }
    numberOfBars++;

    if(numberOfBars <1) return;

    if (Close[0] > Close[1])
    aboveS++;

    if (Close[0] < Close[1])
    belowS++;

    sessionClose = aboveS/(aboveS + belowS);

    Print("Current session number of bars = " + numberOfBars.ToString() + “ // “ +“ sessionClose = “ + sessionClose.ToString()+ “ // “ + "Date = " + ToDay(Time[0]).ToString() + " // " + “Time = “ + ToTime(Time[0]).ToString());

    }

    #2
    Hello,

    Thank you for the post.

    I noted one problem in the syntax, the following items should not be marked private while inside of a method:

    Code:
    if (Bars.FirstBarOfSession)
    {
    numberOfBars = 0;
    [B]private[/B] int aboveS = 0;
    [B]private[/B] int belowS = 0;
    sessionClose = 0.00;
    }
    these should instead be:

    Code:
    if (Bars.FirstBarOfSession)
    {
    numberOfBars = 0;
    aboveS = 0;
    belowS = 0;
    sessionClose = 0.00;
    }
    using private inside of a method such as OnBarUpdate should throw a compile error, additionally you have declared the variable in the variables section, the reset on the first bar of session would need to be just the variable name to associate the correct variable.

    The second item I noted is the math you are using, int being used with double. Generally you would need to cast the int to a double to ensure the math is correct as well.

    int math in C# can be deceiving or you can end up with rounded values in the case the value was between 0 and 1. you can test adjusting the type of the two variables to a double instead:

    Code:
    private double aboveS = 0;
    private double belowS = 0;
    private double sessionClose = 0.00;
    below is the corrected syntax, I did print the values and see it is a value between 0 and 1, so using int specifically would cause rounding in this situation.

    Code:
    private int numberOfBars = 0;
    private double aboveS = 0;
    private double belowS = 0;
    private double sessionClose = 0.00;
    
    protected override void OnBarUpdate()
    {
    	if (Bars.FirstBarOfSession)
    	{
    		numberOfBars = 0;
    		aboveS = 0;
    		belowS = 0;
    		sessionClose = 0.00;
    	}
    	numberOfBars++;
    
    	if (numberOfBars < 1) return;
    
    	if (Close[0] > Close[1])
    		aboveS++;
    
    	if (Close[0] < Close[1])
    		belowS++;
    
    	sessionClose = aboveS / (aboveS + belowS);
    
    	Print("Current session number of bars = " + numberOfBars.ToString() + " sessionClose: " + sessionClose);
    
    }
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      it works perfectly! thank you so much

      Comment


        #4
        Originally posted by Laimis View Post
        it works perfectly! thank you so much
        Doubles do not make lexical sense for counting what are essentially integers, and they take more resources (no matter how small) to boot. In this simple case, that may not be important: it can become important in large programs.

        You are better off using your original code, and casting the integers in the final calculation where you need a double.

        Code:
        sessionClose = (double)aboveS / (double)(aboveS + belowS);
        It is also more elegant.

        NB: I always cast both, simply because it makes it more understandable when I come to read it later, but it is really only necessary to cast one quantity in an expression. Arithmetic that involves a mixture of double and int, will return a double.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        27 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 02-22-2024, 01:11 AM
        5 responses
        32 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 04-23-2024, 09:53 PM
        2 responses
        49 views
        0 likes
        Last Post wzgy0920  
        Started by Kensonprib, 04-28-2021, 10:11 AM
        5 responses
        193 views
        0 likes
        Last Post Hasadafa  
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,235 views
        0 likes
        Last Post xiinteractive  
        Working...
        X