Return and standard deviation on DataSet
Author: rbryant
Creation Date: 6/10/2018 6:56 AM

#### rbryant

#1
Hi all
I want to run code on a Dataset, and show the following: % return last 6 months, and standard deviation of daily returns (as a % of Close) the last 6 months for each security. This is to go into mean variance optimisation.

eg. the output may look like, with made up numbers:

Security 6 month return Daily st dev over last 6 months
SPY 10.5% 2.5%
TLT 7.2% 1.8%
.
.
.

This could be done using either using Print Debug or in tabular format like the Correlation Matrix code does. It would just be run on the last bar of the data i.e. analyse the 6 months up to and including the last bar.

The st dev would annualised ideally by x SQRT(252), and the 6 month return doubled to be annual (arithmetically).

Does anyone have any pointers which would set me on the right path, or is willing to do it for a small fee?

Thanks and regards
Rod

#2
Hi Rod,

CODE:

CODE:

#### rbryant

#3
Eugene

Thank you so much:) . A couple of questions, and yes I am poor at C#.

What is the difference between the two sets of code?
What does "Close>>months" do in " var pc = Close>>months;"?

The stdev would ideally be done on daily bars, rather than monthly. and then multiplied by SQRT(252) to annualise it, assuming 252 trading days per year. I can do that with SetScaleDaily() I presume.

Best regards!

Rod

#### Eugene

#4
QUOTE:
What does "Close>>months" do in " var pc = Close>>months;"?

Rod, you'll find answer to your question re: >> operator if you open the QuickRef tool > DataSeries Object.

QUOTE:
The stdev would ideally be done on daily bars, rather than monthly. and then multiplied by SQRT(252) to annualise it, assuming 252 trading days per year.

Here goes. But we need to make assumption that 6 months contain 252/2 = 126 bars. Please give this a try and see if it works for you:

CODE:

#### rbryant

#5
Thank you Eugene. It seems to work, though I will spot calculate some to check them.

I get the >> now, thanks. I guess it isn't an issue if a dataseries is shifted 6 months to the right so that it goes into the future.

On the StdDev, I looked in the WL docs and Googled around with regards to C# but couldn't find the syntax for the parameters in StdDev: How did you know the inputs which are required in the parentheses for "StdDev.Value(b,_return,halfYear,StdDevCalculation.Sample)" and their order?

Thanks a lot again.

Regards
Rod

#### Eugene

#6
QUOTE:
I get the >> now, thanks. I guess it isn't an issue if a dataseries is shifted 6 months to the right so that it goes into the future.

It's shifted to the right so the script accesses the data 6 months from the past on the current (here: latest charted) bar.

QUOTE:
On the StdDev, I looked in the WL docs and Googled around with regards to C# but couldn't find the syntax for the parameters in StdDev:

It's simpler than you think:

1. Strike Ctrl-F11.
2. Highlight "StdDev".
3. Click "More info..." to get to indicator's Wiki documentation (here: StdDev).

#### rbryant

#7
Ah, thanks Eugene!

#### Eugene

#8
You're welcome!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).