I'm trying to learn how to code expert advisers so I got something simple to start with.
The idea is for the EA to open a long position when price crosses a MA from below.
Than, when price comes back down and crosses the MA from above the long position would be closed and a short will be open.
The idea seems simple and the code below seems to be right but nothing happens.
Could somebody point me where my mistake is?
extern double Lots = 0.01;
extern int Slippage = 7;
extern string comment = "MA_Template";
extern int MagicNumber = 0;
/////////////////////////////////////
int ThisBarTrade = 0;
int Position = 0;
int init() {
CalcPosition();
}
int start() {
double MA;
bool closed = false;
if ( ThisBarTrade != Bars) {
MA = iMA(NULL,0,5,0,0,0,0);
// Buy Condition
if ( MA >= Bid && Position <=0) {
CalcPosition();
if (Position < 0) {
if ( CloseSingleSell() == false) return (0); // failure but it may try again
}
if (OpenSingleBuy() == false) return (0); // failure to buy but it may try again
ThisBarTrade = Bars;
}
// Sell Condition
if ( MA <= Ask && Position >= 0) {
CalcPosition();
if(Position > 0) {
if(CloseSingleBuy()== false) return(0); // failure but it may try again
}
if (OpenSingleSell() == false) return (0); // failure to sell but it may try again
ThisBarTrade = Bars;
}
}
}
//+------------------------------------------------------------------+
void CalcPosition() {
Position = 0;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol() == Symbol() ) {
if (OrderType() == OP_BUY) Position += 1;
if (OrderType() == OP_SELL) Position -=1;
}
}
}
bool CloseSingleBuy() {
bool closed;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber()) {
if ( OrderType() == OP_BUY) {
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
Print ("Attempting to CloseSingleBuy order # " + i + " " + Bid);
if (closed == -1) {
return(false);
} else {
return(true);
}
}
}
}
return (false);
}
bool CloseSingleSell() {
bool closed;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber() ) {
if ( OrderType() == OP_SELL) {
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
Print ("Attempting to CloseSingleSell order # " + i + " " + Ask);
if (closed == -1) {
return(false);
} else {
return(true);
}
}
}
}
return (false);
}
bool OpenSingleBuy() {
if (Position != 0) return(false);
int ticket = -1;
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
ticket = OrderSend( Symbol(), OP_BUY, Lots, Ask, Slippage, 0.0, 0.0, comment + " buy", MagicNumber, 0, Lime);
if (ticket <0) {
Print ("Failed to OpenSingleBuy, error # ", GetLastError());
return (false);
} else {
Print ("Successfully placed order with OpenSingleBuy");
return (true);
}
return (false);
}
bool OpenSingleSell() {
if (Position != 0) return(false);
int ticket = -1;
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
ticket = OrderSend( Symbol(), OP_SELL, Lots, Bid, Slippage, 0.0, 0.0, comment + " sell", MagicNumber, 0, Lime);
if (ticket <0) {
Print ("Failed to OpenSingleSell, error # ", GetLastError());
return (false);
} else {
Print ("Successfully placed order with OpenSingleSell");
return (true);
}
return (false);
}
The idea is for the EA to open a long position when price crosses a MA from below.
Than, when price comes back down and crosses the MA from above the long position would be closed and a short will be open.
The idea seems simple and the code below seems to be right but nothing happens.
Could somebody point me where my mistake is?
extern double Lots = 0.01;
extern int Slippage = 7;
extern string comment = "MA_Template";
extern int MagicNumber = 0;
/////////////////////////////////////
int ThisBarTrade = 0;
int Position = 0;
int init() {
CalcPosition();
}
int start() {
double MA;
bool closed = false;
if ( ThisBarTrade != Bars) {
MA = iMA(NULL,0,5,0,0,0,0);
// Buy Condition
if ( MA >= Bid && Position <=0) {
CalcPosition();
if (Position < 0) {
if ( CloseSingleSell() == false) return (0); // failure but it may try again
}
if (OpenSingleBuy() == false) return (0); // failure to buy but it may try again
ThisBarTrade = Bars;
}
// Sell Condition
if ( MA <= Ask && Position >= 0) {
CalcPosition();
if(Position > 0) {
if(CloseSingleBuy()== false) return(0); // failure but it may try again
}
if (OpenSingleSell() == false) return (0); // failure to sell but it may try again
ThisBarTrade = Bars;
}
}
}
//+------------------------------------------------------------------+
void CalcPosition() {
Position = 0;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol() == Symbol() ) {
if (OrderType() == OP_BUY) Position += 1;
if (OrderType() == OP_SELL) Position -=1;
}
}
}
bool CloseSingleBuy() {
bool closed;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber()) {
if ( OrderType() == OP_BUY) {
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
Print ("Attempting to CloseSingleBuy order # " + i + " " + Bid);
if (closed == -1) {
return(false);
} else {
return(true);
}
}
}
}
return (false);
}
bool CloseSingleSell() {
bool closed;
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber() ) {
if ( OrderType() == OP_SELL) {
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
Print ("Attempting to CloseSingleSell order # " + i + " " + Ask);
if (closed == -1) {
return(false);
} else {
return(true);
}
}
}
}
return (false);
}
bool OpenSingleBuy() {
if (Position != 0) return(false);
int ticket = -1;
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
ticket = OrderSend( Symbol(), OP_BUY, Lots, Ask, Slippage, 0.0, 0.0, comment + " buy", MagicNumber, 0, Lime);
if (ticket <0) {
Print ("Failed to OpenSingleBuy, error # ", GetLastError());
return (false);
} else {
Print ("Successfully placed order with OpenSingleBuy");
return (true);
}
return (false);
}
bool OpenSingleSell() {
if (Position != 0) return(false);
int ticket = -1;
while(!IsTradeAllowed()) Sleep(MathRand() / 10);
RefreshRates();
ticket = OrderSend( Symbol(), OP_SELL, Lots, Bid, Slippage, 0.0, 0.0, comment + " sell", MagicNumber, 0, Lime);
if (ticket <0) {
Print ("Failed to OpenSingleSell, error # ", GetLastError());
return (false);
} else {
Print ("Successfully placed order with OpenSingleSell");
return (true);
}
return (false);
}