Click here to Login




How to calculate the average of a time series using the money management tool

Updated on 2012-03-17





Using the money management tool, you can dynamically calculate metrics and then take trading decisions based on these metrics.

For example, you can calculate the number of consecutive losing trades for each trading day using the following script in the "OnClosePosition" event:

int losers = 0;
if(Variables.IsVariableExists("MaxConsecutiveLoser"))
{
losers = (int)Variables.GetVariable("MaxConsecutiveLoser");
}

if(Functions.GetPositionDetails().Performance < 0)
{
losers++;
}
else
{
losers = 0;
}
Variables.SetVariable("MaxConsecutiveLoser", (int)losers);


In the "OnEndPeriod" event, you can then get the value of the "MaxConsecutiveLoser" variable and stores it in an array using "Variables.SetVariable" function.

To get an average value over N-days, use the following code:

if(Variables.IsVariableExists("MaxConsecutiveLoser"))
{
int losers = (int)Variables.GetVariable("MaxConsecutiveLoser");

ArrayList losersArray = new ArrayList();
if(Variables.IsVariableExists("MaxConsecutiveLoserArray"))
{
losersArray = (ArrayList)Variables.GetVariable("MaxConsecutiveLoserArray");
}

losersArray.Add((double)losers);

double sum = Sum(losersArray, 10);
double average = sum / 10;

Variables.SetVariable("MaxConsecutiveLoserArray", losersArray);

Divers.Output(losers + "\n" + " - Array Count: " + losersArray.Count + " - Sum: " + sum + " - Avg: " + average); // Output visible in Trading System Report -> Details tab
}

#functions#
double Sum(ArrayList list, int bars)
{
double sum = 0;
int start = Math.Max(0, list.Count - bars);
for(int i=start;i < list.Count;i++)
{
sum = sum + (double)list[i];
}
return sum;
}


The time-series, which is stored in an Array, is passed to the "Sum" function to calculate its sum over N-bars. The result is then divided by "N" to get an average value.










one comment (Log in)

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.