def simulateEMA(self): start_id = self.ema2_n initial_sma_1 = sma(self.prices[:start_id], self.ema1_n) initial_sma_2 = sma(self.prices[:start_id], self.ema2_n) prev_ema1 = initial_sma_1 prev_ema2 = initial_sma_2 last_signal = Signal.HODL last_price = None for closing_price in self.prices[start_id:]: ema1 = ema(self.ema1_n, closing_price, prev_ema1) ema2 = ema(self.ema2_n, closing_price, prev_ema2) if ema1 > ema2: signal = Signal.BUY else: signal = Signal.SELL if last_signal != signal: order_completed = False if signal == Signal.BUY: self.coins, self.fiat, order_completed = trading.place_buy_order( price=closing_price, coins=self.coins, fiat=self.fiat) elif signal == Signal.SELL: self.coins, self.fiat, order_completed = trading.place_sell_order( price=closing_price, coins=self.coins, fiat=self.fiat) if order_completed and self.display_orders: print('%s,%f,%f,%f,%f' % (signal, closing_price, self.coins, self.fiat, self.get_balance(self.prices[start_id]))) prev_ema1 = ema1 prev_ema2 = ema2 last_signal = signal last_price = closing_price remaining_coins = trading.buy(last_price, self.fiat) print('=' * 80) print('[-] %-20s: %f' % ('Coin amount', self.coins)) print('[-] %-20s: %f (%f coins)' % ('Fiat amount', self.fiat, remaining_coins)) print('-' * 80) print('[-] %-20s: %f [!]' % ('Final coin amount', self.get_balance(last_price))) print('[-] %-20s: %f' % ('Final value', self.get_fiat_balance(last_price))) print('=' * 80)
def test_sma_basic(): assert sma([0, 1, 0], 2) == 0.5 assert sma([1, 2, 3], 1) == 3 assert sma([0, 3], 2) == 1.5 assert sma([1, 2, 3], 1) == 3 assert sma([1, 2, 3], 2) == 2.5 assert sma([1, 2, 3], 3) == 2
def test_sma_advanced(): assert sma([ 7.14, 6.95, 6.8, 6.6, 7.15, 6.2, 6.5, 6.0, 5.52, 6.28, 6.3, 6.79, 6.5, 6.52, 6.26 ], 8) != 6
def test_sma_corner_cases(): assert sma(None, None) is None assert sma([], None) is None assert sma([], 0) is None assert sma([], 1) is None assert sma([0], 1) == 0 assert sma([1], 1) == 1 assert sma([0, 0], 1) == 0 assert sma([0, 0], 2) == 0 assert sma([0, 1], 1) == 1 assert sma([0, 0, 1], 1) == 1 assert sma([0, 1, 0], 1) == 0
def test_sma(): assert sma([11, 12, 13, 14, 15], 5) == 13 assert sma([12, 13, 14, 15, 16], 5) == 14 assert sma([13, 14, 15, 16, 17], 5) == 15