Skip to content

Commit

Permalink
Merge pull request #29 from cmorgan/test
Browse files Browse the repository at this point in the history
fix some tests
  • Loading branch information
Robert Carver committed Sep 5, 2016
2 parents f899ab1 + fa94b59 commit 2a68e99
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 133 deletions.
4 changes: 2 additions & 2 deletions syscore/accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def pandl_with_data(price, trades=None, marktomarket=True, positions=None,


def get_positions_from_forecasts(price, get_daily_returns_volatility, forecast,
use_fx, value_of_price_point, daily_risk_capital,
**kwargs):
use_fx, value_of_price_point,
daily_risk_capital, **kwargs):
"""
Work out position using forecast, volatility, use_fx, value_of_price_point
(this will be for an arbitrary daily risk target)
Expand Down
9 changes: 6 additions & 3 deletions syscore/algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ def robust_vol_calc(x, days=35, min_periods=10, vol_abs_min=0.0000000001, vol_fl
:type floor_min_periods: int
:returns: pd.DataFrame -- volatility measure
"""

# Standard deviation will be nan for first 10 non nan values
Expand All @@ -143,7 +141,10 @@ def robust_vol_calc(x, days=35, min_periods=10, vol_abs_min=0.0000000001, vol_fl
vol, floor_days, floor_min_quant, floor_min_periods)
# set this to zero for the first value then propogate forward, ensures
# we always have a value
vol_min.set_value(vol_min.index[0], 0.0)

# TODO vol_min as series?
target_col = vol_min.columns[0]
vol_min.set_value(vol_min.index[0], target_col, 0.0)
vol_min = vol_min.ffill()

