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
}
}

Comment