def _calculate_leverage(values_by_stock, ts_leverage, ts_long_exposure, ts_short_exposure, ts_net_exposure):

    for r_index, r_val in values_by_stock.iterrows():
        f_long = 0
        f_short = 0
        for val in r_val.values[:-1]:
            if np.isnan(val) == False:
                if val >= 0:
                    f_long = f_long + val
                else:
                    f_short = f_short + val
                
        f_lev = (f_long + abs(f_short)) \
                /(f_long + r_val.values[-1] + f_short)
        f_net = (f_long - abs(f_short)) \
                /(f_long + r_val.values[-1] + f_short)
        f_long_ex = (f_long) \
                /(f_long + r_val.values[-1] + f_short)
        f_short_ex = (abs(f_short)) \
                /(f_long + r_val.values[-1] + f_short)                

        if np.isnan(f_lev): f_lev = 0
        if np.isnan(f_net): f_net = 0
        if np.isnan(f_long): f_long = 0
        if np.isnan(f_short): f_short = 0

        ts_leverage = ts_leverage.append(pand.Series(f_lev, index = [r_index] ))
        ts_long_exposure = ts_long_exposure.append(pand.Series(f_long_ex, index = [r_index] ))
        ts_short_exposure = ts_short_exposure.append(pand.Series(f_short_ex, index = [r_index] ))
        ts_net_exposure = ts_net_exposure.append(pand.Series(f_net, index = [r_index] ))

    return ts_leverage, ts_long_exposure, ts_short_exposure, ts_net_exposure
@summary calculates leverage based on the dataframe values_by_stock
dataset/ETHPy150Open QuantSoftware/QuantSoftwareToolkit/QSTK/qstksim/tradesim.py/_calculate_leverage