For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Improve n period Sharpe Ratio indicator

  • Hi,
    I have written a code to calculate the sharpe ratio of security.

    class sharpe(bt.Indicator):
        lines = ('sharpe',)
        def __init__(self):
            #self.roc= / - 1.0
        def next(self):
            iterdata =
            returns= [a1 / a2 - 1 for a1, a2 in zip(iterdata[1:], iterdata)]
            avgret= np.average(returns)
            self.lines.sharpe[0] = (avgret/stdev)

    Here I have taken Rf to be zero.
    Can someone optimize this code (write a better version) if possible?

Log in to reply