You must select an order before using OrderOpenPrice() etc.
void OrderEntry(int direction) { if(direction==0) { if(OpenOrdersThisPair (Symbol())==0) { int buyticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, NULL, MagicNumber, 0, Green); if (buyticket>0 && OrderSelect(buyticket,SELECT_BY_TICKET)) if(!OrderModify(buyticket, OrderOpenPrice(),Ask-StopLoss*pips, Ask+TakeProfit*pips, 0, CLR_NONE)) Print ("ERROR ",GetLastError()); } } }
Please note code pasted without blank lines after every line.
int buyticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, NULL, MagicNumber, 0, Green); if (buyticket>0)OrderModify(buyticket, OrderOpenPrice(),Ask-StopLoss*pips, Ask+TakeProfit*pips, 0, CLR_NONE);
- MT4: You can not use any Trade Functions until you first select an order.
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
(your broker will be similar).
Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)
-
You must select an order before using OrderOpenPrice() etc.
Please note code pasted without blank lines after every line.
Thanks for answer. It still did not work when I tried to change it. Maybe I should post the whole code? I wonder if the EA is just too old. Written back in 2013 or something when things where called int init, int deinit and int start.
-
“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
How To Ask Questions The Smart Way. (2004)
When asking about code
Be precise and informative about your problem -
Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
Thanks for answer. It still did not work when I tried to change it. Maybe I should post the whole code? I wonder if the EA is just too old. Written back in 2013 or something when things where called int init, int deinit and int start.
What is not working.
What errors do you get?
-
“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
How To Ask Questions The Smart Way. (2004)
When asking about code
Be precise and informative about your problem -
Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
Relax. The errors are the same. If one has to fit into the "society" 100 % and follow all rules at all times I guess I will have to find out about it another way. I had the feeling nobody would help with this even if I posted all code. It seems I might have been wrong. Here it is:
#property copyright "Copyright 2022, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict extern double LotSize = 0.01; extern double StopLoss = 30; extern double TakeProfit = 30; extern int BollingerPeriod = 20; extern int BollingerDeviation = 2; double pips; extern int MagicNumber = 1237; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { double ticksize = MarketInfo (Symbol(), MODE_TICKSIZE); if (ticksize == 0.00001 || ticksize == 0.0001) pips = ticksize*10; else pips = ticksize; return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(IsNewCandle()) CheckForBollingerBandTrade(); } void CheckForBollingerBandTrade() { double MiddleBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_MAIN, 1); double LowerBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_LOWER, 1); double UpperBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_UPPER,1); double PrevMiddleBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_MAIN, 2); double PrevLowerBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_LOWER, 2); double PrevUpperBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_UPPER, 2); if(Close[1]>LowerBB&&Close[2]<PrevLowerBB)OrderEntry(0); if(Close[1]<UpperBB&&Close[2]>PrevUpperBB)OrderEntry(1); } //+------------------------------------------------------------------+ void OrderEntry(int direction) { if(direction==0) { if(OpenOrdersThisPair (Symbol())==0) int buyticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, NULL, MagicNumber, 0, Green); if(buyticket>0)OrderModify(buyticket, OrderOpenPrice(),Ask+StopLoss*pips, Ask-TakeProfit*pips, 0, CLR_NONE); } if(direction==1) { if(OpenOrdersThisPair (Symbol())==0) int sellticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, NULL, MagicNumber, 0, Red); if(sellticket>0)OrderModify(sellticket, OrderOpenPrice(),Bid+StopLoss*pips, Bid-TakeProfit*pips, 0, CLR_NONE); } } //+------------------------------------------------------------------+ //Checks to see if any orders open on this currency pair. //+------------------------------------------------------------------+ int OpenOrdersThisPair (string pair) { int total =0; for (int i=OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == pair) total++; } return(total); } bool IsNewCandle() { static int BarsOnChart = 0; if (Bars == BarsOnChart) return (false); BarsOnChart = Bars; return(true); }
'buyticket' - undeclared identifier BB-MACD EA.mq4
return value of 'OrderModify' should be checked BB-MACD EA.mq4
'sellticket' - undeclared identifier BB-MACD EA.mq4
return value of 'OrderModify' should be checked BB-MACD EA.mq4
return value of 'OrderSelect' should be checked BB-MACD EA.mq4
2 errors, 3 warnings
When new on a forum it is just not to read this, read that, important notice. At first I just saw the mql5 part of the forum, but when I read a post where people chopped the head off someone who had posted mql4 related post in the mql5 section I new I had to search further. These last errors and warnings I don't know if they can be part of the Code(Alt+S) or not.
Erlend Christiansen #:
void OrderEntry(int direction) { if(direction==0) { if(OpenOrdersThisPair (Symbol())==0) int buyticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, NULL, MagicNumber, 0, Green); if(buyticket>0)OrderModify(buyticket, OrderOpenPrice(),Ask+StopLoss*pips, Ask-TakeProfit*pips, 0, CLR_NONE); } if(direction==1) { if(OpenOrdersThisPair (Symbol())==0) int sellticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, NULL, MagicNumber, 0, Red); if(sellticket>0)OrderModify(sellticket, OrderOpenPrice(),Bid+StopLoss*pips, Bid-TakeProfit*pips, 0, CLR_NONE); } }
You have completely ignored my post where I corrected your errors!
I have actually not. Where it says if buyticket you modified my code and ended with some like Print "ERROR" GetLastError. I tried wiriting that and got exactly the same errors and warnings. So I changed it back to how it was before. I thought if I posted the whole code maybe that would uncover something else. I can write the way you proposed for the buyticket and sellticket and paste it all in here again. But then comes the part later in the code which will "crash" with your proposal.
You posted
if (buyticket>0 && OrderSelect(buyticket,SELECT_BY_TICKET))
In my original code later:
int OpenOrdersThisPair (string pair) { int total =0; for (int i=OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
OrderSelect will be mentioned twice. Or maybe that does not matter.
I have actually not. Where it says if buyticket you modified my code and ended with some like Print "ERROR" GetLastError. I tried wiriting that and got exactly the same errors and warnings. So I changed it back to how it was before. I thought if I posted the whole code maybe that would uncover something else. I can write the way you proposed for the buyticket and sellticket and paste it all in here again. But then comes the part later in the code which will "crash" with your proposal.
You posted
if (buyticket>0 && OrderSelect(buyticket,SELECT_BY_TICKET))
I posted more code than that.
Nothing in my modifications will cause any crash
If you had used my code the errors and warning that you mentioned will no longer happen.
In my original code later:
OrderSelect will be mentioned twice. Or maybe that does not matter.
int OpenOrdersThisPair (string pair) { int total =0; for (int i=OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
You never mentioned this warning in your original post.
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
You should check that the OrderSelect() succeeds
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
}
It doesn't matter if OrderSelect() is used elsewhere in your code but you must remember that if you call a function that uses OrderSelect() any selected order that you may be using will be overwritten. (This is not the case with your posted code.)
So utilise the code that I have posted to modify your EA and report back.
I posted more code than that.
Nothing in my modifications will cause any crash
If you had used my code the errors and warning that you mentioned will no longer happen.
You never mentioned this warning in your original post.
You should check that the OrderSelect() succeeds
It doesn't matter if OrderSelect() is used elsewhere in your code but you must remember that if you call a function that uses OrderSelect() any selected order that you may be using will be overwritten. (This is not the case with your posted code.)
So utilise the code that I have posted to modify your EA and report back.
Now I really tried to get this done. It has taken too long because I have not had much time for coding. Now it is almost without warnings.
//+------------------------------------------------------------------+ //| BB-MACD EA.mq4 | //| Copyright 2022, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict extern double LotSize = 0.01; extern double StopLoss = 30; extern double TakeProfit = 30; extern int BollingerPeriod = 20; extern int BollingerDeviation = 2; double pips; extern int MagicNumber = 1237; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { double ticksize = MarketInfo (Symbol(), MODE_TICKSIZE); if (ticksize == 0.00001 || ticksize == 0.0001) pips = ticksize*10; else pips = ticksize; return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(IsNewCandle()) CheckForBollingerBandTrade(); } void CheckForBollingerBandTrade() { double MiddleBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_MAIN, 1); double LowerBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_LOWER, 1); double UpperBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_UPPER,1); double PrevMiddleBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_MAIN, 2); double PrevLowerBB=iBands(NULL,0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_LOWER, 2); double PrevUpperBB=iBands(NULL, 0, BollingerPeriod, BollingerDeviation, 0, 0, MODE_UPPER, 2); if(Close[1]>LowerBB&&Close[2]<PrevLowerBB)OrderEntry(0); if(Close[1]<UpperBB&&Close[2]>PrevUpperBB)OrderEntry(1); } //+------------------------------------------------------------------+ void OrderEntry(int direction) { if(direction==0) { if(OpenOrdersThisPair (Symbol())==0) { int buyticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, NULL, MagicNumber, 0, Green); if(buyticket>0 && OrderSelect(buyticket,SELECT_BY_TICKET)) if(!OrderModify(buyticket, OrderOpenPrice(), Ask-StopLoss*pips, Ask+StopLoss*pips, 0, CLR_NONE)) Print ("ERROR", GetLastError()); } } if(direction==1) { if(OpenOrdersThisPair (Symbol())==0) { int sellticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, NULL, MagicNumber, 0, Red); if(sellticket>0 && OrderSelect(sellticket, SELECT_BY_TICKET)) if(!OrderModify(sellticket, OrderOpenPrice(),Bid+StopLoss*pips, Bid-TakeProfit*pips, 0, CLR_NONE)) Print ("ERROR", GetLastError()); } } } //+------------------------------------------------------------------+ //Checks to see if any orders open on this currency pair. //+------------------------------------------------------------------+ int OpenOrdersThisPair (string pair) { int total =0; for (int i=OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == pair) total++; } return(total); } bool IsNewCandle() { static int BarsOnChart = 0; if (Bars == BarsOnChart) return (false); BarsOnChart = Bars; return(true); } 'BB-MACD EA.mq4' BB-MACD EA.mq4 1 1 return value of 'OrderSelect' should be checked BB-MACD EA.mq4 95 7 0 errors, 1 warnings, 154 msec elapsed 1 2
I am sorry that I missed a bracket when trying to code your suggestion the first time. I missed the bracket after :
if(OpenOrdersThisPair (Symbol())==0) {
3 brackets in such small space seemed just too much. Anyway. With one warning only the EA should work? Thanks for help!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I would have asked the guy I am copying this code from directly, but that is not so easy.
Here is the code:
The errors and warnings I get is:
'buyticket' - undeclared identifier
return value of 'OrderModify' should be checked
I have tried to understand exactly what it all means and I tried to change the code after if(buyticket>0) with if else statement print string. Same error.