# apply the vol floor
Expand All @@ -152,6 +153,8 @@ def robust_vol_calc(x, days=35, min_periods=10, vol_abs_min=0.0000000001, vol_fl
else:
vol_floored = vol

if isinstance(vol_floored, pd.Series):
vol_floored = vol_floored.to_frame()
return vol_floored


Expand Down
8 changes: 4 additions & 4 deletions syscore/pdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def apply_cap(pd_series, capvalue):
>>> x=pd.Series([2.0, 7.0, -7.0, -6.99], pd.date_range(pd.datetime(2015,1,1), periods=4))
>>> apply_cap(x, 5.0)
2015-01-01 2
2015-01-02 5
2015-01-03 -5
2015-01-04 -5
2015-01-01 2.0
2015-01-02 5.0
2015-01-03 -5.0
2015-01-04 -5.0
Freq: D, dtype: float64
"""
# Will do weird things otherwise
Expand Down
150 changes: 76 additions & 74 deletions syscore/tests/test_Accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import pandas as pd
import numpy as np

from syscore.accounting import pandl, get_positions_from_forecasts, get_trades_from_positions
from syscore.accounting import (pandl_with_data,
get_positions_from_forecasts, )
# get_trades_from_positions) # DEPRECATED


dt_range1 = pd.date_range(start=pd.datetime(2014, 12, 30), periods=10)
Expand Down Expand Up @@ -49,77 +51,77 @@ def test_get_positions_from_forecasts(self):
np.testing.assert_almost_equal(position.position.values,
expected_pos)

def test_get_trades_from_positions(self):
positions = pd.DataFrame(
[np.nan, 2, 3, np.nan, 2, 3, 3.1, 4, 3, 5, 7],
dt_range2)

price = pd.DataFrame(
[100, 103, np.nan, 106, 110, 105, np.nan, 106, 120, np.nan, 142],
dt_range2)

# test delayed fill
delayfill = True
roundpositions = True
get_daily_returns_volatility = None
forecast = None
fx = None
value_of_price_point = None
trades = get_trades_from_positions(price,
positions,
delayfill,
roundpositions,
get_daily_returns_volatility,
forecast,
fx,
value_of_price_point)

np.testing.assert_almost_equal(
trades.trades,
[2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])

np.testing.assert_almost_equal(
trades.fill_price[:-1],
[106.0, 106.0, 105.0, 106.0, 120.0, 142.0, 142.0])

# test none delayed fill
delayfill = False
trades = get_trades_from_positions(price,
positions,
delayfill,
roundpositions,
get_daily_returns_volatility,
forecast,
fx,
value_of_price_point)

np.testing.assert_almost_equal(
trades.trades,
[2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])

np.testing.assert_almost_equal(
trades.fill_price,
[103.0, 106.0, 110.0, 105.0, 106.0, 120.0, 142.0, 142.0])

# test roundpositions
delayfill = True
roundpositions = False
trades = get_trades_from_positions(price,
positions,
delayfill,
roundpositions,
get_daily_returns_volatility,
forecast,
fx,
value_of_price_point)

np.testing.assert_almost_equal(
trades.trades,
[2.0, 1.0, -1.0, 1.0, 0.1, 0.9, -1.0, 2.0, 2.0])

np.testing.assert_almost_equal(
trades.fill_price[:-1],
[106.0, 106.0, 105.0, 106.0, 106.0, 120.0, 142.0, 142.0])
# def test_get_trades_from_positions(self):
# positions = pd.DataFrame(
# [np.nan, 2, 3, np.nan, 2, 3, 3.1, 4, 3, 5, 7],
# dt_range2)

# price = pd.DataFrame(
# [100, 103, np.nan, 106, 110, 105, np.nan, 106, 120, np.nan, 142],
# dt_range2)

# # test delayed fill
# delayfill = True
# roundpositions = True
# get_daily_returns_volatility = None
# forecast = None
# fx = None
# value_of_price_point = None
# trades = get_trades_from_positions(price,
# positions,
# delayfill,
# roundpositions,
# get_daily_returns_volatility,
# forecast,
# fx,
# value_of_price_point)

# np.testing.assert_almost_equal(
# trades.trades,
# [2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])

# np.testing.assert_almost_equal(
# trades.fill_price[:-1],
# [106.0, 106.0, 105.0, 106.0, 120.0, 142.0, 142.0])

# # test none delayed fill
# delayfill = False
# trades = get_trades_from_positions(price,
# positions,
# delayfill,
# roundpositions,
# get_daily_returns_volatility,
# forecast,
# fx,
# value_of_price_point)

# np.testing.assert_almost_equal(
# trades.trades,
# [2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])

# np.testing.assert_almost_equal(
# trades.fill_price,
# [103.0, 106.0, 110.0, 105.0, 106.0, 120.0, 142.0, 142.0])

# # test roundpositions
# delayfill = True
# roundpositions = False
# trades = get_trades_from_positions(price,
# positions,
# delayfill,
# roundpositions,
# get_daily_returns_volatility,
# forecast,
# fx,
# value_of_price_point)

# np.testing.assert_almost_equal(
# trades.trades,
# [2.0, 1.0, -1.0, 1.0, 0.1, 0.9, -1.0, 2.0, 2.0])

# np.testing.assert_almost_equal(
# trades.fill_price[:-1],
# [106.0, 106.0, 105.0, 106.0, 106.0, 120.0, 142.0, 142.0])

def test_pandl(self):
fx = pd.DataFrame([2.0] * 10, dt_range1)
Expand All @@ -133,12 +135,12 @@ def test_pandl(self):
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)
ans = pandl_with_data(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)
ans2 = pandl_with_data(price, trades, marktomarket=False, fx=fx)

np.testing.assert_almost_equal(
ans2.pandl_base[1:],
Expand Down
26 changes: 17 additions & 9 deletions syscore/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ def test_robust_vol_calc(self):
returns = prices.diff()
vol = robust_vol_calc(returns, days=35)

self.assertAlmostEqual(vol.iloc[-1, 0], 0.41905275480464305)
self.assertAlmostEqual(vol.iloc[-1][0], 0.41905275480464305)

vol = robust_vol_calc(returns, days=100)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.43906619578902956)


self.assertAlmostEqual(vol.iloc[-1][0], 0.43906619578902956)

def test_robust_vol_calc_min_period(self):
prices = pd_readcsv_frompackage(
"syscore.tests.pricetestdata_min_period.csv")
returns = prices.diff()
vol = robust_vol_calc(returns, min_periods=9)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.45829858614978286)
self.assertAlmostEqual(vol.iloc[-1][0], 0.45829858614978286)
vol = robust_vol_calc(returns, min_periods=10)
self.assertTrue(np.isnan(vol.iloc[-1][0]))

Expand All @@ -39,22 +41,28 @@ def test_robust_vol_calc_min_value(self):
"syscore.tests.pricetestdata_zero_vol.csv")
returns = prices.diff()
vol = robust_vol_calc(returns, vol_abs_min=0.01)
self.assertEqual(vol.iloc[-1, 0], 0.01)
self.assertEqual(vol.iloc[-1][0], 0.01)

def test_robust_vol_calc_floor(self):
prices = pd_readcsv_frompackage(
"syscore.tests.pricetestdata_vol_floor.csv")
returns = prices.diff()
vol = robust_vol_calc(returns)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.54492982003602064)

self.assertAlmostEqual(vol.iloc[-1][0], 0.54492982003602064)

vol = robust_vol_calc(returns, vol_floor=False)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.42134038479240132)

self.assertAlmostEqual(vol.iloc[-1][0], 0.42134038479240132)

vol = robust_vol_calc(returns, floor_min_quant=.5)
self.assertAlmostEqual(vol.iloc[-1, 0], 1.6582199589924964)
self.assertAlmostEqual(vol.iloc[-1][0], 1.6582199589924964)

vol = robust_vol_calc(returns, floor_min_periods=500)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.42134038479240132)
self.assertAlmostEqual(vol.iloc[-1][0], 0.42134038479240132)

vol = robust_vol_calc(returns, floor_days=10, floor_min_periods=5)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.42134038479240132)
self.assertAlmostEqual(vol.iloc[-1][0], 0.42134038479240132)

"""
def test_calc_ewmac_forecast(self):
Expand Down
35 changes: 1 addition & 34 deletions syscore/tests/test_pdutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,11 @@
import unittest
import pandas as pd
import numpy as np
from syscore.pdutils import divide_df_single_column, multiply_df, multiply_df_single_column
from syscore.pdutils import multiply_df, multiply_df_single_column


class Test(unittest.TestCase):

def test_divide_df_single_column(self):
x = pd.DataFrame(dict(a=[2.0, 7.0, -7.0, -7.00, 3.5]),
pd.date_range(pd.datetime(2015, 1, 1), periods=5))
y = pd.DataFrame(dict(b=[2.0, 3.5, 2.0, -3.5, -3.5]),
pd.date_range(pd.datetime(2015, 1, 1), periods=5))
ans = list(divide_df_single_column(x, y).iloc[:, 0])
self.assertEqual(ans, [1., 2., -3.5, 2., -1.])

x = pd.DataFrame(dict(a=[2.0, np.nan, -7.0, np.nan, 3.5]),
pd.date_range(pd.datetime(2015, 1, 1), periods=5))
y = pd.DataFrame(dict(b=[2.0, 3.5, np.nan, np.nan, -3.5]),
pd.date_range(pd.datetime(2015, 1, 2), periods=5))

ans = list(divide_df_single_column(x, y).iloc[:, 0])

self.assertTrue(np.isnan(ans[0]))
self.assertTrue(np.isnan(ans[1]))
self.assertTrue(np.isnan(ans[3]))

self.assertEqual(ans[2], -2.0)

ans = list(divide_df_single_column(
x, y, ffill=(True, False)).iloc[:, 0])
self.assertEqual(ans[1], 1.0)

ans = list(divide_df_single_column(
x, y, ffill=(False, True)).iloc[:, 0])
self.assertEqual(ans[4], 1.0)

ans = list(divide_df_single_column(
x, y, ffill=(True, True)).iloc[:, 0])
self.assertEqual(list(ans)[1:], [1., -2., -2.0, 1., -1.])

def multiply_df_single_column(self):
pass

Expand Down
15 changes: 8 additions & 7 deletions systems/tests/test_forecasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ def testRules(self):
system = System([rules], data)

ans = system.rules.get_raw_forecast("EDOLLAR", "rule0")
self.assertAlmostEqual(ans.iloc[-1][0], 2.1384223788141838, 5)
self.assertAlmostEqual(ans.iloc[-1], 2.1384223788141838, 5)

config = Config(dict(trading_rules=dict(ewmac=dict(
function="systems.provided.example.rules.ewmac_forecast_with_defaults"))))
rules = Rules()
system = System([rules], data, config)
ans = system.rules.get_raw_forecast("EDOLLAR", "ewmac")
self.assertAlmostEqual(ans.iloc[-1][0], 2.1384223788141838, 5)
self.assertAlmostEqual(ans.iloc[-1], 2.1384223788141838, 5)

config = Config("systems.provided.example.exampleconfig.yaml")
rawdata = RawData()

rules = Rules()
system = System([rules, rawdata], data, config)
ans = system.rules.get_raw_forecast("EDOLLAR", "ewmac8")
self.assertAlmostEqual(ans.iloc[-1][0], 0.16438313875, 5)

self.assertAlmostEqual(ans.iloc[-1], 0.16438313875, 5)

def testinitTradingRules(self):
rule = TradingRule((ewmac_forecast_with_defaults, [], {}))
Expand Down Expand Up @@ -120,21 +121,21 @@ def testCallingTradingRule(self):
# Call with default data and config
rule = TradingRule(ewmac_forecast_with_defaults)
ans = rule.call(system, "EDOLLAR")
self.assertAlmostEqual(ans.iloc[-1][0], 2.1384223788141838, 5)
self.assertAlmostEqual(ans.iloc[-1], 2.1384223788141838, 5)

# Change the data source
rule = TradingRule(("systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
["rawdata.get_daily_prices", "rawdata.daily_returns_volatility"], dict()))

ans = rule.call(system, "EDOLLAR")
self.assertAlmostEqual(ans.iloc[-1][0], 0.029376, 5)
self.assertAlmostEqual(ans.iloc[-1], 0.029376, 5)

rule = TradingRule(dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
data=["rawdata.get_daily_prices",
"rawdata.daily_returns_volatility"],
other_args=dict(Lfast=50, Lslow=200)))
ans = rule.call(system, "EDOLLAR")
self.assertAlmostEqual(ans.iloc[-1][0], 3.84426755)
self.assertAlmostEqual(ans.iloc[-1], 3.84426755)

def testCarryRule(self):
data = csvFuturesData("sysdata.tests")
Expand All @@ -147,7 +148,7 @@ def testCarryRule(self):
"rawdata.daily_returns_volatility"],
dict(smooth_days=90))
ans = rule.call(system, "EDOLLAR")
self.assertAlmostEqual(ans.iloc[-1][0], 0.411686026, 5)
self.assertAlmostEqual(ans.iloc[-1], 0.411686026, 5)

def testProcessTradingRuleSpec(self):

Expand Down

0 comments on commit 2a68e99

Please sign in to comment.