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

Coding StdDev of Individual Trade Returns



PureBytes Links

Trading Reference Links

Hello all,

I seek the help of someone more smarter than I are. 

I am trying to calculate the Standard Deviation of the individual trade
returns for a system. My humble snippet of code below is designed to be
pasted at the bottom of a signal so the AvgTrade & StdDev of individual
trade returns can be exported to a csv file.

In order to double check the results produced by the code, I manually
saved the trade returns to an Excel file straight from the "Trades"
section of the TS performance summary. I then used Excel's Std Dev
function (Both sample & population) to see if the results jived with
those produced by my code below.

Unfortunately, the results did not match, and I cannot figure out why.

Is it perhaps because I'm using the built in Strategy Performance
functions "TotalTrades" and "PositionProfit"? Has any one experienced
unreliable results when using the built in Strategy Performance
functions?

Any ideas are apreciated.

Lance Fisher



{Code below}


Vars: AvgTrd(00), SDEV(00), Summer(00), Cntr1(00), Cntr2(00),
SumVar(00);

If LastBarOnChart then begin
	
AvgTrd = 00;
SDEV = 00;

If TotalTrades <> 00 then begin; 
Summer = 00;
 For Cntr1 = 0 to TotalTrades - 1 begin {Calculate Average of all
trades}
 	Summer = Summer + PositionProfit(Cntr1); 
 End;
 
 AvgTrd = Summer / TotalTrades;
 SumVar = 00;

 For Cntr2 = 0 to TotalTrades - 1 begin {Calculate the variances (or
rather, the sum of the variances)}
 	SumVar = SumVar + (PositionProfit(Cntr2) -
AvgTrd)*(PositionProfit(Cntr2) - AvgTrd) / TotalTrades;
 End;
 SDEV = SquareRoot(SumVar); {StdDev = the SqRoot of the sum of the
variances. Right?}
End
Else
	SDEV = 00;

  FileAppend(FileName, Text(GetSymbolName, NetProfit/TotalTrades, SDEV)
+ NewLine) ; {Print AvgTrade & StdDev to a csv file for further
analysis}
End;