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

LineAlert Indicator Nt7 to Nt8

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

    LineAlert Indicator Nt7 to Nt8

    Good Afternoon.

    I would like to import the Indicator from NT7 to NT8 and im having some issues with the code maybe someone could enlight me here with the Code.


    The Code.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("This indicator will alert crossovers for manually drawn lines. Alerts are reset with each new bar or when ManualReset input set to true. ")]
    public class LineAlert : Indicator
    {
    #region Variables
    private double lineLength;
    private double lineSlope;
    private double leftY;
    private double rightY;
    private double leftBarsAgo;
    private double rightBarsAgo;
    private double lineValueAtLastBar;
    
    //User inputs and default values here.
    private bool manualReset = false;
    private bool changeLineColor = true;
    private Color lineColor = Color.Red;
    private string alertMessage = "Price Crossed line here";
    private string soundFile = "Alert2.wav";
    
    #endregion
    
    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Overlay = true;
    CalculateOnBarClose = false;
    }
    
    public override string ToString()
    {
    return Name;
    }
    
    
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (Historical) return;
    
    RemoveDrawObject("noAlert");
    
    //resets alert each new bar.
    if (FirstTickOfBar)
    ResetAlert("AlertLineCrossing");
    
    //Manual Alert reset.
    if (ManualReset)
    {
    ResetAlert("AlertLineCrossing");
    ManualReset = false;
    }
    
    foreach (IDrawObject draw in DrawObjects)
    {
    
    if (draw.UserDrawn &&
    (draw.DrawType == DrawType.Line || draw.DrawType == DrawType.Ray || draw.DrawType == DrawType.ExtendedLine || draw.DrawType == DrawType.HorizontalLine))
    { //Sets some line properties programatically
    ILine globalLine = (ILine) draw;
    globalLine.Locked = false;
    globalLine.Pen.Width = 2;
    
    
    if (ChangeLineColor)
    globalLine.Pen.Color = LineColor;
    
    //Sets right and left points depending on how line is drawn.
    if (globalLine.StartBarsAgo > globalLine.EndBarsAgo)
    {
    leftY = globalLine.StartY;
    rightY = globalLine.EndY;
    leftBarsAgo = globalLine.StartBarsAgo;
    rightBarsAgo = globalLine.EndBarsAgo;
    }
    
    else if (globalLine.StartBarsAgo < globalLine.EndBarsAgo)
    {
    leftY = globalLine.EndY;
    rightY = globalLine.StartY;
    leftBarsAgo = globalLine.EndBarsAgo;
    rightBarsAgo = globalLine.StartBarsAgo;
    }
    
    //Sets lineLength based on lines position relative to most recent updated bar
    if (rightBarsAgo <= 0 && leftBarsAgo >= 0) //most Likely Alert scenario here
    lineLength = leftBarsAgo + Math.Abs(rightBarsAgo);
    
    else if (leftBarsAgo < 0 && rightBarsAgo < 0) //Alert possible --eventually, but no crossing is available yet
    lineLength = Math.Abs(rightBarsAgo) - Math.Abs(leftBarsAgo);
    
    else if (leftBarsAgo > 0 && rightBarsAgo > 0) //no alert case.
    lineLength = Math.Abs(rightBarsAgo - leftBarsAgo);
    
    lineSlope = ((rightY - leftY) / lineLength); //Sets slope
    
    if(draw.DrawType == DrawType.HorizontalLine)
    lineValueAtLastBar = globalLine.EndY;
    
    else if (leftBarsAgo == 0)
    lineValueAtLastBar = leftY;
    
    else if (leftBarsAgo > 0)
    lineValueAtLastBar = leftY + leftBarsAgo * lineSlope;
    
    //No Alert handling here.
    if ((leftBarsAgo < 0 && rightBarsAgo < 0) || (leftBarsAgo > 0 && rightBarsAgo > 0) && draw.DrawType != DrawType.HorizontalLine )
    DrawTextFixed("noAlert", "Check start & end points for your line(s). 1 or more is not eligible for an alert. ", TextPosition.TopRight);
    
    else if (leftBarsAgo == rightBarsAgo && draw.DrawType != DrawType.HorizontalLine)
    DrawTextFixed("noAlert", "Your line(s) is not eligible for an alert. No alerts for vertical lines", TextPosition.TopRight);
    
    else
    {
    //Alert Checking is done here. Can add your own actions to the block if desired.
    if(CrossBelow(Close, lineValueAtLastBar, 1) || CrossAbove(Close, lineValueAtLastBar, 1))
    {
    Alert("AlertLineCrossing", Cbi.Priority.High, alertMessage, soundFile, 0, Color.White, Color.Black);
    }
    }
    }
    }
    
    }
    
    #region Properties
    [Description("Set this value to true to manually reset the alert. ")]
    [GridCategory("Parameters")]
    public bool ManualReset
    {
    get { return manualReset; }
    set { manualReset = value; }
    }
    [Description("Allows the script to change color of manually placed lines.")]
    [GridCategory("Parameters")]
    public bool ChangeLineColor
    {
    get { return changeLineColor; }
    set { changeLineColor = value; }
    }
    [XmlIgnore()]
    [Description("Sets the color for all manually drawn lines and rays. ")]
    [GridCategory("Parameters")]
    public Color LineColor
    {
    get { return lineColor; }
    set { lineColor = value; }
    }
    
    [Browsable(false)]
    public string lineColorSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( lineColor); }
    set { lineColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }
    
    [Description("The message that appears in File > new > Alerts window")]
    [GridCategory("Parameters")]
    public string AlertMessage
    {
    get { return alertMessage; }
    set { alertMessage = value; }
    }
    
    [Description("The name of the sound file. NT will look for this file in \\Program Files\\NinjaTrader\\sounds")]
    [GridCategory("Parameters")]
    public string SoundFile
    {
    get { return soundFile; }
    set { soundFile = value; }
    }
    
    
    #endregion
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private LineAlert[] cacheLineAlert;
    public LineAlert LineAlert()
    {
    return LineAlert(Input);
    }
    
    public LineAlert LineAlert(ISeries<double> input)
    {
    if (cacheLineAlert != null)
    for (int idx = 0; idx < cacheLineAlert.Length; idx++)
    if (cacheLineAlert[idx] != null && cacheLineAlert[idx].EqualsInput(input))
    return cacheLineAlert[idx];
    return CacheIndicator<LineAlert>(new LineAlert(), input, ref cacheLineAlert);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.LineAlert LineAlert()
    {
    return indicator.LineAlert(Input);
    }
    
    public Indicators.LineAlert LineAlert(ISeries<double> input )
    {
    return indicator.LineAlert(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.LineAlert LineAlert()
    {
    return indicator.LineAlert(Input);
    }
    
    public Indicators.LineAlert LineAlert(ISeries<double> input )
    {
    return indicator.LineAlert(input);
    }
    }
    }
    
    #endregion
    and i'm having some issues with:
    Color - Line 33
    GridCategory -
    GridcategoryAtribute -

    Thanks in advance.

    #2
    Hello Rxxar,

    The properties in the script have changed, rather than trying to convert those items directly I could suggest to use the indicator wizard to generate those for you. Brushes in NT8 are easily generated this way and also include the serialization property. To do that you can Open the NinjaScript editor, right click on Indicators -> New indicator. Open the Inputs section and define the inputs you want including Brush inputs. Finally click generate and then you can copy/paste the properties that were made into your script. Closing the indicator without saving will discard it.

    NT8 now uses Brush for colors: https://ninjatrader.com/support/help...th_brushes.htm

    The Attributes for properties can be found here: https://ninjatrader.com/support/help...sub=attributes

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      can you share nt8 line alert indcator if possible?

      Comment


        #4
        Originally posted by hir04068 View Post
        can you share nt8 line alert indcator if possible?
        Good morning,

        I have the Ninja7 version but I couldn't convert it to Ninja8 due my absence of knowledge about C#. But anyway if you can update the code would be awesome.



        Attached Files

        Comment


          #5
          Pay me if I finish converting it.

          Rxxar

          Comment


            #6
            Did anybody make it work in NT8?

            Comment


              #7
              It took a while but I had Finally Compiled It. I have an issue now, there is no sound Alert when the price hits the Line but it changes the Color of the line when its hitted. Is somebody Capable of fixing it??

              Code:
              #region Using declarations
              using System;
              using System.Collections;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Diagnostics.CodeAnalysis;
              using System.Windows;
              using System.Reflection;
              using System.Windows.Media;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Gui;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Gui.Tools;
              using NinjaTrader.NinjaScript.DrawingTools;
              using NinjaTrader.NinjaScript.Indicators;
              #endregion
              
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.Indicators
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              public class LinesAlert : Indicator
              {
              #region Variables
              private double lineLength;
              private double lineSlope;
              private double leftY;
              private double rightY;
              private double leftBarsAgo;
              private double rightBarsAgo;
              private double lineValueAtLastBar;
              
              //User inputs and default values here.
              private bool manualReset = false;
              private bool changeLineColor = true;
              private Brush lineColor = Brushes.Red;
              private string alertMessage = "Price Crossed line here";
              private string soundFile = "Alert2.wav";
              
              #endregion
              
              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              private void Initialize()
              {
              IsOverlay = true;
              Calculate = Calculate.OnEachTick;
              }
              
              protected override void OnStateChange()
              {
              switch (State)
              {
              case State.SetDefaults:
              Name = "LineAlert";
              Description = "This indicator will alert crossovers for manually drawn lines. Alerts are reset with each new bar or when ManualReset input set to true. ";
              Initialize();
              break;
              }
              }
              
              public override string ToString()
              {
              return Name;
              }
              
              
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              if ((State == State.Historical)) return;
              
              RemoveDrawObject("noAlert");
              
              //resets alert each new bar.
              if (IsFirstTickOfBar)
              RearmAlert("AlertLineCrossing");
              
              //Manual Alert reset.
              if (ManualReset)
              {
              RearmAlert("AlertLineCrossing");
              ManualReset = false;
              }
              
              foreach (IDrawingTool draw in DrawObjects)
              {
              
              if (draw.IsUserDrawn &&
              (draw is DrawingTools.Line || draw is DrawingTools.Ray || draw is DrawingTools.ExtendedLine || draw is DrawingTools.HorizontalLine))
              { //Sets some line properties programatically
              NinjaTrader.NinjaScript.DrawingTools.Line globalLine = (NinjaTrader.NinjaScript.DrawingTools.Line) draw;
              globalLine.IsLocked = false;
              globalLine.Stroke.Width = 2;
              
              
              if (ChangeLineColor)
              globalLine.Stroke.Brush = LineColor;
              
              //Sets right and left points depending on how line is drawn.
              if (globalLine.StartAnchor.SlotIndex > globalLine.EndAnchor.SlotIndex)
              {
              leftY = globalLine.StartAnchor.Price;
              rightY = globalLine.EndAnchor.Price;
              leftBarsAgo = globalLine.StartAnchor.SlotIndex;
              rightBarsAgo = globalLine.EndAnchor.SlotIndex;
              }
              
              else if (globalLine.StartAnchor.SlotIndex < globalLine.EndAnchor.SlotIndex)
              {
              leftY = globalLine.EndAnchor.Price;
              rightY = globalLine.StartAnchor.Price;
              leftBarsAgo = globalLine.EndAnchor.SlotIndex;
              rightBarsAgo = globalLine.StartAnchor.SlotIndex;
              }
              
              //Sets lineLength based on lines position relative to most recent updated bar
              if (rightBarsAgo <= 0 && leftBarsAgo >= 0) //most Likely Alert scenario here
              lineLength = leftBarsAgo + Math.Abs(rightBarsAgo);
              
              else if (leftBarsAgo < 0 && rightBarsAgo < 0) //Alert possible --eventually, but no crossing is available yet
              lineLength = Math.Abs(rightBarsAgo) - Math.Abs(leftBarsAgo);
              
              else if (leftBarsAgo > 0 && rightBarsAgo > 0) //no alert case.
              lineLength = Math.Abs(rightBarsAgo - leftBarsAgo);
              
              lineSlope = ((rightY - leftY) / lineLength); //Sets slope
              
              if(draw is DrawingTools.HorizontalLine)
              lineValueAtLastBar = globalLine.EndAnchor.Price;
              
              else if (leftBarsAgo == 0)
              lineValueAtLastBar = leftY;
              
              else if (leftBarsAgo > 0)
              lineValueAtLastBar = leftY + leftBarsAgo * lineSlope;
              
              //No Alert handling here.
              if ((leftBarsAgo < 0 && rightBarsAgo < 0) || (leftBarsAgo > 0 && rightBarsAgo > 0) ) //&& draw != DrawingTools.HorizontalLine
              Draw.TextFixed(this,"noAlert", "Check start & end points for your line(s). 1 or more is not eligible for an alert. ", TextPosition.TopRight);
              
              else if (leftBarsAgo == rightBarsAgo) //&& draw != DrawingTools.HorizontalLine)
              Draw.TextFixed(this,"noAlert", "Your line(s) is not eligible for an alert. No alerts for vertical lines", TextPosition.TopRight);
              
              else
              {
              //Alert Checking is done here. Can add your own actions to the block if desired.
              if(CrossBelow(Close, lineValueAtLastBar, 1) || CrossAbove(Close, lineValueAtLastBar, 1))
              {
              Alert("AlertLineCrossing", Priority.High,Instrument.MasterInstrument.Name + " "+BarsPeriod.ToString() + " "+ AlertMessage , soundFile, 5, Brushes.White,Brushes.Black);
              }
              }
              }
              }
              
              }
              
              #region Properties
              [NinjaScriptProperty]
              [Display(Description = "Set this value to true to manually reset the alert. ", GroupName = "Parameters", Order = 1)]
              public bool ManualReset
              {
              get { return manualReset; }
              set { manualReset = value; }
              }
              [NinjaScriptProperty] [Display(Description = "Allows the script to change color of manually placed lines.", GroupName = "Parameters", Order = 1)] public bool ChangeLineColor
              {
              get { return changeLineColor; }
              set { changeLineColor = value; }
              }
              [XmlIgnore()]
              [NinjaScriptProperty] [Display(Description = "Sets the color for all manually drawn lines and rays. ", GroupName = "Parameters", Order = 1)]
              public Brush LineColor
              {
              get { return lineColor; }
              set { lineColor = value; }
              }
              
              [Browsable(false)]
              public string lineColorSerialize
              {
              get { return Serialize.BrushToString(lineColor); }
              set { lineColor = Serialize.StringToBrush(value); }
              }
              
              [NinjaScriptProperty]
              [Display(Description = "The message that appears in File > new > Alerts window", GroupName = "Parameters", Order = 1)]
              public string AlertMessage
              {
              get { return alertMessage; }
              set { alertMessage = value; }
              }
              
              [NinjaScriptProperty]
              [Display(Description = "The name of the sound file. NT will look for this file in \\Program Files (x86)\\NinjaTrader 8\\sounds", GroupName = "Parameters", Order = 1)]
              public string SoundFile
              {
              get { return soundFile; }
              set { soundFile = value; }
              }
              
              
              #endregion
              }
              }
              
              #region NinjaScript generated code. Neither change nor remove.
              
              namespace NinjaTrader.NinjaScript.Indicators
              {
              public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
              {
              private LinesAlert[] cacheLinesAlert;
              public LinesAlert LinesAlert(bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              return LinesAlert(Input, manualReset, changeLineColor, lineColor, alertMessage, soundFile);
              }
              
              public LinesAlert LinesAlert(ISeries<double> input, bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              if (cacheLinesAlert != null)
              for (int idx = 0; idx < cacheLinesAlert.Length; idx++)
              if (cacheLinesAlert[idx] != null && cacheLinesAlert[idx].ManualReset == manualReset && cacheLinesAlert[idx].ChangeLineColor == changeLineColor && cacheLinesAlert[idx].LineColor == lineColor && cacheLinesAlert[idx].AlertMessage == alertMessage && cacheLinesAlert[idx].SoundFile == soundFile && cacheLinesAlert[idx].EqualsInput(input))
              return cacheLinesAlert[idx];
              return CacheIndicator<LinesAlert>(new LinesAlert(){ ManualReset = manualReset, ChangeLineColor = changeLineColor, LineColor = lineColor, AlertMessage = alertMessage, SoundFile = soundFile }, input, ref cacheLinesAlert);
              }
              }
              }
              
              namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
              {
              public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
              {
              public Indicators.LinesAlert LinesAlert(bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              return indicator.LinesAlert(Input, manualReset, changeLineColor, lineColor, alertMessage, soundFile);
              }
              
              public Indicators.LinesAlert LinesAlert(ISeries<double> input , bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              return indicator.LinesAlert(input, manualReset, changeLineColor, lineColor, alertMessage, soundFile);
              }
              }
              }
              
              namespace NinjaTrader.NinjaScript.Strategies
              {
              public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
              {
              public Indicators.LinesAlert LinesAlert(bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              return indicator.LinesAlert(Input, manualReset, changeLineColor, lineColor, alertMessage, soundFile);
              }
              
              public Indicators.LinesAlert LinesAlert(ISeries<double> input , bool manualReset, bool changeLineColor, Brush lineColor, string alertMessage, string soundFile)
              {
              return indicator.LinesAlert(input, manualReset, changeLineColor, lineColor, alertMessage, soundFile);
              }
              }
              }
              
              #endregion

              Regards

              Comment


                #8
                Hello Rxxar,

                Thanks for the post.

                From what I can see the alert sound path is not correct. Are you seeing the alert in the alerts window and just not hearing it? If so you need to make the path like the following:



                Code:
                Alert("AlertLineCrossing", Priority.High,Instrument.MasterInstrument.Name + " "+BarsPeriod.ToString() + " "+ AlertMessage , [B]NinjaTrader.Core.Globals.InstallDir+@"\sounds\" + soundFile[/B], 5, Brushes.White,Brushes.Black);
                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Good afternoon Jesse. Thanks for the reply and the update.

                  I'm not seeing the alert or Hearing it when the price crosses the drawn Line. I have updated the code but it didn't work.

                  Comment


                    #10
                    Hello Rxxar,

                    If you are also not seeing the alert that may indicate the condition is not true. Have you tried using a Print before the condition to see if its true?

                    Code:
                    Print(Close[0] + " " lineValueAtLastBar + " or " + Close[0] + " " + lineValueAtLastBar);
                    if(CrossBelow(Close, lineValueAtLastBar, 1) || CrossAbove(Close, lineValueAtLastBar, 1))

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by bill2023, Yesterday, 08:51 AM
                    7 responses
                    42 views
                    0 likes
                    Last Post bill2023  
                    Started by yertle, Today, 08:38 AM
                    6 responses
                    25 views
                    0 likes
                    Last Post ryjoga
                    by ryjoga
                     
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    24 views
                    0 likes
                    Last Post algospoke  
                    Started by ghoul, Today, 06:02 PM
                    3 responses
                    16 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    46 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X