I'm in the process of developing a strategy and am considering implementing trailing rather than sticking to a fixed profit target and stop-loss. My current setup isn't performing as expected. Here's the issue:
I aim to create a trailing mechanism that, upon entry, sets a stop-loss at 50 ticks. As the market moves favorably, I want the mechanism to track the market movement tick by tick, maintaining a 10-tick distance. I attempted to utilize the setrtrailstop method, but it hasn't achieved the desired results. Can anyone identify a mistake in my approach or offer any advice on how to improve it?
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyCustomStrategy";
Calculate = Calculate.OnEachTick;
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 = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
// Add Bollinger Bands and Stochastics indicators
bollinger = Bollinger(2, 20);
stochastics = Stochastics(3, 14, 1);
}
else if (State == State.DataLoaded)
{
// Add your custom indicator calls here
AddChartIndicator(bollinger);
AddChartIndicator(stochastics);
}
}
protected override void OnBarUpdate()
{
//Ensure we have enough bars for the Bollinger Bands calculation
if (CurrentBar < 20) return;
CheckEntryConditions();
TrailingStrategy();
}
private void CheckEntryConditions()
{
// Check to ensure we're not already in a position
if (Position.MarketPosition == MarketPosition.Flat)
{
if ("entry condition long")
{
EnterLong("LongEntry");
CurrentTriggerPriceLong = Close[0] + (TrailFrequency * TickSize);
CurrentStopPriceLong = Close[0] - (TrailStopDistance * TickSize);
double initialStopLossPrice = Close[0] - (50 * TickSize); // 50 ticks above the entry price
SetStopLoss("longEntry", CalculationMode.Price, initialStopLossPrice, false);
Draw.ArrowUp(this, "buySignal" + CurrentBar, false, 0, Low[0] - TickSize * 5, Brushes.Green);
}
else if ("entry condition short")
{
// Enter short position
EnterShort("ShortEntry");
CurrentTriggerPriceShort = Close[0] - (TrailFrequency * TickSize);
CurrentStopPriceShort = Close[0] + (TrailStopDistance * TickSize);
double initialStopLossPrice = Close[0] + (50 * TickSize);
SetStopLoss("shortEntry", CalculationMode.Price, initialStopLossPrice, false);
// Draw an arrow down for a sell signal
Draw.ArrowDown(this, "sellSignal" + CurrentBar, false, 0, High[0] + TickSize * 5, Brushes.Red);
}
}
}
private void TrailingStrategy()
{
if (Position.MarketPosition == MarketPosition.Long)
{
// For a long position, update the trailing stop only if the price has moved favorably.
if (Close[0] > CurrentTriggerPriceLong)
{
CurrentTriggerPriceLong = Close[0] + (TrailFrequency * TickSize);
CurrentStopPriceLong = Close[0] - (TrailStopDistance * TickSize);
ExitLongStopMarket(0, true, Position.Quantity, CurrentStopPriceLong, "LongExit", "LongEntry");
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
// For a short position, update the trailing stop only if the price has moved favorably.
if (Close[0] < CurrentTriggerPriceShort)
{
CurrentTriggerPriceShort = Close[0] - (TrailFrequency * TickSize);
CurrentStopPriceShort = Close[0] + (TrailStopDistance * TickSize);
ExitShortStopMarket(0, true, Position.Quantity, CurrentStopPriceShort, "ShortExit", "ShortEntry");
}
}
}
this is what currently happens and it should hit the stopp loss instead of keeping the position open until the end of the day.
Comment