Skip to content

Commit

Permalink
Merge pull request #2 from cmorgan/fix
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
robcarver17 committed Jan 11, 2016
2 parents 8e62454 + e57caee commit 0fd9950
Show file tree
Hide file tree
Showing 17 changed files with 652 additions and 501 deletions.
2 changes: 1 addition & 1 deletion examples/introduction/prebakedsystems.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
system = futures_system()
print(system.accounts.portfolio().sharpe())
system.accounts.portfolio().curve().plot()
show()
show()
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Requirements:

python >= 3.4.3
pandas >= 0.17.0
matlotplib > 1.4.3
matplotlib > 1.4.3
pyyaml >= 3.11
numpy >= 1.10.1
scipy >= 0.16.1
scipy >= 0.16.1
193 changes: 115 additions & 78 deletions syscore/accounting.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions syscore/genutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from math import copysign


def TorF(x):
if x:
return "T"
else:
return "F"


def str_of_int(x):
"""
Returns the string of int of x, handling nan's or whatever
Expand Down
162 changes: 115 additions & 47 deletions syscore/tests/test_Accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,139 @@
from syscore.accounting import pandl, get_positions_from_forecasts, get_trades_from_positions


dt_range1 = pd.date_range(start=pd.datetime(2014, 12, 30), periods=10)
dt_range2 = pd.date_range(start=pd.datetime(2015, 1, 1), periods=11)


class Test(unittest.TestCase):

def test_get_positions_from_forecasts(self):
fx = pd.DataFrame(
[2.0] * 10, pd.date_range(start=pd.datetime(2014, 12, 30), periods=10))
price = pd.DataFrame([100, 103, 105, 106, 110, 105, np.nan, 106,
120, np.nan, 142], pd.date_range(start=pd.datetime(2015, 1, 1), periods=11))
forecast = pd.DataFrame([np.nan, np.nan, np.nan, np.nan, 10.0, 10.0, 15.0, 15.0,
5.0, 0.0, -5.0], pd.date_range(start=pd.datetime(2015, 1, 1), periods=11))
fx = pd.DataFrame([2.0] * 10, dt_range1)
price = pd.DataFrame(
[100, 103, 105, 106, 110, 105, np.nan, 106, 120, np.nan, 142],
dt_range2)
forecast = pd.DataFrame(
[np.nan, np.nan, np.nan, np.nan, 10.0, 10.0, 15.0, 15.0, 5.0, 0.0, -5.0],
dt_range2)
value_of_price_point = 150.0
position = get_positions_from_forecasts(
price, None, forecast, fx, value_of_price_point, min_periods=1)

self.assertAlmostEqual(list(position.position.values)[4:], [
2523.4937866824253, 905.72296272461699, 1358.5844440869255, 1358.5844440869255, 248.78044993282995, 0.0, -248.78044993282995])
daily_return_volatility = None
position = get_positions_from_forecasts(price,
daily_return_volatility,
forecast,
fx,
value_of_price_point,
min_periods=1)

# TODO this has been divided by ten to what was here previously, error?
expected_pos = [np.nan, np.nan, np.nan, np.nan,
252.34937866824254,
90.572296272461699,
135.85844440869255,
135.85844440869255,
24.878044993282998,
0.0,
-24.878044993282995]

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], pd.date_range(start=pd.datetime(2015, 1, 1), periods=11))
price = pd.DataFrame([100, 103, np.nan, 106, 110, 105, np.nan, 106,
120, np.nan, 142], pd.date_range(start=pd.datetime(2015, 1, 1), periods=11))
#trades=get_trades_from_positions(price, positions, delayfill, roundpositions, None, None, None, None)
trades = get_trades_from_positions(
price, positions, True, True, None, None, None, None)

self.assertEqual(list(trades.trades), [
2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])
self.assertEqual(list(trades.fill_price)[
:-1], [106.0, 106.0, 105.0, 106.0, 120.0, 142.0, 142.0])

trades = get_trades_from_positions(
price, positions, False, True, None, None, None, None)

self.assertEqual(list(trades.trades), [
2.0, 1.0, -1.0, 1.0, 1.0, -1.0, 2.0, 2.0])
self.assertEqual(list(trades.fill_price), [
103.0, 106.0, 110.0, 105.0, 106.0, 120.0, 142.0, 142.0])

trades = get_trades_from_positions(
price, positions, True, False, None, None, None, None)

self.assertEqual(list(trades.trades), [
2.0, 1.0, -1.0, 1.0, 0.1, 0.9, -1.0, 2.0, 2.0])
self.assertEqual(list(trades.fill_price)[
:-1], [106.0, 106.0, 105.0, 106.0, 106.0, 120.0, 120.0, 142.0, 142.0])
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, pd.date_range(start=pd.datetime(2014, 12, 30), periods=10))
price = pd.DataFrame([100, 103, 105, 106, 110, 105, 104.5,
np.nan, 120, np.nan, 142], pd.date_range(start=pd.datetime(2015, 1, 1), periods=11))
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)
self.assertEqual(list(ans.pandl_base)[1:], [
0.0, 10.4, 6., 14., -16., -9., 15., 48., 78., 40.])
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)
self.assertEqual(list(ans2.pandl_base)[1:], [
10.4, 6., 0., -2., 6., 48., 78.])

np.testing.assert_almost_equal(
ans2.pandl_base[1:],
[10.4, 6., 0., -2., 6., 48., 78.])

if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
11 changes: 7 additions & 4 deletions syscore/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@author: rob
'''
import unittest as ut

import numpy as np

from syscore.pdutils import pd_readcsv_frompackage
Expand All @@ -14,17 +15,19 @@ class Test(ut.TestCase):

def test_robust_vol_calc(self):
prices = pd_readcsv_frompackage(
"syscore", "pricetestdata.csv", ["tests"])
"syscore.tests.pricetestdata.csv")

returns = prices.diff()
vol = robust_vol_calc(returns, days=35)

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

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

def test_robust_vol_calc_min_period(self):
prices = pd_readcsv_frompackage(
"syscore", "pricetestdata_min_period.csv", ["tests"])
"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)
Expand All @@ -33,14 +36,14 @@ def test_robust_vol_calc_min_period(self):

def test_robust_vol_calc_min_value(self):
prices = pd_readcsv_frompackage(
"syscore", "pricetestdata_zero_vol.csv", ["tests"])
"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)

def test_robust_vol_calc_floor(self):
prices = pd_readcsv_frompackage(
"syscore", "pricetestdata_vol_floor.csv", ["tests"])
"syscore.tests.pricetestdata_vol_floor.csv")
returns = prices.diff()
vol = robust_vol_calc(returns)
self.assertAlmostEqual(vol.iloc[-1, 0], 0.54492982003602064)
Expand Down
2 changes: 1 addition & 1 deletion sysdata/csvdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_instrument_price(self, instrument_code):
filename = os.path.join(self._datapath, instrument_code + "_price.csv")
instrpricedata = pd_readcsv(filename)
instrpricedata.columns = ["price"]
instrpricedata=instrpricedata.groupby(level=0).last()
instrpricedata = instrpricedata.groupby(level=0).last()
return instrpricedata

def get_instrument_raw_carry_data(self, instrument_code):
Expand Down
Loading

0 comments on commit 0fd9950

Please sign in to comment.