Forum Moderators: coopster

Message Too Old, No Replies

Consecutive negative numbers

tracking consecutive negative numbers

         

brian7742

6:49 am on Jan 9, 2010 (gmt 0)

10+ Year Member



Hello,

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.

TheMadScientist

7:17 am on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The first thing that comes to mind is to select the col, maybe based on date range if it gets to be too many, and loop through them with PHP?


$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.

TheMadScientist

7:27 am on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$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().

brian7742

12:49 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



Oh man I had forgotten I made this post and have been banging my head since haha.

Your code worked perfectly! Thank you so much for your help.