#Include <Excel.au3>
#Include <GUIConstantsEx.au3>
#Include <Array.au3>
#Include <Math.au3>
#Include <ProgressConstants.au3>
While ProcessExists("EXCEL.EXE")
ProcessClose("EXCEL.EXE")
WEnd
;create GUI
$GUIHandle=GUICreate ( "Trading system simulation" , 500 , 450 , 0 , 0)
GUISetState (@SW_SHOW,$GUIHandle)
;path to Excel file containing past trades
GUICtrlCreateLabel ( "File to analyze:", 50, 50 , 400 , 20)
$input1=GUICtrlCreateInput ( "C:\Documents and Settings\Administrator\My Documents\Simulation.xls", 50, 70 , 400 , 20)
;number of past trades
GUICtrlCreateLabel ( "Number of trades in backtest:", 50, 100 , 400 , 20)
$input2=GUICtrlCreateInput ( "637", 50, 120 , 400 , 20)
;number of theoretical trades to create
GUICtrlCreateLabel ( "Number of future trades to create:", 50, 150 , 400 , 20)
$input3=GUICtrlCreateInput ( "637", 50, 170 , 400 , 20)
;number of iterations
GUICtrlCreateLabel ( "# of iterations:", 50, 200 , 400 , 20)
$input4=GUICtrlCreateInput ( "10000", 50, 220 , 400 , 20)
;path to Excel file to save for results
GUICtrlCreateLabel ( "Save results to:", 50, 250 , 400 , 20)
$input5=GUICtrlCreateInput ( "C:\Documents and Settings\Administrator\My Documents\Result.xls", 50, 270 , 400 , 20)
;start button
$button=GuiCtrlCreateButton("Start", 150, 320, 200, 20)
;progress bar
$label=GUICtrlCreateLabel ( "Progress...", 50, 350 , 400 , 20)
$progressbar=GUICtrlCreateProgress ( 50, 370 , 400 , 20, $PBS_SMOOTH)
;GUI message loop
While 1
$msg=GUIGetMsg()
If $msg=$button Then ExitLoop
WEnd
;disable GUI controls
GUICtrlSetState ($input1, $GUI_DISABLE)
GUICtrlSetState ($input2, $GUI_DISABLE)
GUICtrlSetState ($input3, $GUI_DISABLE)
GUICtrlSetState ($input4, $GUI_DISABLE)
GUICtrlSetState ($input5, $GUI_DISABLE)
GUICtrlSetState ($button, $GUI_DISABLE)
;read GUI inputs
$readpath=GUICtrlRead($input1)
$oldtrades=GUICtrlRead($input2)
$newtrades=GUICtrlRead($input3)
$nit=GUICtrlRead($input4)
$savepath=GUICtrlRead($input5)
;set the dimension of trade arrays
Dim $backtest[$oldtrades]
Dim $sample[$newtrades]
Dim $cumulative[$newtrades]
Dim $drawdown[$newtrades]
Dim $profit[$nit]
Dim $maxdrawdown[$nit]
Dim $profitdistribution[100]
Dim $maxdrawdowndistribution[100]
;read past trades and store them into backtest array
$oExcel=_ExcelBookOpen($readpath,0)
For $i=1 To $oldtrades
$backtest[$i-1]=_ExcelReadCell($oExcel,$i,1)
Next
_ExcelBookClose($oExcel,0)
;iterate
For $iteration=1 To $nit
;create new sample of theoretical trades picking random past trades
For $trade=0 To $newtrades-1
$sample[$trade]=$backtest[Int(Random(0,$oldtrades))]
Next
;create cumulative profit array
$cumulative[0]=$sample[0]
For $tradetoadd=1 To $newtrades-1
$cumulative[$tradetoadd]=$cumulative[$tradetoadd-1]+$sample[$tradetoadd]
Next
;create drawdown array
If $cumulative[0]>=0 Then
$drawdown[0]=0
Else
$drawdown[0]=$cumulative[0]
EndIf
For $drawdowntoadd=1 To $newtrades-1
$drawdown[$drawdowntoadd]=_Max(_ArrayMax($cumulative,1,1,$drawdowntoadd),0)-$cumulative[$drawdowntoadd]
Next
;retrieve profit and max drawdown
$profit[$iteration-1]=$cumulative[$newtrades-1]
$maxdrawdown[$iteration-1]=_ArrayMax($drawdown,1,1,$newtrades-1)
;set progress bar
$progress=$iteration*100/$nit
GUICtrlSetData($progressbar, $progress)
Next
;open Excel book to save results to
$oExcel=_ExcelBookOpen($savepath,0)
;set progress bar
GUICtrlSetData($label,"Completing...")
GUICtrlSetData($progressbar, 0)
;determine profit and max drawdown granularity for distribution chart
$profit_granularity=(_ArrayMax($profit,1,0,$nit-1)-_ArrayMin($profit,1,0,$nit-1))/100
$maxdrawdown_granularity=(_ArrayMax($maxdrawdown,1,0,$nit-1)-_ArrayMin($maxdrawdown,1,0,$nit-1))/100
;sort profit and max drawdown arrays in ascending order
_ArraySort($profit)
_ArraySort($maxdrawdown)
;search for confidence intervals
For $iteration=0 To $nit-1
If $profit[$iteration]>0 Then
ExitLoop
EndIf
Next
$string3=($nit-$iteration)/$nit
_ExcelWriteCell($oExcel,$string3,1,4)
_ExcelWriteCell($oExcel,$profit[$nit/100*10-1],2,4)
_ExcelWriteCell($oExcel,$profit[$nit/100*5-1],3,4)
_ExcelWriteCell($oExcel,$profit[$nit/100*1-1],4,4)
_ExcelWriteCell($oExcel,$maxdrawdown[$nit/100*90-1],5,4)
_ExcelWriteCell($oExcel,$maxdrawdown[$nit/100*95-1],6,4)
_ExcelWriteCell($oExcel,$maxdrawdown[$nit/100*99-1],7,4)
;calculate average profit and max drawdown
$profitsum=0
$maxdrawdownsum=0
For $i=0 To $nit-1
$profitsum=$profitsum+$profit[$i]
$maxdrawdownsum=$maxdrawdownsum+$maxdrawdown[$i]
Next
$averageprofit=$profitsum/$nit
$averagemaxdrawdown=$maxdrawdownsum/$nit
_ExcelWriteCell($oExcel,$averageprofit,8,4)
_ExcelWriteCell($oExcel,$averagemaxdrawdown,9,4)
;set array indexes to start search from
$profitindex=0
$maxdrawdownindex=0
;group profit and maxdrawdown to plot distribution
For $i=1 To 100
$minprofit=$profit[0]+($i-1)*$profit_granularity
$maxprofit=$profit[0]+$i*$profit_granularity
$minmaxdrawdown=$maxdrawdown[0]+($i-1)*$maxdrawdown_granularity
$maxmaxdrawdown=$maxdrawdown[0]+$i*$maxdrawdown_granularity
While 1
If $profitindex>=$nit Then ExitLoop
If $profit[$profitindex]<$maxprofit Then
$profitdistribution[$i-1]+=1
$profitindex+=1
Else
ExitLoop
EndIf
WEnd
While 1
If $maxdrawdownindex>=$nit Then ExitLoop
If $maxdrawdown[$maxdrawdownindex]<$maxmaxdrawdown Then
$maxdrawdowndistribution[$i-1]+=1
$maxdrawdownindex+=1
Else
ExitLoop
EndIf
WEnd
;write values to Excel
$string1=Round($minprofit,2)&"-"&Round($maxprofit,2)
$string2=Round($minmaxdrawdown,2)&"-"&Round($maxmaxdrawdown,2)
_ExcelWriteCell($oExcel,$string1,$i+10,1)
_ExcelWriteCell($oExcel,$profitdistribution[$i-1],$i+10,2)
_ExcelWriteCell($oExcel,$string2,$i+10,3)
_ExcelWriteCell($oExcel,$maxdrawdowndistribution[$i-1],$i+10,4)
;set progress bar
GUICtrlSetData($progressbar, $i)
Next
_ExcelBookClose($oExcel,1)
GUIDelete($GUIHandle)
MsgBox(0,"Trading system simulation","Done")