xm
xm
// Input Parameters
input double InitialCapital = 1000.0; // Starting capital in USD
input double TargetCapital = 6000.0; // Target capital in USD
input double MinLotSize = 0.1; // Minimum lot size
input double LossLimitPercent = 20.0; // Maximum loss limit in percentage
input double PriceDifferenceUSD = 5.0; // Minimum price difference to trigger
trade
input int ATRPeriod = 14; // ATR period for stop-loss
input double ATRMultiplier = 1.5; // ATR multiplier for initial stop-loss
input double TrailingStopUSD = 2.5; // Trailing stop in USD
input double Spread = 1.5; // Spread on slow broker for UK100Cash
input string FastPriceFilePath = "fastprice.txt"; // File path (relative to MQL5\
Files)
input int RetryAttempts = 3; // Number of retry attempts
input int RetryDelayMs = 200; // Delay between retries in milliseconds
input double RiskPercent = 1.0; // Risk per trade (%)
// Global Variables
CTrade trade; // Trading object
double currentCapital; // Current account balance
int tradeCount = 0; // Number of trades executed
bool isInitialized = false; // Initialization flag
datetime lastTradeTime = 0; // Track last trade time for delay
int atrHandle = INVALID_HANDLE; // Handle for ATR indicator
double atr[]; // Array for ATR values
ulong lastTicket = 0; // Track the last opened position ticket
double highestPriceSinceEntry = 0.0; // Track highest price for trailing stop
(buy)
double lowestPriceSinceEntry = 0.0; // Track lowest price for trailing stop
(sell)
double fastBrokerPrice = 0.0; // Fast broker price from IC Markets
//+------------------------------------------------------------------+
//| Expert Initialization Function |
//+------------------------------------------------------------------+
int OnInit()
{
// Ensure UK100Cash is available
if (!SymbolSelect("UK100Cash", true))
{
Print("Symbol UK100Cash not available!");
return(INIT_FAILED);
}
Print("--------------------------------------------------");
isInitialized = true;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert Tick Function |
//+------------------------------------------------------------------+
void OnTick()
{
if (!isInitialized) return;
if (lots > 0)
{
double marginRequired;
if (!OrderCalcMargin(ORDER_TYPE_BUY, "UK100Cash", lots, slowBrokerAsk,
marginRequired))
{
Print("Margin calculation failed!");
return;
}
if (AccountInfoDouble(ACCOUNT_FREEMARGIN) < marginRequired)
{
Print("Insufficient margin for ", DoubleToString(lots, 2), " lots.");
return;
}
if (positionType == POSITION_TYPE_BUY)
{
if (currentPrice > highestPriceSinceEntry)
highestPriceSinceEntry = currentPrice;
double trailingStop = highestPriceSinceEntry - TrailingStopUSD;
double currentSL = PositionGetDouble(POSITION_SL);
if (trailingStop > currentSL && currentPrice > trailingStop)
{
if (trade.PositionModify(lastTicket, trailingStop, 0))
Print("Trailing Stop updated for Buy: SL=",
DoubleToString(trailingStop, 2));
}
else if (currentPrice <= trailingStop)
{
ClosePosition(ticket, "Buy");
}
}
else if (positionType == POSITION_TYPE_SELL)
{
if (currentPrice < lowestPriceSinceEntry)
lowestPriceSinceEntry = currentPrice;
double trailingStop = lowestPriceSinceEntry + TrailingStopUSD;
double currentSL = PositionGetDouble(POSITION_SL);
if (trailingStop < currentSL && currentPrice < trailingStop)
{
if (trade.PositionModify(lastTicket, trailingStop, 0))
Print("Trailing Stop updated for Sell: SL=",
DoubleToString(trailingStop, 2));
}
else if (currentPrice >= trailingStop)
{
ClosePosition(ticket, "Sell");
}
}
}
}
currentCapital = AccountInfoDouble(ACCOUNT_BALANCE);
}
//+------------------------------------------------------------------+
//| Helper Functions |
//+------------------------------------------------------------------+
int attempt = 0;
bool fileReadSuccess = false;
double price = 0.0;
if (!fileReadSuccess)
{
Print("All attempts failed to read fast broker price file! Using fallback.");
price = slowBrokerAsk + 0.1;
}
return price;
}
//+------------------------------------------------------------------+
//| Expert Deinitialization Function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if (atrHandle != INVALID_HANDLE)
IndicatorRelease(atrHandle);
Print("EA stopped. Reason: ", reason);
}
//+------------------------------------------------------------------+
Print("Calculated Lot Size: ", DoubleToString(lots, 2));