I hope you are all doing well. This is my first post here, and I must admit that I'm not a professional coder. I've been working on a trading strategy, and I'm currently facing an issue that has me stumped.
My strategy takes only one trade and is unable to take a second trade even after my strategy criteria have been met. While I've gone through my code, everything seems fine to me, but I suspect I might have overlooked something.
I'm sharing a snippet of my strategy code below. Please take a look and see if you can spot any potential issues. Your expertise would be highly appreciated. Thank you.
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Fast = 1;
Slow = 18;
//Description = @"Enter the description for your new custom Strategy here.";
Name = "AkATMStrategyAndEMA";
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;
}
else if (State == State.Configure)
{
smaFast = SMA(Fast);
smaSlow = SMA(Slow);
smaFast.Plots[0].Brush = Brushes.Goldenrod;
smaSlow.Plots[0].Brush = Brushes.SeaGreen;
AddChartIndicator(smaFast);
AddChartIndicator(smaSlow);
atmStrategyId = string.Empty;
atmStrategyOrderId = string.Empty;
}
}
protected override void OnBarUpdate()
{
if (State < State.Realtime)
return;
// Check if there's an active ATM strategy, and exit the method
if (atmStrategyId.Length > 0)
return;
// Clear the ATM strategy ID after the trade is closed.
if (Position.MarketPosition == MarketPosition.Flat)
{
atmStrategyId = string.Empty;
}
if (Position.MarketPosition == MarketPosition.Flat)
{
if (CrossAbove(smaFast, smaSlow, 1) && Close[0] > smaSlow[0])
{
atmStrategyId = GetAtmStrategyUniqueId();
atmStrategyOrderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, High[0], 0, TimeInForce.Day,
atmStrategyOrderId, "SampleATMEMA", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
{
if (atmCallbackId == atmStrategyId)
{
if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
{
isAtmStrategyCreated = true;
}
}
});
}
else if (CrossBelow(smaFast, smaSlow, 1) && Close[0] < smaSlow[0])
{
atmStrategyId = GetAtmStrategyUniqueId();
atmStrategyOrderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Sell, OrderType.Limit, Low[0], 0, TimeInForce.Day,
atmStrategyOrderId, "SampleATMEMA", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
{
if (atmCallbackId == atmStrategyId)
{
if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
{
isAtmStrategyCreated = true;
}
}
});
}
}
}
Comment