Come codificare? - pagina 325

 

Codice

cja:
Ciao mladen,

Hai tempo per dare un'occhiata a questo?

Cordiali saluti CJA

Ciao mladen,

Solo una nota veloce per farti sapere che ho ottenuto la soluzione alla mia richiesta di codice trailing così puoi togliere questo dalla tua lista di cose da fare.

Saluti CJA

 

Sono contento che tu abbia risolto il problema

Buon proseguimento della codifica e del trading

cja:
Ciao mladen,

Solo una nota veloce per farti sapere che ho ottenuto la soluzione alla mia richiesta di codice trailing così puoi togliere questo dalla tua lista di cose da fare.

Saluti CJA
 

Salve,

ho bisogno di controllare se l'ultimo ordine chiuso del mio EA era in profitto o in perdita.

Ho pensato di controllare gli ultimi due ordini chiusi in profitto dell'EA e sommarli.

Il problema è che ho diversi EA nello stesso conto, quindi è necessario controllare l'ordine History con MagicNumber specifico. Ho provato in diverse modalità ma senza successo.

Potete aiutarmi?

Grazie, grazie.

 

dasio

Questa funzione restituisce il profitto dell'ultimo ordine chiuso (come puoi vedere, puoi anche specificare il numero magico come parametro, quindi risolve il tuo problema con istanze multiple):

double lastOrderProfit(int magicNumber=0)

{

datetime lastTime = 0;

double lastProfit = 0;

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastProfit = OrderProfit()+OrderSwap()+OrderCommission();

}

return(lastProfit);

}

Devi solo controllare se il ritorno della funzione è positivo o negativo

dasio:
Ciao,

Ho bisogno di controllare se l'ultimo ordine chiuso del mio ea era in profitto o in perdita.

Ho pensato di controllare gli ultimi due ordini chiusi in profitto dell'EA e sommarli.

Il problema è che ho diversi EA nello stesso conto quindi è necessario controllare l'ordine History con un MagicNumber specifico. Ho provato in diverse modalità ma senza successo.

Potete aiutarmi?

Grazie.
 
mladen:
dasio

Questa funzione restituisce il profitto dell'ultimo ordine chiuso (come puoi vedere, puoi anche specificare il numero magico come parametro, quindi risolve il tuo problema con istanze multiple):

double lastOrderProfit(int magicNumber=0)

{

datetime lastTime = 0;

double lastProfit = 0;

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastProfit = OrderProfit()+OrderSwap()+OrderCommission();

}

return(lastProfit);

}
Devi solo controllare se il ritorno della funzione è positivo o negativo

Quindi devo solo richiamare la funzione con il mio magicnumber e mi restituisce il profitto dell'ultimo ordine chiuso del mio ea, giusto?

 

Sì, è giusto

dasio:
Quindi devo solo richiamare la funzione con il mio magicnumber e mi dà come risultato il profitto dell'ultimo ordine chiuso del mio ea, giusto?
 
mladen:
Sì, è giusto

Grazie.

Ma se avvio l'ea e non ci sono ordini nella storia, come posso richiamare la funzione solo se il mio ea chiude una posizione?

 

Allora usa prima questa funzione:

int closedSoFarForMagic(int magicNumber=0)

{

int closedSoFar=0;

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

closedSoFar++;

}

return(closedSoFar);

}

Se closedSoFarForMagic() > 0 allora sai che ci sono già ordini chiusi per un determinato numero magico nella storia e quindi puoi controllare il profitto dell'ultimo ordine

dasio:
Grazie, ma se avvio l'ea e non ci sono ordini nella storia, come posso richiamare la funzione solo se il mio ea chiude una posizione?
 

vma croce fantail

Ciao, sto cercando di fare un indicatore che mostra le frecce quando due vma si incrociano. Sono un principiante di mql e il mio indicatore ovviamente non funziona, sono bloccato. Qualcuno può aiutarmi?

L'indicatore originale è MA - fantail vma 3

#property copyright "Copyright © 2007, Forex-TSD.com "

#property link "https://www.forex-tsd.com/"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Black

#property indicator_width1 2

//#property indicator_color2 Red

//#property indicator_width2 2

//---- input parameters

//For both user settings, 2 is fast, 8 is slow.Weight=2.3 gives late entry.

extern int ADX_Length=2;

extern double Weighting=2.0;

extern int MA_Length=1;//This must be =1 so that the VMA base line does not get averaged.

extern int MA_Mode=1;

//---- buffers

double MA[];

double VMA[];

double VarMA[];

double ADX[];

double ADXR[];

double sPDI[];

double sMDI[];

double STR[];

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

IndicatorBuffers(8);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,MA);

SetIndexBuffer(1,VMA);

// SetIndexStyle(2,DRAW_LINE);

SetIndexBuffer(2,VarMA);

SetIndexBuffer(3,ADX);

SetIndexBuffer(4,ADXR);

SetIndexBuffer(5,sPDI);

SetIndexBuffer(6,sMDI);

SetIndexBuffer(7,STR);

//---- name for DataWindow and indicator subwindow label

string short_name="MA - Fantail vma 3";

IndicatorShortName(short_name);

SetIndexLabel(0,"MA - Fantail vma 3");

//----

SetIndexDrawBegin(0,2*MA_Length+40);

// SetIndexDrawBegin(1,2*MA_Length+40);//Used for displaying internal signals.

//---- Safety limits for user settings

if (ADX_Length<2)ADX_Length=2;

if (ADX_Length>8)ADX_Length=8;

if (Weighting<1)Weighting=1;

if (Weighting>8)Weighting=8;

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int limit, i, j, counted_bars=IndicatorCounted();

//----

if ( counted_bars < 0 ) return(-1);

if ( counted_bars ==0 ) limit=Bars-1;

if ( counted_bars < 1 )

for(i=1;i<2*(MA_Length+ADX_Length+10);i++)

{

VMA=Close;

VarMA=Close;

MA=Close;

STR = High-Low;

sPDI = 0;

sMDI = 0;

ADX=0;

ADXR=0;

}

if(counted_bars>0) limit=Bars-counted_bars;

limit--;

for(i=limit; i>=0; i--)

{

double Hi = High;

double Hi1 = High;

double Lo = Low;

double Lo1 = Low;

double Close1= Close;

double Bulls = 0.5*(MathAbs(Hi-Hi1)+(Hi-Hi1));

double Bears = 0.5*(MathAbs(Lo1-Lo)+(Lo1-Lo));

if (Bulls > Bears) Bears = 0;

else

if (Bulls < Bears) Bulls = 0;

else

if (Bulls == Bears) {Bulls = 0;Bears = 0;}

sPDI = (Weighting*sPDI + Bulls)/(Weighting+1);//ma weighting

sMDI = (Weighting*sMDI + Bears)/(Weighting+1);//ma weighting

double TR = MathMax(Hi-Lo,Hi-Close1);

STR = (Weighting*STR + TR)/(Weighting+1);//ma weighting

if(STR>0 )

{

double PDI = sPDI/STR;

double MDI = sMDI/STR;

}

if((PDI + MDI) > 0)

double DX = MathAbs(PDI - MDI)/(PDI + MDI);

else DX = 0;

ADX = (Weighting*ADX + DX)/(Weighting+1);//ma weighting

double vADX = ADX;

double ADXmin = 1000000;

for (j=0; j<=ADX_Length-1;j++) ADXmin = MathMin(ADXmin,ADX);

double ADXmax = -1;

for (j=0; j<=ADX_Length-1;j++) ADXmax = MathMax(ADXmax,ADX);

double Diff = ADXmax - ADXmin;

if(Diff > 0) double Const = (vADX- ADXmin)/Diff; else Const = 0;

VarMA=((2-Const)*VarMA+Const*Close)/2;//Same equation but one less array, mod 10 Sept 2007.

}

for(j=limit; j>=0; j--) MA[j] = iMAOnArray(VarMA,0,MA_Length,0,MA_Mode,j);

//----

return(0);

}

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

[/PHP]

and this is mine

[PHP]

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

//| VMA cross arrows.mq4 |

//| thefxpros@katamail.com |

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

#property copyright "thefxpros"

#property link "thefxpros@katamail.com"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 C'255,198,198'

#property indicator_color2 C'198,255,198'

#property indicator_color3 Crimson

#property indicator_color4 DarkGreen

#property indicator_width1 3

#property indicator_width2 3

#property indicator_width3 2

#property indicator_width4 2

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

extern int VMA1_ADXLenght = 2;

extern double VMA1_Weighting = 2.0;

extern int VMA1_Length = 1;

extern int VMA1_Mode = 1;

extern int VMA2_ADXLenght = 2;

extern double VMA2_Weighting = 3.0;

extern int VMA2_Length = 3;

extern int VMA2_Mode = 2;

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

double buffer1[];

double buffer2[];

double buffer3[];

double buffer4[];

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

int init()

{

SetIndexStyle (0,DRAW_HISTOGRAM);

SetIndexStyle (1,DRAW_HISTOGRAM);

SetIndexBuffer (0,buffer3);

SetIndexBuffer (1,buffer4);

SetIndexBuffer (2,buffer1);

SetIndexBuffer (3,buffer2);

return(0);

}

int deinit()

{

return(0);

}

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

int start()

{

int counted_bars=IndicatorCounted();

int limit,i;

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

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

for(i=limit; i>=0; i--)

{

buffer1 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA1_Weighting, VMA1_Length, VMA1_Mode, i);

buffer2 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA2_Weighting, VMA2_Length, VMA2_Mode, i);

buffer3 = buffer1;

buffer4 = buffer2;

}

return(0);

}

 

thefxpros,

Controlla prima il nome dell'indicatore:

"MA - fatailvma 3"

Dovrebbe essere "fantail" lì

Inoltre, nella chiamata all'indicatore iCustom() ti manca il numero del buffer. Sto solo ipotizzando che questo è ciò che stavi cercando di ottenere

buffer1 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA1_Weighting, VMA1_Length, VMA1_Mode,0, i);

buffer2 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA2_Weighting, VMA2_Length, VMA2_Mode,1, i);[/PHP]

thefxpros:
Hi, i'm trying to make an indicator that shows arrows when two vma crosses. I'm a mql beginner and my indicator obviously does not work, I'm stuck. Somebody can help me?

The original indicator is MA - fantail vma 3

#property copyright "Copyright © 2007, Forex-TSD.com "

#property link "https://www.forex-tsd.com/"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Black

#property indicator_width1 2

//#property indicator_color2 Red

//#property indicator_width2 2

//---- input parameters

//For both user settings, 2 is fast, 8 is slow.Weight=2.3 gives late entry.

extern int ADX_Length=2;

extern double Weighting=2.0;

extern int MA_Length=1;//This must be =1 so that the VMA base line does not get averaged.

extern int MA_Mode=1;

//---- buffers

double MA[];

double VMA[];

double VarMA[];

double ADX[];

double ADXR[];

double sPDI[];

double sMDI[];

double STR[];

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

IndicatorBuffers(8);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,MA);

SetIndexBuffer(1,VMA);

// SetIndexStyle(2,DRAW_LINE);

SetIndexBuffer(2,VarMA);

SetIndexBuffer(3,ADX);

SetIndexBuffer(4,ADXR);

SetIndexBuffer(5,sPDI);

SetIndexBuffer(6,sMDI);

SetIndexBuffer(7,STR);

//---- name for DataWindow and indicator subwindow label

string short_name="MA - Fantail vma 3";

IndicatorShortName(short_name);

SetIndexLabel(0,"MA - Fantail vma 3");

//----

SetIndexDrawBegin(0,2*MA_Length+40);

// SetIndexDrawBegin(1,2*MA_Length+40);//Used for displaying internal signals.

//---- Safety limits for user settings

if (ADX_Length<2)ADX_Length=2;

if (ADX_Length>8)ADX_Length=8;

if (Weighting<1)Weighting=1;

if (Weighting>8)Weighting=8;

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int limit, i, j, counted_bars=IndicatorCounted();

//----

if ( counted_bars < 0 ) return(-1);

if ( counted_bars ==0 ) limit=Bars-1;

if ( counted_bars < 1 )

for(i=1;i<2*(MA_Length+ADX_Length+10);i++)

{

VMA=Close;

VarMA=Close;

MA=Close;

STR = High-Low;

sPDI = 0;

sMDI = 0;

ADX=0;

ADXR=0;

}

if(counted_bars>0) limit=Bars-counted_bars;

limit--;

for(i=limit; i>=0; i--)

{

double Hi = High;

double Hi1 = High;

double Lo = Low;

double Lo1 = Low;

double Close1= Close;

double Bulls = 0.5*(MathAbs(Hi-Hi1)+(Hi-Hi1));

double Bears = 0.5*(MathAbs(Lo1-Lo)+(Lo1-Lo));

if (Bulls > Bears) Bears = 0;

else

if (Bulls < Bears) Bulls = 0;

else

if (Bulls == Bears) {Bulls = 0;Bears = 0;}

sPDI = (Weighting*sPDI + Bulls)/(Weighting+1);//ma weighting

sMDI = (Weighting*sMDI + Bears)/(Weighting+1);//ma weighting

double TR = MathMax(Hi-Lo,Hi-Close1);

STR = (Weighting*STR + TR)/(Weighting+1);//ma weighting

if(STR>0 )

{

double PDI = sPDI/STR;

double MDI = sMDI/STR;

}

if((PDI + MDI) > 0)

double DX = MathAbs(PDI - MDI)/(PDI + MDI);

else DX = 0;

ADX = (Weighting*ADX + DX)/(Weighting+1);//ma weighting

double vADX = ADX;

double ADXmin = 1000000;

for (j=0; j<=ADX_Length-1;j++) ADXmin = MathMin(ADXmin,ADX);

double ADXmax = -1;

for (j=0; j<=ADX_Length-1;j++) ADXmax = MathMax(ADXmax,ADX);

double Diff = ADXmax - ADXmin;

if(Diff > 0) double Const = (vADX- ADXmin)/Diff; else Const = 0;

VarMA=((2-Const)*VarMA+Const*Close)/2;//Same equation but one less array, mod 10 Sept 2007.

}

for(j=limit; j>=0; j--) MA[j] = iMAOnArray(VarMA,0,MA_Length,0,MA_Mode,j);

//----

return(0);

}

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

and this is mine

[PHP]

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

//| VMA cross arrows.mq4 |

//| thefxpros@katamail.com |

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

#property copyright "thefxpros"

#property link "thefxpros@katamail.com"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 C'255,198,198'

#property indicator_color2 C'198,255,198'

#property indicator_color3 Crimson

#property indicator_color4 DarkGreen

#property indicator_width1 3

#property indicator_width2 3

#property indicator_width3 2

#property indicator_width4 2

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

extern int VMA1_ADXLenght = 2;

extern double VMA1_Weighting = 2.0;

extern int VMA1_Length = 1;

extern int VMA1_Mode = 1;

extern int VMA2_ADXLenght = 2;

extern double VMA2_Weighting = 3.0;

extern int VMA2_Length = 3;

extern int VMA2_Mode = 2;

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

double buffer1[];

double buffer2[];

double buffer3[];

double buffer4[];

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

int init()

{

SetIndexStyle (0,DRAW_HISTOGRAM);

SetIndexStyle (1,DRAW_HISTOGRAM);

SetIndexBuffer (0,buffer3);

SetIndexBuffer (1,buffer4);

SetIndexBuffer (2,buffer1);

SetIndexBuffer (3,buffer2);

return(0);

}

int deinit()

{

return(0);

}

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

int start()

{

int counted_bars=IndicatorCounted();

int limit,i;

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

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

for(i=limit; i>=0; i--)

{

buffer1 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA1_Weighting, VMA1_Length, VMA1_Mode, i);

buffer2 = iCustom(NULL, 0, "MA - fatail vma 3", VMA1_ADXLenght, VMA2_Weighting, VMA2_Length, VMA2_Mode, i);

buffer3 = buffer1;

buffer4 = buffer2;

}

return(0);

}

Motivazione: