MQL4 - automated forex trading   /  

Forum

Vary simple expert advisor

Back to topics list To post a new topic, please log in or register

avatar
2
sonyado 2006.03.04 13:43 
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. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://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!
Comparative Analysis of 30 Indicators and Oscillators

Comparative Analysis of 30 Indicators and Oscillators

The article describes an Expert Advisor that allows conducting the comparative analysis of 30 indicators and oscillators aiming at the formation of an effective package of indexes for trading.


avatar
30
CockeyedCowboy 2006.03.04 22:32 
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.

avatar
2
sonyado 2006.03.05 10:10 
Thank you CockeyedCowboy!
It works better, but still opening only buy orders and it doesn't seеm to work on H1 tameframe.
Back to topics list  

To add comments, please log in or register