namespace NinjaTrader.NinjaScript.Strategies
{
public class BreakEvenBuilderExample : Strategy
{
private double StopPrice;
private double TriggerPrice;
private int TriggerState;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "BreakEvenBuilderExample";
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;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
BreakEvenTrigger = 5;
InitialStopDistance = -10;
StopPrice = 0;
TriggerPrice = 0;
TriggerState = 0;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
// Set 1
if ((TriggerState <= 2)
&& (Position.MarketPosition == MarketPosition.Flat))
{
TriggerState = 0;
}
if (CurrentBars[0] < 1)
return;
// Set 2
if ((Position.MarketPosition == MarketPosition.Flat)
&& (CrossBelow(Close, High, 1))
&& (State == State.Realtime))
{
TriggerState = 1;
EnterShort(Convert.ToInt32(DefaultQuantity), @"entry");
}
// Set 3
if ((TriggerState == 1)
&& (Position.MarketPosition == MarketPosition.Short))
{
TriggerState = 2;
StopPrice = (Position.AveragePrice + (InitialStopDistance * TickSize)) ;
TriggerPrice = (Position.AveragePrice + (BreakEvenTrigger * TickSize)) ;
}
// Set 4
if ((TriggerState == 2)
&& (Close[0] <= TriggerPrice))
{
TriggerState = 3;
StopPrice = Position.AveragePrice;
Draw.Diamond(this, @"BreakEvenBuilderExample Diamond_1", true, 0, (High[0] + (2 * TickSize)) , Brushes.DarkCyan);
}
// Set 5
if (TriggerState >= 2)
{
ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), StopPrice, @"exit", @"entrty");
}
}
Comment