Exemplo n.º 1
0
    def test_buy(self):
        ticker = "NEOUSDT"
        price = 100
        quantity = .5
        test_account = Account(balance=100)
        test_account.buy(price, quantity, ticker)

        result = test_account.position_exists("NEOUSDT")
        self.assertEqual(True, result)

        position = test_account.get_position("NEOUSDT")
        self.assertEqual(float(-50), position["cash"])
Exemplo n.º 2
0
 def test_buy_sell_2(self):
     ticker = "NEOUSDT"
     price = 100
     quantity = .5
     test_account = Account(balance=100)
     test_account.buy(price, quantity, ticker)
     self.assertEqual(test_account.get_balance(), 50)
     self.assertEqual(test_account.position_exists(ticker), True)
     test_account.buy(price, quantity, ticker)
     self.assertEqual(test_account.get_balance(), 0)
     test_account.sell(100, 1, ticker)
     self.assertEqual(test_account.get_balance(), 100)
     self.assertEqual(test_account.position_exists(ticker), False)
     str(test_account)
Exemplo n.º 3
0
    def test_buy_sell(self):
        ticker = "NEOUSDT"
        price = 1000
        quantity = 1
        test_account = Account(balance=100)
        test_account.buy(price, quantity, ticker)
        str(test_account)

        price = 1005
        quantity = 1
        test_account.sell(price, quantity, ticker)
        str(test_account)
        result = test_account.position_exists("NEOUSDT")
        self.assertEqual(False, result)
Exemplo n.º 4
0
def run():
    logging.basicConfig(filename='log.txt',
                        filemode='a',
                        format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                        datefmt='%d-%b-%y %H:%M:%S',
                        level=logging.DEBUG)
    logging.info('Initiating Run')
    auth_client = Account()
    history = History('BTC-USD')
    sma50 = history.sma(50)
    sma100 = history.sma(100)
    if sma50 > sma100:
        bull_flag = True
    else:
        bull_flag = False
    while True:
        if datetime.now().minute == 0:
            history = History('BTC-USD')
            if bull_flag is False and history.sma(50) > history.sma(100):
                buy = auth_client.buy('BTC-USD')
                logging.info(f'Golden Cross: {buy}')
                bull_flag = True
            if bull_flag is True and history.sma(50) < history.sma(100):
                sell = auth_client.sell('BTC-USD')
                logging.info(f'Death Cross: {sell}')
                bull_flag = False
            else:
                logging.info('No Crossover event')
            time.sleep((60 * 60) - datetime.now().minute * 60 - (datetime.now().microsecond / 1000000))
        else:
            time.sleep((60 * 60) - datetime.now().minute * 60 - (datetime.now().microsecond / 1000000))
Exemplo n.º 5
0
class Backtest(object):
    def __init__(self, strategy, symbol, n):

        print("Starting backtest for " + symbol + " using " + strategy)

        self.start_time = time.time()

        self._account = Account()

        prices = Technical.get_historical_prices(symbol)[-n:]

        method_to_use = getattr(self, strategy)

        result = method_to_use(prices)

        self.backtest(symbol, result, prices, method_to_use.__name__)

    def strat(self, prices):

        result = []

        return result

    def bb_strat(self, prices):
        upper_results = Analyzer.cross_upper_bb(prices)
        lower_results = Analyzer.cross_lower_bb(prices)

        result = []

        for i in range(0, len(upper_results)):
            #upper bands
            if upper_results[i] and not upper_results[i - 1]:
                result.append('b')
            elif not upper_results[i] and upper_results[i - 1]:
                result.append('h')
            #lower bands
            elif lower_results[i] and not lower_results[i - 1]:
                result.append('s')
            elif not lower_results[i] and lower_results[i - 1]:
                result.append('h')

            #else
            else:
                result.append('h')

        return result

    def backtest(self, symbol, indicators, prices, strategy):
        closes = [p['close'] for p in prices]

        #symbol = 'aapl'

        for i in range(0, len(indicators)):
            if indicators[i] == 'b':
                self._account.buy(symbol, 15, closes[i])
            elif indicators[i] == 's':
                self._account.sell(symbol, 15, closes[i])

        self._account.liquidate(symbol, closes[i])

        result = {
            'balance': self._account._balance,
            'shares': self._account._quantity
        }

        exec_time = time.time() - self.start_time
        print(symbol + " backtest (" + str(strategy) + ") took " + exec_time +
              " to execute")

        ##################
        ## write to csv ##
        ##################

        d = datetime.date.today()
        # get abs path of directory by splitting abspath (directory | filename.ext)
        directory, _ = os.path.split(os.path.abspath(__file__))

        f = open(directory + '/backtests/logs/backtest_log.csv', 'a+')
        f.write(symbol + ',')
        f.write(str(strategy) + ',')
        f.write(str(len(closes)) + ',')
        f.write(str(d) + ',')
        f.write(str(self._account._balance) + ',')
        f.write(str(self._account._quantity) + ',')
        f.write(str(len(indicators)) + '\n')
        f.close()

        e = open(directory + '/backtests/logs/' + symbol + '_backtest_log.csv',
                 'a+')
        '''
        e.write(symbol+',')
        e.write(str(strategy)+',')
        e.write(str(d)+',')
        e.write(str(self._account._balance)+',')
        e.write(str(self._account._quantity)+'\n')
        '''
        for b in self._account._balances:
            e.write(str(b) + '\n')
        e.close()

        g = open(
            directory + '/backtests/results/' + symbol +
            '_backtest_result.csv', 'a+')
        g.write(symbol + ',')
        g.write(str(strategy) + ',')
        g.write(str(len(closes)) + ',')
        g.write(str(d) + ',')
        g.write(str(self._account._balance) + ',')
        g.write(str(self._account._quantity) + ',')
        g.write(str(len(indicators)) + '\n')
        g.close()

        return result