def test_pandl(self):
        fx = pd.DataFrame([2.0] * 10, dt_range1)
        price = pd.DataFrame(
            [100, 103, 105, 106, 110, 105, 104.5, np.nan, 120, np.nan, 142],
            dt_range2)

        trades = pd.concat([
            pd.DataFrame(
                dict(trades=[2, 1, -1, np.nan, 1],
                     fill_price=[102.9, 105.5, 106.5, np.nan, 106.]),
                pd.date_range(start=pd.datetime(2015, 1, 2), periods=5)),
            pd.DataFrame(
                dict(trades=[-1, 1, -1], fill_price=[107, 119, 132]),
                pd.date_range(start=pd.datetime(2015, 1, 8), periods=3))
        ])

        ans = pandl(price, trades, marktomarket=True, fx=fx)
        np.testing.assert_almost_equal(
            ans.pandl_base[1:],
            [0.0, 10.4, 6., 14., -16., -9., 15., 48., 78., 40.])

        ans2 = pandl(price, trades, marktomarket=False, fx=fx)

        np.testing.assert_almost_equal(ans2.pandl_base[1:],
                                       [10.4, 6., 0., -2., 6., 48., 78.])
    def test_pandl(self):
        fx = pd.DataFrame([2.0] * 10, dt_range1)
        price = pd.DataFrame(
            [100, 103, 105, 106, 110, 105, 104.5, np.nan, 120, np.nan,
             142], dt_range2)

        trades = pd.concat([
            pd.DataFrame(
                dict(
                    trades=[2, 1, -1, np.nan, 1],
                    fill_price=[102.9, 105.5, 106.5, np.nan, 106.]),
                pd.date_range(start=pd.datetime(2015, 1, 2), periods=5)),
            pd.DataFrame(
                dict(trades=[-1, 1, -1], fill_price=[107, 119, 132]),
                pd.date_range(start=pd.datetime(2015, 1, 8), periods=3))
        ])

        ans = pandl(price, trades, marktomarket=True, fx=fx)
        np.testing.assert_almost_equal(ans.pandl_base[1:], [
            0.0, 10.4, 6., 14., -16., -9., 15., 48., 78., 40.
        ])

        ans2 = pandl(price, trades, marktomarket=False, fx=fx)

        np.testing.assert_almost_equal(ans2.pandl_base[1:],
                                       [10.4, 6., 0., -2., 6., 48., 78.])
示例#3
0
        def _pandl_for_instrument(system, instrument_code, this_stage,
                                  percentage, delayfill, roundpositions):

            this_stage.log.msg("Calculating pandl for instrument for %s" %
                               instrument_code,
                               instrument_code=instrument_code)

            price = this_stage.get_daily_price(instrument_code)
            positions = this_stage.get_notional_position(instrument_code)
            fx = this_stage.get_fx_rate(instrument_code)
            value_of_price_point = this_stage.get_value_of_price_move(
                instrument_code)

            if percentage:
                capital = this_stage.get_notional_capital()
            else:
                capital = None

            instr_pandl = pandl(price=price,
                                positions=positions,
                                delayfill=delayfill,
                                roundpositions=roundpositions,
                                fx=fx,
                                value_of_price_point=value_of_price_point,
                                capital=capital)

            return instr_pandl
示例#4
0
 def _pandl_for_instrument_forecast(system, instrument_code, rule_variation_name,  this_stage, delayfill):
     price=this_stage.get_instrument_price(instrument_code)
     forecast=this_stage.get_capped_forecast(instrument_code, rule_variation_name)
     get_daily_returns_volatility=this_stage.get_daily_returns_volatility(instrument_code)
     
     pandl_fcast=pandl(price=price,   delayfill=delayfill,  
                        get_daily_returns_volatility=get_daily_returns_volatility, 
                        forecast=forecast, capital=0.0)
     return pandl_fcast
示例#5
0
 def _pandl_for_instrument(system, instrument_code, this_stage, percentage, delayfill, roundpositions):
     price=this_stage.get_instrument_price(instrument_code)
     positions=this_stage.get_notional_position(instrument_code)
     fx=this_stage.get_fx_rate(instrument_code)
     value_of_price_point=this_stage.get_value_of_price_move(instrument_code)
     
     if percentage:
         capital=this_stage.get_notional_capital()
     else:
         capital=None
     
     instr_pandl=pandl(price=price,  positions=positions, delayfill=delayfill, roundpositions=roundpositions, 
                     fx=fx, value_of_price_point=value_of_price_point, capital=capital )
 
     return instr_pandl
示例#6
0
文件: account.py 项目: Sayan-Paul/kod
        def _pandl_for_instrument_forecast(
                system, instrument_code, rule_variation_name, this_stage, delayfill):

            this_stage.log.msg("Calculating pandl for instrument forecast for %s %s" % (instrument_code, rule_variation_name),
                               instrument_code=instrument_code, rule_variation_name=rule_variation_name)

            price = this_stage.get_daily_price(instrument_code)
            forecast = this_stage.get_capped_forecast(
                instrument_code, rule_variation_name)
            get_daily_returns_volatility = this_stage.get_daily_returns_volatility(
                instrument_code)

            pandl_fcast = pandl(price=price, delayfill=delayfill,
                                get_daily_returns_volatility=get_daily_returns_volatility,
                                forecast=forecast, capital=0.0)
            return pandl_fcast
示例#7
0
文件: account.py 项目: Sayan-Paul/kod
        def _pandl_for_subsystem(
                system, instrument_code, this_stage, percentage, delayfill, roundpositions):

            this_stage.log.msg("Calculating pandl for subsystem for instrument %s" % instrument_code,
                               instrument_code=instrument_code)

            price = this_stage.get_daily_price(instrument_code)
            positions = this_stage.get_subsystem_position(instrument_code)
            fx = this_stage.get_fx_rate(instrument_code)
            value_of_price_point = this_stage.get_value_of_price_move(
                instrument_code)

            if percentage:
                capital = this_stage.get_notional_capital()
            else:
                capital = None

            instr_pandl = pandl(price=price, positions=positions, delayfill=delayfill, roundpositions=roundpositions,
                                fx=fx, value_of_price_point=value_of_price_point, capital=capital)

            return instr_pandl
示例#8
0
        def _pandl_for_instrument_forecast(system, instrument_code,
                                           rule_variation_name, this_stage,
                                           delayfill):

            this_stage.log.msg(
                "Calculating pandl for instrument forecast for %s %s" %
                (instrument_code, rule_variation_name),
                instrument_code=instrument_code,
                rule_variation_name=rule_variation_name)

            price = this_stage.get_daily_price(instrument_code)
            forecast = this_stage.get_capped_forecast(instrument_code,
                                                      rule_variation_name)
            get_daily_returns_volatility = this_stage.get_daily_returns_volatility(
                instrument_code)

            pandl_fcast = pandl(
                price=price,
                delayfill=delayfill,
                get_daily_returns_volatility=get_daily_returns_volatility,
                forecast=forecast,
                capital=0.0)
            return pandl_fcast
    fast_ewma = pd.ewma(price, span=Lfast)
    slow_ewma = pd.ewma(price, span=Lslow)
    raw_ewmac = fast_ewma - slow_ewma

    vol = robust_vol_calc(price.diff())

    return divide_df_single_column(raw_ewmac, vol)

"""
Try it out

(this isn't properly scaled at this stage of course)
"""
instrument_code = 'EDOLLAR'
price = data.get_instrument_price(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
print(ewmac.tail(5))

from matplotlib.pyplot import show
ewmac.plot()
show()

"""
Did we make money?
"""

from syscore.accounting import pandl
account = pandl(price, forecast=ewmac)
account.curve().plot()
show()
示例#10
0
    vol = robust_vol_calc(price.diff())

    return divide_df_single_column(raw_ewmac, vol)


"""
Try it out

(this isn't properly scaled at this stage of course)
"""
instrument_code = 'EDOLLAR'
price = data.get_daily_price(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
ewmac.columns = ['forecast']
print(ewmac.tail(5))

from matplotlib.pyplot import show
ewmac.plot()
show()
"""
Did we make money?
"""

from syscore.accounting import pandl
account = pandl(price, forecast=ewmac, capital=0.0)
account.curve().plot()
show()

print(account.stats())
    return divide_df_single_column(raw_ewmac, vol)


"""
Try it out

(this isn't properly scaled at this stage of course)
"""
instrument_code = "EDOLLAR"
price = data.get_daily_price(instrument_code)
ewmac = calc_ewmac_forecast(price, 32, 128)
ewmac.columns = ["forecast"]
print(ewmac.tail(5))

from matplotlib.pyplot import show

ewmac.plot()
show()

"""
Did we make money?
"""

from syscore.accounting import pandl

account = pandl(price, forecast=ewmac, capital=0.0)
account.curve().plot()
show()

print(account.stats())