ninjatrader/strategies/TrendBreakout.cs

116 lines
3.9 KiB
C#

#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;
using NinjaTrader.Gui.PropertiesTest;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TrendBreakout : Strategy
{
private ATR atr;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Taken from Chapter 3 of Kaufman Constructs Trading Systems";
Name = "Trend Breakout";
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;
IsInstantiatedOnEachOptimizationIteration = true;
Period = 120;
ATRPeriod = 14;
ATRMultiplier = 2;
}
else if (State == State.DataLoaded)
{
atr = ATR(ATRPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Period)
return;
double highestHigh = MAX(High, Period)[1];
double lowestLow = MIN(Low, Period)[1];
/*double atrValue = atr[0];
double stopLoss = ATRMultiplier * atrValue;
double profitTarget = ATRMultiplier * atrValue;*/
if (Close[0] > highestHigh)
{
EnterLong();
//SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
//SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
}
/*if (Close[0] < lowestLow)
{
EnterShort();
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
}*/
if (Position.MarketPosition == MarketPosition.Long && Close[0] < lowestLow)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
#region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Period", Description = "Period for highest high and lowest low", Order = 1, GroupName = "Trend Breakout")]
public int Period { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Period", GroupName = "Trend Breakout", Order = 2)]
public int ATRPeriod { get; set; }
[Range(0.1, double.MaxValue), NinjaScriptProperty]
[Display(Name = "ATR Multiplier", GroupName = "Trend Breakout", Order = 3)]
public double ATRMultiplier { get; set; }
#endregion
}
}