Pseudocode Drawdown Need Help

Discussion in 'App Development' started by K-Pia, Apr 3, 2016.

  1. K-Pia

    K-Pia

    Hi Guys,

    I use mathematica as my main programing language.
    However I am stuck with the drawdown calculation.
    Browsed the web to find a solution but not solved.

    How / What should I use to calculate it ?
    Could you give me a pseudocode ?
    Or the mathemica code ?

    The problem is that I do not know how to
    Tell it to forget past Min after a new Max...

    What's the concepts / tools involved ?
     
  2. userque

    userque

    I don't code in mathematica, but I assume you are finding these mins and max's over a certain time frame.

    You can use a sliding window of a set length. It moves forward for each trading day.

    Or, you can keep track of previous min/max's in variables.

    You really don't provide enough information (for me) to attempt writing pseudocode. :)
     
  3. K-Pia

    K-Pia

    Well ... I am asking for a template,
    How would you code it ?

    I am not for a fixed interval, time frame.
    But to reset the min when a new equity high is reached.
    Which is to reset the timeframe at each & every new high (Max).
    Then from there, DD = Max (New High) - Min (After New High)
     
    Last edited: Apr 3, 2016
  4. userque

    userque

    Maybe this helps? Not sure as I don't code mathmetica
    I think I understand you now.

    if new high then
    ....max=price
    ....DAYS=1
    end if
    min=minimal value from today going back DAYS days
    DAYS=DAYS+1
    DD=(max-min)/min*100
    ----------------
    If there is a new high, set the days variable to 1. This keeps track of the number of days since the new high.

    Everyday, set the min to the minimal value for the last DAYS days
    Everyday, recalculate DD as a percentage. It usually won't change.
    Everyday, add one to DAYS
     
    Last edited: Apr 3, 2016
    K-Pia likes this.
  5. userque

    userque

    If you are trying to do it un-dynamically...with static data:

    Find max
    Find position of max within stored data
    Find min from that position to most recent data
    DD=(max-min)/min*100

    I could probably do it in excel without even using code, if that would help. Are you trying to do this with static data, or "real-time" as new data is added?
     
    K-Pia likes this.
  6. dartmus

    dartmus

    This is for calculating drawdown. If u want max favorable excursion instead?... it's not much different, just change it around a bit.

    if u have built-in reserved words or functions for determining position status use them or declare your own. For instance when u enter a trade assign a val of 1 or -1 to a marketposition var or reset a counter to BarsSinceEntry=0 and increment the counter everybar the position is open.

    // this resets DD, pick 1 don't use both
    if marketposition<>mp[1] then drawdown=0;
    if barssinceentry=0 then DD=0;

    // update everybar
    if mp>0 then DD=maxlist(DD, entryprice-L);
    if mp<0 then DD=maxlist(DD,H-entryprice);

    I didn't check for accuracy. You might have to change the formulas slightly if my logic is wrong.
     
    K-Pia likes this.
  7. R1234

    R1234

    cumulative_equity_today = cumulative_equity_yesterday + pnl_today

    peak_today = max(peak_yesterday, cumulative_equity_today)

    dd = cumulative_equity_today - peak_today

    the above assumes a non compounded return stream. slight tweak will make it work for compounded.