MQL4 Learning - page 118

 

HELP Needed: iRSIonArray issue

Hi.

I just discovered that all functions of indicator on array such as iRSIonArray, iCCIonArray, iMAonArray etc, show different results if run as an Expert Advisor.

I write small code, as indicator, the copy/paste it to Expert Advisor and got wrong results.

double tempArray[][6];

double Array_eurusd[1500];

ArrayCopyRates(tempArray,"EURUSD",PERIOD_H1);

for(int i=0;i<1500;i++)

Array_eurusd=tempArray[4]; //Get close price

ArraySetAsSeries(Array_eurusd,true);

double irsi=iRSI("EURUSD",PERIOD_H1,14,PRICE_CLOSE,0);

double irsiOnArray=iRSIOnArray(Array_eurusd,1500,14,0);

Comment("Function iRSI= "+irsi+"\n"+

"Function iRSIOnArray= "+irsiOnArray);

I wonder what might cause the values to fail when running the same code by Expert Advisor?

Files:
rsi.jpg  46 kb
 

Access to the windows api functions

Hi,

how can I include the "windows.h" lib in my ea?

 

...

You can not. It (the windows.h file) is incompatible with mql

Use WinUser32 or make your own prototypes in an include file for the api parts you wish to call from mql (you can see from WinUser32.mqh how to do that)

sunshineh:
Hi, how can I include the "windows.h" lib in my ea?
 

Want to learn EA Programming

I have been trying to learn how to read and write EA code. I bought a book called expert advisor programming but I find it takes me an hour to get through three pages. I have no background in programming and I'm more of a visual learner. Can someone give me some tips or suggestions maybe explain how they learned to code. Thanks

 
Doodman:
I have been trying to learn how to read and write EA code. I bought a book called expert advisor programming but I find it takes me an hour to get through three pages. I have no background in programming and I'm more of a visual learner. Can someone give me some tips or suggestions maybe explain how they learned to code. Thanks

Hi Doodman,

You sound like me awhile back, even had to ask my wife how to get on the internet What i would do is get an Ea i would be interested in and do little mods then run it on the back tester,that is if i ever got it to compile. But as often as i could would study the code do little minor changes and go from there. There are a lot of different learning sections in the forum that you can utilize, but what helped me the most was trial and error a lot of it and finally realizing the learning never ends, at least for me.

 

What is the explanation mark used in code

A basic question no doubt for some, but can someone explain what the significance of the explanation mark is in this code

if(variable_2 == 1 && variable_2L != 1) return(true);

now I understand this below so what is the difference please?

if(variable_2 == 1 && variable_2L == 1) return(true);

I've also found it in code such as this if(!UseFilter) return(true);

Weird when in fact the variable name is simply UseFilter

So can someone give me that Einstein moment please?

 

...

It is a "not"

So, translated to simple language, your first example would be : if variable_2 is equal to 1 and variable_2L is not equal to 1 return true, in in !UseFilter it would mean : if not UseFilter, and so on ...

shawndowney:
A basic question no doubt for some, but can someone explain what the significance of the explanation mark is in this code

if(variable_2 == 1 && variable_2L != 1) return(true);

now I understand this below so what is the difference please?

if(variable_2 == 1 && variable_2L == 1) return(true);

I've also found it in code such as this if(!UseFilter) return(true);

Weird when in fact the variable name is simply UseFilter

So can someone give me that Einstein moment please?
 

Wow what staggering implications this little exclamation mark has - thanks!

 
mladen:
It is a "not" So, translated to simple language, your first example would be : if variable_2 is equal to 1 and variable_2L is not equal to 1 return true, in in !UseFilter it would mean : if not UseFilter, and so on ...

So just working through my code if I have the following scenario...

extern bool UseFilter = false;

bool direction(int mode)

