[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Repeat Posting on ETC - How to calculate Risk properly



PureBytes Links

Trading Reference Links

> Thanks to Michael Paauwe for reminding us that we should compare
> system results to buy and hold. However, we need to be aware that TS
> does not consider a drawdown from an equity peak as long as the
> trade is still profitable. 

I have included a code which I wrote sometime ago and which can be 
included in any system in order to store the total equity on each 
bar and calculate some sharpe ratio. Attached you will find a dll 
which I used to accomplish this since it is necessary to store the 
total equity for each day somewhere without running into 
the maxbars back problem.

Don't expect too much from the code it was my working version. (I 
just recompiled the dll - if someone runs into problems with it 
please email)

Gerrit Jacobsen
http://www.tickscape.com 
beta test- screenshot for those who can't wait.

{-------------------------------------------------Begin SystemReport
Add this code section to your system code - this code must be adapted 
for each system.

The Report calculates a special sharp ratio.

It calculates a very rigid sharp ratio of a fixed time length window
which is continously shifted forward in the testing period. 

The technique shown here can be used to calculate other profit
measures as well. The advantage is that this report measures the
profit ratios over a fixed time (bar) window which is shifted forward
and therefore reports from different systems can be compared to each
other even if other data lengths was used.

The technique is accomplished with the ts_kitsr.dll which lets you
store values. In this case the total equity (inclusive the
non-realised profit/loss) is stored and can be used later in order to
calculate profit ratios.

The final reports are in d:\ts40\SystSumm.TXT and d:\ts40\SystDetl.TXT

needs ts_kitsr.dll which lets you store values in a large array of max
13.000 values

change the directories as appropriate

The code needs to be adapted for every new system - see the comments

}

Vars: 
FloatDummy(float(0)), PriceperCon(Float(0)), FloatDummy2(float(0)),
Sharpe(float(0)), Counter(INT(0)), GJProfit(Float(0)),
D2Profit(Float(0)), D1Profit(Float(0)),MyAverage(float(0)),
CumProf(float(0)), CumProfQuad(float(0)), StandardDev(float(0)),
MyString(""),MaxEquity(float(0)), MyDrawDown(float(0)),
Counter2(INT(0));


Inputs: 		      LastBar(0), 	{LastBarNumber to be included in Report}
   NoOfBars(20);	{Which sharpe average do you want to observe -
   expressed in bars}

DefineDLLFunc:"d:\ts\prog\ts_kitsr.dll",BOOL,"GetEquity", INT,
LPFLOAT, LPFLOAT;
DefineDLLFunc:"d:\ts\prog\ts_kitsr.dll",BOOL,"SetEquity", INT,
LPFLOAT, LPFLOAT;

If Currentbar = 1 then begin
{	FileDelete("d:\ts40\SystSumm.TXT");
 FileDelete("d:\ts40\SystDetl.TXT"); } 

 FloatDummy2 = 0;
 FloatDummy = 0;



 For Counter = 1 to 13000 begin      
  SetEquity(Counter, &FloatDummy, &FloatDummy2); {make sure Array is
  clean}
 End;
end;

FloatDummy = GrossProfit+Grossloss+OpenPositionProfit; {calculate
total equity} FloatDummy2 = 0; SetEquity(Currentbar, &FloatDummy,
&FloatDummy2); {store equity}

IF  NoOfBars > 0 and LastBar > NoOfBars and MaxContractsHeld > 0 and
Currentbar = Lastbar then begin    {begin on last bar to calculate
profit ratios}
 CumProf = 0;
 For Counter = NoOfBars + 1 to Currentbar  begin {loop over all bars}
  GetEquity(Counter - NoOfBars, &D1Profit, &FloatDummy2);  {get
  previosly stored equity at start of window} GetEquity(Counter,
  &D2Profit, &FloatDummy2); {get previosly stored equity at end of
  window} MaxEquity = -99999999999; MyDrawdown = 0; For Counter2 =
  (Counter - NoOfBars) to Counter begin {loop over window}
   GetEquity(Counter2,&FloatDummy,&FloatDummy2); 
   IF FloatDummy > MaxEquity then MaxEquity = FloatDummy; {calculate
   max equity} IF FloatDummy - MaxEquity  < MyDrawDown then MyDrawDown
   = FloatDummy - MaxEquity; {calculate max drawdown}
  End;
  GJProfit=100 * (D2Profit - D1Profit) /
  (MaxContractsHeld*Margin-MyDrawDown); {calculate GJ Profit ratio}
  SetEquity(Counter, &D2Profit, &GJProfit); {store GJ Profit ratio}
  CumProf = CumProf + GJProfit; Mystring =
  NumToStr(Counter-NoOfBars,0)+"	"
  +NumToStr(Variable1,2)+"	"+NumToStr(Variable2,1)+ 
  "	"+NumToStr(Variable3,0)+ 
   {Put here whichever parameters of your system you want to show in
   your report}
     "	"+NumToStr(GJProfit,2)+NewLine;
{		FileAppend("d:\ts40\SystDetl.TXT", Mystring);} {include this line
if you want a detailed daily report saved}
 end;
 MyAverage = CumProf / (Currentbar - NoOfBars);

 CumProfQuad = 0;
 For Counter = NoOfBars + 1 to Currentbar begin
  GetEquity(Counter, &FloatDummy, &GJProfit);
  CumProfQuad = CumProfQuad + Power((MyAverage - GJProfit), 2);
 end;

 StandardDev = SquareRoot(CumProfQuad / (Currentbar - NoOfBars -1));
 Sharpe = MyAverage / StandardDev;

 Print("Var1:", Variable1:5:4,"Var2:",Variable2:2:1, "Var3:",
 Variable3:3:0);      {the variables can be any kind of parameter
 which is optimized during testing and which you want to print}

 Print("	Shrp:",Sharpe:4:2,"  Av.%Ret:", MyAverage:6:2, "% 
 NPr:",NetProfit:7:0, "  NoTr.:", TotalTrades:4:0);

 Mystring = NumToStr(Variable1,2)+"	"+NumToStr(Variable2,1)+ 
 "	"+NumToStr(Variable3,0)+ {the variables can be any kind of
 parameter which is optimized during testing and which you want to
 print}
   "	"+NumToStr(Sharpe,2)+"	"+NumToStr(MyAverage,2)+"	"+NumToStr(NetPr
   ofit,0)+"	" + NumToStr(TotalTrades,0)+NewLine;

 FileAppend("d:\ts40\SystSumm.TXT", Mystring);

End;
{---------------------------------------------------------------------
-----------------End SystemReport}
Gerrit Jacobsen
http://www.tickscape.com