{ Indicator: _SharpeRatioQ.RS Description: Divides a time period into segments, and calculates price gain in percent for each segment. If logarithmic growth is occuring, each segment will have a similar percentage gain. Calculation load: With Offset=0, calculations include LastBarOnChart which is always changing with each tick. Use this to ensure most recent data is included in analysis. With Offset=1, calculations do NOT include LastBarOnChart. This eliminates the constant calculations that are triggered while Close of current bar continuously updates with each tick throughout the day. Use this setting to minimize calculation load while monitoring stocks during the day, Author: Mark J. Krisburg Last Modified: 10/04/07 10/20/07 Load addtional data indicator formatting not required 01/09/08 Add RSSum = Mult1*SR1Smooth + Mult2*SR2Smooth + Mult3*SR3Smooth 01/22/08 Add 4th SR (SR4) 08/09/08 minor formatting enhancements 08/18/08 improve consistence between chart and RS versions 01/30/10 code cleanup and post to forum } Inputs: Price(Close), SegLength(1), // use = 1 for weekly or monthly monthly charts, =5 for daily charts (1 week) or = 22 (1 month) SegsPerYear(251), // = 52 if SegLength = 5 on Day Chart(1 week), = 12 if SegLength = 1 on Monthly Chart Segments1(20), Segments2(40), Offset1(0), // = 0 when want to include current day, = 1 when want to exclude current day so each tick does not cause recalculation Offset2(0), // = 0 when want to include current day, = 1 when want to exclude current day so each tick does not cause recalculation CalcSeconds(60), // Sharpe Ratio recalculation interval in seconds RiskFreeReturn(2), // annualized risk free return in percent. MinShortPrice(5), // minimum price of stock that may be shorted // cause a recalculation (one day delay does not change result significantly). SegColor(Cyan), // color for segments PosColor(green), // color for positive values of SR and gain NegColor(red), // color for negative values of SR and gain OffsetColor(darkgray), // color for zero offsets OffsetWarningColor(magenta), // color to warn user that offsets are non-zero CalcColor(yellow); // color for reporting calculation statistics Vars: x(0), xSR1(0), xSR2(0), intrabarpersist avgRR1(0), // average return rate for 'Segments1' segments intrabarpersist sdRR1(0), // standard deviation of return rates intrabarpersist annRR1(0), // annualized rate of return = RR * (Segments per year) intrabarpersist annSD1(0), // annualized standard deviation of returns = sdRR * Sqrt(Segments per year) intrabarpersist excessRR1(0), // excess return = annualized rate of return - risk free return intrabarpersist SR1(0), // Sharpe Ratio = Excess return / annualized standard deviation of returns intrabarpersist AbsSR1(0), // absolute value of Sharpe Ratio SR1Color(white), intrabarpersist avgRR2(0), // average return rate for 'SegMents2' segments intrabarpersist sdRR2(0), // standard deviation of return rates intrabarpersist annRR2(0), // annualized rate of return = RR * 12 (1 segment = 1 month) intrabarpersist annSD2(0), // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month) intrabarpersist excessRR2(0), // excess return = annualized rate of return - risk free return intrabarpersist SR2(0), // Sharpe Ratio = Excess return / annualized standard deviation of returns intrabarpersist AbsSR2(0), // absolute value of Sharpe Ratio SR2Color(white), DoCalc(True), // True when new SR calculation should be done ReqDate1(""), // text date format of offset date requested by user (adjusted if falls on weekend or holiday) ReqDate2(""), // text date format of offset date requested by user (adjusted if falls on weekend or holiday) ReqOffset1(0), // offset of reference date requested by user (OffsetDate will be adjusted if ReqDate falls on weekend or holiday) ReqOffset2(0), // offset of reference date requested by user (OffsetDate will be adjusted if ReqDate falls on weekend or holiday) OffsetColor1(white), OffsetColor2(white), OffsetDate1(""), // reference date (end date) SR1 calculations are based on OffsetDate2(""); // reference date (end date) SR2 calculations are based on { calculate SR after LastBarOnChart reached but not more than every CalcSeconds } DoCalc = _TickSeconds(CalcSeconds) > 0; if DoCalc then begin if Segments1 > 0 then SR1 = _SharpeRatio(Price, SegLength, SegsPerYear, Segments1, RiskFreeReturn, Offset1, avgRR1, sdRR1, annRR1, annSD1, excessRR1, SR1); if Segments2 > 0 then SR2 = _SharpeRatio(Price, SegLength, SegsPerYear, Segments2, RiskFreeReturn, Offset2, avgRR2, sdRR2, annRR2, annSD2, excessRR2, SR2); if SR1 >= 0 then SR1Color = PosColor else SR1Color = NegColor; if SR2 >= 0 then SR2Color = PosColor else SR2Color = NegColor; end; if Segments1 > 0 then begin // display Segments 1 Plot10(SegLength,"Seg Len"); Plot11(Segments1,"Len 1", SegColor); Plot12(Offset1,"Off Set 1",OffsetColor1); if (annRR1 > 0) or (AnnRR1 < 0 and Price > MinShortprice) then begin // if negative annRR, don't display if stock not shortable Plot13(AbsValue(SR1),"ABS SR1 ", SR1Color); // don't do this if using SR to find zero SR stocks to range trade end; Plot14(SR1,"SR 1", SR1Color); Plot15(annRR1, "Ann RR1 %", iff(annRR1 > 0, PosColor, NegColor)); end; if Segments2 > 0 then begin // display Segments 2 Plot21(Segments2,"Len 2", SegColor); Plot22(Offset2,"Off Set 2",OffsetColor2); if (annRR2 > 0) or (AnnRR2 < 0 and Price > MinShortPrice) then begin // if negative annRR, don't display if stock not shortable Plot23(AbsValue(SR2),"ABS SR 2", SR2Color); // don't do this if using SR to find zero SR stocks to range trade end; Plot24(SR2,"SR 2", SR2Color); Plot25(annRR2, "Ann RR2 %", iff(annRR2 > 0, PosColor, NegColor)); end;