I came across a situation where if you EnterShort() and SetStopLoss() and SetProfitTarget() on a bar that decreases in price. Then on the next bar EnterLong() where the price is increasing and try and also SetStopLoss() you will get an error and your strategy will be disabled. Since the SetStopLoss() was on the bar that EnterShort() the stop loss is set at a price higher than the second bar where you EnterLong() causing the error since you cannot have a stop loss above where you are trying to EnterLong(). The way I have been getting around this by setting the stop loss before EnterLong() on the second bar.
I feel like this is not how managed orders should work I read in the documentation that the SetStopLoss and SetProfitTargets should be cancelled automatically on strategies that have IsUnmanaged = false;
Here is a short test strategy to demonstrate this problem. Set Calculate = Calculate.OnEachTick; and IsUnmanaged = false;
bool enteredShortPosition = false;
bool enteredLongPosition = false;
protected override void OnBarUpdate()
{
if (State == State.Realtime)
{
if (CurrentBar < 1) return;
if (Position.MarketPosition == MarketPosition.Flat && Close[0] <= 5255.00 && enteredShortPosition == false)
{
EnterShort(6, CurrentBar.ToString());
SetStopLoss(CalculationMode.Price, 5255.50);
SetProfitTarget(CalculationMode.Price, 5251.00);
enteredShortPosition = true;
}
if (Position.MarketPosition == MarketPosition.Flat && Close[0] >= 5251.25 && enteredShortPosition == true && enteredLongPosition == false)
{
//Will prevent errors if set before EnterLong
//SetStopLoss(CalculationMode.Price, 5250.75);
EnterLong(6, CurrentBar.ToString());
//Will cause errors and diable strategy if SetStopLoss is set after EnterLong
SetStopLoss(CalculationMode.Price, 5250.75);
SetProfitTarget(CalculationMode.Price, 5253.70);
enteredLongPosition = true;
}
}
}
Run this strategy for two bars and you will get an error that disables the strategy. Then change the code so the SetStopLoss is not commented out before the EnterLong and comment out the SetStopLoss(CalculationMode.Price, 5250.75); that comes after EnterLong and you will no longer get an error. This is being caused because the SetStopLoss was first set at a higher price than when it enters long but this should not be since the strategy IsUnmanaged = false; the first SetStopLoss should have been cancelled when the proft target was hit.
Is this a bug? Is this how SetStopLoss should work, shouldn't the first SetStopLoss() be canceled once the profit target was hit? What is the correct way of handling this without having to SetStopLoss before the EnterLong?
Thanks

Comment