Mql5 programming - expert bot source code

Joined
Nov 4, 2024
Messages
1
Reaction score
0
I have a nice expert advisor. I'm not familiar with mql5 language. It has several errors such as maybe MqlTradeRequest and MqlTradeResults headers are needed in the script, many "if" cycles should not be written on "global scope", some "if" cycles are written correctly. Here I give you the code to improve for me. It has more than 19 errors... For example "request.action", "request.magic", "request.price", etc, are unrecognizable commands.


//+------------------------------------------------------------------+
//| Function to open a random trade with ATR-based stop-loss and TP |
//+------------------------------------------------------------------+
void OpenRandomTrade() {
ENUM_ORDER_TYPE direction = (MathRand() % 2 == 0) ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
double price = (direction == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);

// Retrieve ATR values
double atrValueArray[1];
double atrAverageArray[1];

if (CopyBuffer(atrHandle, 0, 0, 1, atrValueArray) > 0 && CopyBuffer(atrAverageHandle, 0, 0, 1, atrAverageArray) > 0) {
double atrValue = atrValueArray[0];
double atrAverage = atrAverageArray[0];

if (atrValue <= 0 || atrAverage <= 0) {
Print("Error calculating ATR values");
return;
}

// Calculate ATR-based stop-loss distance
double stopLossDistance = atrValue * ATRMultiplier;
double stopLossLevel = (direction == ORDER_TYPE_BUY) ? price - stopLossDistance : price + stopLossDistance;

// Determine volatility level and dynamic take-profit multiplier
double tpMultiplier;
if (atrValue > atrAverage * VolatilityThresholdHigh) {
tpMultiplier = 3.0;
} else if (atrValue > atrAverage * VolatilityThresholdLow) {
tpMultiplier = 2.5;
} else {
tpMultiplier = 2.0;
}

// Calculate take-profit based on volatility-adjusted multiplier
double takeProfitDistance = stopLossDistance * tpMultiplier;
double takeProfitLevel = (direction == ORDER_TYPE_BUY) ? price + takeProfitDistance : price - takeProfitDistance;

// Open the trade with calculated stop-loss and take-profit
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);

request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = currentLotSize;
request.price = price;
request.sl = stopLossLevel;
request.tp = takeProfitLevel;
request.magic = MAGIC_NUMBER;
request.type = direction;
request.deviation = 3;

if (OrderSend(request, result)) {
if (result.retcode == TRADE_RETCODE_DONE) {
Print("Trade opened successfully with ticket #", result.order);
lastTradeCloseTime = TimeCurrent() + TradeDuration * 60;
} else {
Print("Error opening trade: ", result.retcode);
}
} else {
Print("Error sending order: ", GetLastError());
}
} else {
Print("Error retrieving ATR values");
}
}

//+------------------------------------------------------------------+
//| Function to manage and close trades after holding duration |
//+------------------------------------------------------------------+
void ManageOpenTrades() {
if (PositionSelect(_Symbol)) {
double profit = PositionGetDouble(POSITION_PROFIT);

// Check if holding duration has elapsed
if (TimeCurrent() >= lastTradeCloseTime) {
// Close the trade
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);

request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = PositionGetDouble(POSITION_VOLUME);
request.type = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
request.price = (request.type == ORDER_TYPE_SELL) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
request.magic = MAGIC_NUMBER;

if (OrderSend(request, result)) {
if (result.retcode == TRADE_RETCODE_DONE) {
Print("Trade closed after 8 minutes with profit: ", profit);
// Adjust lot size based on profit
currentLotSize = (profit <= 0) ? currentLotSize * LotMultiplier : InitialLotSize;
} else {
Print("Error closing trade: ", result.retcode);
}
} else {
Print("Error sending close order: ", GetLastError());
}
}
}
}

I tested the code and unfortunately it was full of errors. You can view errors in MetaEditor 5.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,695
Latest member
StanleyDri

Latest Threads

Top