{

if(!UseFilter) return(true);

In the above example the explanantion mark would create a complete reversal of the outcome for the value of 'direction'. With it the value is true, wihtout it the value is as per extern bool being false!

 

Need help with Magic Number

Hello everyone.. I have a problem with this ea, it infact opens tons of positions. But the funniest thing is that if i put in the ordersend magicnumber to 0 the problem is gone, but i still need the magicnumber...

#property copyright "Skox"

#property link ""

#property indicator_separate_window

double Punto;

extern bool DisplayInfo=true;

extern string S1 ="Lotti da aprire, minimo 0.02 ";

extern double BE=15;

extern double Lotti=0.2;

extern string S2 ="Numero posizioni massime. Minimo 2";

extern int NPosMax=2;

static double trl;

extern double StopLoss = 15;

extern double TrailingStop = 7;

static int ticketTL, ticketTL2;

string Cross;

double buffer;

int TF,i;

string cmt;

int New_Bar;

bool NoTradeFlag;

datetime Time_0;

extern int MagicNumber=987569;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

Time_0=Time[0];

// Compatibility wiht 3,4,5 digits prices

if(Digits>=4) Punto = 0.0001;

else Punto = 0.01;

//----Gestione TimeFrame, =1 fino a 4h, =2 oltre

if (Period()<=240) TF=1;

else TF=2;

Cross=Symbol();

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//---Stampa informazioni su CHART

void PrintInfo;

{

bool setupBARb, setupBARs;

string cmt="";

cmt = "========================";

cmt = cmt + "\nAccount Name: [ " + AccountName() + " ]";

cmt = cmt + "\nAccount Leverage: [ " + DoubleToStr( AccountLeverage(), 0 ) + " ]";

cmt = cmt + "\nMin Lot: [ " + DoubleToStr( MarketInfo(Symbol(),MODE_MINLOT), 3 ) + " ]";

cmt = cmt + "\nLot Step: [ " + DoubleToStr( MarketInfo(Symbol(),MODE_LOTSTEP), 3 ) + " ]";

cmt = cmt + "\nCurrent Profit: [ " + DoubleToStr(AccountEquity()-AccountBalance(),2) + " ]";

cmt = cmt + "\nAccount Balance: [ " + DoubleToStr(AccountBalance(),2) + " ]";

cmt = cmt + "\nAccount Equity: [ " + DoubleToStr(AccountEquity(),2) + " ]";

cmt = cmt + "\n========================";

Comment(cmt);

}

//---Indicatori

double Atr=iATR(NULL,0,20,0);

double fastMA=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);

double slowMA=iMA(NULL,0,40,0,MODE_SMA,PRICE_CLOSE,0);

double Adx=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);

//---Funzione Buffer

if (TF==1) buffer=2*Punto;

else buffer=0.001*Close[0];

//--- verifica la presenza di ordini

int ordtot=OrdersTotal()-1;

for(i=ordtot;i>=0;i--){

OrderSelect(i,SELECT_BY_POS);

//--- c'è un ordine di questo EA

// if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Cross) Gestisci();

}

//--- Se setup bar esiste setupBAR=true, altrimenti false

// setupbar per long

if (Low[1]>fastMA && Low[1]>slowMA && Close[1]>Open[1] && (Close[1]-Low[1])>(High[1]-Low[1])*0.7 && High[1]<High && (Close[1]-Open[1])25)

{setupBARb = true;}

else{setupBARb = false;}

// setupbar per short

if (Low[1]<fastMA && Low[1]<slowMA && Close[1]<Open[1] && (Close[1]-Low[1])Low && (Open[1]-Close[1])25)

{setupBARs = true;}

else setupBARs = false;

//---Funzione prezzo

double prezzo;

if(OrderType()==OP_BUY) prezzo=Ask;

if(OrderType()==OP_SELL) prezzo=Bid;

//--- Controllo numero ordini

double Spread=Ask-Bid;

int ticket;

//---Gestione TrailingStop. La variabile TrailingStop è external. Trailing è il nuovo livello di stoploss per ordini con trailingstop

double TSTP=TrailingStop*Punto;

// double Trailing;

//---Gestione ordini e modifica di trailingstop e targetprofit

int OrdTot=OrdersTotal();

