smika: maybe it is a slippage issue? slippage_close is set to 3.
|
|
|
Thanks for your great reply. This helpde me a lot. I do understand why things doesn't work, but I can't solve this issue, so maybe you can help me again.
This is the code I have as optiions in the menu:
input string SL_Settings = "--------------------< SL Management >--------------------";//SL Management Settings ............................................................................................................ input bool ema3over5 = 1 ; // SL when 3 over 5 crossed on open of the next candle input bool ema3over15 = 1; // SL when 3 over 15 crossed on current candle input bool tdicrossrg = 1; // SL when green crossed red candle close input bool tdicrossrb = 1; // SL when green crossed blue candle close
In the global code:
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// double ClosingArray[100], point=0.0001, DrawDowns=0, DDBuffer=0, Drawdown=0, Sloss = 0, Tprof = 0;int Pip=1, lotdigit=0;string text[26], prefix="";bool Buy=0, Sell=0; datetime Bar = 0; datetime now; bool IsNewBar() { if (now != Time[0]) { now = Time[0]; return (true); } return (false); } //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ void OnInit() { ArrayResize(text,26);Bar = 0; if(MarketInfo(Symbol(),MODE_LOTSTEP)==1) lotdigit=0; if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) lotdigit=1; if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) lotdigit=2; int digits=(int)MarketInfo(Symbol(),MODE_DIGITS); if(digits==4 || (Bid<1000 && digits==2)){ Pip=1;} else Pip=10; if(digits<=1) point = 1; //CFD & Indexes if(digits==4 || digits==5) point = 0.0001; if((digits==2 || digits==3) && Bid>1000) point = 1; if((digits==2 || digits==3) && Bid<1000) point = 0.01; if(StringFind(NULL,"XAU")>-1 || StringFind(NULL,"xau")>-1 || StringFind(NULL,"GOLD")>-1) point = 0.1;//Gold if(IsTesting()) prefix="Test"+IntegerToString(MagicNumber)+Symbol();else prefix=IntegerToString(MagicNumber)+Symbol(); return; }
Expert tick funtion:
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //+------------------------------------------------------------------+ // Check History... if(Bars < 10){ Print("Not enough bars for working the EA");return;} //+------------------------------------------------------------------+ Entry();Emaclose();Emacurrent();tdicurrent();tdigreenblue();
SL configs:
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Close when 3 crossed the 5 at begining of a new bar | //+------------------------------------------------------------------+ void Emaclose() { if(!ema3over5) return; double ema3prev; double ema5prev; ema3prev = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_OPEN, 0); ema5prev = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, 0); if (IsNewBar()) for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY) { if(ema3prev < ema5prev) bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Red)) Alert("the order 3over5 isn't close because Err no. ", GetLastError()); else Alert("Order closed by 3 over 5 succesfully"); } if(OrderType() == OP_SELL) { if(ema3prev > ema5prev) bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("the order 30ver5 isn't close because Err no. ", GetLastError()); else Alert("Order closed by 3 over 5 succesfully"); } } }} //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Close when 3 crossed the 15 on current candle | //+------------------------------------------------------------------+ void Emacurrent() { if(!ema3over15) return; double ema3; double ema15; ema3 = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0); ema15 = iMA(NULL, 0, 15, 0, MODE_EMA, PRICE_CLOSE, 0); for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY) { if(ema3 < ema15) bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Red)) Alert("the order 3over15 isn't close because Err no. ", GetLastError()); else Alert("Order closed by 3 over 15 succesfully"); } if(OrderType() == OP_SELL) { if(ema3 > ema15) bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("the order 3over15 isn't close because Err no. ", GetLastError()); else Alert("Order closed by 3 over 15 succesfully"); } } }} //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Close when current green crosses red | //+------------------------------------------------------------------+ void tdicurrent() { if(!tdicrossrg) return; double curTdiGreen, curTdiRed; double prevTdiGreen, prevTdiRed; curTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,0); prevTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,1); curTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,0); prevTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,1); if (IsNewBar()) for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY) { if ((curTdiGreen<curTdiRed) && (prevTdiGreen>prevTdiRed)) bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Red)) Alert("the order GR isn't close because Err no. ", GetLastError()); else Alert("Order closed by RG succesfully"); } if(OrderType() == OP_SELL) { if ((curTdiGreen>curTdiRed)&&(prevTdiGreen<prevTdiRed)) bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("the order GR isn't close because Err no. ", GetLastError()); else Alert("Order closed by RG succesfully"); } } }} //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| Close when current green crosses red | //+------------------------------------------------------------------+ void tdigreenblue() { if(!tdicrossrb) return; double curTdiGreen, prevTdiGreen; double curTdiBlueUp, curTdiBlueDown; double prevTdiBlueUp, prevTdiBlueDown; curTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,0); prevTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,1); curTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,0); prevTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,1); curTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,0); prevTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,1); if (IsNewBar()) for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY) { if ((curTdiGreen<curTdiBlueUp) && (prevTdiGreen>prevTdiBlueUp)) bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Red)) Alert("the order GB isn't close because Err no. ", GetLastError()); else Alert("Order closed GB succesfully"); } if(OrderType() == OP_SELL) { if ((curTdiGreen>curTdiBlueDown)&&(prevTdiGreen<prevTdiBlueDown)) bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("the order GB isn't close because Err no. ", GetLastError()); else Alert("Order closed by GB succesfully"); } } }}
So what's my probem? When I Set all the SL settings true, all my orders will close only when 3 over 15 EMA is hit. When I set 3 over 15 to false, All the orders will be closed by 3 over 5 EMA. When I set also this one to false, all the orders will be clossed by TDI red over green. How can I tell the EA, that when all the SL settings are true, It can close on all those settings and not just only one? And why the EA will use this order, and not another order?
Smika
smika:
And why the EA will use this order, and not another order? bool Oc = OrderClose(OrderTicket(), OrderLots(), Ask, slippage_close, Red); // close buy order if(!OrderClose(OrderTicket(), OrderLots(), Ask, slippage_close, Red)) |
|
|
Thanks for the quick reply! This looks great. I wil try this. I learn a lot of it.
Thanks for the quick reply! This looks great. I wil try this. I learn a lot of it.
I changes the code, but now it doesn't use any SL :-)
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| SL Management | //+------------------------------------------------------------------+ void slmanagement() { double ema3open, ema5open, ema3, ema15, curTdiGreen, curTdiRed, prevTdiGreen, prevTdiRed, curTdiBlueUp, curTdiBlueDown, prevTdiBlueUp, prevTdiBlueDown; ema3open = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_OPEN, 0); ema5open = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, 0); ema3 = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0); ema15 = iMA(NULL, 0, 15, 0, MODE_EMA, PRICE_CLOSE, 0); curTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,0); prevTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,1); curTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,0); prevTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,1); curTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,0); prevTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,1); curTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,0); prevTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,1); bool closeBuy1 = !ema3over5 || (IsNewBar() && ema3open < ema5open); bool closeBuy2 = !ema3over15 || ema3 < ema15; bool closeBuy3 = !tdicrossrg || (IsNewBar() && (curTdiGreen<curTdiRed) && (prevTdiGreen>prevTdiRed)); bool closeBuy4 = !tdicrossgb || (IsNewBar() && (curTdiGreen<curTdiBlueUp) && (prevTdiGreen>prevTdiBlueUp)); bool closeBuyOrders = closeBuy1 && closeBuy2 && closeBuy3 && closeBuy4; bool closeSell1 = !ema3over5 || (IsNewBar() && ema3open > ema5open); bool closeSell2 = !ema3over15 || ema3 > ema15; bool closeSell3 = !tdicrossrg || (IsNewBar() && (curTdiGreen>curTdiRed) && (prevTdiGreen<prevTdiRed)); bool closeSell4 = !tdicrossgb || (IsNewBar() && (curTdiGreen>curTdiBlueUp) && (prevTdiGreen<prevTdiBlueUp)); bool closeSellOrders = closeSell1 && closeSell2 && closeSell3 && closeSell4; for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY && closeBuyOrders) { bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue)) Alert("The order isn't close because Err no. ", GetLastError()); else Alert("Order closed succesfully"); } if(OrderType() == OP_SELL && closeSellOrders) { bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("The order isn't close because Err no. ", GetLastError()); else Alert("Order closed succesfully"); } } }}
Maybe I need some sleep first :-), but can't find the issue yet. I also cganed void on tick: Entry();slmanagement();. I also share this so other people can learn from it.
I changes the code, but now it doesn't use any SL :-)
Maybe I need some sleep first :-), but can't find the issue yet. I also cganed void on tick: Entry();slmanagement();. I also share this so other people can learn from it.
If I change bool closeBuyOrders = closeBuy1 && closeBuy2 && closeBuy3 && closeBuy4; to bool closeBuyOrders = closeBuy1 || closeBuy2 || closeBuy3 || closeBuy4; It is closing my orders again. But again, only for 3 over 15. Not for the other conditions. It makes me crazy :-)
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH// //+------------------------------------------------------------------+ //| SL Management | //+------------------------------------------------------------------+ void Slmanagement() { double ema3open, ema5open, ema3, ema15; double curTdiGreen, curTdiRed, prevTdiGreen, prevTdiRed, curTdiBlueUp, curTdiBlueDown, prevTdiBlueUp, prevTdiBlueDown; ema3open = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_OPEN, 0); ema5open = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, 0); ema3 = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0); ema15 = iMA(NULL, 0, 15, 0, MODE_EMA, PRICE_CLOSE, 0); curTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,0); prevTdiBlueUp = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,1,1); curTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,0); prevTdiBlueDown = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,3,1); curTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,0); prevTdiGreen = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,4,1); curTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,0); prevTdiRed = iCustom(NULL,0,"TDI Red Green.ex4",10,5,34,2,1,7,0,5,1); bool closeBuy1 = (!ema3over5 || (ema3open < ema5open)); bool closeBuy2 = (!ema3over15 || (ema3 < ema15)); bool closeBuy3 = (!tdicrossrg || ((curTdiGreen<curTdiRed) && (prevTdiGreen>prevTdiRed))); bool closeBuy4 = (!tdicrossgb || ((curTdiGreen<curTdiBlueUp) && (prevTdiGreen>prevTdiBlueUp))); bool closeBuyOrders = (closeBuy1 || closeBuy2 || closeBuy3 || closeBuy4); bool closeSell1 = (!ema3over5 || (ema3open > ema5open)); bool closeSell2 = (!ema3over15 || (ema3 > ema15)); bool closeSell3 = (!tdicrossrg || ((curTdiGreen>curTdiRed) && (prevTdiGreen<prevTdiRed))); bool closeSell4 = (!tdicrossgb || ((curTdiGreen>curTdiBlueUp) && (prevTdiGreen<prevTdiBlueUp))); bool closeSellOrders = (closeSell1 || closeSell2 || closeSell3 || closeSell4); for(int i = 0;i < OrdersTotal();i++) { bool Os = OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber)) { if(OrderType() == OP_BUY && closeBuyOrders) { bool Oc = OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue); // close buy order if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage_close,Blue)) Alert("The order isn't close because Err no. ", GetLastError()); else Alert("Order closed succesfully"); } if(OrderType() == OP_SELL && closeSellOrders) { bool Oc = OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red); // close sell order if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage_close,Red)) Alert("The order isn't close because Err no. ", GetLastError()); else Alert("Order closed succesfully"); } } }}When I set 1 of the 4 close options to false, it doens't work anymore. What happens then, is that the EA opens an order and then closed the same time. How to solve this?
if ema3over5 is false, closeBuy1 is true
try

- 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,
I am trying to vreate an EA. 1 of the things the EA ust do, Is to close an order when the 3 over 5 EMA conditions are met on the open of a new candle. When I try this in the strategy tester, i looks fine, but on a live account it doesn't work. Maybe it is a slippage issue? Or is the code wrong?
The parameter slippage_close is set to 3. Maybe someone can give me some hints to let it work.