I am trying to move my stop to BE after price moves past a certain threshold, not sure what i am doing wrong but it doesn’t seem to be working despite my print statements are printing. This is my method (i know i have an extra criteria in the long side, i removed it on the short side just for testing)
I am using this method CheckAndMoveToBreakEven(currentPrice) to move stops
private void CheckAndMoveToBreakEven(double currentPrice)
{
if (Position.MarketPosition == MarketPosition.Flat)
{
_stopMovedToBE = false;
return;
}
double averagePrice = Position.AveragePrice;
string positionType = Position.MarketPosition == MarketPosition.Long ? "Long" : "Short";
if (Position.MarketPosition == MarketPosition.Long && !_stopMovedToBE)
{
double beTriggerPrice = averagePrice + breakEven;
Print($"[{positionType}] Checking BE | Avg Price: {averagePrice} | Current Price: {currentPrice} | BE Trigger Price: {beTriggerPrice}");
if (currentPrice >= beTriggerPrice)
{
double newStopPrice = averagePrice + breakEvenOffset;
Print($"LONG | Moving stop to BE: {newStopPrice}");
ExitLongStopMarket(0, true, Position.Quantity, newStopPrice, "BE Stop", "Long Level");
_stopMovedToBE = true;
}
else
{
Print($"LONG | Not at BE yet. Current: {currentPrice}, Target: {beTriggerPrice}");
}
}
else if (Position.MarketPosition == MarketPosition.Short && !_stopMovedToBE)
{
double beTriggerPrice = averagePrice - breakEven;
Print($"[{positionType}] Checking BE | Avg Price: {averagePrice} | Current Price: {currentPrice} | BE Trigger Price: {beTriggerPrice}");
if (currentPrice <= beTriggerPrice)
{
double newStopPrice = averagePrice - breakEvenOffset;
Print($"SHORT | Moving stop to BE: {newStopPrice}");
ExitShortStopMarket(0, true, Position.Quantity, newStopPrice, "BE Stop", "Short Level");
_stopMovedToBE = true;
}
else
{
Print($"SHORT | Not at BE yet. Current: {currentPrice}, Target: {beTriggerPrice}");
}
}
}
I am calling this in OnBarUpdate()
double currentPrice = Input[0]; // or Bid / Ask depending on strategy
if (!(Position.MarketPosition == MarketPosition.Flat))
{
Print($"Checking for BE, current price: {currentPrice}");
CheckAndMoveToBreakEven(currentPrice);
}
In the print statement it seems everythingshould be working, and even shows it making some change to the order, but in fact the order never changes. Any ideas why?