В чем проблема этого кода EA(экспертная система), что он не работает?
Я использую демо-счет и пытаюсь протестировать очень простого робота EA, который покупает акции только по заранее указанной цене. Я мог бы скомпилировать его без ошибок, но когда я тестирую его на предыдущих данных (в тесте стратегии), он не совершает никакой сделки, хотя условие цены достигает нескольких раз! (Часть истории пуста).
В чем проблема и как я могу это исправить?
#property copyright "xxx"
#property link "https://www.mql5.com"
#property version "1.00"
input int StopLoss=250; // Stop Loss
input int TakeProfit=250; // Take Profit
input int EA_Magic=12345; // EA Magic Number
input double Lot=1000; // Lots to Trade
double p_close; // Variable to store the close value of a bar
int STP, TKP; // To be used for Stop Loss & Take Profit values
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
STP = StopLoss;
TKP = TakeProfit;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- Define some MQL5 Structures we will use for our trade
MqlTick latest_price; // To be used for getting recent/latest price quotes
MqlTradeRequest mrequest; // To be used for sending our trade requests
MqlTradeResult mresult; // To be used to get our trade results
MqlRates mrate[]; // To be used to store the prices, volumes and spread of each bar
ZeroMemory(mrequest); // Initialization of mrequest structure
MqlTradeCheckResult mmresult;
/*
Let's make sure our arrays values for the Rates, ADX Values and MA values
is store serially similar to the timeseries array
*/
// the rates arrays
ArraySetAsSeries(mrate,true);
//--- Get the last price quote using the MQL5 MqlTick Structure
if(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
//--- Get the details of the latest 3 bars
if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
{
Alert("Error copying rates/history data - error:",GetLastError(),"!!");
ResetLastError();
return;
}
//--- we have no errors, so continue
//--- Do we have positions opened already?
bool Buy_opened=false; // variable to hold the result of Buy opened position
bool Sell_opened=false; // variables to hold the result of Sell opened position
if(PositionSelect(_Symbol)==true) // we have an opened position
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
Buy_opened=true; //It is a Buy
}
else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
Sell_opened=true; // It is a Sell
}
}
// Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
p_close=mrate[1].close; // bar 1 close price
if(p_close == 2622 || 2629)
{
// any opened Buy position?
if(Buy_opened)
{
Alert("We already have a Buy Position!!!");
return; // Don't open a new Buy Position
}
ZeroMemory(mrequest);
mrequest.action = TRADE_ACTION_DEAL; // immediate order execution
// mrequest.price = NormalizeDouble(latest_price.ask,_Digits); // latest ask price
// mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
// mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
mrequest.symbol = _Symbol; // currency pair
mrequest.volume = Lot; // number of lots to trade
mrequest.magic = EA_Magic; // Order Magic Number
mrequest.type = ORDER_TYPE_BUY; // Buy Order
mrequest.type_filling = ORDER_FILLING_IOC; //ORDER_FILLING_FOK; // Order execution type
mrequest.deviation=100; // Deviation from current price
//--- send order
// OrderSend(mrequest,mresult);
if(OrderSend(mrequest,mresult))
Print("Everything is OK");
else Alert("There is a problem here");
if(OrderCheck(mrequest,mmresult))
Print("Everything is OK");
else Alert("There is a problem hereee");
// get the result code
if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
{
Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
}
else
{
Alert("The Buy order request could not be completed -error:",GetLastError());
ResetLastError();
return;
}
}
}
РЕДАКТИРОВАТЬ: я снова проверил свой код, когда рынок открыт и получил эти сообщения с предупреждением:
There is a problem here
There is a problem hereee
The Buy order request could not be completed -error:4756