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

how to use absolute value method

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

    how to use absolute value method

    hello
    please can you help in how to get this indicator compiling which should be signalling when there is a wide body candle next to a small body candle.
    Thank you.

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class abaBodiesModified : Indicator
    {
    private EMA EMA1;
    private ROC ROC1;
    private ABS ABS1;
    private ABS ABS2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "abaBodiesModified";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    IsAutoScale = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    First_body_size_Min = -0.01;
    First_body_size_Max = 0.01;
    Second_Bar_Multiplier = 8.0;
    AddPlot(Brushes.DodgerBlue, "Pinch");

    }
    else if (State == State.Configure)
    {
    EMA1 = EMA(Close, 8);
    ROC1 = ROC(Close, 1);
    abs1 = abs(Close[0] - Open[0]);
    abs2 = abs(Close[1] - Open[1]);

    EMA1.Plots[0].Brush = Brushes.Goldenrod;

    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 4)
    return;
    {
    Value[0] = +0;
    }



    // Set 1
    if (
    // conditions for SMALL BODY bar for LONGs
    // using ROC indicator in order to set percentage value of the body of 1st candle. for ex +- 0.01%
    (
    (ROC1[1] < First_body_size_Max)
    && (ROC1[1] > First_body_size_Min))

    // conditions for WIDE BODY bar for LONGs
    // using absolute value because 1st bar can be either upbar or down bar,
    // ie value of 1st bar body can be either positive or negative

    && (Close[0] > Open[0])
    && abs1 > (abs2*Second_Bar_Multiplier)
    )
    {
    BackBrush = Brushes.ForestGreen;
    }

    // Set 2
    if (
    // conditions for SMALL BODY bar for SHORTs
    (
    (ROC1[1] < First_body_size_Max)
    && (ROC1[1] > First_body_size_Min))
    // conditions for WIDE BODY bar for SHORTs
    && (Close[0] < Open[0])
    && abs1 > (abs2*Second_Bar_Multiplier)
    )
    {
    BackBrush = Brushes.OrangeRed;
    }

    }

    public override void OnCalculateMinMax()
    {
    MinValue = - 1;
    MaxValue = + 1;
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(-99, double.MaxValue)]
    [Display(Name="First_body_size_Min", Order=1, GroupName="Parameters")]
    public double First_body_size_Min
    { get; set; }

    [NinjaScriptProperty]
    [Range(-99, double.MaxValue)]
    [Display(Name="First_body_size_Max", Order=2, GroupName="Parameters")]
    public double First_body_size_Max
    { get; set; }



    [NinjaScriptProperty]
    [Range(-99, double.MaxValue)]
    [Display(Name="Second_Bar_Multiplier", Order=6, GroupName="Parameters")]
    public double Second_Bar_Multiplier
    { get; set; }
    #endregion

    }
    }

    #2
    searching on internet I found out that the name of the method for getting the absolute value is Math.Abs.
    So I think I was able to compile the indicator, that appears to be owrking correctly.
    I attach it here for anyone interested in using it and/or anyone willing to improve it.
    I would be grateful if anyone could modify it so that the input parameters could be inserted with numbers shown in percentage format (as shown in yellow on the attached chart image).
    Modifying the script in such a way is way too difficult for me.
    Thanks.


    Click image for larger version  Name:	input in percent.jpg Views:	1 Size:	199.9 KB ID:	1056102
    Last edited by guidoisot; 05-09-2019, 01:39 PM. Reason: minor changes on CandlestickBody.cs

    Comment


      #3
      Originally posted by guidoisot View Post
      searching on internet I found out that the name of the method for getting the absolute value is Math.Abs.
      So I think I was able to compile the indicator, that appears to be owrking correctly.
      I attach it here for anyone interested in using it and/or anyone willing to improve it.
      I would be grateful if anyone could modify it so that the input parameters could be inserted with numbers shown in percentage format (as shown in yellow on the attached chart image).
      Modifying the script in such a way is way too difficult for me.
      Thanks.


      Click image for larger version

