Esempio n. 1
0
    def test_MIBasic_singleModel_online(self):

        TRAINING_RUN_ID = "94b227b9d7b22c920333aa36d23669c8"
        DATASET_ID = "4234f0f1b6fcc17f6458696a6cdf5101"

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(
            env.createModel("MIBasicModel",
                            "Test-MIBasicModel",
                            args=(CredentialsStore(), DATASET_ID,
                                  TRAINING_RUN_ID, 0)))

        # Extract 3pm indices
        # crop = ppl.cropTime(asset1.values, "15:00", "16:00")
        # idx = [asset1.values.index.get_loc(crop.index[x]) for x in range(len(crop))]
        idx = [19, 42, 44, 67, 90]

        c = 0
        for i in idx:
            env.append(Asset("DOW", self.asset1.values[c:i]))
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))
            c = i + 1
        env.append(Asset("DOW", self.asset1.values[c:]))

        # Check results
        self.assertTrue(
            np.allclose(np.prod(utils.getPeriodReturns(p.returns) + 1),
                        0.9941924543457394))
Esempio n. 2
0
    def test_meanreversion_singleModel_online_partials(self):

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(env.createModel("MeanReversion", "Test-BuyAndHold"))

        env.append(Asset("DOW", self.asset1.values[0:-1]))

        slice = self.asset1.values[-1:].copy()
        slice["Close"] = np.nan
        env.append(Asset("DOW", slice))

        res1 = p.getUnderlyingAllocations()["DOW"]["bar"].values.flatten()

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(env.createModel("MeanReversion", "Test-BuyAndHold"))

        env.append(Asset("DOW", self.asset1.values))

        res2 = p.getUnderlyingAllocations()["DOW"]["bar"].values.flatten()

        self.assertTrue(np.allclose(res1, res2))
Esempio n. 3
0
    def test_kellyWeights_multiModel_online_partials(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = np.array([-.1, .2, -.3, .4, -.5, .6, -.7, .8, -1])

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    randomSignals[loc:loc + len(window)],
                    randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework for a partial input (bar only)
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "KellyOptimizer",
                                    "KellyWeights",
                                    opts={"window": 4}))
        p.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))
        p.addModel(RandomModel("TestModel2", env))

        env.append(Asset("DOW", self.asset1.values[0:-1]))

        slice = self.asset1.values[-1:].copy()
        slice["Close"] = np.nan
        env.append(Asset("DOW", slice))

        res1 = p.getUnderlyingAllocations()["DOW"]["bar"].values.flatten()

        # Calculate returns via TradeFramework for a full input (bar/gap)
        env = SandboxEnvironment("TradeFair")
        p2 = env.createPortfolio("MyPortfolio",
                                 optimizer=env.createOptimizer(
                                     "KellyOptimizer",
                                     "KellyWeights",
                                     opts={"window": 4}))
        p2.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))
        p2.addModel(RandomModel("TestModel2", env))

        env.append(Asset("DOW", self.asset1.values))

        res2 = p2.getUnderlyingAllocations()["DOW"]["bar"].values.flatten()

        self.assertTrue(np.allclose(res1, res2))
Esempio n. 4
0
    def test_buyAndSell_singleModel_online(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = np.array([1, 1, 0, -1, 0, -1, 1, -1, 0])

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    np.zeros(len(window)), randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(RandomModel("RandomModel", env))

        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        # Calculate returns manually
        mRet = randomSignals[:-1] * np.diff(
            self.asset1.values["Close"]) / self.asset1.values["Close"][:-1]

        self.assertTrue(np.allclose(p.returns.values[:, 0][1:], mRet.values))
Esempio n. 5
0
    def test_kellyWeights_multiModel_online(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = np.array([-.1, .2, -.3, .4, -.5, .6, -.7, .8, -1])

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    randomSignals[loc:loc + len(window)],
                    randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "KellyOptimizer",
                                    "KellyWeights",
                                    opts={"window": 4}))
        p.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))
        p.addModel(RandomModel("TestModel2", env))

        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        self.assertTrue(
            np.allclose(p.returns["Open"].values.flatten(), [
                0., 0., 0., 0., 0., 4.04651163, 6.16666667, 15.26666667,
                16.03333333
            ]))
