def test_decimals(self):
     # Long with decimals
     a = exchange.Account(2)
     a.enter_position('Long', 1, 0.00000001)
     self.assertEqual(a.total_value(0.00000002), 3)
     a.close_position(a.positions[0], 1, 0.00000002)
     self.assertEqual(a.buying_power, 3)
     # Short with decimals
     a = exchange.Account(2)
     a.enter_position('Short', 1, 0.00000002)
     self.assertEqual(a.total_value(0.00000001), 2.5)
     a.close_position(a.positions[0], 1, 0.00000001)
     self.assertEqual(a.buying_power, 2.5)
Beispiel #2
0
 def test_decimals(self):
     # Long with decimals
     a = exchange.Account(2)
     a.EnterPosition('Long', 1, 0.00000001)
     self.assertEqual(a.TotalValue(0.00000002), 3)
     a.ClosePosition(a.Positions[0], 1, 0.00000002)
     self.assertEqual(a.BuyingPower, 3)
     # Short with decimals
     a = exchange.Account(2)
     a.EnterPosition('Short', 1, 0.00000002)
     self.assertEqual(a.TotalValue(0.00000001), 2.5)
     a.ClosePosition(a.Positions[0], 1, 0.00000001)
     self.assertEqual(a.BuyingPower, 2.5)
 def test_errors(self):
     a = exchange.Account(1000)
     self.assertRaises(ValueError, a.enter_position, 'Long', 2000, 10)
     self.assertRaises(ValueError, a.enter_position, 'Long', -500, 10)
     self.assertRaises(ValueError, a.enter_position, 'Long', 500, -10)
     # Enter valid position
     a.enter_position('Long', 250, 10)
     a.enter_position('Short', 250, 10)
     Long = a.positions[0]
     Short = a.positions[1]
     self.assertRaises(ValueError, a.close_position, Long, 0.5, -20)
     self.assertRaises(ValueError, a.close_position, Long, 1.01, 20)
     self.assertRaises(ValueError, a.close_position, Long, -0.5, 20)
     self.assertRaises(ValueError, a.close_position, Short, 1.01, 20)
     self.assertRaises(ValueError, a.close_position, Short, -0.5, 20)
 def test_both(self):
     a = exchange.Account(1000)
     a.enter_position('Long', 200, 20)
     a.enter_position('Short', 250, 25)
     self.assertEqual(a.buying_power, 550)
     self.assertEqual(a.total_value(25), 1050)
     Long = a.positions[0]
     Short = a.positions[1]
     a.close_position(Long, 0.5, 40)
     a.close_position(Short, 0.5, 12.5)
     self.assertEqual(a.buying_power, 937.5)
     self.assertEqual(a.total_value(12.5), 1187.5)
     a.close_position(Long, 1.0, 50)
     a.close_position(Short, 1.0, 50)
     self.assertEqual(a.buying_power, 1187.5)
     self.assertEqual(a.total_value(100), 1187.5)
Beispiel #5
0
 def test_both(self):
     a = exchange.Account(1000)
     a.EnterPosition('Long', 200, 20)
     a.EnterPosition('Short', 250, 25)
     self.assertEqual(a.BuyingPower, 550)
     self.assertEqual(a.TotalValue(25), 1050)
     Long = a.Positions[0]
     Short = a.Positions[1]
     a.ClosePosition(Long, 0.5, 40)
     a.ClosePosition(Short, 0.5, 12.5)
     self.assertEqual(a.BuyingPower, 937.5)
     self.assertEqual(a.TotalValue(12.5), 1187.5)
     a.ClosePosition(Long, 1.0, 50)
     a.ClosePosition(Short, 1.0, 50)
     self.assertEqual(a.BuyingPower, 1187.5)
     self.assertEqual(a.TotalValue(100), 1187.5)
 def test_short(self):
     a = exchange.Account(1000)
     # Win on a short
     a.enter_position('Short', 500, 10)
     a.enter_position('Short', 500, 10)
     self.assertEqual(a.buying_power, 0)
     self.assertEqual(a.total_value(10), 1000)
     S0 = a.positions[0]
     S1 = a.positions[1]
     a.close_position(S0, 0.5, 5)
     a.close_position(S1, 0.5, 5)
     self.assertEqual(a.buying_power, 750)
     self.assertEqual(a.total_value(5), 1500)
     a.close_position(S0, 0.5, 2.5)
     a.close_position(S1, 0.5, 2.5)
     self.assertEqual(a.buying_power, 1187.5)
     self.assertEqual(a.total_value(2.5), 1625)
     # Lose on a short
     a.enter_position('Short', 1000, 2)
     S2 = a.positions[2]
     a.close_position(S2, 0.5, 4)
     self.assertEqual(a.buying_power, 187.5)
     self.assertEqual(a.total_value(4), 587.5)
 def test_long(self):
     a = exchange.Account(1000)
     # Win on a long
     a.enter_position('Long', 500, 10)
     a.enter_position('Long', 500, 10)
     self.assertEqual(a.buying_power, 0)
     self.assertEqual(a.total_value(10), 1000)
     L0 = a.positions[0]
     L1 = a.positions[1]
     a.close_position(L0, 0.5, 20)
     a.close_position(L1, 0.5, 20)
     self.assertEqual(a.buying_power, 1000)
     self.assertEqual(a.total_value(20), 2000)
     a.close_position(L0, 0.5, 40)
     a.close_position(L1, 0.5, 40)
     self.assertEqual(a.buying_power, 2000)
     self.assertEqual(a.total_value(40), 3000)
     # Lose on a long
     a.enter_position('Long', 1000, 50)
     L2 = a.positions[2]
     a.close_position(L2, 0.5, 25)
     self.assertEqual(a.buying_power, 1250)
     self.assertEqual(a.total_value(25), 2125)
