MQL4 Learning - page 84

 

Problem with EA coding

Hello!

I`m newbie on forex market. I`m trying to build my own EA, and I succeed but i have few question about some features...

I trade on 5 different currency pairs: EURUSD, AUDUSD, USDCAD, USDJPY and NZDUSD...on H1 timeframe.

I want to implement some new features in EA, for example when EA open long trade on one currency pair and that trade goes to -50pips that my EA open another long trade on the same pair.

And, if possible to code that they both close when I have cumulative profit...maybe first one is -20 pips, but the second is +30 and to close immediatelly both (so I have +10 pips profit).

Could someone help me code it?

 

need help

hello

please someone can explain me why this code dosen't work correctly

instead drawing 8 MA on chart it draws only first two i.e 5 and 8 periods moving averages

#property indicator_chart_window // Indicator is drawn in the main window

#property indicator_buffers 8 // Number of buffers

#property indicator_color1 Lime // Color of the 1st line

#property indicator_color2 Blue // Color of the 2nd line

#property indicator_color3 Red

#property indicator_color4 SeaGreen

#property indicator_color5 MediumVioletRed

#property indicator_color6 DeepSkyBlue

#property indicator_color7 OrangeRed

#property indicator_color8 Orange

extern int Aver5=5; // number of bars for calculation

extern int Aver8=8; // number of bars for calculation

extern int Aver21=21; // number of bars for calculation

extern int Aver55=55; // number of bars for calculation

extern int Aver89=89; // number of bars for calculation

extern int Aver144=144; // number of bars for calculation

extern int Aver233=233; // number of bars for calculation

extern int Aver377=377; // number of bars for calculation

double Buf_5[],Buf_8[],

Buf_21[],Buf_55[],

Buf_89[],Buf_144[],

Buf_233[],Buf_377[]; // Declaring indicator arrays

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

int init() // Special function init()

{

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

SetIndexBuffer(0,Buf_5); // Assigning an array to a buffer

SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(0,0);

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

SetIndexBuffer(1,Buf_8); // Assigning an array to a buffer

SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(1,0);

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

SetIndexBuffer(2,Buf_21); // Assigning an array to a buffer

SetIndexStyle (2,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(2,0);

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

SetIndexBuffer(3,Buf_55); // Assigning an array to a buffer

SetIndexStyle (3,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(3,0);

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

SetIndexBuffer(4,Buf_89); // Assigning an array to a buffer

SetIndexStyle (4,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(4,0);

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

SetIndexBuffer(5,Buf_144); // Assigning an array to a buffer

SetIndexStyle (5,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(5,0);

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

SetIndexBuffer(6,Buf_233); // Assigning an array to a buffer

SetIndexStyle (6,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(6,0);

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

SetIndexBuffer(7,Buf_377); // Assigning an array to a buffer

SetIndexStyle (7,DRAW_LINE,STYLE_SOLID,1);// Line style

SetIndexShift(7,0);

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

return;

}

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

int start()

{

ma(Aver5,Buf_5);

ma(Aver8,Buf_8);

ma(Aver21,Buf_21);

ma(Aver55,Buf_55);

ma(Aver89,Buf_89);

ma(Aver144,Buf_144);

ma(Aver233,Buf_233);

ma(Aver377,Buf_377);

return;

}

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

void ma(int& average,double& buffer[])

{

int n,i;

int Counted_bars;

double Sum;

Counted_bars=IndicatorCounted(); // Number of counted bars

i=Bars-Counted_bars-1; // Index of the first uncounted

while(i>=0) // Loop for uncounted bars

{

Sum=0; // Nulling at loop beginning

for(n=i;n<=i+average-1;n++) // Loop of summing values

{

Sum=Sum + Close[n]; // Accumulating maximal values sum

}

buffer=Sum/average; // Value of buffer on i bar

i--; // Calculating index of the next bar

}

}

thanks

 

How to bypass/trick MODE_STOPLEVEL

Hello, I'm using a MoveStopOnce with SL slide and Trailing Stop technique and was wondering why it wasn't triggering until way past my set BE level.

I was using settings of for example when price gets to +21, move SL to BE+19 then Trail every 3 pips after that. The issue was that BEslide didn't trigger first SL move until price got to +28 pips then I realized my broker for USH/CHF has a MODE_STOPLEVEL of 8 so hence the reason why BE wasn't getting triggered at my set amount of +21 pips, it wasn't allowing the SL being moved so close to price.

Is there anyway around that? or is it just that the Broker will not allow SL within MODE_STOPLEVEL of price no matter what Technique I use?

I guess I could somehow monitor the SL moves internally within the EA and not physically move the SL then when price drops to where it would have hit SL if BE and Trail was actually working then close the trade at that point ??

I'm just not that good of coder

Any help would be greatly appreciated.

Thank you

 

Global crosshairs

Hi all,

is there a way to create global crosshairs in mt4? have several different tf up and would like to scroll through one tf while seeing position on others. have looked on net..found .mq4 from forex fac but could not get to work.

thx

 

How do I compare time?

Hello,

How can I compare the time in MQ4?

If for instance I wanted my EA to trade at either 7am, 1pm or 4pm only, how would I do this?

Any help would be appreciated.

Nick

 
if(TimeHour(TimeCurrent()) == 7 ||

TimeHour(TimeCurrent()) == 13 ||

TimeHour(TimeCurrent()) == 16)

{....your code here...}

TimeCurrent() return server time

TimeLocal() return local time (your PC time)

 

perfect,

Many thanks,

Nick

 

Limiting Open Orders for each instance of EA

I have coded an EA but I want to limit the amount of open orders it has in each instance it is running, for example, if the EA is running on the USDJPY and EURUSD, how can I keep it to having a maximum of 3 orders open for each pair. I tried using orderstotal() but that gives me all the open orders for the account, and I want to be able to trade manually around the EAs. Anyone have some pseudo-code or ideas about how to implement this?

-Merc

 

Use the following Subroutine. Of course you can add buy limit and sell limit to it.

int CountTrades(int Magic)

{

int count=0;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic)

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

if(OrderType()==OP_BUY || OrderType()==OP_SELL ||

OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)

count++;

}//for

return(count);

}[/CODE]

Now you can call it in your main routine something as

[CODE]

NumberOfActiveTrades = CountTrades(Magic);

Hope this helps.

 

Thank you Maji, that was the code I needed and it worked, just couldn't wrap my head around the algo. I appreciate your help..

-Merc

Reason: