{ Function IdealEqtyOpt5 Calculate an objective function based on the correlation between the equity curve generated by a trading system and the "ideal" equity curve. The ideal equity curve is the equity curve generated by a stop and reverse system that reverses at perfect reversal points. The objective function value and system input parameter values are appended to a text file for later analysis. Copyright 2005 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 } Param6 (NumericSimple), { system parameter 6 } Param7 (NumericSimple), { system parameter 7 } Param8 (NumericSimple), { system parameter 8 } Param9 (NumericSimple), { system parameter 9 } Param10 (NumericSimple), { system parameter 10 } RevPts (NumericSimple), { points to reverse } RevFr (NumericSimple), { fraction of ATR to reverse } NRev (NumericSimple), { length of ATR } RevType (NumericSimple), { 0 = pts; 1 = ATR } FName (StringSimple), { file name to write results to } PrintLog (TrueFalse), { flag for writing to print log } WriteOpt (TrueFalse); { True -> write optimization; false -> write equity} Var: LowC (0), { lowest close } HighC (0), { highest close } TrDir (0), { direction of ideal trade } RevSz (0), { points needed to reverse } HighBar (0), { number of bar with highest close } LowBar (0), { number of bar with lowest close } ibar (0), { bar counter } XVal (0), { value of x, system equity } YVal (0), { value of y, ideal 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), { objective function } TotalEqty (0), { open plus closed trade equity } ii (0), { loop counter } NBars (0), { number of bars } NTrSys (0), { number of system trades } NTrIdeal (0), { number of ideal "trades" } StrOut (""); Array: EqtySys[5000](0), { equity at each bar, system trades } EqtyIdeal[5000](0), { equity at each bar, ideal trades } TrSys[1000](0), { equity at end of system trades } DatesSys[1000](0), { dates of system trades } TrIdeal[1000](0), { equity at end of ideal trades } DatesIdeal[1000](0); { dates of ideal trades } If BarNumber = 1 then Begin If Close > Close[1] then TrDir = 1 Else TrDir = -1; HighC = Close; HighBar = BarNumber; LowC = Close; LowBar = BarNumber; DatesSys[0] = DateToJulian(date); DatesIdeal[0] = DateToJulian(date); End; If RevType = 0 then RevSz = RevPts Else RevSz = RevFr * average(TrueRange, NRev); If Close > HighC then Begin HighC = Close; HighBar = BarNumber; End; If Close < LowC then Begin LowC = Close; LowBar = BarNumber; End; { Reverse ideal trade from long to short } If TrDir = 1 and HighC - Close >= RevSz then Begin TrDir = -1; NTrIdeal = NTrIdeal + 1; If PrintLog then Print("Date: ", date[BarNumber - HighBar]:0:0, " Time: ", time[BarNumber - LowBar]:0:0, " Close = ", HighC:0:4, " Direction: ", TrDir); LowC = Lowest(Close, BarNumber - HighBar + 1); LowBar = BarNumber - LowestBar(Close, BarNumber - HighBar + 1); { Adjust previous equity values from reversal point to current bar } For ii = ibar - (BarNumber - HighBar - 1) to ibar - 1 begin EqtyIdeal[ii] = EqtyIdeal[ii - 1] + (Close[ibar - ii + 1] - Close[ibar - ii]) * BigPointValue; End; { Record equity value and date for ideal trade } TrIdeal[NTrIdeal] = EqtyIdeal[ibar - (BarNumber - HighBar)]; DatesIdeal[NTrIdeal] = DateToJulian(date[BarNumber - HighBar]); End; { Reverse ideal trade from short to long } If TrDir = -1 and Close - LowC >= RevSz then Begin TrDir = 1; NTrIdeal = NTrIdeal + 1; If PrintLog then Print("Date: ", date[BarNumber - LowBar]:0:0, " Time: ", time[BarNumber - LowBar]:0:0, " Close = ", LowC:0:4, " Direction: ", TrDir); HighC = Highest(Close, BarNumber - LowBar + 1); HighBar = BarNumber - HighestBar(Close, BarNumber - LowBar + 1); { Adjust previous equity values from reversal point to current bar } For ii = ibar - (BarNumber - LowBar - 1) to ibar - 1 begin EqtyIdeal[ii] = EqtyIdeal[ii - 1] + (Close[ibar - ii] - Close[ibar - ii + 1]) * BigPointValue; End; { Record equity value and date for ideal trade } TrIdeal[NTrIdeal] = EqtyIdeal[ibar - (BarNumber - LowBar)]; DatesIdeal[NTrIdeal] = DateToJulian(date[BarNumber - LowBar]); End; { Keep track of equity curve of trading system } TotalEqty = NetProfit + OpenPositionProfit; If ibar < 5000 then EqtySys[ibar] = TotalEqty; { Record equity value and date for system trade } If TotalTrades > NTrSys then Begin NTrSys = NTrSys + 1; TrSys[NTrSys] = NetProfit; DatesSys[NTrSys] = DateToJulian(date); End; { Keep track of equity curve from ideal trades } If ibar > 0 and ibar < 5000 then Begin If TrDir = 1 then EqtyIdeal[ibar] = EqtyIdeal[ibar - 1] + (Close - Close[1]) * BigPointValue Else If TrDir = -1 then EqtyIdeal[ibar] = EqtyIdeal[ibar - 1] + (Close[1] - Close) * BigPointValue Else EqtyIdeal[ibar] = 0; End; ibar = ibar + 1; { Compute objective function and write to file } If LastBarOnchart then Begin NBars = ibar; { Calculate correlation between system and ideal equity curves } For ii = 0 to NBars - 1 Begin XVal = EqtySys[ii]; YVal = EqtyIdeal[ii]; SumX = SumX + XVal; SumY = SumY + YVal; SumXY = SumXY + (XVal * YVal); SumXX = SumXX + (XVal * XVal); SumYY = SumYY + (YVal * YVal); End; 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; ObjectFn = CorrCoef; { Write results to file } If WriteOpt then Begin { Write objective function to file along with system parameters } StrOut = NumtoStr(ObjectFn, 4) + ", " + NumtoStr(Param1, 3) + ", " + NumtoStr(Param2, 3) + ", " + NumtoStr(Param3, 3) + ", " + NumtoStr(Param4, 3) + ", " + NumtoStr(Param5, 3) + ", " + NumtoStr(Param6, 3) + ", " + NumtoStr(Param7, 3) + ", " + NumtoStr(Param8, 3) + ", " + NumtoStr(Param9, 3) + ", " + NumtoStr(Param10, 3) + NewLine; FileAppend(FName, StrOut); End Else Begin { Write equity curves to file } FileAppend(FName, "Equity curves: bar number, system equity, ideal equity: " + NewLine); For ii = 0 to NBars - 1 Begin StrOut = NumtoStr(ii, 0) + ", " + NumtoStr(EqtySys[ii], 2) + ", " + NumtoStr(EqtyIdeal[ii], 2) + NewLine; FileAppend(FName, StrOut); End; { Write system trades to file } FileAppend(FName, NewLine + "System Trades (date, equity): " + NumtoStr(NTrSys, 0) + " trades" + NewLine); For ii = 0 to NTrSys Begin StrOut = NumtoStr(DatesSys[ii], 0) + ", " + NumtoStr(TrSys[ii], 2) + NewLine; FileAppend(FName, StrOut); End; { Write ideal trades to file } FileAppend(FName, NewLine + "Ideal Trades (date, equity): " + NumtoStr(NTrIdeal, 0) + " trades" + NewLine); For ii = 0 to NTrIdeal Begin StrOut = NumtoStr(DatesIdeal[ii], 0) + ", " + NumtoStr(TrIdeal[ii], 2) + Newline; FileAppend(FName, StrOut); End; End; End; IdealEqtyOpt5 = ObjectFn;