Name:	input in percent.jpg
Views:	1760
Size:	199.9 KB
ID:	1056102
      Just multiply your calculated values by 100 to turn them into percentages. That is the definition of percent.

      e.g.,
      instead of:
      Code:
      Math.Abs((Close[1]-Open[1])/Open[1]) < MaxSizeFirstCandleBody_as_perc_of_Price
      use:
      Code:
      Math.Abs((Close[1]-Open[1]) * 100/Open[1]) < MaxSizeFirstCandleBody_as_perc_of_Price
      The left hand side of that inequality is now expressed as a percentage.

      Do the same for other calculations that you will use as comparators.

      Comment


        #4
        thank you.
        your suggestion is very helpful because it allows to take off two zeros. From your reply, I understand it is not possible to have the % symbol on the fields where I wrote the % numbers in yellow, because these fields can only accept doubles.
        Last edited by guidoisot; 05-04-2019, 03:58 PM.

        Comment


          #5
          Originally posted by guidoisot View Post
          thank you.
          your suggestion is very helpful because it allows to take off two zeros. From your reply, I understand it is not possible to have the % symbol on the fields where I wrote the % numbers in yellow, because these fields can only accept doubles.
          I would not say that it is impossible: it is just a bit convoluted, to little purpose, I would say. Essentially you would need to make that parameter text, rather than a double, then parse the text into a double before using it. You would also need to modify the PropertyGrid itself to place text (namely the "%" sign) after the input.

          It might make more sense to simply place a "%" sign in brackets as part of the label (sort of as a hint), then place a more elaborate statement in the description of the parameter. Just my suggestion. You may be able to see that I have been down this road before.

          Comment


            #6
            Hello guidoisot,

            Thank you for your post.

            ABS is not an indicator and cannot be referenced as such. It can be called like this:

            Code:
            private EMA EMA1;
            private ROC ROC1;
            //create new series to hold our abs values
            private Series<double> Abs1;
            private Series<double> Abs2;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                Description = @"Enter the description for your new custom Indicator here.";
                Name = "abaBodiesModified";
                Calculate = Calculate.OnBarClose;
                IsOverlay = true;
                DisplayInDataBox = true;
                DrawOnPricePanel = true;
                DrawHorizontalGridLines = true;
                DrawVerticalGridLines = true;
                PaintPriceMarkers = true;
                IsAutoScale = true;
                ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                //See Help Guide for additional information.
                IsSuspendedWhileInactive = true;
                First_body_size_Min = -0.01;
                First_body_size_Max = 0.01;
                Second_Bar_Multiplier = 8.0;
                AddPlot(Brushes.DodgerBlue, "Pinch");
            
                }
                else if (State == State.Configure)
                {
                EMA1 = EMA(Close, 8);
                ROC1 = ROC(Close, 1);
                Abs1[0] = Math.Abs(Close[0] - Open[0]);
                Abs2[0] = Math.Abs(Close[1] - Open[1]);
            
                EMA1.Plots[0].Brush = Brushes.Goldenrod;
            
                }
                else if (State == State.DataLoaded)
                {                
                    Abs1 = new Series<double>(this);
                    Abs2 = new Series<double>(this);
                }
            }
            
            
            protected override void OnBarUpdate()
            {
            if (CurrentBars[0] < 4)
            {
                Value[0] = +0;
                return;
            }
            
            
            
            // Set 1
            if (
            // conditions for SMALL BODY bar for LONGs
            // using ROC indicator in order to set percentage value of the body of 1st candle. for ex +- 0.01%
            (
            (ROC1[1]< First_body_size_Max)
            && (ROC1[1] > First_body_size_Min))
            
            // conditions for WIDE BODY bar for LONGs
            // using absolute value because 1st bar can be either upbar or down bar,
            // ie value of 1st bar body can be either positive or negative
            
            && (Close[0] > Open[0])
            && Abs1[0] > (Abs2[0] * Second_Bar_Multiplier)
            )
            {
            BackBrush = Brushes.ForestGreen;
            }
            
            // Set 2
            if (
            // conditions for SMALL BODY bar for SHORTs
            (
            (ROC1[1] < First_body_size_Max)
            && (ROC1[1] > First_body_size_Min))
            // conditions for WIDE BODY bar for SHORTs
            && (Close[0] < Open[0])
            && Abs1[0] > (Abs2[0] * Second_Bar_Multiplier)
            )
            {
            BackBrush = Brushes.OrangeRed;
            }
            
            }
            Here is a link to our help guide on System.Math():
            https://ninjatrader.com/support/help...m_highlightsub =math

            Please let us know if we may be of further assistance to you.
            Last edited by NinjaTrader_ChelseaB; 08-28-2022, 07:11 PM.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Sparkyboy, Today, 10:57 AM
            0 responses
            0 views
            0 likes
            Last Post Sparkyboy  
            Started by TheMarlin801, 10-13-2020, 01:40 AM
            21 responses
            3,916 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by timmbbo, 07-05-2023, 10:21 PM
            3 responses
            152 views
            0 likes
            Last Post grayfrog  
            Started by Lumbeezl, 01-11-2022, 06:50 PM
            30 responses
            808 views
            1 like
            Last Post grayfrog  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X