I'm pretty close in the following but at a loss why the strategy is acting as follows
here is the premise of this strategy
When I switch from a green bar to a red bar (or vice versa) signifying change in direction and I have 2 bars in a row, the strategy enters an order at the close of the second up or down bar. It does this perfectly.
Once I am in profit x ticks (based on inputs) I want the SL to move to break even. Unfortunately it does this once but every time price moves ourside of the ticks profit it keeps moving the SL and doesn't keep it at breakeven.
Here is the code I have. Any help would be appreciated as I'm a newbie to this and can't seem to find why
Thanks,
amespace NinjaTrader.NinjaScript.Strategies
{
public class RenkoStrategyScalper : Strategy
{
private bool GotBar;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "RenkoStrategyScalper";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 2;
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;
Contracts = 1;
Target1 = 10;
Stop = 10;
Target2 = 20;
Contracts2 = 1;
Breakeven = 10;
GotBar = false;
}
else if (State == State.Configure)
{
SetProfitTarget(@"First", CalculationMode.Ticks, Target1);
SetStopLoss("", CalculationMode.Ticks, Stop, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
// Set 1
if ((Close[0] > Open[0])
&& (Close[1] < Open[1]))
{
GotBar = true;
}
// Set 2
if ((Close[0] > Open[1])
&& (GotBar == true)
&& (Close[1] > Close[2]))
{
GotBar = false;
BarBrush = Brushes.Lime;
SetStopLoss("", CalculationMode.Ticks, Stop, false);
EnterLong(Convert.ToInt32(Contracts2), second);
EnterLong(Convert.ToInt32(Contracts), First);
}
// Set 3
if ((Close[0] < Open[0])
&& (Close[1] > Open[1]))
{
GotBar = true;
}
// Set 4
if ((Close[0] < Open[1])
&& (GotBar == true)
&& (Close[1] < Close[2]))
{
GotBar = false;
BarBrush = Brushes.Red;
SetStopLoss("", CalculationMode.Ticks, Stop, false);
EnterShort(Convert.ToInt32(Contracts2), second);
EnterShort(Convert.ToInt32(Contracts), First);
}
// Set 5
if ((Position.MarketPosition == MarketPosition.Long)
&& (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) == Breakeven))
{
BackBrush = Brushes.CornflowerBlue;
Draw.Dot(this, @"RenkoBarScalper Dot_1", false, 0, Position.AveragePrice, Brushes.White);
SetStopLoss("", CalculationMode.Price, Position.AveragePrice, false);
}
// Set 6
if ((Position.MarketPosition == MarketPosition.Short)
&& (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) == Breakeven))
{
BackBrush = Brushes.CornflowerBlue;
Draw.Dot(this, @"RenkoBarScalper Dot_1", false, 0, Position.AveragePrice, Brushes.White);
SetStopLoss("", CalculationMode.Price, Position.AveragePrice, false);
}
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Contracts", Description="Number of Contracts", Order=1, GroupName="Parameters")]
public int Contracts
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Target1", Description="Profit Target 1", Order=2, GroupName="Parameters")]
public int Target1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Stop", Description="Stop Loss", Order=3, GroupName="Parameters")]
public int Stop
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Target2", Description="Profit Target 2", Order=4, GroupName="Parameters")]
public int Target2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Contracts2", Description="Number of Contracts Position 2", Order=5, GroupName="Parameters")]
public int Contracts2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Breakeven", Description="Move to Breakeven when in profits", Order=6, GroupName="Parameters")]
public int Breakeven
{ get; set; }
#endregion
}
}
Comment