{_SystemHistory function by Alex Matulich rev 8 November 2003 Copyright (c) 2003 by Unicorn Research Corporation. All rights reserved. During an optimization, set the filename argument to an empty string "". If you don't, this function recreates a file on each recalculation of your strategy, which will greatly slow down the optimization. This function outputs the profit, initial risk, initial volatility, maximum adverse excursion (MAE), and maximum favorable excursion (MFE) for every closed position. Units output are in dollars, not market units, although inputs *are* in market units. The inputs to this function are a file name, your stop price, and your measure of volatility, which you pass to this function on each bar. Here's an example of how to use _SystemHistory, using a crude "Turtle-like" trading system: ----------------------------------------------------------------------- Inputs: filename(""), EntryLen(24), StopLength(12); Vars: StopPrice(0), signl(0); {Generate buy or sell signal} signl = 0; if Close > Highest(High, EntryLen)[1] then signl = 1; {buy signal} if Close < Lowest(Low, EntryLen)[1] then signl = -1; {sell signal} {Execute buy or sell signal and set initial stop} if signl > 0 then begin Buy("L") next bar open; if MarketPosition <= 0 then StopPrice = Highest(Low, StopLength); ExitLong("xl1") next bar at StopPrice stop; end; if signl < 0 then begin Sell("S") next bar open; if MarketPosition >= 0 then StopPrice = Lowest(High, StopLength); ExitShort("xs1") next bar at StopPrice stop; end; {Update stop for open position} if MarketPosition > 0 then begin StopPrice = MinList(StopPrice, Highest(Low, StopLength)); ExitLong("xl") next bar at StopPrice stop; end; if MarketPosition < 0 then begin StopPrice = MaxList(StopPrice, Lowest(High, StopLength)); ExitShort("xs") next bar at StopPrice stop; end; {Output the history} value1 = _SystemHistory(filename, StopPrice, AvgTrueRange(15)); ----------------------------------------------------------------------- } inputs: filename(string), {full pathname for .csv file} {WARNING: avoid \hb, \he, \pb, \pe, \wb, or \we, in the file name. TS interprets these as "jump commands." For example, the filenames "c:\hello.csv" or "c:\data\performance.csv" will result in an error message because they contain \he and \pe, respectively.} StopPrice(NumericSeries), {current stop price} mktvolatility(NumericSeries); {current volatility} vars: risk(0), mktvolat(0), nclosed(0), MAE(0), MFE(0), j(0); if StrLen(filename) > 0 then begin if currentbar <= 1 then begin {Initializations to occur on first bar} nclosed = 0; FileDelete(filename); FileAppend(filename,"P&L,risk,volat,MAE,MFE"+NewLine); {file header} end; {Do the following while a position is open} if marketposition <> 0 then begin if BarsSinceEntry = 1 then begin {first bar after position opens} risk = AbsValue(EntryPrice - StopPrice[1]) * BigPointValue; mktvolat = mktvolatility[1] * BigPointValue; if marketposition > 0 then begin MAE = MinList(BigPointValue*(Low - EntryPrice), 0); MFE = MaxList(BigPointValue*(High - EntryPrice), 0); end else begin MAE = MinList(BigPointValue*(EntryPrice - High), 0); MFE = MaxList(BigPointValue*(EntryPrice - Low), 0); end; end else begin {we're past the first bar in the open position} {risk = risk[1];} {save initial risk} {mktvolat = mktvolat[1];} {save initial volatility} if marketposition > 0 then begin {calculate new MAE & MFE} MAE = MinList(MAE[1], BigPointValue*(Low - EntryPrice), 0); MFE = MaxList(MFE[1], BigPointValue*(High - EntryPrice), 0); end else begin MAE = MinList(MAE[1], BigPointValue*(EntryPrice - High), 0); MFE = MaxList(MFE[1], BigPointValue*(EntryPrice - Low), 0); end; end; end; {nclosed = nclosed[1];} {Output results whenever a position is closed, or on last bar of chart} j = iff(LastBarOnChart and marketposition<>0, 0, 1); if totaltrades > nclosed or j = 0 then begin FileAppend(filename, numtostr(PositionProfit(j),2)+","+ {output profit} numtostr(risk[j],2)+","+ {initial risk} numtostr(mktvolat[j],2)+","+ {initial volatilty} numtostr(MinList(MAE[j],positionprofit(j)),2)+","+ {MAE} numtostr(MaxList(MFE[j],positionprofit(j)),2) {MFE} +NewLine); nclosed = totaltrades; end; end; _SystemHistory = 1; {dummy return value}