I have a problem with DoubleToStr

 

Hi

function to check if there is a market trend - based on the intersection of the long and short moving average:

int IsAnyTrend()

{

double LongMa1, LongMa2, ShortMa1, ShortMa2;

LongMa1=iMA(NULL,0,LongMovingPeriod,0,MODE_EMA,PRICE_CLOSE,1);

ShortMa1=iMA(NULL,0,ShortMovingPeriod,0,MODE_EMA,PRICE_CLOSE,1);

LongMa2=iMA(NULL,0,LongMovingPeriod,0,MODE_EMA,PRICE_CLOSE,2);

ShortMa2=iMA(NULL,0,ShortMovingPeriod,0,MODE_EMA,PRICE_CLOSE,2);


if(ShortMa2>LongMa2 && ShortMa1<LongMa1) trendinfo = -1;


if(ShortMa2<LongMa2 && ShortMa1>LongMa1) trendinfo = 1;



Print("Trend=",DoubleToStr(trendinfo,0));

return(trendinfo);

}


The problem is that despite the trend, the memory DoubleToStr is reset after 15 candles and exposed to 0 What could be causing this?

 
Please edit your post . . .


Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.

 
danielo87:

The problem is that despite the trend, the memory DoubleToStr is reset after 15 candles and exposed to 0 What could be causing this?


DoubleToStr() is a function not a variable. trendinfo is your variable, so if it is 0 then it is 0.x Try adding a print for your Short and Long MAs to see if they are equal.
 
 
It seems to me that the value should be saved to a file and read it.
 
danielo87:
It seems to me that the value should be saved to a file and read it.

Pointless . . . . print these values ShortMa2, LongMa2, ShortMa1, LongMa1

Neither of these tests will be true so trendinfo will be 0.0

if(ShortMa2>LongMa2 && ShortMa1<LongMa1) trendinfo = -1;

if(ShortMa2<LongMa2 && ShortMa1>LongMa1) trendinfo = 1;
 
danielo87:
It seems to me that the value should be saved to a file and read it.

Why are you using doubletostr, when your function returns an integer value?....

I suspect that there are conditions that you haven't considered, which is why a zero value is being returned.

You could change your logic to something like this:

if(condition 1 &&  condition 2){

        return(1);

}else if( condition 3 &&  condition 4){

        return(-1);

}else{

        print(ma1 value); 

        print(ma2 value); 

        print(ma3 value); 

        print(ma4 value);  

} //This would allow you to determine which other conditions you need to consider...
Reason: