MQL4 Learning - page 69

 

Do a search for money management and martingale then take a look at the code in the EA's you find. That should give you a good start in the right direction.

Lux

 
xdealer:
Hi all,

this is my first post

Is there a way to detect open/close/modify trade events in mt4? There aren't any event callbacks are there?

I want to detect everything about a trade (by EA or manually) ASAP, what is the best way to do it?

I know I can parse the logs, but is there a more efficient way?

Thanks

It seems that I have to cycle though all orders and do the housekeeping, there is not even callbacks.

If this is ture then what is the best way to do to monitor all orders from different chart windows? Can an EA run w/o being attached to a window?

 

changing lot size

I've tried to edit this line:

err=OrderSend(Symbol(),OP_BUYLIMIT,0.1,NormalizeDouble(op,Digits),3,sl,tp,

from 0.1 lots to accountbalance/30000 (i.e. 0.01 for every $300) but nothing seems to work?

 

Code mq4 ?? what is for ATR14 < ATR15

Code mq4 ?? what is for ATR14 < ATR15 ?

1)VARIABLE

what is the variable ?

2) BUY Order

ATR14 < ATR15

3) SELL Order

ATR14 < ATR15

What isd the code mq4 for this ?

 

EA based on custom indicator

Hey guys I am new here. I don't have any programing experience so I would like to ask you for help.

I would like to create and EA based on 1 custom indicator but I simply can't do it. I was trying for over a week now and no luck.

My first problem is putting the indicator code in the EA ( I tried iCustom and some kind of alternative when you copy just a part of the indicator's code in the EA).

And after I somehow managed to input the code in the EA using iCustom function I don't have any idea how to make it to work

The indicator basically shows you when the crossover occurs and sends you alerts and arrows ( blue and red) depending of the buy / sell orders.

This is the indicator code:

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 DodgerBlue

#property indicator_width1 3

#property indicator_color2 Magenta

#property indicator_width2 3

extern string note1 = "First Moving Average";

extern int MA1 = 3;

extern string note2 = "0=sma, 1=ema, 2=smma, 3=lwma";

extern int MA1Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma

extern string note2b = " 0=close, 1=open, 2=high, 3=low,";

extern string note2c = " 4=median(high+low)/2,";

extern string note2d = " 5=typical(high+low+close)/3,";

extern string note2e = " 6=weighted(high+low+close+close)/4";

extern int MA1Price = 1;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4

extern string note3 = "--------------------------------------------";

extern string note4 = "Second Moving Average";

extern int MA2 = 7;

extern string note5 = "0=sma, 1=ema, 2=smma, 3=lwma";

extern int MA2Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma

extern string note5b = " 0=close, 1=open, 2=high, 3=low,";

extern string note5c = " 4=median(high+low)/2,";

extern string note5d = " 5=typical(high+low+close)/3,";

extern string note5e = " 6=weighted(high+low+close+close)/4";

extern int MA2Price = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4

extern string note6 = "--------------------------------------------";

extern string note7 = "Arrow Type";

extern string note8 = "0=Thick, 1=Thin, 2=Hollow, 3=Round";

extern string note9 = "4=Fractal, 5=Diagonal Thin";

extern string note10 = "6=Diagonal Thick, 7=Diagonal Hollow";

extern string note11 = "8=Thumb, 9=Finger";

extern int ArrowType=2;

extern string note12 = "--------------------------------------------";

extern string note13 = "turn on Alert = true; turn off = false";

extern bool AlertOn = true;

extern string note14 = "--------------------------------------------";

extern string note15 = "send Email Alert = true; turn off = false";

extern bool SendAnEmail=false;

double CrossUp[];

double CrossDown[];

string AlertPrefix, MA1short_name, MA2short_name;

string GetTimeFrameStr() {

switch(Period())

{

case 1 : string TimeFrameStr="M1"; break;

case 5 : TimeFrameStr="M5"; break;

case 15 : TimeFrameStr="M15"; break;

case 30 : TimeFrameStr="M30"; break;

case 60 : TimeFrameStr="H1"; break;

case 240 : TimeFrameStr="H4"; break;

case 1440 : TimeFrameStr="D1"; break;

case 10080 : TimeFrameStr="W1"; break;

case 43200 : TimeFrameStr="MN1"; break;

default : TimeFrameStr=Period();

}

return (TimeFrameStr);

}

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

if (ArrowType == 0) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 233);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 234);

}

else if (ArrowType == 1) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 225);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 226);

}

else if (ArrowType == 2) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 241);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 242);

}

else if (ArrowType == 3) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 221);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 222);

}

else if (ArrowType == 4) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 217);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 218);

}

else if (ArrowType == 5) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 228);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 230);

}

else if (ArrowType == 6) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 236);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 238);

}

else if (ArrowType == 7) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 246);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 248);

}

else if (ArrowType == 8) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 67);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 68);

}

else if (ArrowType == 9) {

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0, 71);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1, 72);

}

SetIndexBuffer(0, CrossUp);

SetIndexBuffer(1, CrossDown);

//---- indicator short name

switch(MA1Mode)

{

case 1 : MA1short_name="EMA"; break;

case 2 : MA1short_name="SMMA"; break;

case 3 : MA1short_name="LWMA"; break;

default :

MA1Mode=0;

MA1short_name="SMA";

}

switch(MA2Mode)

{

case 1 : MA2short_name="EMA"; break;

case 2 : MA2short_name="SMMA"; break;

case 3 : MA2short_name="LWMA"; break;

default :

MA2Mode=0;

MA2short_name="SMA";

}

AlertPrefix=Symbol()+" ("+GetTimeFrameStr()+"): ";

//----

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

bool NewBar()

{

static datetime lastbar;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}

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

//| Custom indicator iteration function |

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

int start() {

int limit, i, counter;

double MA1now, MA2now, MA1previous, MA2previous, MA1after, MA2after;

double Range, AvgRange;

int counted_bars=IndicatorCounted();

//---- check for possible errors

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

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

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

counter=i;

Range=0;

AvgRange=0;

for (counter=i ;counter<=i+9;counter++)

{

AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);

}

Range=AvgRange/10;

MA1now = iMA(NULL, 0, MA1, 0, MA1Mode, MA1Price, i);

MA1previous = iMA(NULL, 0, MA1, 0, MA1Mode, MA1Price, i+1);

MA1after = iMA(NULL, 0, MA1, 0, MA1Mode, MA1Price, i-1);

MA2now = iMA(NULL, 0, MA2, 0, MA2Mode, MA2Price, i);

MA2previous = iMA(NULL, 0, MA2, 0, MA2Mode, MA2Price, i+1);

MA2after = iMA(NULL, 0, MA2, 0, MA2Mode, MA2Price, i-1);

if ((MA1now > MA2now) && (MA1previous MA2after)) {

CrossUp = Low - Range*1.5;

if (NewBar())

{

if (AlertOn) {

Alert(AlertPrefix+MA1short_name+" ("+MA1+") "+"crosses UP " + MA2short_name+" ("+MA2+")");

}

if (SendAnEmail) {

SendMail(AlertPrefix,MA1short_name+" ("+MA1+") "+"crosses UP " + MA2short_name+" ("+MA2+")");

}

}

}

else if ((MA1now MA2previous) && (MA1after < MA2after)) {

CrossDown = High + Range*1.5;

if (NewBar())

{

if (AlertOn) {

Alert(AlertPrefix+MA1short_name+" ("+MA1+") "+"crosses DOWN " + MA2short_name+" ("+MA2+")");

}

if (SendAnEmail) {

SendMail(AlertPrefix,MA1short_name+" ("+MA1+") "+"crosses DOWN " + MA2short_name+" ("+MA2+")");

}

}

}

}

return(0);

}

What I would like to achieve is to make an EA that will do this:

Let's say crossover occurs and indicator show blue arrow ( BUY SIGNAL) ,I want EA to buy. They the indicator shows red arrow and I want EA to close the previous order (BUY) and make an immediate SELL order. Kinda like SAR type of thing.

Please help me on this one. I would be very grateful on any advice you can give me. Thanks

 

Lots traded per bar

Hi all,

Can someone give me an advice how to get actual number of lots traded per tick or per bar? I was searching throu MQ4 docs but trade volume has been defined as a tick count. Since one tick seems to be an actual trade, I am curious how I can get number of lots per each trade or total per each bar. MarketInfo does not have anything about traded lots, only Bid/Ask. Looks like MT4 does not provide this information directly. Is there any indirect way?

Thanks

 

...

Deleted by mladen...

 

Can I do Buffer=Close/Close?

Does anyone know how to make Percent decrease or Percent increase indicator?

I did that

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

{

Buffer=Close/Close

}

But That was not working

So I changed above part / to - like that

Buffer=Close-Close

this Indicator was working

And I tried to

Comment(Close[0]/Close[1])

This value was commented correctly.

So I can't understand why Buffer=Close/Close is not work.

Does any one answer my question?

and thanks for reading all.

 

Percent Change

Try this:

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 Green

#property indicator_width1 2

double buff[];

int init()

{

SetIndexBuffer(0,buff); SetIndexStyle(0, DRAW_HISTOGRAM);

return(0);

}

int start()

{

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

{

buff=(1-Close/Close)*100.0;

}

return(0);

}

 

Entry Point.

With my EA that I have. I am trying to program it to take a position at the next time frame. If the indicators are true and it sends it to take a position then I want it to wait till the next frame. I.E with a 15 min. Chart at 10:35 the program is true for a "buy" position I want it to wait till 10:45 to enter. 3

Reason: