Forum Moderators: coopster
I've hit a wall in my current project that I can't seem to break through. I would greatly appreciate any help.
I am tracking investment strategies via mysql and php. Closed trades are entered into the mysql DB one at a time.
What I need to do is come up with the largest consecutive amount of loss before the strategy began to recoup itself. This is known as the maximum draw down.
For example: (mysql DB entries)
1: Trade 1: +100
2: Trade 2: -100
3: Trade 3: -100
4: Trade 4: +300
In the example above, The maximum draw down would be -200, and that would be known when a new high balance was made, on Trade 4.
Does anyone have any idea on how I can do this? I just can't seem to wrap my head around how to track this statistic.
Any help would be greatly appreciated.
$loss_periods=0; $loss=array(); $loss_tracked=0;/* I'm calling the DB Col 'amount' */
while($result=mysql_fetch_array($query)) {
if(intval($result['amount'])<0) {
$loss[$loss_periods]['amount']+=abs(intval($result['amount']));
$loss[$loss_periods]['days']++;
$loss_tracked=1;
}
elseif($loss_tracked===1) {
$loss_periods++;
$loss_tracked=0;
}
}for($i=0; $i < $loss_periods; $i++) {
echo '<pre>';
print_r($loss[$i]);
echo '</pre>'."\n";
}
Something like the preceding should give you an idea or two...
I haven't tested though, just coding here on the site.
$loss_periods=0; $loss=array(); $loss_tracked=0;/* I'm calling the DB Col 'amount' */
while($result=mysql_fetch_array($query)) {
if(intval($result['amount'])<0) {
$loss['amount'][$loss_periods]+=abs(intval($result['amount']));
$loss['days'][$loss_periods]++;
$loss_tracked=1;
}
elseif($loss_tracked===1) {
$loss_periods++;
$loss_tracked=0;
}
}asort($loss['amount']);
print_r($loss['amount']);
I forgot to give you the highest value before, I just edited the preceding post for this one and left it so you could see both versions... This one will order the array $loss['amount'] based on value and keep the key intact, so you should be able to get the key and keep it together with the number of days there were losses for to get to the amount. Make sure you add the - sign to the front if you don't have them designated as a loss some other way, because I used the absolute value for totaling, so the numbers are positive in the array().