7

Hello!
I'm trying to create a very simple expert advisor, but it doesn't work properly. This is the idea:

If price goes higher then previouse high entry buy order is sent.
If price goes lower then previous low entry sell order is sent.

If there is open buy order and price goes lower then previous low, the buy order is closed and sell order is opened.
If there is open sell order and price does higher then previous high, the sell order is closed and buy order is opened.

The timeframe is 1 hour.
This is the expert advisor that I've wrote:

//+------------------------------------------------------------------+
//| Reversal.mq4 |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double Lots=0.1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

int init()
{
int cnt=0, ticket, total;
//----
total=OrdersTotal();
if(total<1)
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money - Free margin = ",AccountFreeMargin());
return(0);
}
{
if(ticket>0)
{
}
else
return(0);
}
if(Bid<Low[1])
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Red);
if(ticket>0)
{
Print("SELL order opened: ",OrderOpenPrice());
}
else
Print("Error opening SELL order: ",GetLastError());
return(0);
}
return(0);
}
//proverka na poziciite
for(cnt=0;cnt<total;cnt++)
{
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
{
if(Bid<Low[1])
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Red);
if(ticket>0)
{
Print("SELL order opened: ",OrderOpenPrice());
}
else
Print("Error opening SELL order: ",GetLastError());
return(0);
}
}
else
{
{
if(ticket>0)
{
}
else
return(0);
}
}
}
}
return(0);
}

// the end.

The expert opens only buy orders and it doesn't seams to work on H1 timeframe.
I'll appreciate any halp!
524

I have not examined your code but what jumps out at me is that you coded the whole thing in the init() function. That part of the code only gets visited once at execution time. Therefore it gets run only once when you startup your EA.

Change the line in you code from;

int init()

To the start function;

int start()

and try your code again it may work after that.
7

Thank you CockeyedCowboy!
It works better, but still opening only buy orders and it doesn't seеm to work on H1 tameframe.