You read the iCustom values when a new bar forms. That's ok, but in this case you need to look at the previous bar, not the current one. That is for the shift value 0 you will have Open[0], High[0], Low[0], Close[0] all at the same value, so the indicator loop quits without setting any flags (i==0 -> openRange==closeRange).
Ok I thought that's the only error but you got at least another one that is here:
if ( rates_total-prev_calculated==1 ) i=0; //New bar
Your indicator will not be able to do the right calculation when i is 0 for the reason I mentioned earlier. Either try with i=1 so that the last bar is handled retrospectively, or allow your indicator to work on all ticks (that is remove this line where you've put '//same bar'.) HTH
I haven't tested this in the backtester but it enters orders on a live price feed.
Indicator: I changed EMPTY_VALUE to 0. Just a personal preference makes it easier when placing orders with iCustom
#property copyright "Joey van Horen" #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 8 #property indicator_plots 8 #property indicator_label1 "FibBuy" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrYellowGreen #property indicator_style1 STYLE_SOLID #property indicator_width1 2 #property indicator_label2 "FibSell" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 2 //--- plot M15_20 #property indicator_label3 "yl61" #property indicator_type3 DRAW_LINE #property indicator_color3 clrWhite #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- plot M30_20 #property indicator_label4 "yl" #property indicator_type4 DRAW_LINE #property indicator_color4 clrWhite #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- plot H1_20 #property indicator_label5 "y38" #property indicator_type5 DRAW_LINE #property indicator_color5 clrWhite #property indicator_style5 STYLE_SOLID #property indicator_width5 1 //--- plot H4_20 #property indicator_label6 "y61" #property indicator_type6 DRAW_LINE #property indicator_color6 clrWhite #property indicator_style6 STYLE_SOLID #property indicator_width6 1 //--- plot D1_20 #property indicator_label7 "yh" #property indicator_type7 DRAW_LINE #property indicator_color7 clrWhite #property indicator_style7 STYLE_SOLID #property indicator_width7 1 //--- plot W1_20 #property indicator_label8 "yh61" #property indicator_type8 DRAW_LINE #property indicator_color8 clrWhite #property indicator_style8 STYLE_SOLID #property indicator_width8 1 extern bool DEBUG=false; //indicator buffers double fibBuy[]; double fibSell[]; // Fibo Levels double yl61[]; double yl[]; double y38[]; double y61[]; double yh[]; double yh61[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping IndicatorBuffers(8); IndicatorDigits(5); SetIndexArrow(0,233);//uparrow SetIndexArrow(1,234);//downarrow SetIndexBuffer(0,fibBuy); SetIndexBuffer(1,fibSell); SetIndexBuffer(2,yl61); SetIndexBuffer(3,yl); SetIndexBuffer(4,y38); SetIndexBuffer(5,y61); SetIndexBuffer(6,yh); SetIndexBuffer(7,yh61); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- int open_range, close_range; // Opening/Closing relative to signal lines int i=0; if (rates_total==prev_calculated) return(rates_total); //Same bar if ( rates_total-prev_calculated==1 ) i=0; //New bar if (prev_calculated==0) i=rates_total-2; //else i=0; if (DEBUG) Print ("First Time thru? prev_calculated=",prev_calculated," i=",i); while (i>=0) { if (DEBUG) Print ("Calling Fibonacci Levels for Candle ",i); int shift=iBarShift(NULL,PERIOD_D1,Time[i],true); if (shift==-1) return(rates_total); datetime startoflastday=iTime(NULL,PERIOD_D1,shift+1); datetime endoflastday=iTime(NULL,PERIOD_D1,shift); int countofbars=Bars(NULL,PERIOD_H1,startoflastday,endoflastday); if (countofbars>=12) { yh[i] =iHigh(NULL,PERIOD_D1,shift+1); yl[i] =iLow(NULL,PERIOD_D1,shift+1); } else { yh[i] =iHigh(NULL,PERIOD_D1,shift+2); yl[i] =iLow(NULL,PERIOD_D1,shift+2); } y38[i]=((yh[i]-yl[i])*0.382)+yl[i]; y61[i]=((yh[i]-yl[i])*0.618)+yl[i]; yh61[i]=((yh[i]-yl[i])*0.618)+yh[i]; yl61[i]=yl[i]-((yh[i]-yl[i])*0.618); if (DEBUG) Print ("YH ",yh[i]," YL ",yl[i], " Y38 ",y38[i], " Y61 ",y61[i], " Y161 ",yh61[i], " Y061 ",yl61[i]); // =============================================== // Process candles and raise signals as needed // =============================================== open_range=fiboRange(open[i], yl61[i], yl[i], y38[i], y61[i], yh[i], yh61[i]); close_range=fiboRange(close[i], yl61[i], yl[i], y38[i], y61[i], yh[i], yh61[i]); if (DEBUG) Print("Candle ",i," Opening Price is ",open[i]," Closing Price is ",close[i]); if (DEBUG) Print("Candle ",i," Opening Range is ",open_range," Closing Range is ",close_range); if ((close_range > open_range)&&(open_range>0)) { fibSell[i]=High[i]; } else fibSell[i]=0; //Changed from EMPTY_VALUE to 0 if ((close_range < open_range)&&(open_range<6)) { fibBuy[i]=Low[i]; } else fibBuy[i]=0; //Changed from EMPTY_VALUE to 0 if (DEBUG && fibSell[i] != 0) Print("Candle ",i," Fibonacci Sell Symbol is in place"); //Changed from EMPTY_VALUE to 0 if (DEBUG && fibBuy[i] != 0) Print("Candle ",i," Fibonacci Buy Symbol is in place"); //Changed from EMPTY_VALUE to 0 i--; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate current Fibo "Range" for number. We need to compare //| start and current "ranges" to determine if price has moved across //| fibo lines //+------------------------------------------------------------------+ int fiboRange(double this_price, const double fibol61, const double fibol, const double fibo38, const double fibo61, const double fiboh, const double fiboh61) { int this_range=0; if (this_price < fibol61) { this_range = 0; } else if (this_price < fibol) { this_range = 1; } else if (this_price < fibo38) { this_range = 2; } else if (this_price < fibo61) { this_range = 3; } else if (this_price < fiboh) { this_range = 4; } else if (this_price < fiboh61) { this_range = 5; } else { this_range = 6; } return(this_range); }
EA: I made a few changes deleted a few things and shifted parts around. i have annotated some changes i made to help follow along.
//+------------------------------------------------------------------+ //| combining EA.mq4 | //| Joey van Horen | //| | //+------------------------------------------------------------------+ #property copyright "Joey van Horen based on Jim Dandy video's" #property version "1.00" #property strict extern double LotSize=0.1; extern double StopLoss=50; extern double TakeProfit=50; extern int MagicNumber=1234; double pips; extern bool DEBUG=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //automatically correct pips to be compatible with all currency pairs double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE); if(ticksize==0.00001 || ticksize==0.001) pips=ticksize*10; else pips=ticksize; return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(IsNewCandle())CheckForCombiTrade(); //will only run on each new candle } //Check or it is a new candle function bool IsNewCandle() { static int BarsOnChart=0; if (Bars==BarsOnChart) return (false); BarsOnChart=Bars; return (true); } void CheckForCombiTrade() { //+------------------------------------------------------------------+ //| indicator parameters | //+------------------------------------------------------------------+ //Fibonacci double fibBuy=iCustom(Symbol(),0,"FiboForFiboEA",0,1); //Changed it to check previous bar + current symbol and chart timeframe. double fibSell=iCustom(Symbol(),0,"FiboForFiboEA",1,1); //Changed it to check previous bar + current symbol and chart timeframe. Print ("fibBuy=",fibBuy, "& fibSell=",fibSell); double SellSL=(Ask+(StopLoss*pips)); double SellTP=(Ask-(TakeProfit*pips)); double BuySL=(Bid-(StopLoss*pips)); double BuyTP=(Bid+(TakeProfit*pips)); int buyticket=0; int sellticket=0; //------------Check No Open Positions On Symbol-------- int TotalOpenSymbolOrders=0; for(int i=0; i<OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY||OrderType()==OP_SELL) //If either Buys or Sells open on this symbol count them. { TotalOpenSymbolOrders++; //Adding up total amount of orders on this symbol. } } } //+------------------------------------------------------------------+ //| Sell combination direction 0 + order entry | //+------------------------------------------------------------------+ //Fibonacci if (fibSell!=0) //You're calling fibSell changed this from EMPTY_VALUE to 0 to make it easier. { if(TotalOpenSymbolOrders<1){ //This checks no other trades are open for this symbol //if(OpenOrdersThisPair(Symbol())==0) sellticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3*pips,SellSL,SellTP,NULL,MagicNumber,0,clrRed); // This places the sell order with SL and TP. Before, you placed the order then tried to modify it afterwards. No need just assign the SL and TP now. Also slippage 3*pips. if(sellticket>0) { Print("Order",sellticket,"was successfully placed."); } if(sellticket<0) { Print("Order",sellticket,"was NOT successfully placed.",GetLastError()); } } } //+------------------------------------------------------------------+ //| Buy combination direction 1 + order entry | //+------------------------------------------------------------------+ //Fibonacci if (fibBuy!=0) //You're calling fibBuy changed this from EMPTY_VALUE to 0 to make it easier. { if(TotalOpenSymbolOrders<1){ //This checks no other trades are open for this symbol //if(OpenOrdersThisPair(Symbol())==0) buyticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3*pips,BuySL,BuyTP,NULL,MagicNumber,0,clrOrange); // This places the buy order with SL and TP. Before, you placed the order then tried to modify it afterwards. No need just assign the SL and TP now. Also slippage 3*pips. if(buyticket>0) { Print("Order",buyticket,"was successfully placed."); } if(buyticket<0) { Print("Order",buyticket,"was NOT successfully placed.",GetLastError()); } } } } //+------------------------------------------------------------------+
Hope this helps. I have only given it a quick check that orders are being placed by changing the fibBuy i Custom last digit from 1 to (whatever number of candles back last buy arrow was). This way it enters the trade now and was doing that fine. However, you will need to leave it running to make sure it takes all trades correctly when the indicator plots the arrows at the time. I normally just use OrdersTotal instead of calculating the total number of orders of a particular symbol, perhaps someone else can verify that the TotalOpenSymbolOrders++ is correct.
I would also have a scan through the code to make sure there are no parts you don't want. I was only going from your code to try and guess what you wanted.
Anyway, it might help start you in the right direction or confuse the situation further.
Good Luck :)
if ( rates_total-prev_calculated==1 ) i=0; //New bar
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
New candle - MQL4 programming forum

- 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 have made a basic indicator that gives a buy or sell signal when Fibonacci levels are crossed.
I want to use iCustom in my EA to call for the first two buffers which should contain the value of fibBuy and fibSell.
The indicator itself works fine on the chart and when I check the data window it shows the value of fibBuy and fibSell. When conditions are not met, the value of fibBuy and fibSell remain empty like they are supposed to.
However when I call for buffer 0 or 1 in iCustom and run the strategy tester it does not work. If I call for any of the other buffers the values are being returned just fine.
Does somebody have any clue what the problem could be?