It seems that most of your code is in the global scope. That is not allowed. It must be with in a function or method.
Do you have some coding background in other computer languages?
Please inform of the errors and show us where they occur!
Thank you Fernando.
The errors are as follows:
1. 'for' - expressions are not allowed on a global scope (This occurred just after // Loop through all open positions and calculate the total lot size, take profit and stop loss)
2. 'if' - expressions are not allowed on a global scope (This occurred just after // Calculate the average take profit and average stop loss)
3. 'for' - expressions are not allowed on a global scope (This occurred just after // Loop through all positions again and modify the take profit and stop loss to the average values)
It seems that most of your code is in the global scope. That is not allowed. It must be with in a function or method.
Do you have some coding background in other computer languages?
Fernando,
I do not have a coding background. I am a newbie to the world of coding.
Kindly help modify (if possible).
Many thanks.
1. 'for' - expressions are not allowed on a global scope (This occurred just after // Loop through all open positions and calculate the total lot size, take profit and stop loss)
2. 'if' - expressions are not allowed on a global scope (This occurred just after // Calculate the average take profit and average stop loss)
3. 'for' - expressions are not allowed on a global scope (This occurred just after // Loop through all positions again and modify the take profit and stop loss to the average values)
Already answered in previous post. Your functional code cannot be in the global scope. It should be in a function.
For Scripts, that would be in the OnStart event handler, but for an EA it might be the OnTick event handler.
Already answered in previous post. Your functional code cannot be in the global scope. It should be in a function.
For Scripts, that would be in the OnStart event handler, but for an EA it might be the OnTick event handler.
Immediately i put it all under the Onstart event, the number of errors on compilation increased, hence, it still not working. This is how i included it under the Onstart event:
void OnStart () { // Declare variables double totalTakeProfit = 0.0; double totalStopLoss = 0.0; double totalLotSize = 0.0; double avgTakeProfit = 0.0; double avgStopLoss = 0.0; // Input settings for desired take profit and stop loss input double desiredTakeProfit = 5; // Default to 5 pips input double desiredStopLoss = 10; // Default to 10 pips // Calculate take profit and stop loss in points double takeProfitInPoints = desiredTakeProfit * _Point; double stopLossInPoints = desiredStopLoss * _Point; // Loop through all open positions and calculate the total lot size, take profit and stop loss for (int i = 0; i < PositionsTotal(); i++) { if (PositionSelect(i)) { // Calculate the take profit level based on open price of the position double takeProfit = PositionGetDouble(POSITION_PRICE_OPEN) + takeProfitInPoints; // Calculate the stop loss level based on open price of the position double stopLoss = PositionGetDouble(POSITION_PRICE_OPEN) - stopLossInPoints; // Calculate the lot size of the position double lotSize = PositionGetDouble(POSITION_VOLUME); // Add take profit of the position to the total totalTakeProfit += takeProfit * lotSize; // Add stop loss of the position to the total totalStopLoss += stopLoss * lotSize; // Add lot size of the position to the total totalLotSize += lotSize; } } // Calculate the average take profit and average stop loss if (totalLotSize > 0) { avgTakeProfit = totalTakeProfit / totalLotSize; avgStopLoss = totalStopLoss / totalLotSize; } // Loop through all positions again and modify the take profit and stop loss to the average values for (int i = 0; i < PositionsTotal(); i++) { if (PositionSelect(i)) { // Modify the take profit level to the average value bool takeProfitModified = PositionModify(PositionGetDouble(POSITION_STOPLOSS), avgTakeProfit); // Modify the stop loss level to the average value bool stopLossModified = PositionModify(avgStopLoss, PositionGetDouble(POSITION_TAKEPROFIT)); if (takeProfitModified && stopLossModified) { Print("Position ", PositionGetInteger(POSITION_TICKET), " modified to average take profit: ", DoubleToString(avgTakeProfit), ", average stop loss: ", DoubleToString(avgStopLoss)); } else { Print("Position ", PositionGetInteger(POSITION_TICKET), " modification failed."); } } } }
Not really feasible because this concept of global scope is a very basic level of programming knowledge that you need to research and learn.
I also have no idea what it is you are trying to achieve.
Even if I assume it is a script and not an EA, putting it in the OnStart event handler it still not going to solve the problem because you have many other errors ...
// Declare variables double totalTakeProfit = 0.0; double totalStopLoss = 0.0; double totalLotSize = 0.0; double avgTakeProfit = 0.0; double avgStopLoss = 0.0; // Input settings for desired take profit and stop loss input double desiredTakeProfit = 5; // Default to 5 pips input double desiredStopLoss = 10; // Default to 10 pips // Calculate take profit and stop loss in points double takeProfitInPoints = desiredTakeProfit * _Point; double stopLossInPoints = desiredStopLoss * _Point; // Script OnStart event handler void OnStart() { // Loop through all open positions and calculate the total lot size, take profit and stop loss // ... rest of your code ... }
You will need to dedicate some time and effort learning the basics of coding.
MQL is based on C/C++ so looking into those languages may also help your learn general basic coding skills too.
for (int i = 0; i < PositionsTotal(); i++) { if (PositionSelect(i)) //.... }What does this line of code do exactly?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi there folks,
I need a bit of help. I am pretty much a beginner in mql5 coding i tried getting this piece of code to work but i keep getting 3 compilation errors. Its a simple TP and SL averaging script. Kindly help take a look.
I will very much appreciate.
Thank you.