Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 300

 
buyanov:

But nothing has changed.

Both the error and the warning remain.

Give me the WHOLE code, not just a piece of it. Do you expect everyone to guess? Then ask here - here everyone knows everything about everything at once.

 

I give you the WHOLE code.

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
buyanov:

I give you the WHOLE code.

It's just huge...

 

All code

"

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property version "Cross20170703 1.00"


//=============================================== =========================

extern int Period_FastMA=4;

extern int Period_SlowMA=8;


extern int MaxTickNum=30;

extern int MaxAttempt=10;

extern int k_slippage=1;


extern double k_SL=10.0;//*Point()

extern double k_TP=30.0;//*Point()


extern double TrailDist=30.0;//*Point()

extern double k_lots=0.3;

extern bool ExFin=false;



int//**************************************************** ************************************


res,

count,

i,

NextTick=0,

time,

fintime,

BegMlSecTime,

EndMlSecTime,

deltatime,

Attempt,

slippage=5,

TickNum=0,

pos,

ticket,

MaxInterval1000,

CrossNum=0,

comis=4,

att,

attempt,



err;



double //**************************************************** *******************************


total,

lots,

spread,


fastma,

slowMA,


delta,


last delta,

PrevDelta,


Comis=0.00004,

openprice,

closeprice,

SL,

TP,


profit,

PROFIT;


bool//************************************************** **********************************************


FirstCross=false,

NextCross=false,

CurType,

success=true

enable=true,


BegUp,

BegDn,

BuyOpened=false,

SellOpened=false;


string//**************************************************** **********************************************


rec,

str,

record,

BegTimeCurrent,

EndTimeCurrent,

CurTime,

StrCur,

stringbeg,

StringEnd;

//************************************************** ******************************************************* ******


//=============================================== ================================================= ===============================

//=============================================== ================================================= ================================

//=============================================== ================================================= ================================




//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>

//------------------------------------------------ -------------------------------------------------- --------------------------


//=============================================== ================================================= ============================


int OnDeinit(const int res)

{

Alert("beg OnDeinit()");

err=StringToInteger((string)err);

return(res);

Alert("end OnDeinit()");

}

//OnDeinit();-----------

//------------------------------------------------ -------------------------------------------------- -----------------------




//=============================================== ================================================= =============================

void ExpertStop()

{

Alert("ExpertStop");

if((ExFin==true)&&(TickNum==MaxTickNum))

{

ExpertRemove();

OnDeinit(res);

}

} //end ExpertStop


//------------------------------------------------ -------------------------------------------------- ------------------------



//=============================================== ================================================= =========================

int ErrorDescription()