Beispiel #8
0
 def test_short(self):
     a = exchange.Account(1000)
     # Win on a short
     a.EnterPosition('Short', 500, 10)
     a.EnterPosition('Short', 500, 10)
     self.assertEqual(a.BuyingPower, 0)
     self.assertEqual(a.TotalValue(10), 1000)
     S0 = a.Positions[0]
     S1 = a.Positions[1]
     a.ClosePosition(S0, 0.5, 5)
     a.ClosePosition(S1, 0.5, 5)
     self.assertEqual(a.BuyingPower, 750)
     self.assertEqual(a.TotalValue(5), 1500)
     a.ClosePosition(S0, 0.5, 2.5)
     a.ClosePosition(S1, 0.5, 2.5)
     self.assertEqual(a.BuyingPower, 1187.5)
     self.assertEqual(a.TotalValue(2.5), 1625)
     # Lose on a short
     a.EnterPosition('Short', 1000, 2)
     S2 = a.Positions[2]
     a.ClosePosition(S2, 0.5, 4)
     self.assertEqual(a.BuyingPower, 187.5)
     self.assertEqual(a.TotalValue(4), 587.5)
Beispiel #9
0
 def test_long(self):
     a = exchange.Account(1000)
     # Win on a long
     a.EnterPosition('Long', 500, 10)
     a.EnterPosition('Long', 500, 10)
     self.assertEqual(a.BuyingPower, 0)
     self.assertEqual(a.TotalValue(10), 1000)
     L0 = a.Positions[0]
     L1 = a.Positions[1]
     a.ClosePosition(L0, 0.5, 20)
     a.ClosePosition(L1, 0.5, 20)
     self.assertEqual(a.BuyingPower, 1000)
     self.assertEqual(a.TotalValue(20), 2000)
     a.ClosePosition(L0, 0.5, 40)
     a.ClosePosition(L1, 0.5, 40)
     self.assertEqual(a.BuyingPower, 2000)
     self.assertEqual(a.TotalValue(40), 3000)
     # Lose on a long
     a.EnterPosition('Long', 1000, 50)
     L2 = a.Positions[2]
     a.ClosePosition(L2, 0.5, 25)
     self.assertEqual(a.BuyingPower, 1250)
     self.assertEqual(a.TotalValue(25), 2125)
Beispiel #10
0
    def start(self, data, capital=None, logic=None, simulation=False, **kwargs):
        self.data = data
        self.model = Model(**kwargs)

        # ====================== #
        #    Initial Training    #
        # ====================== #
        
        X, y = [], []                                   
        for i in range(self.trainStr, self.trainEnd+1):

            Xs = [data.iloc[i][var] for var in self.features]
            X.append(Xs)                             
            
            # Find the stock price movement for day 2
            y1 = helpers.change(data.iloc[i+1].open, data.iloc[i+1].close)
            if y1 > 0: y.append(1)  # If it went up, classify as 1
            else:      y.append(-1) # If it went down, classify as -1
        
        self.model.fit(X, y)

        # ====================== #
        #         Testing        #
        # ====================== #       
        
        if simulation:
            self.account = exchange.Account(capital)

        for i in range(self.testStr, self.testEnd):
            
            # ==================================== #
            #  DAY 1 @ 8:00 PM | Markets closed    #
            #  Make prediction for DAY 2           #
            #  Update Buy/Sell count (or neither)  #
            # ==================================== #        
            
            Xs = [data.iloc[i][var] for var in self.features]
            neg, pos = self.model.predict(Xs)
            
            if pos >= self.buyThreshold:  # Positive confidence >= buyThreshold
                prediction =  1 
                confidence = pos
            
            elif neg >= self.sellThreshold: # If negative confidence >= sellThreshold
                prediction = -1
                confidence = neg

            else: prediction = confidence = 0
            
            if simulation:
                # Update account variables
                self.account.Date = data.iloc[i]['date']
                self.account.Equity.append(self.account.TotalValue(data.iloc[i]['close']))

                # Execute trading logic
                logic(self.account, data.iloc[i], prediction, confidence)

                # Cleanup empty positions
                self.account.PurgePositions()

            if not simulation:
                # ==================================== #
                #  DAY 2 @ 4:30 PM | Markets closed    #
                #  Analyze results from DAY 2          #
                #  Record if prediction was correct    #
                # ==================================== #
                
                # Case 1/2: Prediction is positive (buy), next day performance is/isn't positive 
                if prediction == 1:
                    self.totalBuys += 1
                    if helpers.change(data.iloc[i+1].open, data.iloc[i+1].close) > 0:
                        self.correctBuys += 1
                
                # Case 3/4: Prediction is negative (sell), next day performance is/isn't negative
                elif prediction == -1:
                    self.totalSells += 1
                    if helpers.change(data.iloc[i+1].open, data.iloc[i+1].close) < 0:
                        self.correctSells += 1
            
            # ====================== #
            #     Update Model       #
            #     if specified       #
            # ====================== #
            
            if self.continueTraining:
                X.append(Xs)     
                
                if change(data, i+1) > 0: y.append(1)
                else:                     y.append(-1)
                
                self.model.fit(X, y)