def analyze_transactions(filename, plot_name, share_table, show_transactions=False):
    html_file  =  open("./"+plot_name+"/report-"+plot_name+".html","a")
    html_file.write("<pre>\n\nTransaction Statistics\n")
    #calc stats

    #first pass
    reader=csv.reader(open(filename,'rU'), delimiter=',')
    reader.next()
    prev=0
    first=1
    diffs=[]
    volume=0
    start=0
    sold=0
    bought=0
    end=0
    rets=[]
    efficiencies=[]
    holds=[]
    commissions=[]
    # slippage=[]
    buy_dates=[] #matrix of when stocks were bought (used for matches)
    for row in reader:
        weighted_ret=0
        weighted_e=0
        weighted_hold=0
        num_stocks=0
        volume+=1
        if(row[7]!=''):
            commissions.append(float(row[7]))
            # slippage.append(float(row[8]))
        if first:
            #add na for first trade efficiency
            start=dp.parse(row[3])
            first=0
            prev=dp.parse(row[3])
        else:
            if row[2] == "Buy":
                bought=bought+float(row[5])
                buy_dates.append({"date":dp.parse(row[3]),"stock":row[0],"amount":row[4],"price":float(row[5])})
            elif row[2] == "Sell":
                #sold at price
                sold=sold+float(row[5])
                #get number of stocks for this sell
                stocks=float(row[4])
                #try and match trade (grab first date of stocks)
                for date in buy_dates:
                    #while stocks are left
                    if(stocks>0):
                        #match a date
                        if(date["stock"]==row[1]):
                            stocks_sold=0
                            #use as many stocks from date as necessary
                            leftover=float(date["amount"])-stocks
                            if(leftover>0):
                                date["amount"]=leftover
                                stocks_sold=stocks
                                #compute stats
                                temp_e=calculate_efficiency(date["date"], dp.parse(row[3]), row[0])
                                weighted_ret=(weighted_ret*num_stocks+(float(row[5])/date["price"])*stocks_sold)/(num_stocks+stocks_sold)
                                weighted_hold=(weighted_hold*num_stocks+(dp.parse(row[3])-date["date"]).days*stocks_sold)/(num_stocks+stocks_sold)
                                weighted_e=(weighted_e*num_stocks+temp_e*stocks_sold)/(num_stocks+stocks_sold)
                                num_stocks=num_stocks+stocks_sold
                                break
                            else:
                                stocks_sold=float(date["amount"])
                                stocks=stocks-stocks_sold
                                #compute stats
                                temp_e=calculate_efficiency(date["date"], dp.parse(row[3]), row[0])
                                weighted_ret=(weighted_ret*num_stocks+(float(row[5])/date["price"])*stocks_sold)/(num_stocks+stocks_sold)
                                weighted_hold=(weighted_hold*num_stocks+(dp.parse(row[3])-date["date"]).days*stocks_sold)/(num_stocks+stocks_sold)
                                weighted_e=(weighted_e*num_stocks+temp_e*stocks_sold)/(num_stocks+stocks_sold)
                                num_stocks=num_stocks+stocks_sold
                                date["stock"]="DONE"
                                #buy_dates.remove(date)
            #elif row[2] == "Sell Short":
                #do nothing
            #elif row[2] == "Buy to Cover":
                #do nothing
            # elif row[2] == "Deposit Cash":

            if(prev!=dp.parse(row[3])):
                diffs.append(dp.parse(row[3])-prev)
                prev=dp.parse(row[3])
                end=prev
        holds.append(weighted_hold)
        efficiencies.append(weighted_e)
        rets.append(weighted_ret*100)

    avg_period=sum(diffs, dt.timedelta(0))/len(diffs)
    avg_hold=_ignore_zeros_average(holds)
    t=sold/(bought+sold)
    turnover=t/(end-start).days
    efficiency=_ignore_zeros_average(efficiencies)
    avg_com=_ignore_zeros_average(commissions)
    # avg_slip=_ignore_zeros_average(slippage)
    avg_ret=_ignore_zeros_average(rets)

    #print stats
    html_file.write("\nNumber of trades:         %10d" % volume)
    html_file.write("\nAverage Trading Period:   %10s" % str(avg_period).split(",")[0])
    html_file.write("\nAverage Position Hold:    %5d days" % avg_hold)
    html_file.write("\nAverage Daily Turnover:   %%%9.4f" % (turnover*100))
    html_file.write("\nAverage Trade Efficiency: %%%9.4f" % (efficiency*100))
    html_file.write("\nAverage Commissions:      %10d" % avg_com)
    # html_file.write("\nAverage Slippage:         %10d" % avg_slip)
    html_file.write("\nAverage Return:           %%%9.4f\n\n" % avg_ret)

    html_file.write("Positions by Date\n")
    for date in share_table.index:
        html_file.write("\nPosition for day: "+str(date).split()[0])
        html_file.write("\n")
        i=0
        for item in share_table.ix[date]:
            if(item>0):
                html_file.write("%15s" % str(share_table.columns[i]))
            i=i+1
        html_file.write("\n")
        for item in share_table.ix[date]:
            if(item>0):
                html_file.write("%15s" % str(item))
        html_file.write("\n\nTransactions for day:\n")
        reader=csv.reader(open(filename,'rU'), delimiter=',')
        a=0
        cash=0
        if(show_transactions):
            for row in reader:
                if a==0:
                    html_file.write("   Date      | ")
                    html_file.write("   Name    | ")
                    html_file.write("   Type    | ")
                    html_file.write("  Price    | ")
                    html_file.write("  Shares   | ")
                    html_file.write("Commission | ")
                    # html_file.write(" Slippage  | ")
                    html_file.write("OnHand Cash| ")
                    html_file.write("Efficiency  | ")
                    html_file.write(" Returns    ")
                    html_file.write(" | ")
                    html_file.write("\n")
                    a=1
                else:
                    compute=False
                    if(len(str(row[3]).split())>2):
                        compute=(dt.datetime.strptime(str(row[3]),"%b %d, %Y")==date)
                    else:
                        compute=(str(row[3])==str(date))
                    if(compute):
                        var=row[2]
                        if var == "Cash Deposit":
                            cash=cash+float(row[6])
                            var="Deposit"
                        elif var == "Cash Withdraw":
                            cash=cash-float(row[6])
                            var="Withdraw"
                        else:
                            cash=cash-float(row[6])
                        var=var.split(" ")[0]
                        html_file.write("%12s | " % str(row[3].split(':')[0]))
                        html_file.write("%10s | " % str(row[0]))
                        html_file.write("%10s | " % str(var))
                        html_file.write("%10s | " % str(row[5]))
                        html_file.write("%10s | " % str(row[4]))
                        html_file.write("%10s | " % str(row[7]))
                        html_file.write("%10s | " % str(row[8]))
                        html_file.write("%10s | " % str(round(cash,2)))
                        html_file.write(" %%%9.2f | " % (efficiencies[a-1]*100))
                        html_file.write(" %%%9.2f " % (rets[a-1]))
                        a=a+1
                        html_file.write(" | ")
                        html_file.write("\n")

    html_file.close()
@summary computes various statistics for given filename and appends to report assumed via plot_name
dataset/ETHPy150Open QuantSoftware/QuantSoftwareToolkit/QSTK/qstktools/csv2fund.py/analyze_transactions