{

Alert("beg ErrorDescription");

switch(err)

{ case 0: err="No errors"; break;

case 1: err="No error, but result unknown"; break;

case 2: err="General error"; break;

case 3: err="Invalid parameters"; break;

case 4: err="Trade server is busy"; break;

case 5: err="Old version of client terminal"; break;

case 6: err="No connection to trade server"; break;

case 7: err="Insufficient rights"; break;

case 8: err="Requests too frequent"; break;

case 9: err="Illegal operation disrupting the server"; break;

case 64: err="Account blocked"; break;

case 65: err="Incorrect account number"; break;

case 128: err="Deal timeout expired"; break;

case 129: err="Incorrect price"; break;

case 130: err="Wrong stops"; break;

case 131: err="Incorrect volume"; break;

case 132: err="Market Closed"; break;

case 133: err="Trading prohibited"; break;

case 134: err="Not enough money to complete the operation"; break;

case 135: err="Price changed"; break;

case 136: err="No prices"; break;

case 137: err="Broker busy"; break;

case 138: err="New prices"; break;

case 139: err="The order is blocked and is already being processed"; break;

case 140: err="Purchase only allowed"; break;

case 141: err="Too many requests"; break;

case 145: err="Modification is not allowed because the order is too close to the market"; break;

case 146: err="Trading subsystem busy"; break;

case 147: err="Use of order expiration date is prohibited by the broker"; break;

case 148: err="The number of open and pending orders has reached the limit set by the broker.";break;

//

case 4000: err="No error"; break;

case 4001: err="Invalid function pointer"; break;

case 4002: err="Array index is out of range"; break;

case 4003: err="No memory for function stack"; break;

case 4004: err="Stack overflow after recursive call"; break;

case 4005: err="There is no memory on the stack to pass parameters"; break;

case 4006: err="Out of memory for string parameter"; break;

case 4007: err="Out of memory for temporary string"; break;

case 4008: err="Uninitialized string"; break;

case 4009: err="Uninitialized string in array"; break;

case 4010: err="Out of memory for string array"; break;

case 4011: err="String too long"; break;

case 4012: err="Zero remainder"; break;

case 4013: err="Division by zero"; break;

case 4014: err="Unknown command"; break;

case 4015: err="Incorrect transition"; break;

case 4016: err="Uninitialized array"; break;

case 4017: err="DLL calls not allowed"; break;

case 4018: err="Unable to load library"; break;

case 4019: err="Unable to call function"; break;

case 4020: err="Calls to external library functions are not allowed"; break;

case 4021: err="Not enough memory for string returned from function"; break;

case 4022: err="System busy"; break;

case 4050: err="Invalid number of function parameters"; break;

case 4051: err="Invalid function parameter value"; break;

case 4052: err="String function internal error"; break;

case 4053: err="Array error"; break;

case 4054: err="Incorrect use of timeseries array"; break;

case 4055: err=" Custom indicator error"; break;

case 4056: err="Arrays are incompatible"; break;

case 4057: err="Error processing global variables"; break;

case 4058: err="Global variable not found"; break;

case 4059: err="Function not allowed in test mode"; break;

case 4060: err="Function not allowed"; break;

case 4061: err="Error sending mail"; break;

case 4062: err="String parameter expected"; break;

case 4063: err="An integer parameter is expected"; break;

case 4064: err="double expected"; break;

case 4065: err="An array is expected as a parameter"; break;

case 4066: err="Requested historical data in update state"; break;

case 4067: err="Error while executing a trade operation"; break;

case 4099: err="End of file"; break;

case 4100: err="Error while working with file"; break;

case 4101: err="Invalid filename"; break;

case 4102: err="Too many open files"; break;

case 4103: err="Unable to open file"; break;

case 4104: err="Incompatible file access mode"; break;

case 4105: err="No orders selected"; break;

case 4106: err="Unknown character"; break;

case 4107: err="Incorrect price parameter for trade function"; break;

case 4108: err="Invalid ticket number"; break;

case 4109: err="Trading is not allowed. You must enable the Allow EA to trade option in the EA properties."; break;

case 4110: err="Long positions are not allowed. You need to check the EA properties."; break;

case 4111: err="Short positions are not allowed. You need to check the EA properties."; break;

case 4200: err="Object already exists"; break;

case 4201: err="An unknown object property was requested"; break;

case 4202: err="Object does not exist"; break;

case 4203: err="Unknown object type"; break;

case 4204: err="No object name"; break;

case 4205: err="Object coordinates error"; break;

case 4206: err="The specified subwindow was not found"; break;

default: err="Error while working with object";

}

Alert("end ErrorDescription");

return(err);

}//End ErrorDescription()

//------------------------------------------------ -------------------------------------------------- ------------------------


//=============================================== ================================================= =========================

int GetNext(NextTick)

//>>>>>>>>>>>>>>>>>>>>>>

{

Alert("beg GetNext(NextTick)");

Alert(NextTick);

while(RefreshRates()==false)Count++;

Alert("Count=",Count,"NextTick=",NextTick);

Alert(" ");

Alert(RefreshRates(),Count,NextTick);

return(NextTick);

Alert(NextTick);

//-------------------

Alert("end GetNext(NextTick)");

}

//end GetNext(NextTick)

//=============================================== ================================================= =========================


//------------------------------------------------ -------------------------------------------------- ------------------------

void OnTick()

{

//***********************************

Alert("beg OnTick");

//***********************************

FastMA=NormalizeDouble(iMA(NULL,0,Period_FastMA,0,MODE_SMA,PRICE_CLOSE,1),Digits());//Period_FastMA=4

SlowMA=NormalizeDouble(iMA(NULL,0,Period_SlowMA,0,MODE_SMA,PRICE_CLOSE,1),Digits());//Period_SlowMA=8

GetNext(NextTick);

Alert("NextTick=",NextTick);

Delta=NormalizeDouble(FastMA-SlowMA,Digits()); Alert("beg OnTick");

Alert(Delta,Digits());

Alert("FastMA=",DoubleToStr(FastMA ,Digits()),"SlowMA=",DoubleToStr(SlowMA,Digits()), "Delta=",DoubleToStr(Delta,Digits()));

Alert("end OnTick");

//------------------------------------------------ -------------------------------------------------- --------------------------

}// end (end OnTick)

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<

//------------------------------------------------ -------------------------------------------------- ----------------------------



//=============================================== ================================================= ============================



void Init()

{

Alert(" ");

Alert("beg Init()");

Alert(" ");



StringBeg=StringConcatenate("TimeToStr(TIME_DATE,TIME_SECONDS)=",TimeToStr(TIME_DATE,TIME_SECONDS));

alert(stringbeg);


record=StringConcatenate("MaxTickNum=",MaxTickNum);

alert(record);


record=StringConcatenate("k_SL=",DoubleToStr(k_SL,Digits()));

alert(record);


record=StringConcatenate("k_TP=",DoubleToStr(k_TP,Digits()));

alert(record);


record=StringConcatenate("k_lots=",DoubleToStr(k_lots,Digits()));

alert(record);


Alert("end Init()");


}//end Init()

//------------------------------------------------ -------------------------------------------------- ------------------------




//=============================================== ================================================= ======================

int GetTick(NextTick)


{


Alert("beg GetTick");


while (RefreshRates()==false)

NextTick++;

Alert("NextTick=",NextTick);


OrderSelect(NextTick,SELECT_BY_TICKET);


Profit=OrderProfit();


Alert(DoubleToStr(Profit, Digits));


GetNext(NextTick);


Alert("end GetTick");

}

//------------------------------------------------ -------------------------------------------------- -------------------------------------------------- ----


//=============================================== ================================================= ===========================


int NextType()

//>>>>>>>>>>>>>>>>>>>>>

{

Alert("beg NextType");


if(OrderSelect(NextTick,SELECT_BY_TICKET )==true)

{

OpenPrice=OrderOpenPrice();

ClosePrice=OrderClosePrice();

}


if(ClosePrice>OpenPrice)CurType=OP_BUY;


else CurType=OP_SELL;




return(curType);

Alert("end CurType()");

//>>>>>>>>>>>>>>>>>>>>>>>>>

}//"end CurType()";


//------------------------------------------------ -------------------------------------------------- ----------------------------



//=============================================== ================================================= =============================


bool FastMACrossUp()

{


if(!(Delta==LastDelta))Alert("Delta=",DoubleToStr(Delta,5),"LastDelta=",DoubleToStr(LastDelta,5));


Alert("LastDelta=",DoubleToStr(LastDelta,5));

Alert("!(Delta==LastDelta)=",!(Delta==LastDelta));

if(((Delta!=LastDelta) && (Delta>=0) && (LastDelta<=0)))Alert("( Delta!=LastDelta)&&(Delta>=0)&&(LastDelta<=0)= " ,

(Delta!=LastDelta) && (Delta>=0) && (LastDelta<=0));

Alert("(Delta>=0)=",(Delta>=0),"(LastDelta<=0)=",(LastDelta<=0));

if(Delta!=LastDelta)Alert("Delta=",DoubleToStr(Delta,5)," LastDelta=",DoubleToStr(LastDelta,5));

Alert("(Delta>=0)=",(Delta>=0)," ( LastDelta<=0)=",(LastDelta<=0)," TickNum=",TickNum);

Alert("(Delta>=0)=",(Delta>=0));

Alert("LastDelta<=",(LastDelta<=0));

Alert("!(Delta==LastDelta)&&((Delta>=0)&&(LastDelta<=0))=",!(Delta==LastDelta)&&((Delta>=0)&&(LastDelta<=0 )));

Alert(((Delta!=LastDelta)&&((Delta>=0)&&(LastDelta<=0))));

Alert(" (Delta!=LastDelta)= ",(Delta!=LastDelta)," (Delta>=0)= ",(Delta>=0)," (LastDelta<=0)= ",(LastDelta<= 0));

return((Delta!=LastDelta)&&(Delta>=0)&&(LastDelta<=0));

} //end FastMACrossUp()

//------------------------------------------------ -------------------------------------------------- --------------------



//=============================================== ================================================= =====================

bool FastMACrossDn()

{

if(!(Delta==LastDelta)&&(Delta<=0)&&(LastDelta>=0))Alert("Delta=",DoubleToStr(Delta,5)," LastDelta=",DoubleToStr(LastDelta,5)) ;


Alert("LastDelta=",DoubleToStr(LastDelta,5));

Alert("!(Delta==LastDelta)=",!(Delta==LastDelta));

Alert("(Delta<=0)=",(Delta<=0));

Alert("LastDelta>=0",(LastDelta>=0));

if((Delta!=LastDelta) && (Delta<=0) && (LastDelta>=0))

Alert("(Delta<=0)=",(Delta<=0),"(LastDelta>=0)=",(LastDelta>=0));

Alert(" (Delta!=LastDelta)&&((Delta<=0)&&(LastDelta>=0))= ",(Delta!=LastDelta) && (Delta<=0) && (LastDelta>=0));

if(Delta!=LastDelta)Alert(" Delta=",DoubleToStr(Delta,5)," LastDelta=",DoubleToStr(LastDelta,5));

Alert("(Delta<=0)=",(Delta<=0)," (LastDelta>=0)=",(LastDelta>=0)," TickNum=",TickNum);

Alert(" (Delta!=LastDelta)&&(( Delta<=0)&&(LastDelta>=0))= ",(Delta!=LastDelta)&&((Delta<=0)&&(LastDelta>=0)) );

return((Delta!=LastDelta)&&(Delta<=0)&&(LastDelta>=0));

}//end FastMACrossDn()

//------------------------------------------------ -------------------------------------------------- --------------------------------------



//=============================================== ================================================= ====================================

bool FastMAIsAbove()

{

Alert("beg FastMAIsAbove()");

Alert(" (Delta>=0)=",(Delta>=0)," (LastDelta>=0)=",(LastDelta>=0));

if(((Delta>=0)&&(LastDelta>=0)))

Alert(" FastMA= ",DoubleToStr(FastMA,5)," SlowMA= ",DoubleToStr(SlowMA,5));

Alert(" Delta= ",DoubleToStr(Delta,5)," LastDelta= ",DoubleToStr(LastDelta,5));

Alert("end FastMAIsAbove()");

Alert("(Delta==LastDelta)&&(Delta>=0)&&(LastDelta>=0)=",(Delta==LastDelta)&&(Delta>=0)&&(LastDelta>=0));

Alert("end FastMAIsAbove()");

return((Delta==LastDelta)&&(Delta>=0)&&(LastDelta>=0));

}//end FastMAIsAbove()

//=============================================== ================================================= ==================


//------------------------------------------------ -------------------------------------------------- ------------------

bool FastMAIsBelow()

{


return((Delta==LastDelta)&&(Delta<=0)&&(LastDelta<=0));

}//end FastMAIsBelow()*/

"

//------------------------------------------------ -------------------------------------------------- ------------------

//************************************************** *****************************************

"


 
Dmitriy Voevodkin:

Init cannot be started in any way, it is automatically started once when indicator/expert is started, same as DeInit.

Alternatively, you can make a static boolean variable and use it together withIsConnected(). And when the connection is restored at the arrival of a new tick, it must be launched first, some initialization function of its own. ButIsConnected() should be used not in OnCalculate(), but in OnTimer() and my own initialization function should be used at the very beginning of OnCalculate().


Thanks, I'll think about it, I just thought that there are already typical solutions......

 
buyanov:

All code

Wow, that's a lot of alerts.
 
Mihail Marchukajtes:

Thanks, I'll think about it, I just thought there were typical solutions already available......

I've already written a rebuttal to this person's words. OnItit() is an ordinary function. Run it when you need to.

 
buyanov:

All code


I told you already - attach the code using the SRC button. Sorry, are you completely unlearned?

Tell me, who would want to look at this piece of junk?

 
Artyom Trishkin:

I have already written the rebuttal of this man's words. OnItit() is an usual function. You may run it when you need it.


Well, yes, I was wondering why not call it when you connect. Turns out you can :-)

 
buyanov:

All code


Watch out for variable types.

Throw out all unnecessary Alerts

Files:
test.mq4  42 kb
Reason: