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

Re: How to calculate STDev?



PureBytes Links

Trading Reference Links

I messed around with this StdDev of individual trade
returns stuff about 2 years ago. I was unable to do it
without using an array to store the individual trade
results, so I was skeptical when you claimed success
w/ no arrays. 

So I plugged your math into an old piece of system
stat exporter code I wrote awhile back, and lo &
behold, it does indeed match the results calculated in
Excel. Now I wish I'd paid better attention in math
class. Code is below & attached.

The problem I had when originally programming this
using an array, is that the stock StdDevS_a function
doesn't include the zero array element in it's
calculation. I got around this by writing my own
function that used the zero element, but just feeding
the array starting at 1 instead of zero would probably
have worked just as well.

-Lance

{Code below}
{
Instructions:

This was created in 2000i. I have no idea if it will
work in 4, 6, or 7.

Type your .csv file destination in the FileName input
("inside the parenthesis")

If you only want to print stats on one chart, for Ex.
Microsoft, then type MSFT in 
the SymName input to print the header row above the
stats. 

If you want to print stats on, for Ex. every stock in
the Naz100 (using Workspace Assistant),
type the first symbol in the symbol list (in this case
"AAPL") in the SymName input. The header 
row will print only once across the top 3 rows.

If you have difficulty making this work, please e-mail
Gary Fritz for help, not me (Just kidding).
Actually, if you need assistance, contact Omega (or
whatever they call themselves now) & demand 
that they support their product by helping you get
through this.

Enjoy,
-Lance Fisher
}

Inputs: LookBack(20), 
	    MakeFile(00),
	    FileName ("Type your file destination here"),
	    SymName  ("Type symbol of tradeable here");

Vars: MPe(00), SumX(0), SumX2(0), Ntrades(0),
STDEV(0);

{Simple Channel BO
System********************************************************}


Buy next bar at Highest(High, Lookback) stop;

Sell next bar at Lowest(Low, LookBack) stop;

{StdDev of Individual Trade Returns Code (supplied by
Gary Fritz) ***************}

MPe = MarketPosition(0);	

If (MPe[1] = 1 AND MPe[0] <> 1) OR (MPe[1] = -1 AND
MPe[0] <> -1) then begin
	Ntrades = Ntrades + 1;
    
    SumX = SumX + PositionProfit(1);
    SumX2 = SumX2 + Square(PositionProfit(1));
end;

if LastBarOnChart and Ntrades <> 0 then begin
    STDEV = SquareRoot((SumX2 - square(SumX)/Ntrades)
/ (Ntrades-1));
end;

{.CSV File Exporting
Code*********************************************************}

If LastBarOnChart AND MakeFile = 01 then begin
  If GetSymbolName = SymName then begin
		
	   FileAppend(FileName, Text( " ", ",", " ", ",", "
", ",", " ", ",", "NetP", ",", 
		                         "Num", ",", "Num", ",", "
", ",", " ", ",", " ", ",",          
			                     " ", ",", " ", ",", " ", ",",
"Max", ",", "Max", ",",
			   		             "Avg", ",", "Avg", ",", "Avg",
",", "Pct", ",", " ", ",",
							     "Std") + NewLine +

		                    Text( " ", ",", "Net", ",",
"Num", ",", "Prof", ",", "/", ",", 
		                          "Win", ",", "Los", ",",
"Pct", ",", "Avg", ",", "Avg", ",",          
			                      "Avg", ",", "Big", ",",
"Big", ",", "Cons", ",", "Cons", ",",
			   		              "Bars", ",", "Bars", ",",
"Bars", ",", "Time", ",", "TS", ",",
								  "Dev") + NewLine + 

		                    Text( "Mkt", ",", "Prof", ",",
"Trds", ",", "Fact", ",", "DD", ",", 
		                          "Trds", ",", "Trds", ",",
"Prof", ",", "Trd", ",", "Win", ",",          
			                      "Loss", ",", "Win", ",",
"Loss", ",", "Win", ",", "Loss", ",",
			   		              "Win", ",", "Loss", ",", "Trds",
",", "Mkt", ",", "DD", ",",
		                          "Trds") + NewLine);
  End;

  FileAppend(FileName, Text(GetSymbolName, ", ",
NetProfit, ", " , TotalTrades,
  	         ", ", GrossProfit/AbsValue(GrossLoss), ",
", NetProfit/AbsValue(MaxIDDrawDown),
			 ", ", NumWinTrades, ", ", NumLosTrades, 
		     ", ", PercentProfit, ", ",
NetProfit/TotalTrades, 
	 	     ", ", GrossProfit/NumWinTrades, ", ",
GrossLoss/NumLosTrades, 
	         ", ", LargestWinTrade, ", ",
LargestLosTrade, 
	         ", ", MaxConsecWinners, ", ",
MaxConsecLosers, 
             ", ", AvgBarsWinTrade, ", ",
AvgBarsLosTrade, 
             ", ",
(TotalBarsWinTrades+TotalBarsLosTrades)/TotalTrades,
			 ", ",
(TotalBarsWinTrades+TotalBarsLosTrades)/CurrentBar,
		     ", ", MaxIDDrawDown, ", ", STDEV) + NewLine) ;
End;

Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com
Attachment:
STATREPORT.ELA

Attachment: Description: "Description: STATREPORT.ELA"