Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

plot the stopatr indicator on the chart

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

    plot the stopatr indicator on the chart

    I want to plot the stopatr indicator on the chart along with the candles, but it's not working, I always get this error and I don't know what else to do

    ERRO:
    'Line' is an ambiguous reference between 'NinjaTrader.Gui.Line' and 'NinjaTrader.NinjaScript.DrawingTools.Line'

    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

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class StopAtrStrategy : Strategy
    {
    private double atrMultiplier = 1.5;
    private int atrPeriod = 5;
    private bool positionOpen = false;
    private double entryPrice = 0.0;
    private double stopLossPrice = 0.0;

    private ATR atrIndicator;

    private Line highStopAtrLine;
    private Line lowStopAtrLine;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Stop ATR Strategy";
    Name = "StopAtrStrategy";
    Calculate = Calculate.OnBarClose;
    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;

    AddPlot(new Stroke(Brushes.DodgerBlue), PlotStyle.Line, "StopLossPrice");
    }
    else if (State == State.Configure)
    {
    atrIndicator = ATR(atrPeriod);
    AddChartIndicator(atrIndicator);
    SetStopLoss(CalculationMode.Price, 0);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (!positionOpen)
    {
    double stopAtr = CalculateStopAtr();

    // Verifique se atende aos critérios de entrada para compra
    if (Close[0] > stopAtr)
    {
    entryPrice = Close[0];
    stopLossPrice = entryPrice - stopAtr;

    EnterLong();
    positionOpen = true;
    }
    }
    else
    {
    double stopAtr = CalculateStopAtr();

    // Verifique se atende aos critérios de saída
    if (Close[0] < stopLossPrice)
    {
    ExitLong();
    positionOpen = false;
    }
    }

    // Atualize a plot do preço de stop loss
    StopLossPrice[0] = stopLossPrice;

    // Plotar linhas do Stop ATR no gráfico
    double highStopAtr = High[0] + CalculateStopAtr();
    double lowStopAtr = Low[0] - CalculateStopAtr();

    if (highStopAtrLine == null)
    highStopAtrLine = Draw.Line(this, "HighStopAtr", false, Time[0], highStopAtr, Time[0], highStopAtr, Brushes.Red.ToString());
    else
    highStopAtrLine.EndAnchor = new ChartAnchor(Time[0], highStopAtr);

    if (lowStopAtrLine == null)
    lowStopAtrLine = Draw.Line(this, "LowStopAtr", false, Time[0], lowStopAtr, Time[0], lowStopAtr, Brushes.Green.ToString());
    else
    lowStopAtrLine.EndAnchor = new ChartAnchor(Time[0], lowStopAtr);
    }

    private double CalculateStopAtr()
    {
    var atr = atrIndicator.Value[0];
    return atr * atrMultiplier;
    }

    region Properties
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> StopLossPrice
    {
    get { return Values[0]; }
    }
    #endregion
    }
    }​

    #2
    Hello andersja,

    private DrawingTools.Line highStopAtrLine;
    private DrawingTools.Line lowStopAtrLine;​

    Or alternatively, remove the using statement for NinjaTrader.Gui.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    72 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X