Vary simple expert advisor

 
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 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "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(Ask>High[1])
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened: ",OrderOpenPrice());
}
else
Print("Error opening BUY order: ",GetLastError());
return(0);
}
if(Bid<Low[1])
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
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++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid<Low[1])
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened: ",OrderOpenPrice());
}
else
Print("Error opening SELL order: ",GetLastError());
return(0);
}
}
else
{
if(Ask>High[1])
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened: ",OrderOpenPrice());
}
else
Print("Error opening BUY order: ",GetLastError());
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!
 
sonyado

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.
 
Thank you CockeyedCowboy!
It works better, but still opening only buy orders and it doesn't seеm to work on H1 tameframe.
Reason: