MetaTrader 4 QL4 错误:变量声明和全局作用域问题
'A' - unexpected token, probably type is missing? 44444.mq4 41 1 'A' - variable already defined 44444.mq4 41 1 see previous declaration of variable 'A' 44444.mq4 38 8 'B' - unexpected token, probably type is missing? 44444.mq4 44 1 '.' - object required 44444.mq4 44 119 '.' - object required 44444.mq4 44 120 '.' - object required 44444.mq4 44 121 'B' - variable already defined 44444.mq4 44 1 see previous declaration of variable 'B' 44444.mq4 38 11 'D' - unexpected token, probably type is missing? 44444.mq4 47 1 'D' - variable already defined 44444.mq4 47 1 see previous declaration of variable 'D' 44444.mq4 38 14 'if' - expressions are not allowed on a global scope 44444.mq4 56 1
非常抱歉,我之前提供的代码仍然存在错误。在 QL4 中,代码必须在函数体内编写,而不能在全局范围内。以下是经过修正的代码示例,您可以将其放置在适当的函数中:
void OnTick()
{
// 定义指标变量
double A, B, D;
// 计算指标变量A
A = (3 * Close[0] + Low[0] + Open[0] + High[0]) / 6;
// 计算指标变量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;
// 计算指标变量D
D = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
// 定义交易相关变量
double initialLots = 0.5;
double currentLots;
double profitTarget = 40;
double minFluctuation = MarketInfo(Symbol(), MODE_POINT) * 100;
// 执行交易逻辑
if (B > D && OrderMagicNumber() == 92133 && OrderType() == OP_BUY && OrderSymbol() == Symbol()) {
if (OrderTicket() == 0) {
// 开仓多单
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 {
// 补仓多单
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();
}
}
// 判断是否触发锁仓做空
if (MarketInfo(Symbol(), MODE_BID) < OrderOpenPrice() - minFluctuation * 100) {
if (OrderTicket() == 0) {
// 开仓空单
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 {
// 补仓空单
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();
}
}
}
}
// 其他交易逻辑和止盈平仓逻辑,请根据您的需求进行编写
}
请将以上代码放置在 OnTick() 函数中,并确保在 MetaTrader 4 中正确编译和运行。如果您对 QL4 编程语言不熟悉,建议参考 MetaTrader 4 的官方文档和资源进行学习。请注意,以上代码仍然是一个简化的示例,实际的交易逻辑可能需要更加复杂的处理,例如错误处理、止损和止盈等。
原文地址: http://www.cveoy.top/t/topic/bkVt 著作权归作者所有。请勿转载和采集!