Announcement

Collapse
No announcement yet.

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 sjsj2732, Yesterday, 04:31 AM
                  0 responses
                  31 views
                  0 likes
                  Last Post sjsj2732  
                  Started by NullPointStrategies, 03-13-2026, 05:17 AM
                  0 responses
                  286 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  283 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  133 views
                  1 like
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  91 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Working...
                  X