Only division line in there:
double StopLoss = (varHigh - varLow)/2;
You need to be prepared for the high and low being the same val, which results in a 0 after the subtraction.
Only division line in there:
double StopLoss = (varHigh - varLow)/2;
You need to be prepared for the high and low being the same val, which results in a 0 after the subtraction.
The high and low should never be the same value though. One is an MA applied to the high and one to the low of the weekly candle.
The high and low should never be the same value though. One is an MA applied to the high and one to the low of the weekly candle.
...And the code wouldn't generate an error even if they were the same value. 0/2 isn't division by zero, and won't trigger an error. It looks as though the problem must be in an area of the code which isn't included in your original post. Can't see anything in there which could lead to division by zero.
I just realised it was the indicator error not the EA.
This is the code, it must be the averaging I use:
//+------------------------------------------------------------------+ //| Weekly 2LWMAs TP SL Diff.mq4 | //| Copyright © 2009, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" #property indicator_chart_window int scaleX=5, scaleY=5, offsetX=0, offsetY=0, fontSize=10, corner=2, symbolCodeBuy=233, // a symbol code for a buy signal symbolCodeSell=234, // sell signal symbolCodeNoSignal=183; // no signal extern color signalBuyColor=Green, // color of the symbol of a buy signal signalSellColor=Tomato, // for a sell signal noSignalColor=Yellow, // no signal textColor=Black; // color of all writings //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { double varHigh = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0); double varLow = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0); ObjectCreate("signal",OBJ_LABEL,0,0,0,0,0); ObjectSet("signal",OBJPROP_CORNER,corner); ObjectSet("signal",OBJPROP_XDISTANCE,scaleX+offsetX); ObjectSet("signal",OBJPROP_YDISTANCE,scaleY+offsetY); ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor); //1 = up //0 = neutral //-1 = down if (GlobalVariableGet(Symbol()+"BiasDirection") == 1) { ObjectSetText("signal",CharToStr(symbolCodeBuy),fontSize,"Wingdings",signalBuyColor); } if (GlobalVariableGet(Symbol()+"BiasDirection") == 0) { ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor); } if (GlobalVariableGet(Symbol()+"BiasDirection") == -1) { ObjectSetText("signal",CharToStr(symbolCodeSell),fontSize,"Wingdings",signalSellColor); } //Average move beyond the LWMAs int i, Counted_bars; Counted_bars=IndicatorCounted(); i=Bars-Counted_bars-1; double varPriceExceededHigh=0; double varPriceExceededLow=0; double varPriceExceededHighCount=0; double varPriceExceededLowCount=0; double bufferHigh[]; double bufferLow[]; while(i>=0) { for (int j=19; j>=0; j--) { if (High[i+j] > iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j)) { //count this bar towards the total and add it to the var varPriceExceededHighCount++; varPriceExceededHigh= varPriceExceededHigh + (High[i+j] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j)); } if (Low[i+j] < iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j)) { varPriceExceededLowCount++; varPriceExceededLow= varPriceExceededLow + (iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j) - Low[i+j]); } } // for //calculate averages double varavHigh = varPriceExceededHigh/varPriceExceededHighCount; bufferHigh[i] = varavHigh; double varavLow = varPriceExceededLow/varPriceExceededLowCount; bufferLow[i] = varavLow; i--; } // while Comment ( "Difference: " + DoubleToStr(varHigh - varLow, 5) + "\nHigh: " + DoubleToStr(varHigh, 5) + "\nLow: " + DoubleToStr(varLow, 5) + "\nSL: " + DoubleToStr((varHigh - varLow)/2, 5) + "\nTrade size: " + DoubleToStr(AccountBalance()*0.0001, 2) + "\nAv. above high: " + DoubleToStr(varavHigh, 5) + "\nAv. below low: " + DoubleToStr(varavLow, 5) ); //---- return(0); } //+------------------------------------------------------------------+
This is the code, it must be the averaging I use:
SanMiguel you should build code to have a default value in the event that a divide by zero is analytically possible, something along the lines of the following:
//calculate averages double varavHigh = 9999.; // Assume a default value that is meaningful and relevant to the author if (varPriceExceededHighCount!=0) varavHigh = varPriceExceededHigh/varPriceExceededHighCount; // Only perform the division if the denominator is non-zero bufferHigh[i] = varavHigh; // bufferHigh[i] will equal 9999 (or whatever value the author elects to implement) if varPriceExceededHighCount == 0
SanMiguel you should build code to have a default value in the event that a divide by zero is analytically possible, something along the lines of the following:
Shouldn't this part of my code in the loop oprder make it analytically impossible?
varPriceExceededHighCount++;
- 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 a zero divide coming up in my experts journal.
Any obvious errors in the code below?