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 get Price of manually drawn Horizontal Line in Strategy.

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

    How to get Price of manually drawn Horizontal Line in Strategy.

    Hi there,

    I need to find manually drawn Horizontal Line price in strategy to use it in simple algorithm: when current price crosses Horizontal Line price go long.
    My main issues is to find price of drawn Horizontal Line. I am using this code to filter and find Horizontal Line in Objects lists.

    Code:
    foreach (DrawingTool draw in DrawObjects)
    {
    if (draw.GetType().Name == "HorizontalLine")
    {
    Print("Name: " + draw.Name + " GetType().Name: " + draw.GetType().Name + " draw.Tag: " + draw.Tag + " draw.Tag: ");
    }
    }
    Using this code I can find name, drawtype, tag of drawn object(Horizontal Line) but I can't find function like draw.Price.

    Please help me.

    Thank you.

    #2
    Hello Revazi123, thanks for your post.

    I made an example of how to do this in the past. You can download it from here:
    https://ninjatraderecosystem.com/use...izontal-lines/

    The code where I get the price from the horizontal line starts at line 151 and you will see I get the start anchor price with "l1.StartAnchor.Price"

    Please let me know if you have any questions on this material.

    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello ChrisL,

      Thanks for reply. I have tried to use your code and implement it in mine. this is what I have code:
      Code:
      #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.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class CustomStrategyForAndrei : Strategy
      {
      private Boolean _init = false;
      private string _priceFormat;
      private double _priceHorizontalLine;
      HorizontalLine myHorizontalLine;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Custom strategy for Andrei B.";
      Name = "CustomStrategyForAndrei";
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      PositionVolumeSize = 1;
      PositionStopLoss = 10;
      PositionTakeProfit = 10;
      LineShiftFromCurrentHigh = 10;
      }
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);
      }
      }
      
      protected override void OnBarUpdate()
      {
      //Add your custom strategy logic here.
      if (CurrentBar < BarsRequiredToTrade &&
      CurrentBar < 3 )
      {
      return;
      }
      
      
      if (!_init)
      {
      _init = true;
      int Digits = 0;
      if (TickSize.ToString().StartsWith("1E-"))
      {
      Digits = Convert.ToInt32(TickSize.ToString().Substring(3));
      }
      else if (TickSize.ToString().IndexOf(".") > 0)
      {
      Digits = TickSize.ToString().Substring(TickSize.ToString(). IndexOf("."), TickSize.ToString().Length - 1).Length - 1;
      }
      _priceFormat = string.Format("F{0}", Digits);
      }
      
      foreach (Gui.NinjaScript.IChartObject thisObject in ChartPanel.ChartObjects)
      {
      if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e)
      {
      myHorizontalLine = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e;
      
      //_priceHorizontalLine = myHorizontalLine.StartAnchor.Price.ToString(_price Format);
      _priceHorizontalLine = myHorizontalLine.StartAnchor.Price;
      }
      }
      
      Print("_priceHorizontalLine: " + _priceHorizontalLine.ToString(_priceFormat));
      
      // if (Close[0] > price &&
      // Close[1] < price )
      // {
      // EnterLong();
      // }
      // else if (Close[0] > price &&
      // Close[1] < price )
      // {
      // EnterShort();
      // }
      }
      
      #region Properties
      [NinjaScriptProperty]
      [Range(0.01, double.MaxValue)]
      [Display(Name="PositionVolumeSize", Order=1, GroupName="Parameters")]
      public double PositionVolumeSize
      { get; set; }
      
      [NinjaScriptProperty]
      [Range(0, double.MaxValue)]
      [Display(Name="PositionStopLoss", Order=2, GroupName="Parameters")]
      public double PositionStopLoss
      { get; set; }
      
      [NinjaScriptProperty]
      [Range(0, double.MaxValue)]
      [Display(Name="PositionTakeProfit", Order=3, GroupName="Parameters")]
      public double PositionTakeProfit
      { get; set; }
      
      [NinjaScriptProperty]
      [Range(0, double.MaxValue)]
      [Display(Name="LineShiftFromCurrentHigh", Order=4, GroupName="Parameters")]
      public double LineShiftFromCurrentHigh
      { get; set; }
      #endregion
      
      }
      }
      But unfortunately _priceHorizontalLine prints 0.00000
      Please if you can see any issues in my code let me know.
      Thanks you very much.

      Comment


        #4
        Hello Revazi123, thanks for your reply.

        It looks like it reads the line data once at the beginning of the script when if (!_init) is true. Does the line initialize after running on the first real time bar? Try it on a 10 second chart, running the code on the first real time bar. e.g.

        OnBarUpdate()
        {
        if(State == State.Historical) return;

        //run line identification code here and print data
        }

        You will need to debug by adding Print statements throughout the code to ensure the object is being set properly e.g.

        if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e)
        {
        Print("Found Horizontal Line");
        myHorizontalLine = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e;
        _priceHorizontalLine = myHorizontalLine.StartAnchor.Price;
        Print(_priceHorizontalLine);
        }

        I look forward to hearing from you.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hello Revazi123, thanks for your reply.

          It looks like it reads the line data once at the beginning of the script when if (!_init) is true. Does the line initialize after running on the first real time bar? Try it on a 10 second chart, running the code on the first real time bar. e.g.

          OnBarUpdate()
          {
          if(State == State.Historical) return;

          //run line identification code here and print data
          }

          You will need to debug by adding Print statements throughout the code to ensure the object is being set properly e.g.

          if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e)
          {
          Print("Found Horizontal Line");
          myHorizontalLine = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLin e;
          _priceHorizontalLine = myHorizontalLine.StartAnchor.Price;
          Print(_priceHorizontalLine);
          }

          I look forward to hearing from you.
          Hello ChrisL,

          Thanks for reply. Yes it is not getting Print("Found Horizontal Line"); I don't know why.
          If I use code what I first posted above then I can filter correctly and find out Horizontal Line but this new code doesn't works..
          What I am saying is that
          Code:
          if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLine)
          this code doesn't works and I don't know why.

          Thanks

          Comment


            #6
            Hello Revazi123, thanks for your reply.

            Are there Horizontal Line objects on the chart before starting the strategy (not Lines or Rays, or any other type of line)? Does my original script work properly on your system? (it works on my end)

            I look forward to hearing from you.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              Hello Revazi123, thanks for your reply.

              Are there Horizontal Line objects on the chart before starting the strategy (not Lines or Rays, or any other type of line)? Does my original script work properly on your system? (it works on my end)

              I look forward to hearing from you.
              Hello ChrisL,

              Thanks for reply. Yes there is Horizontal Line on chart and yes your original script works fine too but I have copied your code and don't understand why its not working.

              Thanks

              Comment


                #8
                O.. I find out. I have deleted Horizontal line and redrawed it and now its working. Same thing was with your script too.

                Thank you very much for your help.

                Comment


                  #9
                  This thread was a GREAT help to me! I'm making a slight tweak to only "look for" SOLID horizontal lines. So I've added an if-statement once I find any-ole horizontal line:

                  if ( myHorizontalLine.DashStyleHelper == DashStyleHelper.Solid) ...then capture and store the price, etc.....

                  Should that be the correct 'property' of a horizontal line to be checking? My strategy uses Dash-Dot-Dot horizontal lines to show stop-loss and profit-targets, so I don't want to pick those up with this routine.
                  Thanks so much for this! -Bill-

                  Comment


                    #10
                    The if-statement I added above didn't work, nor did several other variations I tried. Does anyone know how to tell if the horizontal line encountered is a SOLID line? thanks, -Bill-

                    Comment


                      #11
                      Hello Bill, thanks for your post.

                      The HorizontalLine object has a Stroke property. Have you accessed this within your script? From the Stroke property, it has a dashStyleHelper property that can be read.

                      Please let me know if I can assist any further.
                      Chris L.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by aa731, Today, 02:54 AM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by BarzTrading, Today, 07:25 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post BarzTrading  
                      Started by i019945nj, 12-14-2023, 06:41 AM
                      5 responses
                      65 views
                      0 likes
                      Last Post i019945nj  
                      Started by ruudawakening, Today, 12:58 AM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by thread, Yesterday, 11:58 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X