智能交易系统没有开仓交易



我试图在 mql4 中创建一个 EA,它根据给定的条件打开和关闭交易头寸,但它在匹配条件后没有打开交易,EA 工作直到显示买入和卖出的信号,之后什么也没发生。我该如何调试?

void CloseBuyPosition()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
string Cp = OrderSymbol();
if (_Symbol == Cp)
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL);
}
}
}
void CloseSellPosition()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
string Cp = OrderSymbol();
if (_Symbol == Cp)
if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, NULL);
}
}
}
void OnTick()
{
string signal = "";
double Sar = iSAR(_Symbol, _Period, 0.02, 0.2, 0);
if (Sar < Low[1] && Open[1] < Close[1])
{
signal = "buy";
}
if (Sar > High[1] && Open[1] > Close[1])
{
signal = "sell";
}
if (signal == "buy" && OrdersTotal() == 0)
OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, 20, 100, NULL, 0, 0, Green);
if (signal == "sell" && OrdersTotal() == 0)
OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red);
Comment("The Signal is :", signal);
if (Open[1] > Close[1] && OrdersTotal() > 0)
CloseBuyPosition();
if (Open[1] < Close[1] && OrdersTotal() > 0)
CloseSellPosition();
}

第 0 步:
检查您的 EA 是否在 MetaTrader4 终端内启动,并具有实际进行交易的活动权限。

第 1 步:
检查代码,因为至少有一些基本的自调试工具(GetLastError()Print()比间歇性和自毁性要好得多,即内部具有零历史深度的 GUI 文本,但最后一个可见Comment()

步骤2:
分析日志,其中所有打印的详细信息将帮助您跟踪根本原因(经纪人拒绝,关闭市场,有缺陷的价格水平以及OrderSend()呼叫被拒绝的许多可能原因(

int  OrderSend( string   symbol,              // symbol
int      cmd,                 // operation
double   volume,              // volume
double   price,               // price
int      slippage,            // slippage
double   stoploss,            // stop loss <------------ PRICE-DOMAIN levels, not INT-s
double   takeprofit,          // take profit <---------- PRICE-DOMAIN levels, not INT-s
string   comment = NULL,      // comment
int      magic = 0,           // magic number
datetime expiration = 0,      // pending order expiration
color    arrow_color = clrNONE// color
);

void OnTick()
{
/* string signal = "";
double Sar = iSAR( _Symbol, _Period, 0.02, 0.2, 0 );
if ( Sar <  Low[1] && Open[1] < Close[1] )      signal = "buy";
if ( Sar > High[1] && Open[1] > Close[1] )      signal = "sell";
if ( signal ==  "buy" && OrdersTotal() == 0 )   OrderSend( _Symbol, OP_BUY,  0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
if ( signal == "sell" && OrdersTotal() == 0 )   OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );
Comment( "The Signal is :", signal );
if ( Open[1] > Close[1] && OrdersTotal() > 0 )  CloseBuyPosition();
if ( Open[1] < Close[1] && OrdersTotal() > 0 )  CloseSellPosition();
*/
static int count_of_NOPs =  0;
if ( OrdersTotal() == 0 )
{    if (  Open[1] < Close[1]
&&  Low[1] > iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
) {  
int rc = OrderSend( _Symbol, OP_BUY,  0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_BUY  ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
rc,
GetLastError()
)
);
count_of_NOPs =  0; //------------------------------ .RESET
return; //------------------------------------------^ J.I.T./RET
}
if (   Open[1] > Close[1]
&& High[1] < iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
) { 
int rc = OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_SELL ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
rc,
GetLastError()
)
);
if ( )
count_of_NOPs =  0; //------------------------------ .RESET
return; //------------------------------------------^ J.I.T./RET
}
/* OTHERWISE: */
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID NOP... (for %d time)",
++count_of_NOPs
)
);
// */
}
else
{    if ( Open[1] > Close[1] ) {    Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseBuyPosition()...",
OrdersTotal()
)
);
CloseBuyPosition();
count_of_NOPs =  0; //---------- .RESET
return; //----------------------^ J.I.T./RET
}
if ( Open[1] < Close[1] )  {   Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseSellPosition()...",
OrdersTotal()
)
);
CloseSellPosition();
count_of_NOPs =  0; //---------- .RESET
return; //----------------------^ J.I.T./RET
}
/* OTHERWISE: */
Print( StringFormat( "HAVING %d ORDERS ACTIVE... DID NOP... (for %d time)",
OrdersTotal(),
++count_of_NOPs
)
);
// */
}

最新更新