MQL4 Learning - page 65

 

Cant figure out this error

Below is an indicator that I am trying to write, I went through the tutorial on the MQL4 site, and I get an error when I compile the file. It says it expects a "0" variable...where I declare double F_MA[], well there is a 0 there...

what I want to do or am trying to do (besides learning to write in mql4) is draw an arrow up when either of the fast ma or slow ma turns up and the other is already up (or down for a short)...of course I also would like to dray the moving averages, but haven't gotten that far yet...here I thought this would be simple, I just want to input different MA's and see the results on the chart is all...lol and here I have been racking my brain for 2 days, time to take a short break and I might think of something...

Thank you for any ideas or directions!

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 Blue

#property indicator_color2 Red

//---- input parameters

extern int FAST_MA; //fast MA

extern int SLOW_MA; //slow MA

extern int barsToProcess=1000;

//---- buffers

double ExtMapBuffer1[];

double ExtMapBuffer2[];

double ExtMapBuffer3[];

double ExtMapBuffer4[];

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,ExtMapBuffer1);

SetIndexStyle(1,DRAW_LINE);

SetIndexBuffer(1,ExtMapBuffer2);

SetIndexStyle(2,DRAW_ARROW);

SetIndexArrow(2,236);

SetIndexBuffer(2,ExtMapBuffer3);

SetIndexEmptyValue(2,0.0);

SetIndexStyle(3,DRAW_ARROW);

SetIndexArrow(3,238);

SetIndexBuffer(3,ExtMapBuffer4);

SetIndexEmptyValue(3,0.0);

//----

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

//----

{

int counted_bars=IndicatorCounted(),

limit;

if(counted_bars>0)

counted_bars--;

limit=Bars-counted_bars;

if(limit>barsToProcess)

limit=barsToProcess;

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

{

double F_MA[] = iMA(NULL,0,("+FAST_MA+"),0,0,PRICE_CLOSE,i);

double S_MA[] = iMA(NULL,0,("+SLOW_MA+"),0,0,PRICE_CLOSE,i);

if ( (F_MA[1] < F_MA[0]) && (S_MA[1] <= S_MA[0]) || (F_MA[1] <= F_MA[0]) && (S_MA[1] < S_MA[0]) )

ExtMapBuffer3=High+5*Point;

else ExtMapBuffer3=0.0;

if ( (F_MA[1] > F_MA[0]) && (S_MA[1] >= S_MA[0]) || (F_MA[1] >= F_MA[0]) && (S_MA[1] > S_MA[0]) )

ExtMapBuffer4=High+5*Point;

else ExtMapBuffer4=0.0;

}

return(0);

}

//----

return(0);

}

 

Optimising Risk percentage

Right I'm just starting to code money management for my EA and I've come across the standard LotsOptimized() function that chooses the appropriate lot size for a certain maximum risk percentage. Now thats all well and good and it makes sure the lot size is correct and maximises profit quite nicely. However I wanted to vary my risk percentage based on my short term win percentage (last 20 trades say). Now I've come up with this formula that plots a nice curve increasing as my short-term win ratio increases.

Maximum Risk %= (2/300)*(1/(1-STWP))

Where STWP is the short term win ratio. I'm sure you're all thinking what happens in the rare occassion that you get 20 straight successive trades (STWP=1 division by 0)? Well I modified it so that if STWP>0.85 then MaxRisk%=0.05 otherwise it risks over 13% of your equity. Anyone use this or similar approaches to increase your risk when on a winning streak? Here's my code so far, any thoughts? It should hopefully have a built in decrease factor thou it would be easy to say half MaxRisk after your first loss.
double KellyLot()

{

double lot=Lots;

double orders=HistoryTotal();

double STW=1;

double STWP=0;

if(orders>20)

{

int i,j;

for(i=orders;i>=(orders-20);i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

if(OrderProfit()>0) STW=STW+1;

}

STWP=STW/20;

if(STWP==1) {STWP=0.995;} //Prevents division by 0

double KRC=(2/300)*(1/(1-STWP));

if(KRC>=0.05) MaximumRisk=0.05;

if(KRC<0) MaximumRisk=MaximumRisk;

else MaximumRisk=KRC;

}

return(MaximumRisk);

}

double LotsOptimized()

{

double lot=Lots;

int orders=HistoryTotal(); // history orders total

int losses=0; // number of losses orders without a break

//---- select lot size

lot=NormalizeDouble(AccountFreeMargin()*KellyLot()/1000.0,1);

//---- calcuulate number of losses orders without a break

if(DecreaseFactor>0)

{

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

{

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }

if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;

//----

if(OrderProfit()>0) break;

if(OrderProfit()<0) losses++;

}

if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

}

if(lot<0.1) lot=0.1;

return(lot);

}

 
Files:
untitled.jpg  45 kb
 
 

Help please with programmation of Trading System

Hi everyone!!!

My name's Luca, i am new of this forum .

I have a big problem...i am not able to write my trading system in metatrader because i don't know how to express mathematically the concept of divergence and the concept of maximum and minimum.

Can you help me, please? If someone wants to contact me, it is possibile to write me

 

Script MAE and MFE

Hello

I download script MAE-MFE. I compile and fail-error.

link A Script to Calculate MAE and MFE - A Script to Calculate MAE and MFE - MQL4 Code Base

Do you tell me what's wrong?

thank you very much

Files:
 

Example for coding opening and closing orders..

Not Just opening and closing but Verifying that the order has been opened or closed.

I open 2 orders at a time..one with TP and one without.

I need to make sure 1 or both get closed before opening opposite 2 orders.

I've been having random problems with 2 orders NOT getting opened or 2 of the SAME orders getting opened(both with TP).

I do use a while loop to close the orders but don't have any verification for the OPEN orders commands.

I look at the code i have and i think i'm making this Far more difficult than it needs to be. Experienced programmers will know the shortcuts.

Summary- need to open 2 separate orders and verify each one individually AND

Close ALL orders and verify all have been closed. (I'm guessing the CLOSEALL command or something)

THANKS ALOT ALL!

if you happen to know how to ADD Trailing STOP in the code for opening orders..I'd REALLY appreciate that too.

THANKS again

 

Al fin!!Encontre un sitio para aprender de MQL4. Thanks.

rbowles:
I have been working on an EA the last few days and with the help of this forum I have made some progress. I want to know how I would print an arrow or sometime of graphic that indicates where It is getting in a trade and where it is getting out. I no how to do it for an indicator but not for an EA.

Thanks in advance

Randy

Gracias

Gracias por todo, al fin!!!!!!!!!!!!!!

 

Prevent big loses

I am new to programming with MQL4 and so far I have done some testing.

In some tests I have made note that it is relatively simple to make an EA to get some regular income, the problem is that suddenly it have a big lose that cancels my earnings.

What techniques that exist to prevent large loses?