Re: Heat Maps of Your Performance by Day and Hour
Posted: Wed Oct 14, 2026 3:19 pm
2. MetaTrader 5 (MQL5) Implementation
In MQL5, we use the CTrade library for execution. The architecture remains the same: the state of the time window dictates the strictness of the stop modification.
In MQL5, we use the CTrade library for execution. The architecture remains the same: the state of the time window dictates the strictness of the stop modification.
Code: Select all
#include <Trade\Trade.mqh>
CTrade trade;
input double StandardTrailPips = 10.0;
input double AggressiveTrailPips = 3.0;
void OnTick()
{
bool allowed = IsTradingAllowed();
// Always manage open positions
ManageTrailingStops(allowed);
// Gatekeeper for new entries
if(!allowed) return;
// Entry logic...
}
void ManageTrailingStops(bool isTradingAllowed)
{
double activeTrail = isTradingAllowed ? StandardTrailPips : AggressiveTrailPips;
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double trailDistance = activeTrail * 10 * point; // Assuming 5-digit broker
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
double currentStop = PositionGetDouble(POSITION_SL);
double currentTakeProfit = PositionGetDouble(POSITION_TP);
long type = PositionGetInteger(POSITION_TYPE);
if(type == POSITION_TYPE_BUY)
{
double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double newStop = currentBid - trailDistance;
if(currentStop == 0 || newStop > currentStop)
{
trade.PositionModify(ticket, newStop, currentTakeProfit);
}
}
else if(type == POSITION_TYPE_SELL)
{
double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double newStop = currentAsk + trailDistance;
if(currentStop == 0 || newStop < currentStop)
{
trade.PositionModify(ticket, newStop, currentTakeProfit);
}
}
}
}