I am Developing an Strategy and I am stuck with the BE. The initial Profit and StopLoss Setup works fine but the StopLoss I am trying it enters at BE to play after 15 ticks movement does not work. I mimic as my best knowledge the Samples I found here.
(FYI: The Strategy is still only for Short Entries).
Any idea?
Thanks!
#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
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class Keltner3BarsStopBE : Strategy
{
private double MyExitPrice;
private double MyStopPrice;
private double MyEntryPrice;
private KeltnerChannel KeltnerChannel1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "Keltner3BarsStopBE";
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 = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 1;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
KeltnerRatio = 2.5;
KeltnerPeriod = 20;
}
if (State == State.Configure)
{
SetStopLoss(CalculationMode.Ticks, MyStopPrice);
}
else if (State == State.DataLoaded)
{
KeltnerChannel1= KeltnerChannel(Close, KeltnerRatio, Convert.ToInt32(KeltnerPeriod));
KeltnerChannel1.Plots[0].Brush = Brushes.DarkGray;
KeltnerChannel1.Plots[1].Brush = Brushes.Red;
KeltnerChannel1.Plots[2].Brush = Brushes.ForestGreen;
AddChartIndicator(KeltnerChannel1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 110)
return;
int LastCloseBelowUpper = MRO(() => Close[0] < KeltnerChannel1.Upper[0],1, 100);
int LastBarDown = MRO(() => Close[0] < Open[0], 2, 100);
int LastTouchMiddle = MRO(() => Low[0] < KeltnerChannel1.Midline[0], 1, 100);
int PreviousLastCloseAboveUpper = MRO(() => Close[LastCloseBelowUpper] > KeltnerChannel1.Upper[LastCloseBelowUpper],1, 90);
// int NBarsDown = LRO(() => Close[0] > KeltnerChannel1.Upper[0],1, LastTouchMiddle);
// Set 1
if (
//First Bar Down Bigger thn 3 Ticks (Works)
(Close[0] < (Close[1] + (-3 * TickSize)))
//At Least 3 Bars Above Keltner Channel (Works)
&& (LastCloseBelowUpper > 3)
//Second to Last Pullback (Last one is the Entry) is 2 Bars after Keltner Brake or Before (only 1 pullback permited)
&& (LastBarDown > (LastCloseBelowUpper - 3))
//Last Touch LM is Before Previous Touch Upper
&& (LastTouchMiddle < PreviousLastCloseAboveUpper)
)
if ((Times[0][0].TimeOfDay >= new TimeSpan(8, 00, 0))
&& (Times[0][0].TimeOfDay < new TimeSpan(13, 00, 0)))
{
//Conditions
if (Position.MarketPosition == MarketPosition.Flat)
{
//This Should Place the Entry 1 tick below (Works)
MyEntryPrice = (Close[0] + (-1 * TickSize));
//This should plsce the Stop @ the Highest High of 7 bars ago.
MyStopPrice = High[HighestBar(High, 7)];
//This should place the Target @ 2xStop Distance from the Entry Level.
MyExitPrice = (3 * MyEntryPrice) - ( 2 * MyStopPrice);
//Stop and Target
SetStopLoss(@"MyEntry", CalculationMode.Price, MyStopPrice, false);
SetProfitTarget(@"MyEntry", CalculationMode.Price, MyExitPrice);
}
// If a short position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Short)
{
// Once the price is greater than entry price+15 ticks, set stop loss to breakeven
if (Close[0] < Position.AveragePrice - 15 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AveragePrice);
}
}
//Entry
EnterShortLimit(Convert.ToInt32(DefaultQuantity), MyEntryPrice , "MyEntry");
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="KeltnerRatio", Order=1, GroupName="Parameters")]
public double KeltnerRatio
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="KeltnerPeriod", Order=2, GroupName="Parameters")]
public int KeltnerPeriod
{ get; set; }
#endregion
}

Comment