Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looping in python
#11
import pandas as pd
import numpy as np
def perfomance_statistics(initial_capital,pnl):
        
      
    max_draw_down=max_draw_down(initial_capital,pnl)         # max drawdown
    #maxdrawdown=50000
    avgret=ret.mean()                                       # average return                                       
    
    sdret=ret.std()                                          # standard deviation return
    
    sharperatio = avgret / sdret                            #sharpe ratio
    
    #sharperatio = (avgret - expectedannualizedreturn) / sdret
    #sharperatio = (avgret - expectedannualizedreturn/365) / sdret
    # annualizedsharperatio = (avgret * 365 - expectedannualizedreturn) / (sdret * 250**0.5)
    
    calmarratio=avgret/abs(max_draw_down)                     # calmar ratio
    
    consecutivelossctr=0                                    # concecutive loses 
    maxconsecutivelossctr=0
    for i in range(nooftrades):
        if ret[i]>=0:
            consecutivelossctr=0
        else:
            consecutivelossctr+=1
            if  maxconsecutivelossctr < consecutivelossctr:
                maxconsecutivelossctr = consecutivelossctr
    report=R()
    report.profittrades=profittrades
    report.noofprofittrades=noofprofittrades
    report.maxprofitpertrades=maxprofitpertrades
    report.sumprofits=sumprofits
    report.avgprofitpertrades=avgprofitpertrades
    report.losstrades=losstrades
    report.nooflosstrades=nooflosstrades
    report.maxlosspertrades=maxlosspertrades
    report.sumlosses=sumlosses
    report. avglossespertrades= avglossespertrades
    report.nooftrades=nooftrades
    report.sumofret=sumofret
    report.avgret=avgret
    report.hitratio=hitratio
    report.profitfactor=profitfactor
    report.ratioavgwinavgloss=ratioavgwinavgloss
    report.avgwinningtrades=avgwinningtrades
    report.avgloosingtrades=avgloosingtrades
    report.grossprofit=grossprofit
    report.grossloss=grossloss
    report.profitfactor=profitfactor
    report.max_draw_down=max_draw_down
    report.avgret=avgret
    report.sdret=sdret
    report.calmarratio=calmarratio
    report.maxconsecutivelossctr=maxconsecutivelossctr
    reort.max_draw_down=max_draw_down
    report.sharperatio=sharperatio
    
    return report
    
#     return(profittrades,noofprofittrades,maxprofitpertrades,sumprofits,avgprofitpertrades,losstrades,nooflosstrades,maxlosspertrades,sumlosses,
#           avglossespertrades,nooftrades,sumofret,avgret,hitratio,profitfactor, ratioavgwinavgloss,avgwinningtrades,avgloosingtrades,
#            grossprofit,grossloss,profitfactor,maxdrawdown,avgret,sdret,calmarratio,maxconsecutivelossctr)
            
            

        
def max_drawdown(initial_capital,pnl):
    cumul_pnl = np.cumsum(pnl)
    cum_ret = cumul_pnl / initial_capital
    noofrtns=len(pnl)
    ret=pnl/initial_capital
    high_water_mark = cum_ret[0]
    max_dd = 0.0
    for r in range(noofrtns):
        
        if cum_ret[r]>high_water_mark:
                    
            high_water_mark=cum_ret[r]
            dd=(1+cum_ret[r])/(1+high_water_mark)-1
            if dd < max_dd:
                 max_dd=dd
            return max_dd
class R(object):
    maxdrawdown=0
    avgret=0
    sdret =0
    calmarratio=0
    maxconsecutiveloss=0
    max_draw_down=0
    sharperatio=0
    
when I am importing to my jypter note book m getting this error
local variable 'max_draw_down' referenced before assignment
Reply
#12
You need to give us the full text of the traceback. Otherwise, we can't tell where the error is occurring. I don't see where that error would occur in the code you posted, but without the traceback I can't be sure.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
on line 6, right-hand side you have misspelled the name of the function.
it should be
 max_draw_down=max_drawdown(initial_capital,pnl)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping script and writing to Excel in Python Rolflund 2 3,152 Jan-21-2018, 10:31 PM
Last Post: j.crater
  Trouble with creating a looping function in python 2.7 (complex) Afterdarkreader 3 3,058 Dec-12-2017, 06:56 PM
Last Post: Afterdarkreader

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020