# Problem with EA

44

Hello guys, Im here new and also Ive been studying MQL for about a week (no previous experience with programming)... I tried to write a simple EA (only to practice what I've learnt)

The idea is to double the lot size if the previous trade didnt work out well... if it again doesnt work out well then double the lot size again and so on... The signal for Buy/Sell depends on iMA

But Im struggling a lot with OrderSelect() and for() ... I think it selects the wrong order and therefore, it doesnt work properly... Concerning function for() , I do understant what it does but I cant figure out what to put into the condition for continuing the loop, or stopping it in the right moment.

Please, either give me some link to the article which might help me to understand this issue, or if you could simply explain it...

(I went through Help (F1) and a lot of articles, but nothing helped me...)

```extern int High_MA_period = 20;
extern int Low_MA_period = 8;

int OnInit()
{

return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
//here should be some reasonable conditions for finishing this EA. How?
}

void OnTick()
{
//SL and PT settings:
int SL = 10;
int PT = 10;

//settings for parameters for OrderSend:
string comment_B = "Pozice Buy"; string comment_S = "Pozice Sell";
int magic = 273;
int ticket;

//settings of 2 Moving Averages
double High_MA_0 = iMA(NULL,0,High_MA_period,0,0,0,0);
double High_MA_1 = iMA(NULL,0,High_MA_period,0,0,0,1);
double Low_MA_0 = iMA(NULL,0,Low_MA_period,0,0,0,0);
double Low_MA_1 = iMA(NULL,0,Low_MA_period,0,0,0,1);

//static parameter setting --> everything bellow happens only 1 times per candle:
for(int a = 0; a < OrdersTotal(); a++)
{
if(TradedThisBar != Bars && OrdersTotal() == 0)
{
double lots;

if(OrderSelect(a, SELECT_BY_POS, MODE_HISTORY))
{
if(OrderProfit() > 0)
{
lots = 0.01;
Print("Minulý obchod úspěch, ponecháváme 0.01 lotu");
}
if(OrderProfit() < 0)
{
lots = OrderLots() + OrderLots();
Print("Minulý obchod neúspěch, zvyšujeme lot na 2x");
}
}
else
{
lots = 0.01;
Print("Nenalezen minulý obchod");
}

if(High_MA_1 > Low_MA_1 && High_MA_0 < Low_MA_0)
{
if(ticket < 0){Alert("Vstup do pozice se nezdařil!");}
}

if(High_MA_1 < Low_MA_1 && High_MA_0 > Low_MA_0)
{
ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 0, Bid+SL*Point, Bid-PT*Point, comment_S, magic, 0, Red);
if(ticket < 0){Alert("Vstup do pozice se nezdařil!");}
}
}
}
return;
}```
44

Haha, solved it myself... Thanks anyway... For those who might be interested, I deleted "for()" and replaced      if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY) == true)

1108

What if

`  if( OrdersTotal() == 0 && OrdersHistoryTotal() == 0 )`

no order will be taken, ....

also OrderSelect is a fonction which select an order, it should be close an soon as the order as been selected, and the value be stored into variable

``` else
{
lots = 0.01;
Print("Nenalezen minulý obchod");
}
}} // close fonction

```

then 2 case no order in history, an order in history