|
Dave W.
2015-01-15 13:40:16
|
|
Hi. I'm working through how to accomplish certain things in an AMM script. What I'm trying to do is calculate an Average MAE. To do that, I think I need to:
1. Create an array to hold the MAE of each individual position. Do this on the On Close Position event.
2. Use the array created in the On Close Position event and average the results. Do this on the On End Simulation event.
Is that correct? If so, I think I'm appropriately capturing the MAE in the On Close Position event with this code:
// Get the MAE for each closed position, then add to the global tracking array.
double PositionMae = Functions.GetPositionDetails().MAE;
GlPositionMaeArray.Add( (double)PositionMae);
//Add 1 to the count of closed positions.
GlPositionCount =+ 1;
Is there an easy way to average the results? Any help or suggestions would be appreciated.
|
|
|
|
QuantShare
2015-01-16 02:12:51
0
|
|
Best Answer
Hi Dave,
You don't need to store MAE on the OnClosePosition event since that information is stored in "MMPosition" .
All you have to do is loop through all positions in the OnEndSimulation event and average the result.
MMPosition[] pos = Portfolio.GetAllPositions();
double avgMAE = 0;
for(int i=0;i < pos.Length;i++)
{
avgMAE += pos[i].MAE;
}
avgMAE = avgMAE / pos.Length;
|
|
|
|
Dave W.
2015-01-16 08:12:52
0
|
|
Excellent. Greatly appreciate your help.
|
|
|
|
Dave W.
2015-01-16 08:45:27
0
|
|
Is there a similarly simple solution to calculate Standard Deviation in the on OnEndSimulation event code?
I found the function below that takes an an array as input and calculates the standard deviation PL, but if there is as simpler solution, I'd rather use it.
Suggestions are welcome and appreciated...
private double getStandardDeviation(List<double> doubleList)
{
double average = doubleList.Average();
double sumOfDerivation = 0;
foreach (double value in doubleList)
{
sumOfDerivation += (value) * (value);
}
double sumOfDerivationAverage = sumOfDerivation / (doubleList.Count - 1);
return Math.Sqrt(sumOfDerivationAverage - (average*average));
}
|
|
|
|
QuantShare
2015-01-17 02:34:29
0
|
|
For each position, you will need to read close price series, get prices starting from the entry date then pass the array to the "getStandardDeviation" function.
|
|
|
|