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

problem on Math.Abs

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

    problem on Math.Abs

    Hi,
    I'm new in programming.
    I have to calculate the ratio between the body and the whole range of the previous day.
    I tried using the function Math.abs but I must have done something wrong ...
    I enclose here a test script with the function of ratio included, what I did wrong?

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// This strategy enter simply long at high of previoos day.
        /// </summary>
        [Description("This strategy enter simply long at high of previoos day.")]
        public class BreakExample : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
    		private double prevHigh = 0;
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			//prova di calcolo del rapporto tra close-open e high-low del giorno precedente
    			if ((Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0]) / (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0])) < 0.5)
    			return;
    			
    			if (Bars.BarsSinceSession >= 1)
    			{
    				EnterLongStop(DefaultQuantity, PriorDayOHLC().PriorHigh[0], "");
    				
    			}
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Hello,

    Please try the following:
    Code:
    if (Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0])/Math.Abs(PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) < .5)
    return;
    Let me know if this does not work for you.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ok thanks, I'll try later.
      but, why the absolute value in the denominator?

      Comment


        #4
        Hello kantkant2,

        I included the absolute value of both subtraction operations to ensure the division result is positive. It is not necessary to use Math.abs twice. The following should work exactly the same.

        Code:
        if (Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0]/PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) < .5)
        return;

        Let me know if this does not resolve your inquiry.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          many thanks ChelseaB, it seems works better...
          but how can I do for visualize the results in the output window?
          sorry and many thanks for the support!

          Comment


            #6
            Hello kantkant2,

            To print to the output window use the Print() function.

            For example:

            Code:
            Print( (Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0]/PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]).ToString() );
            Below is a link to the help guide on the Print() function.
            http://www.ninjatrader.com/support/h...html?print.htm


            Please let me know if this does not resolve your inquiry.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks again Chelsea,
              sorry for the noise but this code give me wrong results:
              Code:
              (Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0] / PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) > .5)
              this is my output window:

              05/04/2013, Ratio=0.741113270405332, Close=26.83, Open=26.91, High=27.015, Low=26.575

              obviously the ratio is incorrect, in this case it should be 0.181818......

              any idea??

              Comment


                #8
                Hello kantkant2,

                The issue is order of operations.

                The division is happening before the subtraction. My first post (post #2) was actually correct. The denominator being absolute didn't really change anything.

                Code:
                if (Math.Abs(PriorDayOHLC().PriorClose[0] - PriorDayOHLC().PriorOpen[0])/(PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) < .5)
                return;

                Let me know if this does not correct the problem.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  oh, it's true!!

                  thanks, you are probably the best support at world!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Carolscogginsi, Yesterday, 10:45 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Carolscogginsi  
                  Started by RaddiFX, Yesterday, 10:15 AM
                  2 responses
                  15 views
                  0 likes
                  Last Post RaddiFX
                  by RaddiFX
                   
                  Started by patrickmlee007, Yesterday, 09:33 AM
                  2 responses
                  18 views
                  0 likes
                  Last Post patrickmlee007  
                  Started by magnatauren, 08-15-2020, 02:12 PM
                  5 responses
                  208 views
                  0 likes
                  Last Post RaddiFX
                  by RaddiFX
                   
                  Started by rene69851, 05-02-2024, 03:25 PM
                  1 response
                  24 views
                  0 likes
                  Last Post rene69851  
                  Working...
                  X