Step 0:
check, whether your EA was launched inside MetaTrader4 Terminal with an active permission to actually do trades.
Step 1:
check the code, for having at least some elementary self-debugging tools ( GetLastError()
and Print()
are way better than intermittent and self-destroying, i.e. having Zero-depth of history GUI-text inside but a last visible Comment()
Step 2:
Analyze the logs, where all printed details will help you trace the root cause ( Broker-rejections, closed Market, flawed price-levels and many of the possible reasons for an OrderSend()
-call to get rejected )
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
)
);
// */
}