Adjusting the Range Period to the Maximum number of Bars in Exploration

Hi Guys,

I am trying to create an exploration for SharpeRatio of Stocks. So far this works well with a fixed range period. However, when I try to adjust the range period to the maximum number of bars available for the stock (using BarIndex() as shown in the code) it shows me an error "range must be greater than zero and not null"

Would there be a workaround to display each stock's Sharpe Ratio individually using it's maximum historical bars? Many thanks in advance.

//SharpeRange		= LastValue(BarIndex());		// This does not work
SharpeRange		= 30;								// This works
SDF 			= StDev(ROC(Close, 1), SharpeRange);
Sharpe 			= MA(ROC(Close,1), SharpeRange) /(SDF);
AddColumn(Sharpe, "Sharpe", 1.2 );

  • Ming

Try this version:

MinBarsNumber = 10;
SharpeRange = BarCount - 2;
R = ROC( Close, 1 );
SDF = StDev( R, SharpeRange );
Sharpe = MA( R, SharpeRange ) / ( SDF );
Filter = BarCount >= MinBarsNumber;
AddColumn( BarCount, "BarCount", 1.2 );
AddColumn( Sharpe, "Sharpe", 1.2 );

Thanks Milosz,

But I'm still getting an error message "Invalid argument value for MA() function; range must be greater than zero (and not Null)"

Seems the issue is the functions only allow a static period despite the filter. Still appreciate any ideas how to calculate the average returns, StDev of stocks using BarCount instead of a fixed period.

No, it's not true. If you have an Error saying, that for MA() or StDev() function, range must be greater than zero (and not Null), you should make sure, that the range is indeed grater than zero. For example like this:

MinBars = 10; // Your choice
Sharpe = Null;
R = ROC( Close, 1 );
SharpeRange = BarCount - 2;

if( SharpeRange > 0 )
{
    SDF = StDev( R, SharpeRange );
    Sharpe = MA( R, SharpeRange ) / ( SDF );
}
Filter = BarCount >= MinBars;

AddColumn( BarCount, "BarCount", 1.2 );
AddColumn( Sharpe, "Sharpe", 1.2 );
4 Likes

Hi Milosz,
Indeed the range criteria was the issue and this worked perfectly. Thanks so much for your help very much appreciated!

1 Like