if (setupBARb==true && !New_Bar() && OrdTot<NPosMax)

{

OrderSend(Symbol(),OP_BUY,Lotti/2,prezzo,3,Low[1]-buffer-Spread,prezzo+Atr,"Momentum Method",0,0);

OrderSend(Symbol(),OP_BUY,Lotti/2,prezzo,3,Low[1]-buffer-Spread,0,"Momentum Method",MagicNumber,0);

}

if (setupBARs==true && !New_Bar() && OrdTot<NPosMax)

{

OrderSend(Symbol(),OP_SELL,Lotti/2,prezzo,3,High[1]+buffer+Spread,prezzo-Atr,"Momentum Method",0,0,Green);

OrderSend(Symbol(),OP_SELL,Lotti/2,prezzo,3,High[1]+buffer+Spread,0,"Momentum Method",MagicNumber,0);

}

}//chiusura start

//*** gestisce gli ordini aperti

int Gestisci(){

static double trl;

double przopen= OrderOpenPrice();

double sl= OrderStopLoss();

double tp= OrderTakeProfit();

double lotti= OrderLots();

datetime open=OrderOpenTime();

int periodo= Period();

int shift=iBarShift(NULL,0,OrderOpenTime());

// ordine BUY

if(OrderType()==OP_BUY){

double prezzo=Bid;

// dopo 5 candele chiude l'ordine

if(Time[0]>open+(periodo*60)*4){

if(!OrderClose(OrderTicket(),lotti,prezzo,3,Yellow))

Print("Errore chiusura totale OP_BUY # "+GetLastError());

return(0);

}

// porta l'ordine in breakeven

if(sl<przopen){

if((Bid-przopen)>=BE)

if(!OrderModify(OrderTicket(),przopen,przopen,tp+BE,0))

Print("Errore modifica Breakeven BUY # "+GetLastError());

return(0);

}

// chiude la prima parte dell'ordine

if(lotti==Lotti){

if((tp-BE)<=Bid){

if(!OrderClose(OrderTicket(),lotti/2,prezzo,3,Yellow))

Print("Errore 1^ chiusura OP_BUY # "+GetLastError());

else trl=Bid-TrailingStop;

}

return(0);

}

// gestisce il trailing

if(Bid<=trl){

if(!OrderClose(OrderTicket(),lotti,prezzo,3,Yellow))

Print("Errore 2^ chiusura OP_BUY # "+GetLastError());

}else

if((Bid-TrailingStop)>trl) trl=Bid-TrailingStop;

return(0);

}

// ordine SELL

if(OrderType()==OP_SELL){

prezzo=Ask;

// dopo 5 candele chiude l'ordine

if(Time[0]>open+(periodo*60)*4){

if(!OrderClose(OrderTicket(),lotti,prezzo,3,Yellow))

Print("Errore chiusura totale OP_BUY # "+GetLastError());

return(0);

}

// porta l'ordine in breakeven

if(sl<przopen){

if((przopen-Ask)>=BE)

if(!OrderModify(OrderTicket(),przopen,przopen,tp-BE,0))

Print("Errore modifica Breakeven BUY # "+GetLastError());

return(0);

}

// chiude la prima parte dell'ordine

if(lotti==Lotti){

if((tp-BE)>=Ask){

if(!OrderClose(OrderTicket(),lotti/2,prezzo,3,Yellow))

Print("Errore 1^ chiusura OP_BUY # "+GetLastError());

else trl=Ask-TrailingStop;

}

return(0);

}

// gestisce il trailing

if(Ask>=trl){

if(!OrderClose(OrderTicket(),lotti,prezzo,3,Yellow))

Print("Errore 2^ chiusura OP_BUY # "+GetLastError());

}else

if((Ask-TrailingStop)>trl) trl=Ask-TrailingStop;

return(0);

}

}

//---Controlla se è nata una nuova candela

bool New_Bar(){

if (Time_0 != Time[0])

{

New_Bar = 1;

NoTradeFlag=false;

Time_0 = Time[0];

}

}

Reason: