MQL4 Guide - page 2

 
hopokuk:

I would like to program this

IF NUMBER OF OPEN TRADES = 8 THEN CLOSE THE OLDEST OPEN TRADE

I want to close the oldest market order if there are >=8 market orders, but to ignore all the pending orders

thanks
extern int Magic = ...

//-----

int BuyCnt = 0;

int SellCnt = 0;

int cnt = OrdersTotal();

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

{

if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

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

if (OrderMagicNumber() != Magic) continue;

int type = OrderType();

if (type == OP_BUY) BuyCnt++;

if (type == OP_SELL) SellCnt++;

}

if (BuyCnt+SellCnt < 8) return;

//-----

datetime tm = 0;

int ticket = -1;

cnt = OrdersTotal();

for (i=0; i < cnt; i++)

{

if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

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

if (OrderMagicNumber() != Magic) continue;

type = OrderType();

if (type == OP_BUY || type == OP_SELL)

{

if (OrderOpenTime() > tm)

{

tm = OrderOpenTime();

ticket = OrderTicket();

}

}

}

//-----

if (ticket != -1) OrderClose(ticket, ...
 
DooMGuarD:
hi all

How i obtain the initial deposit in account for statistical questions?

regards
#define OP_BALANCE 6

int cnt = OrdersHistoryTotal();

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

{

if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;

if (OrderType() == OP_BALANCE)

{

Alert(OrderProfit());

break;

}

}

And do not forget to set Show All History option.

 

exiting a trade

Lets say I am in a trade based on a stochastic setting. I want to exit based on another stochastic cross totally different from my entry. No problem yet. But lets say at my exit the market continues to go up. I want to change the exit based on 95% of the current stochastic signal line to the next level to keep me in the trade. So once I have my current stochastic setting at 95% and it gets hit, my new exit is 95% of the new setting and once this gets hit my new exit is 95% of that setting. This setup can keep you in a trade until the market peaks and turns down crossing the signal and the trade is exited on the cross. You know when it seems overbought or oversold. By changing the stochastic settings, as the price increases you are able to stay in the market till the trend breaks. Once trade is closed, you start next order with 1st level of settings. Currently I use a main signal on one setting crossing a signal signal on another setting. I only use 1 of the lines each stoch and black the other one out. I want to be able to move at least 4 levels up if the market continues to trend. Of course the opposite would be for a sell.

 
golden188:
Could someone please help me with some language to make my Expert Advisor trade a percentage of my balance? For example, I would like each trade, instead of being x number of lots, to be 10% of my current balance. Does anyone know how to do this? The more explicit the details the more helpful it would be. thanks.

I suggest to use the following function:

extern double Lots = 0.1;

extern double MinLot = 0.1;

extern double MaxLot = 5.0;

extern int LotPrec = 1;

extern bool DynamicLot = false;

extern double LotStep = 0.1;

extern double BalanceStep = 500;

double GetLots()

{

double lots = Lots;

if (DynamicLot)

{

lots = NormalizeDouble(LotStep*AccountBalance()/BalanceStep, LotPrec);

}

lots = MathMax(lots, MinLot);

lots = MathMin(lots, MaxLot);

return (lots);

}

 

help EA

hello RickD

i try make simpel EA.... can u help me rick...

sell:

when price have 70pip range

exampel : price open day : 208.55

low price(low price day) :208.30

and price going up (high price day)to 209.00

OP sell 209.00.....

buy: when price have 70pip range

exampel : price open day : 208.55

high price(high price day) :208.70

and price going down (low price day)to 207.00

OP buy 207.00.....

thks RickD

 
KuPretMan:
hello RickD

i try make simpel EA.... can u help me rick...

sell:

when price have 70pip range

exampel : price open day : 208.55

low price(low price day) :208.30

and price going up (high price day)to 209.00

OP sell 209.00.....

buy: when price have 70pip range

exampel : price open day : 208.55

high price(high price day) :208.70

and price going down (low price day)to 207.00

OP buy 207.00.....

thks RickD

Hi,

Do you mean that I should write the EA for you?

Please send a PM me.

 
 

Alert question

Hi

I wrote Alert but it only works when I open window with my indicator. It works only also when I jump beetwen charts windows, in other words when I jump between charts time levels. It seems that indicator can not count when whorks. How can I solve my problem ? Need help. Is it conected with Indicatorcounted ? Should I do something in parameter of Alert. How to do it ?

Should I use something like:

double

? = ObjectGetValueByShift(string name, int shift));

if (???????????)

Alert("Pattern on " + Symbol() + " " + Period());

Pucio

 

Crossing

Hey Rick, I'm glad you're doing this.

What's the code for crossing of two 2-line indicators? I'm sure it isn't really difficult. Say, the 1st indicator's lines cross each other 1 bar before [MAIN > SIGNAL] = BUY order, 2nd indicator's lines cross one another [MAIN < SIGNAL] = SELL order.

What I came up with is this, but it just doesn't cut it.

if (Time[0] == prevtime)

return(0);

prevtime = Time[0];

//IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0); mentioned later

//IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0); mentioned later

IND2 = iIND2 (NULL, 0, 0, MODE_MAIN, 0); // how to go about these two?

IND2S = iIND2 (NULL, 0, 0, MODE_SIGNAL, 0); // how to go about these two?[/CODE]

Taking into consideration Guru's approach, [with some minor changes and fixed direction b/c it's misspelled all over the lesson ]

[CODE]int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_direction = 0;

if(line1>line2)current_direction = 1; //up

if(line1<line2)current_direction = 2; //down

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0);

IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0);

int isCrossed = Crossed (IND1 ,IND2);

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Yikes! You're screwed! Get back to the manual!!! Ha! ",GetLastError());

return(0);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print(""Yikes! You're screwed! Get back to the manual!!! Ha! "",GetLastError());

return(0);

}

return(0);

}

 

Hi RickD

I am looking for EA to do the following;

If the pending order executed and end with SL the EA will put the same pending order again.

Some time the fluctuation end the trend with SL but if the market hit the same price again, it ends with TP.

Thanks for all your help.

AS

Reason: