예제 #1
0
    def test_total_return(self):
        sim = MarketSimulator('./data')
        sim.initial_cash = 1000000
        sim.simulate("../../sim/test/orders.csv")

        # Test without giving the column
        tr1 = BasicUtils.total_return(sim.portfolio)
        self.assertEquals(tr1, 0.1332629999999999)

        # Test giving the column we want to calculate
        tr2 = BasicUtils.total_return(sim.portfolio, 'Portfolio')
        self.assertEquals(tr2, 0.1332629999999999)
예제 #2
0
    def test_sharpe_ratio(self):
        sim = MarketSimulator('./data')
        sim.initial_cash = 1000000
        sim.simulate("../../sim/test/orders.csv")

        # Test without giving the column
        sr1 = BasicUtils.sharpe_ratio(sim.portfolio, extraAnswers=True)
        self.assertEquals(sr1['sharpe_ratio'], 1.1825359272456812)
        self.assertEquals(sr1['std'], 0.0071658104790118396)
        self.assertEquals(sr1['mean'], 0.00054698326727656884)

        # Test giving the column we want to calculate
        sr2 = BasicUtils.sharpe_ratio(sim.portfolio, 'Portfolio', extraAnswers=True)
        self.assertEquals(sr2['sharpe_ratio'], 1.1825359272456812)
        self.assertEquals(sr2['std'], 0.0071658104790118396)
        self.assertEquals(sr2['mean'], 0.00054698326727656884)
    def test_1(self):
        """
        Loads solution from csv file and test the values
        Tests
        -----
            1. Name of the solution Series: cash, equities, portfolio
            2. Values of the solution Series: cash, equities, portfolio
        """
        # Set up
        self.setUpDataAccess()

        self_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        tests = ["Test_1.csv"]
        tests = [os.path.join(self_dir, "docs", test) for test in tests]

        orders = ["orders_1.csv"]
        orders = [os.path.join(self_dir, "docs", order) for order in orders]

        for test_file, order in zip(tests, orders):
            solution = pd.read_csv(test_file)
            solution = solution.set_index(pd.to_datetime(solution["Date"]))

            simulator = MarketSimulator()
            simulator.initial_cash = 1000000
            simulator.load_trades(order)
            simulator.simulate()

            # Test 1
            self.assertEqual(simulator.cash.name, "Cash")
            self.assertEqual(simulator.equities.name, "Equities value")
            self.assertEqual(simulator.portfolio.name, "Portfolio value")
            # Test 2
            self.assertEqual(simulator.cash, solution["Cash"])
            self.assertEqual(simulator.equities, solution["Equities value"])
            self.assertEqual(simulator.portfolio, solution["Portfolio value"])
예제 #4
0
    def test_1(self):
        '''
        Loads solution from csv file and test the values
        Tests
        -----
            1. Name of the solution Series: cash, equities, portfolio
            2. Values of the solution Series: cash, equities, portfolio
        '''
        # Set up
        self.setUpDataAccess()

        self_dir = os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe())))
        tests = ['Test_1.csv']
        tests = [os.path.join(self_dir, 'docs', test) for test in tests]

        orders = ['orders_1.csv']
        orders = [os.path.join(self_dir, 'docs', order) for order in orders]

        for test_file, order in zip(tests, orders):
            solution = pd.read_csv(test_file)
            solution = solution.set_index(pd.to_datetime(solution['Date']))

            simulator = MarketSimulator()
            simulator.initial_cash = 1000000
            simulator.load_trades(order)
            simulator.simulate()

            # Test 1
            self.assertEqual(simulator.cash.name, 'Cash')
            self.assertEqual(simulator.equities.name, 'Equities value')
            self.assertEqual(simulator.portfolio.name, 'Portfolio value')
            # Test 2
            self.assertEqual(simulator.cash, solution['Cash'])
            self.assertEqual(simulator.equities, solution['Equities value'])
            self.assertEqual(simulator.portfolio, solution['Portfolio value'])
예제 #5
0
from datetime import datetime
from finance.utils.BasicUtils import *
from finance.sim import MarketSimulator

sim = MarketSimulator("../sim/test/data")
sim.initial_cash = 1000000
sim.simulate("MarketSimulator_orders.csv")

print(sim.portfolio[0:10])

print("Total Return:", total_return(sim.portfolio))
print(sharpe_ratio(sim.portfolio, extraAnswers=True))
예제 #6
0
class MarketSimulatorTest(unittest.TestCase):

    def setUp1(self):
        DataAccess('./data').empty_dirs()
        self.sim = MarketSimulator('./data')


    def suite(self):
        suite = unittest.TestSuite()
        suite.addTest(MarketSimulatorTest('test_values'))
        return suite

    def dfCompare(self, df1, df2):
        c = df1 == df2
        for idx, row in c.iterrows():
            if row[0] == False:
                return False
        return True

    def test_values(self):
        self.setUp1()

        self.sim.initial_cash = 1000000
        self.sim.simulate("orders.csv")

        # Test: Length of the DataFrame
        self.assertEqual(len(self.sim.portfolio), 240)
        self.assertEqual(len(self.sim.portfolio.columns), 1)

        # Test: Name of columns
        self.assertEqual(self.sim.portfolio.columns, 'Portfolio')
        self.assertEqual(self.sim.portfolio.index.name, 'Date')

        # Create a test DataFrame
        testdf = pd.DataFrame([0, 0, 0, 0, 0])
        testdf.index.name = 'Date'

        # ========================== HEAD TEST ==============================================
        testdf.columns = ['Portfolio']
        testdf.index = [datetime(2011, 1, 10), datetime(2011, 1, 11), datetime(2011, 1, 12),
                        datetime(2011, 1, 13), datetime(2011, 1, 14)]

        # Portfolio: Test True Positive
        testdf['Portfolio'] = [1000000, 998785, 1002925, 1004800, 1009360]
        self.assertTrue(self.dfCompare(testdf, self.sim.portfolio.head()))
        # Portfolio: Test True Negative
        testdf['Portfolio'] = [11000000, 998785, 1002925, 1004800, 1009360]
        self.assertFalse(self.dfCompare(testdf, self.sim.portfolio.head()))

        # Cash: Test True Positive
        testdf.columns = ['Cash']
        testdf['Cash'] = [490840, 490840, 490840, 429120, 429120]
        self.assertTrue(self.dfCompare(testdf, self.sim.cash.head()))
        # Cash: Test True Negative
        testdf['Cash'] = [490840, 490840, 490840, 429120, 1429120]
        self.assertFalse(self.dfCompare(testdf, self.sim.cash.head()))

        # ========================== TAIL TEST ==============================================
        testdf.columns = ['Portfolio']
        testdf.index = [datetime(2011, 12, 14), datetime(2011, 12, 15), datetime(2011, 12, 16),
                        datetime(2011, 12, 19), datetime(2011, 12, 20)]

        # Portfolio: Test True Positive
        testdf['Portfolio'] = [1114519, 1113031, 1115515, 1116931, 1133263]
        self.assertTrue(self.dfCompare(testdf, self.sim.portfolio.tail()))
        # Portfolio: Test True Negative
        testdf['Portfolio'] = [1114519, 1113031, 1115515, 1116931, 1133264]
        self.assertFalse(self.dfCompare(testdf, self.sim.portfolio.tail()))

        # Cash: Test True Positive
        testdf.columns = ['Cash']
        testdf['Cash'] = [662311, 662311, 662311, 662311, 1133263]
        self.assertTrue(self.dfCompare(testdf, self.sim.cash.tail()))
        # Cash: Test True Negative
        testdf['Cash'] = [662311, 662311, 662311, 662311, 1133264]
        self.assertFalse(self.dfCompare(testdf, self.sim.cash.tail()))
예제 #7
0
 def setUp1(self):
     DataAccess('./data').empty_dirs()
     self.sim = MarketSimulator('./data')
예제 #8
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()