Without wishing to be rude, your code formatting is terrible!
That makes it hard for you to spot an issue, and even harder for someone else to help you; many won't even bother to read the code.
I also see you are using an EA builder. That is fine - everyone has to start somewhere. But you will get some criticism from certain people on here for using it.
I suggest you add this at the top of your code:
Then update your code to the latest style:
// int init()
int OnInit()
// int start()
void OnTick()
Then clean up the formatting.
Then address all the warnings and errors that the compiler gives you.
Then come back if you still have problems.
HTH
C0mput3r: I am sure that the short order is just in a wrong place?
|
|
Thanks Guys, I used an EA builder as a base to work from and then built the rest of the code by googling a ton of examples and also from asking on the forum, so my code is a mosaic of chaos.
I'm newbie but learning quickly, thanks for all the help guys. I really appreciate that you guys are willing to help everytime
C0mput3r: I used an EA builder
|
|
Thanks for the constructive criticism whroeder1.
I can see why you would hate EA builder. I've been spending the past month studying mql4 code so that I can to try become as self sufficient as possible, unfortunately it takes time and that is why I ask a lot of questions. I want to learn to master mql4 coding completely.
I never meant that I wanted someone to code for me, I merely asked if there were any experienced coders out there if they were willing to help a rookie like me figure out where my mistake is.
You guys really are a wealth of information and I am grateful that so many of you are willing to give advice to a total stranger. Sorry if my questions annoy you.
I have taken in what you guys have advised and cleaned up my code but it will still not execute any short orders.
//| FINAL MOMENTUM.mq4 |
//| Anton |
//| |
//+------------------------------------------------------------------+
//#property strict
// External variables
extern double StopLoss = 20;
extern double TakeProfit = 6;
extern int MagicNumber = 23310;
// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;
//------------------------------------------------------------
//------------------------------------------------------------
// Init function
int init()
//int Oninit()
{
RealPoint = RealPipPoint(Symbol());
return(0);
}
//------------------------------------------------------------
//------------------------------------------------------------
// Start function
//int start()
void OnTick()
{
//LONG
//------------------------------------------------------------
//BUY CONDITION 1 -->Count of Bids/Asks
//------------------------------------------------------------
static int countL = 0;
static double lastBid = 0;
if(Bid > lastBid) { countL++; }
else { countL = 0; }
if(countL >= 5)
{
//------------------------------------------------------------
bool buy_condition_1 = (countL >= 5);
countL = 0;
//------------------------------------------------------------
}
lastBid = Bid;
//------------------------------------------------------------
//BUY CONDITION 2 -->Moving Average
//------------------------------------------------------------
double MA1L;
MA1L=iMA(NULL,15,7,0,MODE_SMA,PRICE_OPEN,0);
double MA2L;
MA2L=iMA(NULL,15,21,0,MODE_SMA,PRICE_CLOSE,0);
if(MA1L > MA2L)
{
//------------------------------------------------------------
bool buy_condition_2 = (MA1L > MA2L);
MA1L = 0;
//------------------------------------------------------------
}
//****************************************************************
//EXECUTE BUY ORDER
//****************************************************************
{
if( buy_condition_1 && buy_condition_2 )
{
double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double Leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
double LotSize = ((((AccountBalance/Ask)*Leverage) - 1)/100000);
//------------------------------------------------------------
//------------------------------------------------------------
LongTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);
//------------------------------------------------------------
//------------------------------------------------------------
if(OrderSelect(LongTicket,SELECT_BY_TICKET)==true)
double OpenPrice = OrderOpenPrice();
if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
if(LongStopLoss > 0 || LongTakeProfit > 0)
{
bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
}
ShortTicket = 0;
}
}
// SHORT
//------------------------------------------------------------
//SELL CONDITION 1 -->Count of Bids/Asks
//------------------------------------------------------------
static int countS = 0;
static double lastAsk = 0;
if(Ask < lastAsk) { countS++; }
else { countS = 0; }
if(countS >= 5)
{
//------------------------------------------------------------
bool sell_condition_1 = (countS >= 5);
countS = 0;
//------------------------------------------------------------
}
lastAsk = Ask;
//------------------------------------------------------------
//SELL CONDITION 2 -->Moving Average
//------------------------------------------------------------
if(MA1L < MA2L)
{
//------------------------------------------------------------
bool sell_condition_2 = (MA1L < MA2L);
MA2L = 0;
//------------------------------------------------------------
}
//***************************************************************
//EXECUTE SELL CONDITION
//***************************************************************
{
if( sell_condition_1 && sell_condition_2 )
{
//------------------------------------------------------------
//------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Buy Order",MagicNumber,0,Green);
//------------------------------------------------------------
//------------------------------------------------------------
if(OrderSelect(ShortTicket,SELECT_BY_TICKET)==true)
double OpenPriceShort = OrderOpenPrice();
if(StopLoss > 0) double ShortStopLoss = OpenPriceShort + (StopLoss * RealPoint);
if(TakeProfit > 0) double ShortTakeProfit = OpenPriceShort - (TakeProfit * RealPoint);
if(ShortStopLoss > 0 || ShortTakeProfit > 0)
{
bool ShortMod = OrderModify(ShortTicket,OpenPriceShort,ShortStopLoss, ShortTakeProfit,0);
}
LongTicket = 0;
}
}
//***************************************************************
}
// Pip Point Function
double RealPipPoint(string Currency)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return(CalcPoint);
}
...
I have taken in what you guys have advised and cleaned up my code but it will still not execute any short orders.
//***************************************************************
//EXECUTE SELL CONDITION
//***************************************************************
{
if( sell_condition_1 && sell_condition_2 )
{
//------------------------------------------------------------
//------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Buy Order",MagicNumber,0,Green);
//------------------------------------------------------------
//------------------------------------------------------------
...}
Thanks for the constructive criticism whroeder1.
I can see why you would hate EA builder. I've been spending the past month studying mql4 code so that I can to try become as self sufficient as possible, unfortunately it takes time and that is why I ask a lot of questions. I want to learn to master mql4 coding completely.
I never meant that I wanted someone to code for me, I merely asked if there were any experienced coders out there if they were willing to help a rookie like me figure out where my mistake is.
I admire your resolve and positive attitude.
EA builders can be a good place to start, but you'll want to move away from them as soon as you can for several reasons.
MQL4 has been substantially overhauled in recent years but often these EA builders have not been updated to reflect that. Also, they frequently don't use 'best practice' in their code.
Consider your problem: the EA won't open sell orders. Is that because your logic to trigger a sell order is never satisfied, or because the OrderSend fails? That is where print statements and checking return codes can help (as WHRoeder has suggested).
I know I'm repeating myself, but put this at that top of your code and compile:
It is going to generate 17 errors and 3 warnings. That may seem intimidating and it may be tempting to just remove that pesky line and ignore it. Don't! A properly coded EA will have no errors or warnings. Plus you will learn a lot by fixing those errors and warnings.
It would have given you an error about the LotSize variable for the sell order (as Alain Verleyen has identified):
It would also have identified some other issues with your code and the scope of variables. You can't declare a variable inside an 'if' statement then try to access that variable from outside the 'if' statement. You have done this quite a few times e.g.
{
bool buy_condition_2 = (MA1L > MA2L);
MA1L = 0;
}
...
if( buy_condition_1 && buy_condition_2 )
With #property strict, the compiler would have given you:
I admire your resolve and positive attitude.
EA builders can be a good place to start, but you'll want to move away from them as soon as you can for several reasons.
MQL4 has been substantially overhauled in recent years but often these EA builders have not been updated to reflect that. Also, they frequently don't use 'best practice' in their code.
Consider your problem: the EA won't open sell orders. Is that because your logic to trigger a sell order is never satisfied, or because the OrderSend fails? That is where print statements and checking return codes can help (as WHRoeder has suggested).
I know I'm repeating myself, but put this at that top of your code and compile:
It is going to generate 17 errors and 3 warnings. That may seem intimidating and it may be tempting to just remove that pesky line and ignore it. Don't! A properly coded EA will have no errors or warnings. Plus you will learn a lot by fixing those errors and warnings.
It would have given you an error about the LotSize variable for the sell order (as Alain Verleyen has identified):
It would also have identified some other issues with your code and the scope of variables. You can't declare a variable inside an 'if' statement then try to access that variable from outside the 'if' statement. You have done this quite a few times e.g.
{
bool buy_condition_2 = (MA1L > MA2L);
MA1L = 0;
}
...
if( buy_condition_1 && buy_condition_2 )
With #property strict, the compiler would have given you:
Thank you Honest_knave, I've left #property strict on and worked through every error until all of them were sorted out. I've also finally figured out how to use print to figure out which variables aren't working and fixed them.
I really appreciate all your help, you are really a role model!
Thanks,
Anton

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Guys,
This is probably a simple question but I've converted my code to run short trades too but for some reason it won't execute any short orders.
Could someone please help? I am sure that the short order is just in a wrong place?
The code is written specifically for EUR/USD
Any help will be greatly appreciated!
Thanks,
Anton
//| NEW_CODE_LONG.mq4 |
//| Anton |
//| http://www.fxeabuilder.com |
//+------------------------------------------------------------------+
#property copyright "fxeabuilder.com"
#property link "www.fxeabuilder.com"
// External variables
extern double StopLoss = 10;
extern double TakeProfit = 3;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int MagicNumber = 23310;
// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;
//double Lots
//------------------------------------------------------------
//------------------------------------------------------------
// Init function
int init()
{
RealPoint = RealPipPoint(Symbol());
}
//------------------------------------------------------------
//------------------------------------------------------------
// Start function
int start()
{
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Long
OrderSelect(LongTicket,SELECT_BY_TICKET);
if(OrderCloseTime() != 0 || LongTicket == 0)
//------------------------------------------------------------
//------------------------------------------------------------
//BUY CONDITION 1 -->Count of Bids/Asks
//------------------------------------------------------------
//------------------------------------------------------------
static int countL = 0;
static double lastBid = 0;
if(Bid > lastBid) { countL++; }
else { countL = 0; }
if(countL >= 5)
{
//------------------------------------------------------------
bool buy_condition_1 = (countL >= 5);
countL = 0;
//------------------------------------------------------------
}
lastBid = Bid;
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//BUY CONDITION 2 -->Moving Average
//------------------------------------------------------------
//------------------------------------------------------------
double MovingPeriodL1 = 7;
double MovingPeriodL2 = 21;
double MovingShiftL = 0;
double MA1L;
double MA2L;
MA1L=iMA(NULL,5,MovingPeriodL1,MovingShiftL,MODE_SMA,PRICE_CLOSE,0);
MA2L=iMA(NULL,5,MovingPeriodL2,MovingShiftL,MODE_SMA,PRICE_CLOSE,0);
if(MA1L > MA2L)
{
//------------------------------------------------------------
bool buy_condition_2 = (MA1L > MA2L);
MA1L = 0;
//------------------------------------------------------------
}
//------------------------------------------------------------
//------------------------------------------------------------
//***************************************************************
//***************************************************************
{
if( buy_condition_1 && buy_condition_2 )
{
OrderSelect(ShortTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && ShortTicket > 0)
{
bool ClosedLong = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);
}
double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double Leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
double LotSize = ((((AccountBalance/Ask)*Leverage) - 1)/100000);
//------------------------------------------------------------
//------------------------------------------------------------
LongTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);
//------------------------------------------------------------
//------------------------------------------------------------
OrderSelect(LongTicket,SELECT_BY_TICKET);
double OpenPrice = OrderOpenPrice();
if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
if(LongStopLoss > 0 || LongTakeProfit > 0)
{
bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
}
ShortTicket = 0;
}
}
//***************************************************************
//***************************************************************
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Short
OrderSelect(ShortTicket,SELECT_BY_TICKET);
if(OrderCloseTime() != 0 || ShortTicket == 0)
//------------------------------------------------------------
//------------------------------------------------------------
//SELL CONDITION 1 -->Count of Bids/Asks
//------------------------------------------------------------
//------------------------------------------------------------
static int countS = 0;
static double lastAsk = 0;
if(Ask < lastAsk) { countS++; }
else { countS = 0; }
if(countS >= 5)
{
//------------------------------------------------------------
bool sell_condition_1 = (countS >= 5);
countS = 0;
//------------------------------------------------------------
}
lastAsk = Ask;
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//SELL CONDITION 2 -->Moving Average
//------------------------------------------------------------
//------------------------------------------------------------
double MovingPeriodS1 = 7;
double MovingPeriodS2 = 21;
double MovingShiftS = 0;
double MA1S;
double MA2S;
MA1S=iMA(NULL,5,MovingPeriodS1,MovingShiftS,MODE_SMA,PRICE_CLOSE,0);
MA2S=iMA(NULL,5,MovingPeriodS2,MovingShiftS,MODE_SMA,PRICE_CLOSE,0);
if(MA1S < MA2S)
{
//------------------------------------------------------------
bool sell_condition_2 = (MA1S < MA2S);
MA1S = 0;
//------------------------------------------------------------
}
//------------------------------------------------------------
//------------------------------------------------------------
//***************************************************************
//***************************************************************
{
if( sell_condition_1 && sell_condition_2 )
{
OrderSelect(LongTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && LongTicket > 0)
{
bool ClosedShort = OrderClose(LongTicket,OrderLots(),Bid,0,Red);
}
//double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
//double Leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
//double LotSize = ((((AccountBalance/Ask)*Leverage) - 1)/100000);
//------------------------------------------------------------
//------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Sell Order",MagicNumber,0,Green);
//------------------------------------------------------------
//------------------------------------------------------------
OrderSelect(ShortTicket,SELECT_BY_TICKET);
double OpenPriceShort = OrderOpenPrice();
if(StopLoss > 0) double ShortStopLoss = OpenPriceShort + (StopLoss * RealPoint);
if(TakeProfit > 0) double ShortTakeProfit = OpenPriceShort - (TakeProfit * RealPoint);
if(ShortStopLoss > 0 || ShortTakeProfit > 0)
{
bool ShortMod = OrderModify(ShortTicket,OpenPrice,ShortStopLoss, ShortTakeProfit,0);
}
LongTicket = 0;
}
}
//***************************************************************
//***************************************************************
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
// Pip Point Function
double RealPipPoint(string Currency)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return(CalcPoint);
}