{ Strategy: _SharpeRatio Description: Trades based on cross-over of two Sharpe Ratios of different lengths. Author: Mark J. Krisburg Last Modified: 05/05/10 original version } Inputs: PlotLength(2000), Price(TypicalPrice), 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(50), Segments2(0), Segments3(0), Segments4(0), SmoothSR(1), // smoothing periods for SR plots SmoothSum(1), // smoothing periods for sum of SR SDLength(30), // length for standard deviation of SRSum SDMult(1), // multiplier for displaying SD bands Offset1(0), Offset2(0), Offset3(0), Offset4(0), Mult1(1), // multiplier for SR1 sum = Mult1*SR1 + Mult2*SR2 + Mult3*SR3 + Mult4*SR4 Mult2(1), // multiplier for SR2 sum = Mult1*SR1 + Mult2*SR2 + Mult3*SR3 + Mult4*SR4 Mult3(1), // multiplier for SR3 sum = Mult1*SR1 + Mult2*SR2 + Mult3*SR3 + Mult4*SR4 Mult4(1), // multiplier for SR4 sum = Mult1*SR1 + Mult2*SR2 + Mult3*SR3 + Mult4*SR4 RiskFreeReturn(2), // annualized risk free return in percent. MaxAbsSRRatio12(3), // maximum absolute SR ratio that will be displayed (to avoid distorting scale of other plots) CalcSeconds(60), // Recalculate SR every 'CalcTicks' tick. StopDateYYYYMMDD(0), // if = 0, run until current date, else stop calculations at date = YYYYMMDD ShowSharpe(true), // show Sharpe Ratios ShowZScore(False), // show z-score Sharpe Ratios = (sharpe Ratio - Avg(Sharpe Ratio)/StandarDev(Sharpe Ratio) ShowCalc(False), // show number of times SR calculation done ShowMaxSharpe(False), // show maximum of SR1 and SR2 Sharpe Ratios ShowRatio(False), // Show ratio of SR1/SR2 ShowSum(False), ShowSmoothSum(False), ShowSumZ(False), // show sum of z-scores of Sharpe Ratio ShowSDSum(False), // show standard devision of Sum of Sharpe Ratio ShowDeltaSRSum(false), // show change of SMOOTH SRSum ShowDeltaSRSumAlert(false), // show alert for low change in Smooth SRSum DeltaMin(0.1), // minimum change in SRSum to flag alert ShowHiLoSRSum(true), // show highest high and lowest low of xSRSum HiLoLength(3), // length used to calculate highest high and lowest low of xSRSum MinShortPrice(5), // minimum price of stock that may be shorted // cause a recalculation (one day delay does not change result significantly). Ref1(0.00001), Ref2(0), Ref3(0); Vars: Optimizing(GetAppInfo(aiOptimizing) = 1), x(0), // dummy variable StartBar1(0), // Bar number afterwhich calculations can begin for Segments1 StartBar2(0), // Bar number afterwhich calculations can begin for Segments2 StartBar3(0), // Bar number afterwhich calculations can begin for Segments3 StopDate(CurrentDate), // Last date to perform calculations. (Default = Current Date) avgRR1(0), // average return rate for 'Segments1' segments sdRR1(0), // standard deviation of return rates intrabarpersist annRR1(0), // annualized rate of return = RR * 12 (1 segment = 1 month) annSD1(0), // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month) 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 avgRR2(0), // average return rate for 'SegMents2' segments sdRR2(0), // standard deviation of return rates intrabarpersist annRR2(0), // annualized rate of return = RR * 12 (1 segment = 1 month) annSD2(0), // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month) 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 avgRR3(0), // average return rate for 'SegMents3' segments sdRR3(0), // standard deviation of return rates intrabarpersist annRR3(0), // annualized rate of return = RR * 12 (1 segment = 1 month) annSD3(0), // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month) excessRR3(0), // excess return = annualized rate of return - risk free return intrabarpersist SR3(0), // Sharpe Ratio = Excess return / annualized standard deviation of returns intrabarpersist AbsSR3(0), // absolute value of Sharpe Ratio avgRR4(0), // average return rate for 'SegMents3' segments sdRR4(0), // standard deviation of return rates intrabarpersist annRR4(0), // annualized rate of return = RR * 12 (1 segment = 1 month) annSD4(0), // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month) excessRR4(0), // excess return = annualized rate of return - risk free return intrabarpersist SR4(0), // Sharpe Ratio = Excess return / annualized standard deviation of returns intrabarpersist AbsSR4(0), // absolute value of Sharpe Ratio xSR1(0), // smoothed SR1 xSR2(0), // smoothed SR2 xSR3(0), // smoothed SR3 xSR4(0), // smoothed SR4 SRRatio12(0), // ratio of SR1/SR2 SRSum(0), // sum of all Sharpe Ratios SRSumZ(0), // sum of all Sharpe Ratio Z-Scores xSRSum(0), // smoothed SRSum DeltaSRSum(0), // change in value of SMOOTH SRSum sdSRSum(0), // standard deviation of SRSum over SDLength bars NSRSum(0), // Normalized SRSum (= SRSum/SD(SRSum). Note: not same as z-score: (SRSum - Avg(SRSum))/SD(SRSum) ShowExtra(false), // show any of the "extra" calculations for debugging purposes, etc. DoCalc(True), // True when new SR calculation should be done intrabarpersist Calc(0), // SR calculation number oMean1(0), // average SRx; oMean = Average(SRx, Length) oSD1(0), // standard deviation of SRx; oSD = StandardDev(SRx, Length) oVariance1(0), // variance of SRx OZScore1(0), // z-score of SRx; oZScore = SRx - Average(SRx, Length)/StandardDev(SRx, Length) oMean2(0), // average SRx; oMean = Average(SRx, Length) oSD2(0), // standard deviation of SRx; oSD = StandardDev(SRx, Length) oVariance2(0), // normalized SRx OZScore2(0), // z-score of SRx; oZScore = SRx - Average(SRx, Length)/StandardDev(SRx, Length) oMean3(0), // average SRx; oMean = Average(SRx, Length) oSD3(0), // standard deviation of SRx; oSD = StandardDev(SRx, Length) oVariance3(0), // normalized SRx OZScore3(0), // z-score of SRx; oZScore = SRx - Average(SRx, Length)/StandardDev(SRx, Length) oMean4(0), // average SRx; oMean = Average(SRx, Length) oSD4(0), // standard deviation of SRx; oSD = StandardDev(SRx, Length) oVariance4(0), // normalized SRx OZScore4(0); // z-score of SRx; oZScore = SRx - Average(SRx, Length)/StandardDev(SRx, Length) Once begin if StopDateYYYYMMDD <> 0 then StopDate = StopDateYYYYMMDD - 19000000; end; DoCalc = _TickSeconds(CalcSeconds)> 0; if DoCalc and Date <= StopDate then begin calc = calc + 1; if Segments1 > 0 then x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments1, RiskFreeReturn, Offset1, avgRR1, sdRR1, annRR1, annSD1, excessRR1, SR1); if Segments2 > 0 then x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments2, RiskFreeReturn, Offset2, avgRR2, sdRR2, annRR2, annSD2, excessRR2, SR2); if Segments3 > 0 then x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments3, RiskFreeReturn, Offset3, avgRR3, sdRR3, annRR3, annSD3, excessRR3, SR3); if Segments4 > 0 then x = _SharpeRatio(Price, SegLength, SegsPerYear, Segments4, RiskFreeReturn, Offset4, avgRR4, sdRR4, annRR4, annSD4, excessRR4, SR4); if SmoothSR > 1 then begin xSR1 = AverageFC(SR1, SmoothSR) * Mult1; xSR2 = AverageFC(SR2, SmoothSR) * Mult2; xSR3 = AverageFC(SR3, SmoothSR) * Mult3; xSR4 = AverageFC(SR4, SmoothSR) * Mult4; end else begin xSR1 = SR1 * Mult1; xSR2 = SR2 * Mult2; xSR3 = SR3 * Mult3; xSR4 = SR4 * Mult4; end; if ShowSharpe then begin { plot only when not optimizing } if Optimizing = false then begin value1 = _TLPlot(1, SR1, PlotLength, 1, 0, Tool_Solid, yellow, 0); value1 = _TLPlot(2, SR2, PlotLength, 1, 0, Tool_Solid, cyan, 0); end; end; If SR1 crosses above SR2 and SR2 > SR2[1] then buy next bar at open; if SR1 crosses below SR2 and SR2 < SR2[1] then sell next bar at open; end;