{ Function EqtyCorr Calculate an objective function based on the weighted sum of the correlation coefficient of the equity curve and the net profit. Append the objective function value and system input parameter values to a file. In the following, X represents the bar number, and Y is the total equity (closed trade net profit plus open position profit). Copyright 2004 Breakout Futures www.BreakoutFutures.com } input: Param1 (NumericSimple), { system parameter 1 } Param2 (NumericSimple), { system parameter 2 } Param3 (NumericSimple), { system parameter 3 } Param4 (NumericSimple), { system parameter 4 } Param5 (NumericSimple), { system parameter 5 } CCMin (NumericSimple), { min value of coeff } CCMax (NumericSimple), { max value of coeff } EqMin (NumericSimple), { min net profit } EqMax (NumericSimple), { max net profit } WeightCC (NumericSimple), { weight for coeff } WeightEq (NumericSimple), { weight for net profit } FName (StringSimple); { file name to write results to } Var: XVal (0), { value of x, scaled bar number } YVal (0), { value of y, scaled equity } SumXY (0), { sum of X * Y } SumX (0), { sum of X } SumY (0), { sum of Y } SumXX (0), { sum of X * X } SumYY (0), { sum of Y * Y } CCNum (0), { numerator of correlation coefficient } CCDen (0), { denominator of correlation coefficient } CorrCoef (0), { correlation coefficient } ObjectFn (0), { correlation times net profit } TotalEqty (0), { open plus closed trade equity } TotProf (0), { total profit from weighted sum } PWNum (0), { numerator of profit weights } PWDen (0), { denominator of profit weights } PWFact (100), { factor determining profit weighting } ii (0), { loop counter } NBars (0), { number of bars on chart } StrOut (""); Array: EqtyCh[5000](0); { handles up to 5000 bars of data } TotalEqty = NetProfit + OpenPositionProfit; If BarNumber < 5000 then EqtyCh[BarNumber] = TotalEqty - TotalEqty[1]; XVal = BarNumber/100.; YVal = TotalEqty/10000.; SumX = SumX + XVal; SumY = SumY + YVal; SumXY = SumXY + (XVal * YVal); SumXX = SumXX + (XVal * XVal); SumYY = SumYY + (YVal * YVal); {Print("Bar: ", BarNumber:0:0, " SumX = ", SumX:0:0, " SumY = ", SumY:0:2, " SumXY = ", SumXY:0:2, " SumXX = ", SumXX:0:0, " SumYY = ", SumYY:0:2); } If LastBarOnchart then Begin NBars = BarNumber; { Calculate weighted net profit } for ii = 1 to NBars Begin PWDen = PWDen + (PWFact - 1)/(NBars - 1) * (ii - 1) + 1; End; PWDen = PWDen/NBars; for ii = 1 to NBars Begin PWNum = (PWFact - 1)/(NBars - 1) * (ii - 1) + 1; TotProf = TotProf + EqtyCh[ii] * PWNum/PWDen; End; { Calculate correlation coefficient } CCNum = NBars * SumXY - (SumX * SumY); CCDen = SquareRoot((NBars * SumXX - (SumX * SumX)) * (NBars * SumYY - (SumY * SumY))); if CCDen > 0 then CorrCoef = CCNum/CCDen else CorrCoef = 0; { Objective function is weighted sum of profit plus correlation } ObjectFn = WeightCC * (CorrCoef - CCMin)/(CCMax - CCMin) + WeightEq * (TotProf - EqMin)/(EqMax - EqMin); { write correlation coefficient to file along with system parameters } StrOut = NumtoStr(ObjectFn, 2) + ", " + NumtoStr(TotProf, 2) + ", " + NumtoStr(CorrCoef, 3) + ", " + NumtoStr(Param1, 3) + ", " + NumtoStr(Param2, 3) + ", " + NumtoStr(Param3, 3) + ", " + NumtoStr(Param4, 3) + ", " + NumtoStr(Param5, 3) + Newline; FileAppend(FName, StrOut); End; EqtyCorr = CorrCoef; { Simple Channel BO (breakout) system, based on TradeStation systems Channel Breakout LE and Channel Breakout SE. } input: Length(50); Buy ("ChBrkLE") next bar at HighestFC(High, Length) + 1 point stop; Sell Short ("ChBrkSE") next bar at LowestFC(Low, Length) - 1 point stop; {Value1 = ModSharpe(Length, 0, 0, 0, 0, "C:\bcm\TestMSR.txt");} Value2 = EqtyCorr(Length, 0, 0, 0, 0, .52, .945, 2400, 72000, 1, 10, "C:\bcm\TestCC.txt");