Esempio n. 6
0
    def test_MIBasic_multiModel(self):

        TRAINING_RUN_ID = "94b227b9d7b22c920333aa36d23669c8"
        DATASET_ID1 = "4234f0f1b6fcc17f6458696a6cdf5101"
        DATASET_ID2 = "3231bbe5eb2ab84eb54c9b64a8dcea55"

        agg = [{
            'training_run_id': TRAINING_RUN_ID,
            'datasets': [DATASET_ID1, DATASET_ID2]
        }]

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(
            env.createModel("MIAggregateModel",
                            "Test-MIAggregateModel",
                            args=(CredentialsStore(), agg,
                                  "vote_unanimous_all", 0)))

        env.append(Asset("DOW", self.asset1.values))

        print(np.prod(utils.getPeriodReturns(p.returns) + 1))
        # Check results
        self.assertTrue(
            np.allclose(np.prod(utils.getPeriodReturns(p.returns) + 1),
                        0.9979433892004727))
Esempio n. 7
0
 def setUp(self):
     self.asset1 = Asset(
         "DOW",
         pd.read_csv(dir + '/data/testData1.csv',
                     parse_dates=True,
                     index_col=0,
                     dayfirst=True))
Esempio n. 8
0
 def setUp(self):
     ts = pd.read_csv(dir + '/data/testDOW.csv',
                      parse_dates=True,
                      index_col=0,
                      dayfirst=True)
     ts = ts.tz_localize("UCT")
     ts.index = ts.index.tz_convert("US/Eastern")
     self.asset1 = Asset("DOW", ts)
Esempio n. 9
0
    def test_buyAndHold_singleModel_online_partials(self):

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))

        for i in range(len(self.asset1.values)):
            slice = self.asset1.values[i:i + 1].copy()
            slice["Close"] = np.nan
            env.append(Asset("DOW", slice))
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        # Calculate returns manually
        mRet = np.diff(
            self.asset1.values["Close"]) / self.asset1.values["Close"][:-1]

        self.assertTrue(np.allclose(p.returns.values[:, 0][1:], mRet.values))
Esempio n. 10
0
    def test_MIBasic_multiModel_online(self):

        TRAINING_RUN_ID = "94b227b9d7b22c920333aa36d23669c8"
        DATASET_ID1 = "4234f0f1b6fcc17f6458696a6cdf5101"
        DATASET_ID2 = "3231bbe5eb2ab84eb54c9b64a8dcea55"

        agg = [{
            'training_run_id': TRAINING_RUN_ID,
            'datasets': [DATASET_ID1, DATASET_ID2]
        }]

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(
            env.createModel("MIAggregateModel",
                            "Test-MIAggregateModel",
                            args=(CredentialsStore(), agg,
                                  "vote_unanimous_all", 0)))

        # Extract 3pm indices
        # crop = ppl.cropTime(asset1.values, "15:00", "16:00")
        # idx = [asset1.values.index.get_loc(crop.index[x]) for x in range(len(crop))]
        idx = [19, 42, 44, 67, 90]

        c = 0
        for i in idx:
            env.append(Asset("DOW", self.asset1.values[c:i]))
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))
            c = i + 1
        env.append(Asset("DOW", self.asset1.values[c:]))

        print(np.prod(utils.getPeriodReturns(p.returns) + 1))

        # Check results
        self.assertTrue(
            np.allclose(np.prod(utils.getPeriodReturns(p.returns) + 1),
                        0.9979433892004727))
Esempio n. 11
0
    def test_meanreversion_singleModel_online(self):

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(env.createModel("MeanReversion", "Test-MeanReversion"))
        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        self.assertTrue(
            np.allclose(p.returns["Open"].values.flatten(),
                        [0., 0., 0.1, 0.2, 0.1, 0.2, 0.1, 0.2, 0.1]))
Esempio n. 12
0
    def setUp(self):

        periods = 400
        open = 100
        prices = []
        random.seed(0)
        for i in range(0, periods):
            row = self.generatePrices(open)
            prices.append(row)
            open = row[3]

        self.randomSignals = np.array(
            [random.random() for _ in range(0, periods)])
        self.asset1 = Asset(
            "DOW",
            pd.DataFrame(prices,
                         columns=["Open", "High", "Low", "Close"],
                         index=pd.date_range(start='1/1/2018',
                                             periods=periods)))
Esempio n. 13
0
    def test_kellyWeights_singleModel_online(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = np.array([-1, 1, -1, 1, -1, 1, -1, 1, -1])

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    randomSignals[loc:loc + len(window)],
                    randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "KellyOptimizer", "KellylWeights"))
        p.addModel(RandomModel("TestModel", env))

        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        self.assertTrue(
            np.allclose(
                p.getAsset("TestModel").getAllocations()["DOW"]
                ["gap"].values.flatten(), [
                    -0.01, 0.015, -0.015, 0.0225, -0.0225, 0.03375, -0.03375,
                    0.050625, -0.050625
                ]))

        print(p.values["Close"].values.flatten())

        self.assertTrue(
            np.allclose(p.values["Close"].values.flatten(), [
                1., 1., 1., 2., 4., 14.90909091, 40.46753247, 202.33766234,
                657.5974026
            ]))
Esempio n. 14
0
    def test_buyAndSell_multiModel_online(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = np.array([1, 1, 0, -1, 0, -1, 1, -1, 0])

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    np.zeros(len(window)), randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))
        p.addModel(RandomModel("TestModel2", env))

        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))

        # Test returns were calculated correctly
        self.assertTrue(
            np.allclose(p.returns.values[:, 0][1:],
                        np.array([-.2, .1, -.1, .0, -.1, .0, -.2, .0])))

        print(p.getUnderlyingAllocations()["DOW"]["gap"].values)

        # Test underlying allocations were calculated correctly
        self.assertTrue(
            np.allclose(
                p.getUnderlyingAllocations()["DOW"]["gap"].values,
                np.array([
                    .01, .01, .005, 0, .0051136364, 0, .0104597107, 0,
                    .00475441397
                ])))
Esempio n. 15
0
    def test_MIBasic_singleModel(self):

        TRAINING_RUN_ID = "94b227b9d7b22c920333aa36d23669c8"
        DATASET_ID = "4234f0f1b6fcc17f6458696a6cdf5101"

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "EqualWeightsOptimizer", "EqualWeights"))
        p.addModel(
            env.createModel("MIBasicModel",
                            "Test-MIBasicModel",
                            args=(CredentialsStore(), DATASET_ID,
                                  TRAINING_RUN_ID, 0)))

        env.append(Asset("DOW", self.asset1.values))

        # Check results
        self.assertTrue(
            np.allclose(np.prod(utils.getPeriodReturns(p.returns) + 1),
                        0.9941924543457394))
Esempio n. 16
0
    def test_multiModel_perf_online(self):

        # randomSignals = TODO Pick random numbers between -1 and 1 and round to nearest integer.
        randomSignals = self.randomSignals

        class RandomModel(Model):
            def getSignals(self, idx=0):
                window = self.assets[0].values[idx:]
                loc = self.assets[0].values.index.get_loc(idx)
                signals = pd.DataFrame(np.array([
                    randomSignals[loc:loc + len(window)],
                    randomSignals[loc:loc + len(window)]
                ]).T,
                                       index=window.index,
                                       columns=["bar", "gap"])
                return signals

        # Calculate returns via TradeFramework
        env = SandboxEnvironment("TradeFair")
        p = env.createPortfolio("MyPortfolio",
                                optimizer=env.createOptimizer(
                                    "KellyOptimizer",
                                    "KellyWeights",
                                    opts={"window": 4}))
        p.addModel(env.createModel("BuyAndHold", "Test-BuyAndHold"))
        p.addModel(RandomModel("TestModel2", env))

        start = time.time()
        for i in range(len(self.asset1.values)):
            env.append(Asset("DOW", self.asset1.values[i:i + 1]))
        end = time.time()
        print(end - start)
        self.assertTrue((end - start) < 35, "Operation took too long")

        self.assertTrue(
            np.allclose(p.returns["Open"].values.flatten(), pytest.results))
Esempio n. 17
0
def createAssetFromOHLC(index, ohlc, name="OHLCData"):
    return Asset(name, pd.DataFrame(ohlc, index=index, columns=["Open", "High", "Low", "Close"]))