CAGR formula translation

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #13470 quote
    Stef
    Participant
    Average

    How do I translate this to Probuilder language:

    cagr=((equity/startcapital)^(1/nryears))-1

    PRT does not like the “^” (power of) symbol.

    Regards

    #13509 quote
    Nicolas
    Keymaster
    Master

    I’m sorry, I spent almost 2 hours doing maths formulas to solve the MathPow being not available in math instructions of the platform.. Didn’t solve the formula because of the fractionnal exponent. Seems that I could calculate something if the ‘nryears’ is below 3 but not above.

    I have just sent a request to PRT dev team to add the MathPow possibility in the language, as it is not the first time i’m stuck with this! 🙁

    If any math genius come here, I’ll be glad to have his opinion 🙂

    #13512 quote
    JC_Bywan
    Moderator
    Master

    Ok, I guess I’ve become a PRC-addict, even when I’m away from my trading station I feel the need to come and read what happened since last visit, haha

    Assuming nryears is an integer, how about a recursive approach, for which the number of iterations would depend on the required precision in the end by the user?

    Let’s say to simplify the formula writing that A=equity/startcapital and n=nryears

    and let’s name B = (equity/startcapital)^(1/nryears) = A^(1/n)

    so B^n = A = B * B^(n-1)

    B = A / B ^(n-1)

    and if I use i to rank the iterations from 1 to maximum i desired for sufficient precision and {} for index:

    B{rank i+1} =  ( B{rank i} + B{rank i} ) / 2

    B{rank i+1} =  ( B{rank i} + A / (B{rank i} ^(n-1) ) / 2

    and a code with 2 loops to use the formula:

    something like:

     

    B=1
    
    for i=1 to 10 //(try 20 and 30 and... to see how much is enough for required accuracy in the end)
    
    previousB=B
    
    den=B
    
    for j=2 to nryears-1
    den=den*B
    next
    
    B=(previousB + (equity/startcapital)/den)/2
    
    next
    
    cagr=B-1

    However, I can’t launch prt and test it from here, so it’s probably bugged as I often need to make changes to first writing before getting it right… Also, I have never tried a loop inside a loop in PRT before, don’t know if it can handle it.

    Nicolas thanked this post
    #13516 quote
    Stef
    Participant
    Average

    @Nicolas, I could not solve it either. But, I did come up with the following “indicator” – that can be added to the end of any strategy backtesting code.

    // yearly and monthly returns
    once startcapital=100000 // initial capital amount
    once yearstartcap=startcapital // set initial capital for 1st year
    once mstartcap=startcapital // set initial capital for 1st month
    
    if day<day[1] and month=1 then // new year
    endcapital=startcapital+strategyprofit // ending capital for the year (previous year)
    prof=endcapital-yearstartcap // profit for the year (previous year)
    yearstartcap=endcapital // set starting capital for the new year
    yearreturn=(prof/startcapital)*100 // yearly return as percentage (previous year)
    endif
    
    if day<day[1] and month>month[1] then // new month
    mendcapital=startcapital+strategyprofit // ending capital for the month (previous month)
    mprof=mendcapital-mstartcap // profit for the month (previous month)
    mstartcap=mendcapital // set starting capital for the new month
    mreturn=(mprof/startcapital)*100 // monthly return as percentage (previous month)
    endif
    
    
    graph 0 coloured(0,0,255) // graph zero line
    graph yearreturn coloured(0,200,0) as "Yearly Return %" // graph yearly return (prev year)
    graph mreturn coloured(154,0,154) as "Monthly Return %" // graph monthly return (prev month)

    It calculates yearly and monthly returns, and looks like this (see attachment).

    I would appreciate any comments or suggestions for improvements.

     

    Regards

    Stef

    Bard thanked this post
    Capture.png Capture.png Capture-1.png Capture-1.png
    #13522 quote
    Stef
    Participant
    Average

    @Noobywan. Tx, will try it.

    #13525 quote
    Nicolas
    Keymaster
    Master

    @Noobywan

    Your code is good. It successfully returned the good value for the Wikipedia example attached.

    Here is the code with the same value:

    B=1
    equity = 13000
    startcapital = 9000
    nryears = 3
    
    for i=1 to 10 //(try 20 and 30 and... to see how much is enough for required accuracy in the end)
    
    previousB=B
    
    den=B
    
    for j=2 to nryears-1
    den=den*B
    next
    
    B=(previousB + (equity/startcapital)/den)/2
    
    next
    
    cagr=B-1
    
    RETURN cagr

    Well done! Fractional exponent gimme headache yesterday, you desserve a beer again! 🙂 Maybe I’ll add a post in the Blog in your own name.

    cagr-calculation.png cagr-calculation.png cagr-prorealtime.png cagr-prorealtime.png
    #13555 quote
    Nicolas
    Keymaster
    Master

    @Stef

    You add the code about monthly and yearly returns in the Library, but I don’t see any code of ITF file attached to your post. Could you please, gimme here? Thanks.

    #13576 quote
    Stef
    Participant
    Average

    Here you go…

    Nicolas thanked this post
    yearmonthreturn.itf
    #14037 quote
    JC_Bywan
    Moderator
    Master

    No problem for blog post if you think it can be useful to compensate for the lack of mathpow, with or without my name doesn’t matter, I don’t own the rights to recursivity

    I am going to write a “carte de fidélité – bières” with squares to stamp the beers on, in case some day I happen to drive near your headquarters 😉

    #14038 quote
    Nicolas
    Keymaster
    Master

    Ahaha noobywan, you just have changed my mood from dark to hilarious, thanks a lot. Always got beers at Nicolas’HQ, no worries 😂

    #98204 quote
    undefined
    Participant
    Master

    codeforpower = exp(5*log(2))                 //                result = 32 = 2^5
    codeforlogarithm = log(32) / log (2)               //                result = 5

    Nicolas thanked this post
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

CAGR formula translation


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Stef @stef Participant
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by undefined
6 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 09/21/2016
Status: Active
Attachments: 5 files
Logo Logo
Loading...