Re: Mistake: Ignoring Spreads When Choosing Setups
Posted: Tue Oct 06, 2026 10:06 pm
2. Update the OnTick Method
Replace your current OnTick() method with this. It loops through the active positions managed by this bot, dynamically calculates the 1:1 price based on the actual executed Stop Loss distance, and triggers the close.
Replace your current OnTick() method with this. It loops through the active positions managed by this bot, dynamically calculates the 1:1 price based on the actual executed Stop Loss distance, and triggers the close.
Code: Select all
public override void OnTick()
{
// Keeps the UI math updating
UpdateDashboard();
// Iterate only through positions opened by this specific cBot
foreach (var position in Positions.FindAll("SpreadProtected", SymbolName))
{
// Skip if we already took a partial, or if there is no Stop Loss
if (_partiallyClosedPositions.Contains(position.Id) || !position.StopLoss.HasValue)
continue;
// Calculate the absolute risk distance in price
double riskInPrice = Math.Abs(position.EntryPrice - position.StopLoss.Value);
// Calculate the exact 1:1 target price
double target1To1Price = position.TradeType == TradeType.Buy
? position.EntryPrice + riskInPrice
: position.EntryPrice - riskInPrice;
// Check if current price has reached the 1:1 threshold
bool is1To1Reached = position.TradeType == TradeType.Buy
? Symbol.Bid >= target1To1Price
: Symbol.Ask <= target1To1Price;
if (is1To1Reached)
{
// Divide volume by 2 and safely normalize to broker step sizes
double halfVolume = Symbol.NormalizeVolumeInUnits(position.VolumeInUnits / 2, RoundingMode.Down);
if (halfVolume >= Symbol.VolumeInUnitsMin)
{
// 1. Close half the position asynchronously
ClosePositionAsync(position, halfVolume);
// 2. Optional: Roll remaining stop loss to Entry Price (Breakeven)
ModifyPositionAsync(position, position.EntryPrice, position.TakeProfit);
// 3. Mark this position ID so we never process it again
_partiallyClosedPositions.Add(position.Id);
Print("1:1 Reached. Partial taken and SL moved to BE for Position ID: " + position.Id);
}
}
}
}