Ingolf
I'd love to charge you for this :)
but what you decribe is almost exactly what is in the sample Expert Adviser found in your MetaTrader installation, called 'MACD Sample'
Check it out - for free!
-BB-
Hello BB,
it is nearly the same, but although my program shows no errors at compiling, it doesn´t works: Error opening BUY order: 0
What can I do?
Ingolf
Ingolf
Post the code on here & we'll have a look
See here 'How to insert MQL4 code into message.' for how to post code
-BB-
Ingolf
Post the code on here & we'll have a look
See here 'How to insert MQL4 code into message.' for how to post code
-BB-
//+------------------------------------------------------------------+ //| Ingolf Schaefer.mq4 | //| Copyright © 2008, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" //extern double TakeProfit = 50; extern double Lots = 1.0; //extern double TrailingStop = 30; //extern double MACDOpenLevel=0.3; //extern double MACDCloseLevel=0.2; extern double MATrendPeriod=9; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double MacdCurrent, MacdPrevious, SignalCurrent; double SignalPrevious, MaCurrent, MaPrevious; int cnt, ticket, total; // initial data checks // it is important to make sure that the expert works with a normal // chart and the user did not make any mistakes setting external // variables (Lots, StopLoss, TakeProfit, // TrailingStop) in our case, we check TakeProfit // on a chart of less than 100 bars //if(Bars<100) //{ //Print("bars less than 100"); //return(0); //} //if(TakeProfit<10) //{ //Print("TakeProfit less than 10"); //return(0); // check TakeProfit //} // to simplify the coding and speed up access // data are put into internal variables MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); MaCurrent=iMA(NULL,0,MATrendPeriod,9,MODE_EMA,PRICE_CLOSE,0); MaPrevious=iMA(NULL,0,MATrendPeriod,9,MODE_EMA,PRICE_CLOSE,1); // Kauf bei vorgegebenem Handelssignal: if ((MacdPrevious<SignalPrevious) && (MacdCurrent>(SignalCurrent+0.00005))) { OrderSend(Symbol(),0,1.0,Ask,3,0,0,"Buy",0,0,Green); return(0); } else Print("Error opening BUY order : ",GetLastError()); return(0); // Close bei Trendwende im MACD (Long Position glatt stellen): for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if((OrderType()==OP_BUY) && // check for opened long position (OrderSymbol()==Symbol())) // check for symbol // should it be closed? if(MacdCurrent<(MacdPrevious-0.00003)) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position return(0); // exit } } // Verkauf bei vorgegebenem Handelssignal: if ((MacdPrevious<SignalPrevious) && (MacdCurrent>(SignalCurrent+0.00005))) { OrderSend(Symbol(),OP_BUY,1.0,Ask,3,0,0,"Buy",16384,0,Green); } else Print("Error opening BUY order : ",GetLastError()); return(0); // Close (Short Position glatt stellen): for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if((OrderType()==OP_SELL) && // check for opened short position (OrderSymbol()==Symbol())) // check for symbol // should it be closed? if(MacdCurrent>(MacdPrevious+0.00003)) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } } } //21. Mai 2008 09:00Uhr
Ingolf
> although my program shows no errors at compiling, it doesn´t works: Error opening BUY order: 0
Ah - but it is working just fine as coded...
Because every time this evaluation line is not met
if ((MacdPrevious<SignalPrevious) && (MacdCurrent>(SignalCurrent+0.00005)))
it does the
else
which is the Print statement, which is correctly showing 0, meaning no error...
-BB-
Ingolf
> although my program shows no errors at compiling, it doesn´t works: Error opening BUY order: 0
Ah - but it is working just fine as coded...
Because every time this evaluation line is not met
if ((MacdPrevious<SignalPrevious) && (MacdCurrent>(SignalCurrent+0.00005)))
it does the
else
which is the Print statement, which is correctly showing 0, meaning no error...
-BB-
Hello BB,
thank you for your help. I hope, I can ask you for further questions, if so.
Ingolf
Ingolf
HTH
> I can ask you for further questions...
Try first, then read documents or search on the forum, if still a problem then post on forum :)
Just keep trying!
-BB-
Ingolf
HTH
> I can ask you for further questions...
Try first, then read documents or search on the forum, if still a problem then post on forum :)
Just keep trying!
-BB-
I´ve red, I´ve searched, I´ve written and I´ve tryed out - over and over. But there is no order execution, although I definitely can see on the chart, that a trading signal has taken place. Has anyone an idea, why the program doesn´t execute the orders? I´m to stupid.
It also doesn´t works, if I replace "MacdCurrent>(SignalCurrent+x)" with "MathAbs(MacdCurrent)>(MACDOpenLevel*Point)".
//+------------------------------------------------------------------+ //| MACD Sample.mq4 | //| Copyright © 2005, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ //extern double TakeProfit = 50; //extern double Lots = 0.1; //extern double TrailingStop = 30; extern double MACDOpenLevel=0.3; extern double MACDCloseLevel=0.2; extern double MATrendPeriod=9; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double MacdCurrent, MacdPrevious, SignalCurrent; double SignalPrevious, MaCurrent, MaPrevious; double Lots = AccountBalance()/2*0.0004; double x = 0.00005; //Signalabweichung vom Trigger double y = 0.00003; //Differenz MacdCurrent von MacdPrevious double z = 0; //Differenz EMA9 von Bid oder Ask int cnt, ticket, total; // initial data checks // it is important to make sure that the expert works with a normal // chart and the user did not make any mistakes setting external // variables (Lots, StopLoss, TakeProfit, // TrailingStop) in our case, we check TakeProfit // on a chart of less than 100 bars // if(Bars<100) // { // Print("bars less than 100"); //return(0); // } // if(TakeProfit<10) // { // Print("TakeProfit less than 10"); //return(0); // check TakeProfit // } // to simplify the coding and speed up access // data are put into internal variables MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0); MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1); total=OrdersTotal(); if(total<1) { // no opened orders identified //if(AccountFreeMargin()<(1000*Lots)) // { // Print("We have no money. Free Margin = ", AccountFreeMargin()); // return(0); // } // check for long position (BUY) possibility if((MacdPrevious<SignalPrevious) && (MacdCurrent>(SignalCurrent+x))) { OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"BUY",0,0,CLR_NONE); 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); } // check for short position (SELL) possibility if((MacdPrevious>SignalPrevious) && (MacdCurrent<(SignalCurrent-x))) { OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"SELL",0,0,CLR_NONE); 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); } // it is important to enter the market correctly, // but it is more important to exit it correctly... for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if(MacdCurrent<(MacdPrevious-y)) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position return(0); // exit } // check for trailing stop //if(TrailingStop>0) // { // if(Bid-OrderOpenPrice()>Point*TrailingStop) // { // if(OrderStopLoss()<Bid-Point*TrailingStop) // { // OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); // return(0); // } // } // } } else // go to short position { // should it be closed? if(MacdCurrent>(MacdPrevious+y)) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } if(Bid>(MaCurrent+z)) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } // check for trailing stop // if(TrailingStop>0) // { // if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) // { // if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) // { // OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); // return(0); // } // } // } } } } return(0); } // the end.
(Perhaps I should look for an other possibility to trade automated.)
Ingolf
You're clearly not stupid - your code efforts so far tell us that!
This is tricky stuff...
---
Do a check for me - change the code line to this, run a back-test & look in the Journal for the value going into Lots - may be an odd amount?
// OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"BUY",0,0,CLR_NONE); Print("My lot size is: ", Lots);
Keep trying!
-BB-
Ingolf
You're clearly not stupid - your code efforts so far tell us that!
This is tricky stuff...
---
Do a check for me - change the code line to this, run a back-test & look in the Journal for the value going into Lots - may be an odd amount?
// OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"BUY",0,0,CLR_NONE); Print("My lot size is: ", Lots);
Keep trying!
-BB-
Hello BB,
thanks for the code. Error code was 131 - Invalid trade volume.
I have swoped "double Lots = (AccountBalance()/2)*0.0004;" with "double Lots = 0.1" Now it works.
What a pity, it would be a good relation between Balance and Lots.
Nevertheless the program is functioned only from time to time. If it is because only a demo version?
Ingolf
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I´m looking for an EA, who would me helps and programs this:
at first:
- it can always only 1 (one) order be open
- trading on chart "Symbol ()"
- timeframe used on the chart
- LOT = 1.0 (should be variable)
furthermore:
- OP_BUY:
- if ((MacdPrevious < SignalPrevious) && (MacdCurrent > (SignalCurrent + x))) whereas x = percentage or decimal from SignalCurrent
I believe in other languages is therefore the command "if crossup".
- Close OP_BUY (close means, the open long position will dissolved through a conversely trade withe the same amount) :
- if exist an open long position and (MacdCurrent < (MacdPrevious - y)) whereas y = percentage or decimal from MacdPrevious
- OP_SELL
- if ((MacdPrevious > SignalPrevious) && (MacdCurrent < (SignalCurrent - x)))
I believe in other languages is therefore the command "if crossdown".
- Close OP_SELL
a) if exist an open short position and (MacdCurrent > (MacdPrevious + y))
b) if (Bid > (EMA + z)) whereas z = percentage or decimal from EMA
Bid means the bid price of currency, EMA means Exponential Moving Average. The period of EMA must be selectable.
Function from b) must be disconnectible (means the function must be closable).
Please tell me, how much it is. I would like to pay with PayPal. Would it be o.k.?
sincerely
Ingolf