I created a strategy using ATM order creation and placement. The orders are being placed exactly where I want them to be based on the "entryResult" but my problem is that the strategy places multiple orders. The orders are being placed correctly but I would like it to stop placing orders if there is already a position open. I have tried to use entry handling, I've tried a private bool that is only true when market position is flat, etc. None of those attempts worked. How can I accomplish this?
The portion of my code that controls order placement is below:
// Calculate the trade, create, and place orders.
private void TakeTheTrade()
{
if (State != State.Realtime)
{
return;
}
if (Open[0] == Close[0])
{
if (entryResult < Close[0])
{
// Long ATM Order.
atmStrategyId = GetAtmStrategyUniqueId();
atmStrategyOrderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, "DojiLongStrategy", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
{
// Checks that the call back is returned for the current atmStrategyId stored.
if (atmCallbackId == atmStrategyId)
{
// Check the atm call back for any error codes.
if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
{
// If no error, set private bool to true to indicate the atm strategy is created.
isAtmStrategyCreated = true;
}
}
});
if(isAtmStrategyCreated)
{
// ATM logic.
EnterLong("Long");
}
else if(!isAtmStrategyCreated)
{
// Custom handling for a failed atm Strategy.
Print("Long ATM Doji Strategy failed.");
}
}
else if (entryResult > Close[0])
{
// Short ATM Order.
atmStrategyId = GetAtmStrategyUniqueId();
atmStrategyOrderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, "DojiShortStrategy", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
{
// Checks that the call back is returned for the current atmStrategyId stored.
if (atmCallbackId == atmStrategyId)
{
// Check the atm call back for any error codes.
if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
{
// If no error, set private bool to true to indicate the atm strategy is created.
isAtmStrategyCreated = true;
}
}
});
if(isAtmStrategyCreated)
{
// ATM logic.
EnterShort("Short");
}
else if(!isAtmStrategyCreated)
{
// Custom handling for a failed atm Strategy.
Print("Short ATM Doji Strategy failed.");
}
}
}
}
Comment