Exemplo n.º 1
0
    def test_sharpe_ratio(self):
        array = np.array([1, 1.5, 3, 4, 4.3])
        array_2 = np.array([5, 4.3, 3, 3.5, 1])
        matrix = np.array([array, array_2]).T
        series = pd.Series(array)
        time_series = pd.Series(array_2)
        df = pd.DataFrame(matrix, columns=['c1', 'c2'])

        # Input is np.array of 1 dimmension => float
        ans = Calculator.sharpe_ratio(array)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # Input is np.array of 2 dimmensions => array
        ans = Calculator.sharpe_ratio(matrix)
        self.assertArray(ans)
        sol = np.array([2.38842482, -1.4708528])
        self.assertEqual(ans, sol, 5)
        # Input is pandas.Series => float
        ans = Calculator.sharpe_ratio(series)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # Input is pandas.TimeSeries => float
        ans = Calculator.sharpe_ratio(time_series)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, -1.4708528, 5)
        # Input is pandas.DataFrame with col parameter => float
        ans = Calculator.sharpe_ratio(df, col='c1')
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # --
        ans = Calculator.sharpe_ratio(df, col='c2')
        self.assertFloat(ans)
        self.assertAlmostEqual(ans, -1.4708528, 5)
        # Input is pandas.DataFrame without col parameter => pd.Series
        ans = Calculator.sharpe_ratio(df)
        self.assertSeries(ans)
        sol = pd.Series([2.38842482, -1.4708528],
                        index=['c1', 'c2'],
                        name='Sharpe Ratios')
        self.assertEqual(ans, sol)
Exemplo n.º 2
0
    def test_sharpe_ratio(self):
        array = np.array([1,1.5,3,4,4.3])
        array_2 = np.array([5,4.3,3,3.5,1])
        matrix = np.array([array, array_2]).T
        series = pd.Series(array)
        time_series = pd.Series(array_2)
        df = pd.DataFrame(matrix, columns=['c1', 'c2'])

        # Input is np.array of 1 dimmension => float
        ans = Calculator.sharpe_ratio(array)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # Input is np.array of 2 dimmensions => array
        ans = Calculator.sharpe_ratio(matrix)
        self.assertArray(ans)
        sol = np.array([2.38842482, -1.4708528])
        self.assertEqual(ans, sol, 5)
        # Input is pandas.Series => float
        ans = Calculator.sharpe_ratio(series)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # Input is pandas.TimeSeries => float
        ans = Calculator.sharpe_ratio(time_series)
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, -1.4708528, 5)
        # Input is pandas.DataFrame with col parameter => float
        ans = Calculator.sharpe_ratio(df, col='c1')
        self.assertFloat(ans)
        self.assertAlmostEquals(ans, 2.38842, 5)
        # --
        ans = Calculator.sharpe_ratio(df, col='c2')
        self.assertFloat(ans)
        self.assertAlmostEqual(ans, -1.4708528, 5)
        # Input is pandas.DataFrame without col parameter => pd.Series
        ans = Calculator.sharpe_ratio(df)
        self.assertSeries(ans)
        sol = pd.Series([2.38842482, -1.4708528], index=['c1', 'c2'], name='Sharpe Ratios')
        self.assertEqual(ans, sol)
Exemplo n.º 3
0
from datetime import datetime
import matplotlib.pyplot as plt
from finance.utils import Calculator
from finance.sim import MarketSimulator

# from finance.utils import DataAccess
# DataAccess.path = 'data'

sim = MarketSimulator()
sim.initial_cash = 1000000
sim.load_trades("MarketSimulator_orders.csv")
sim.simulate()

print(sim.portfolio[0:10])

print('Total Return:', Calculator.ret(sim.portfolio))
print(Calculator.sharpe_ratio(sim.portfolio))

sim.portfolio.plot()
# plt.grid(True)
plt.show()
Exemplo n.º 4
0
from datetime import datetime
import matplotlib.pyplot as plt
from finance.utils import Calculator
from finance.sim import MarketSimulator

# from finance.utils import DataAccess
# DataAccess.path = 'data'

sim = MarketSimulator()
sim.initial_cash = 1000000
sim.load_trades("MarketSimulator_orders.csv")
sim.simulate()

print(sim.portfolio.tail())

print('Total Return:', Calculator.ret(sim.portfolio))
print('Sharpe Ratio:', Calculator.sharpe_ratio(sim.portfolio))

sim.portfolio.plot()
plt.grid(True)
plt.show()