'A' - Unexpected Token in MQL4 Code: Variable Type Missing
This article addresses a common error encountered in MQL4 programming: “A’ - unexpected token, probably type is missing.” This error occurs when you try to use a variable without explicitly declaring its data type.
To fix this error, you need to add a type declaration to the variable definition. For example, if you want to declare a variable named 'A' as a double-precision floating-point number, you would use the following code:
double A;
Here's an example of the corrected code:
// Define indicator variables
double A, B, D;
// Calculate indicator variable A
A = (3 * Close[0] + Low[0] + Open[0] + High[0]) / 6;
// Calculate indicator variable B
B = (20 * A + 19 * iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1) + 18 * iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 2) + ... + iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 20)) / 210;
// Calculate indicator variable D
D = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
// Define trading-related variables
double initialLots = 0.5;
double currentLots;
double profitTarget = 40;
double minFluctuation = MarketInfo(Symbol(), MODE_POINT) * 100;
// Execute trading logic
if (B > D && OrderMagicNumber() == 92133 && OrderType() == OP_BUY && OrderSymbol() == Symbol()) {
if (OrderTicket() == 0) {
// Open a long order
currentLots = initialLots;
int ticket = OrderSend(Symbol(), OP_BUY, currentLots, Ask, Slippage, 0, 0, "Buy Order", OrderMagicNumber());
if (ticket > 0) {
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
} else {
// Add to the long order
currentLots = OrderLots() * 0.6;
int ticket = OrderSend(Symbol(), OP_BUY, currentLots, Ask, Slippage, 0, 0, "Buy Order", OrderMagicNumber());
if (ticket > 0) {
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
// Check if a short order should be locked
if (MarketInfo(Symbol(), MODE_BID) < OrderOpenPrice() - minFluctuation * 100) {
if (OrderTicket() == 0) {
// Open a short order
currentLots = initialLots;
int ticket = OrderSend(Symbol(), OP_SELL, currentLots, Bid, Slippage, 0, 0, "Sell Order", OrderMagicNumber());
if (ticket > 0) {
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
} else {
// Add to the short order
currentLots = OrderLots() * 0.6;
int ticket = OrderSend(Symbol(), OP_SELL, currentLots, Bid, Slippage, 0, 0, "Sell Order", OrderMagicNumber());
if (ticket > 0) {
OrderSelect(ticket, SELECT_BY_TICKET);
currentLots = OrderLots();
}
}
}
}
// Other trading logic and take-profit/stop-loss logic should be written according to your needs.
Remember that the '...' in the code example indicates that you need to continue adding the corresponding iMA() function calculations. If you are unfamiliar with the QL4 programming language, we recommend referring to the official documentation and resources of MetaTrader 4 for further learning.
原文地址: https://www.cveoy.top/t/topic/bkNP 著作权归作者所有。请勿转载和采集!