Simple coding question

 
What am I doing wrong with the following code that I don't get the
right results?

if (ma20>=ma34 && (ma20-ma100<0.0004))
maval= 1;
else
if (ma20<=ma34 && (ma100-ma20<0.0004))
maval=-1;
else
maval= 0;

if (Close-ma100<0.0007)
val=1;
else
if (ma100-Close<0.0007)
val=-1;
else
val=0;

in the first case I want ma20 to be above ma34 AND that the difference
between ma20 and ma100 to be less than 4 pips for maval to be 1

in the second case I want that the difference between actual price (or
close price) and ma100 to be less than 7 pips for val to be 1
 
What am I doing wrong with the following code that I don't get the
right results?
Firstly, try
if (ma20 >= ma34 && ma20 - ma100 < 4 * Point)

and

if (Close - ma100 < 7 * Point)

if you're talking pips indeed.
Secondly, what are the wrong results and why do you think they're incorrect? How would we know what to suggest?

 
Looks okay to me. Try adding Print commands with your values and check the output to see if something is different than what you expect.


Markus
 
Thanks for your input guys

I changed the code but for example I've got one case where ma20 - ma100 = 4 pips and it writes maval as 1 and it should only do so if it was 3 ou less pips?
 
Thanks for your input guys

I changed the code but for example I've got one case where ma20 - ma100 = 4 pips and it writes maval as 1 and it should only do so if it was 3 ou less pips?


I would enclose all arithmetics in the parentheses before evaluating it.
i.e. (ma20-ma100) < 4

if (ma20<=ma34 && (ma100-ma20<0.0004))
Like this
if (ma20<=ma34 && ((ma100-ma20)<0.0004))
 
Thanks for your input guys

I changed the code but for example I've got one case where ma20 - ma100 = 4 pips and it writes maval as 1 and it should only do so if it was 3 ou less pips?

Beleive me, there are a lot of issues that can affect the results, e.g. double to int conversion in your example here.
There is no way to give a good suggestion without the code provided.
 
hey sub, I think you got it :)

gonna check other situations to confirm
 
well, apparently not, I made an error in writing the code

Well, here goes the code (Shimodax knows it back and forth; the code has redundant bits but they dont affect the outcome):


//+------------------------------------------------------------------+
//| RD.COmbo.mq4 |
//| Shimodax |
//| |
//+------------------------------------------------------------------+

#property copyright "Shimodax & RickyD"
#property link "http://www.strategybuilderfx.com/forums/showthread.php?t=15187"


#property indicator_separate_window

#property indicator_buffers 4
#property indicator_color1 Gray
#property indicator_color2 OrangeRed
#property indicator_color3 LawnGreen
#property indicator_color4 Gold

#property indicator_maximum 1
#property indicator_minimum -1

#property indicator_level1 1
#property indicator_level2 -1

//---- buffers
extern bool DoAlertForEntry= false;
extern bool DoAlertForExit= false;
extern int HistorySize= 5000;
extern int ColorThreshold= 1;
extern bool DebugLogger= false;
extern bool DebugLoggerData= false;

double LongSignalBuffer[],
ShortSignalBuffer[],
NeutralBuffer[],
SignalBuffer[];

double Osc[],
Osct3[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexBuffer(0,NeutralBuffer);
SetIndexEmptyValue(0, 0.0);

SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexBuffer(1,ShortSignalBuffer);
SetIndexEmptyValue(1, 0.0);

SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexBuffer(2,LongSignalBuffer);
SetIndexEmptyValue(2, 0.0);

SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,SignalBuffer);
// SetIndexEmptyValue(3, 0.0);

IndicatorShortName("RD-Combo()= ");

return(0);
}



int start()
{
static bool didentryalert= false, didexitalert= false;
int counted_bars= IndicatorCounted(),
lastbar, result;

if (Bars<=100)
return(0);

if (counted_bars>0)
counted_bars--;

lastbar= Bars - counted_bars;
lastbar= MathMin(HistorySize, lastbar);

//
// precalc forecast oscillater array
//
ArrayResize(Osc, Bars);
ArrayResize(Osct3, Bars);
ForecastOsc(0, Bars, Osc, Osct3, 15, 3, 0.7);

//
// check for signals
//
Combo(0, lastbar, LongSignalBuffer, ShortSignalBuffer, NeutralBuffer, SignalBuffer, ColorThreshold);



//
// do alerting
//
if (DoAlertForEntry>0 && SignalBuffer[0]!=0 && SignalBuffer[1]==0) {
if (!didentryalert) {
Alert("RD signals trade entry on ", Symbol(),"/",Period());
didentryalert= true;
}
}
else {
didentryalert= false;
}

if (DoAlertForExit>0 && SignalBuffer[0]==0 && SignalBuffer[1]!=0) {
if (!didexitalert) {
Alert("RD signals trade exit on ", Symbol(),"/",Period());
didexitalert= true;
}
}
else {
didexitalert= false;
}

return (0);
}




//+------------------------------------------------------------------+
//| Combo Indicator (All-In-One signal) |
//+------------------------------------------------------------------+
int Combo(int offset, int lastbar, double &longsignalbuf[], double &shortsignalbuf[], double &neutralbuffer[], double &signalbuffer[], int colthreshold)
{

int val,
lookupidx= 0,
signalstate= 0;

double
ma20,
ma34,
ma100;

if (lastbar>Bars)
lastbar= Bars;


for (int i= lastbar+1; i>=offset; i--) {

lookupidx= i;

ma20= iMA(NULL, 0,25, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma34= iMA(NULL, 0,200, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma100= iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, lookupidx);

//
// calc entry signal

//

int maval= 0;


if (ma20>=ma34 && ((ma100-ma20)< 4 * Point)) maval= 1;
else
if (ma20<=ma34 && ((ma100-ma20)< 4 * Point)) maval=-1;
else
maval= 0;



val= maval;


//
// exit signal
//
if (signalstate!=0) {

if (signalstate==1 && (rvimain<0 || rvisignal-rvimain>0.2)) {
signalstate= 0;
}

if (signalstate==-1 && (rvimain>0 || rvimain-rvisignal>0.2)) {
signalstate= 0;
}

if (DebugLogger)
Print(TimeToStr(Time[i]), " #", i, " rvimain= ", rvimain, ", rvisignal= ", rvisignal, ", diff= ", DoubleToStr(rvimain-rvisignal,2),
" => signalstate", signalstate);
}


if (i<=lastbar) { // ignore the first few loops that were only used to catch previous signal state

if (DebugLogger || DebugLoggerData){
if (DebugLoggerData) {
Print(TimeToStr(Time[i]), " # --------------------------------------------------------------------------");
Print(TimeToStr(Time[i]), " #", i, " MA 5/20/100/200", ma5, "/", ma20, "/", ma34, "/", ma100);
Print(TimeToStr(Time[i]), " #", i, " CCI ", cci, ", RVI ", rvimain, "/", rvisignal);
Print(TimeToStr(Time[i]), " #", i, " ADX(now) main/+DI/-DI ", adxmain, "/", adxplus, "/", adxminus);
Print(TimeToStr(Time[i]), " #", i, " ADX(prev) main/+DI/-DI ", adxmain2, "/", adxplus2, "/", adxminus2);
Print(TimeToStr(Time[i]), " #", i, " Forecast blue/red ", fcblue, "/", fcred);
}

if (DebugLogger || DebugLoggerData)
Print(TimeToStr(Time[i]), " #", i, " MAtrend(", maval, ") + CCI(", ccival, ") + FC(", fcval,
") + RVItrend( ", rvival, ") + ADXtrend(", adxval, ") => ", val, " # trade state =", signalstate);
}


longsignalbuf[i]= 0;
shortsignalbuf[i]= 0;
neutralbuffer[i]= 0;

if (val>=colthreshold) {
longsignalbuf[i]= val;
signalstate= 1;
}
else
if (val<=-colthreshold) {
shortsignalbuf[i]= val;
signalstate= -1;
}
else
neutralbuffer[i]= val;

signalbuffer[i]= 2*signalstate;
}
}

return (signalstate);
}




//+------------------------------------------------------------------+
//| Calc forecast buffers from lastbar down to offset |
//+------------------------------------------------------------------+
void ForecastOsc(int offset, int lastbar, double &osc[], double &osct3[], int regress, int t3, double b)
{

int shift, length;
double b2=b*b,
b3=b2*b,
c1=-b3,
c2=(3*(b2+b3)),
c3=-3*(2*b2+b+b3),
c4=(1+3*b+b3+3*b2),
n = 1 + 0.5*(t3-1),
w1 = 2 / (n + 1),
w2 = 1 - w1,
wt,forecastosc,t3_fosc,sum,
e1,e2,e3,e4,e5,e6,tmp,tmp2;

lastbar= MathMin(Bars-31-regress, lastbar);

for (shift= lastbar+30; shift>=offset; shift--) {

length= regress;
sum = 0;
for (int i = length; i>0; i--) {
tmp = length+1;
tmp = tmp/3;
tmp2 = i;
tmp = tmp2 - tmp;
sum = sum + tmp*Close[shift+length-i];
}
tmp = length;

wt = sum*6/(tmp*(tmp+1));

forecastosc= (Close[shift]-wt)/wt*100;

e1 = w1*forecastosc + w2*e1;
e2 = w1*e1 + w2*e2;
e3 = w1*e2 + w2*e3;
e4 = w1*e3 + w2*e4;
e5 = w1*e4 + w2*e5;
e6 = w1*e5 + w2*e6;

t3_fosc = c1*e6 + c2*e5 + c3*e4 + c4*e3;

if (shift<=lastbar) { // don't put swing in cycle into the signal buffers
osc[shift] = forecastosc;
osct3[shift] = t3_fosc;
}

}

return(0);
}
 
ma20= iMA(NULL, 0,25, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma34= iMA(NULL, 0,200, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma100= iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, lookupidx);


The periods in iMA (third parameter) for ma20 and ma34 look suspicious :)
 
ma20= iMA(NULL, 0,25, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma34= iMA(NULL, 0,200, 0, MODE_EMA, PRICE_CLOSE, lookupidx);
ma100= iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, lookupidx);


The periods in iMA (third parameter) for ma20 and ma34 look suspicious :)


I know but that's ok, I called ma34 to the ema200
 
I think the brackets are not necessary, but what Irton says is true.

You may be running into slight rounding differences with double values. Double values do not always have a precise representation for the numbers which they are supposed to store.

If you have a case where you can reproduce it, try a similar approach there (just to make sure this works):

int ma1inpips= MathRound(ma1/Point,0);
int ma2inpips= MathRound(ma2/Point,0);

With integers you can check for precise values, e.g.
if ((ma1inpips-ma2inpips)<4)


Markus
Reason: