Unexpected result from cum function

image

Hi guys,
I am having problems with results of the cum function.
The first is correct
image
The second is wrong
image

I added a setbarsrequired just to try and understand what is happening but doesn't help.
In the second the downchange value is correct but is not accumulated with the cum function. Even if the one value was the only one (it shouldn't be) it should at least be in the accumDownChange in the next statement.

I haven't posted the entire code at this stage as it is quite large and has a few #includes.

Any tips will be appreciated.
Cheers,
Gary

 SetBarsRequired( sbrAll); // probably shouldn't need this but.................
   
_TRACE("============ at the high ======"); 
_TRACE("downTurn " + downTurn);  
upCount = Cum( downTurn );  
_TRACE("upCount " + upCount);    
upChange = IIf(downTurn, Close - ValueWhen(upTurn,close), 0); 
_TRACE("upChange " + upChange);  
accumUpChange = Cum(upChange);    
_TRACE("accumUpChange " + accumUpChange );    
avgUpChange = IIf(downTurn, accumUpChange / upCount, 0);   
_TRACE("avgUpChange " + avgUpChange );  
   
_TRACE("============ at the low ======"); 
_TRACE("upTurn " + upTurn);  
downCount = cum( upTurn );  
_TRACE("downCount " + downCount);  
downChange = IIf(upTurn, ValueWhen(downTurn, close) - close, 0); 
_TRACE("downChange " + downChange);  // shows correct value
accumDownChange = Cum(downChange);   
_TRACE("accumDownChange " + accumDownChange );        // shows incorrect 0 value
avgDownChange = IIf(upTurn, accumDownChange / downCount, 0);   
_TRACE("avgDownChange " + avgDownChange );  
_TRACE(" " ); 
 
 
_TRACE("=================="); 
_TRACE("ValueWhen(downTurn, avgUpChange) "  + ValueWhen(downTurn, avgUpChange)); 
_TRACE("ValueWhen(upturn, avgDownChange) "  + ValueWhen(upturn, avgDownChange)); 
avgChange = (ValueWhen(downTurn, avgUpChange) + ValueWhen(upturn, avgDownChange)) / 2;       
_TRACE("avgChange " + avgChange );  

@garydown

For someone to help you properly, they need a functioning code to test / debug.

Your question produces this,

Most users will look at that and give up attempts at helping.

Best of luck.

I appreciate your tip.
In this case the questions boils down to 3 lines of code.

_TRACE("downChange " + downChange);  // shows correct value
accumDownChange = Cum(downChange);   
_TRACE("accumDownChange " + accumDownChange );        // shows incorrect 0 value

if the first trace shows a correct value why would it not be added by the cum function in the next statement and then shown in the 2nd trace..

I will put together a more usable code example and then post it.

when dealing with arrays, use LastValue() to ensure you get the last value or select a specific index with [ ] so you know what you are comparing output with.

@nsm51 is correct, _TRACE() when passed an array acts a bit like SelectedValue(), we dont know if your 'downChange' array has a zero values.
Also note that if 'downChange' has Nulls (other than at the start) then Cum() will not be able to sum the full series. Perhaps try Cum( Nz( downChange ) ); to do a quick check.

1 Like

Thanks guys,
I changed to using ref()

downChange = IIf(upTurn, ref(Close, sinceDownTurn * -1) - Close, 0); 

instead of valueWhen

downChange = IIf(upTurn, ValueWhen(downTurn, close) - close, 0);

and it is good now
image

Thanks for your assistance,
Gary.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.