def test_BackTest_init(self): """[summary] Testing Candle length is calculated by len(self.candles), where self.candles =list(models.Candle_1h.objects.all()),etc. """ createdf.connectandsave(1) b = ai.BackTest(eval("Candle_1h")) length = len(b.candles) self.assertIs(b.len_candles, length)
def test_BackTestIchimoku(self): createdf.connectandsave() orders = ["", "Sell", "Buy"] b = ai.BackTest(eval("Candle_1h")) t, k, s = 9, 26, 52 events = b.BackTestIchimoku(t, k, s) length = len(events["index"]) print(f"Ichimoku events{events}") if length == 0: self.assertEqual(events["order"], []) else: for i in range(length): self.assertEqual(events["order"][i] in orders, True) self.assertEqual(events["index"][i] < b.len_candles, True)
def test_AddEvents(self): """[summary]Test AddEvents function. It saves events like buy, sell, buy,... and sell, buy, sell, ... . """ createdf.connectandsave(num_data=100) b = ai.BackTest(eval("Candle_1h")) self.assertEqual(BackTestSignalEvents.objects.exists(), False) # testing buy, sell,buy patern event1 = {"index": [], "order": []} r1 = "BUY" i1 = 10 b.AddEvents(events=event1, r=r1, i=i1) signalevent1 = BackTestSignalEvents.objects.last() self.assertEqual(signalevent1.side, "BUY") event2 = {"index": [10], "order": ["BUY"]} r2 = "" i2 = 11 b.AddEvents(events=event2, r=r2, i=i2) signalevent2 = BackTestSignalEvents.objects.last() self.assertEqual(signalevent1, signalevent2) event3 = {"index": [10], "order": ["BUY"]} r3 = "SELL" i3 = 12 b.AddEvents(events=event3, r=r3, i=i3) signalevent3 = BackTestSignalEvents.objects.last() self.assertNotEqual(signalevent2, signalevent3) print(f"SignalEvents are {BackTestSignalEvents.objects.all()}") self.assertEqual(signalevent3.side, "SELL") BackTestSignalEvents.objects.all().delete() # testing sell, buy, sell patern event1 = {"index": [], "order": []} r1 = "SELL" i1 = 10 b.AddEvents(events=event1, r=r1, i=i1) signalevent1 = BackTestSignalEvents.objects.last() self.assertEqual(signalevent1.side, "SELL") event2 = {"index": [10], "order": ["SELL"]} r2 = "" i2 = 11 b.AddEvents(events=event2, r=r2, i=i2) signalevent2 = BackTestSignalEvents.objects.last() self.assertEqual(signalevent1, signalevent2) event3 = {"index": [10], "order": ["SELL"]} r3 = "BUY" i3 = 12 b.AddEvents(events=event3, r=r3, i=i3) signalevent3 = BackTestSignalEvents.objects.last() self.assertNotEqual(signalevent2, signalevent3) print(f"SignalEvents are {BackTestSignalEvents.objects.all()}") self.assertEqual(signalevent3.side, "BUY")
def test_BackTestSma(self): createdf.connectandsave() orders = ["", "Sell", "Buy"] b = ai.BackTest(eval("Candle_1h")) period1 = 7 period2 = 14 events = b.BackTestSma(period1, period2) length = len(events["index"]) print(f"SMA events{events}") if length == 0: self.assertEqual(events["order"], []) else: for i in range(length): self.assertEqual(events["order"][i] in orders, True) self.assertEqual(events["index"][i] < b.len_candles, True)
def test_BsckTestRsi(self): createdf.connectandsave() orders = ["", "Sell", "Buy"] b = ai.BackTest(eval("Candle_1h")) period = 14 buyThread = 70 sellThread = 30 events = b.BackTestRsi(period, buyThread, sellThread) length = len(events["index"]) print(f"Rsi events{events}") if length == 0: self.assertEqual(events["order"], []) else: for i in range(length): self.assertEqual(events["order"][i] in orders, True) self.assertEqual(events["index"][i] < b.len_candles, True)
def test_BackTestMacd(self): createdf.connectandsave() orders = ["", "Sell", "Buy"] b = ai.BackTest(eval("Candle_1h")) fastperiod = 12 slowperiod = 26 signalperiod = 9 events = b.BackTestMacd(fastperiod, slowperiod, signalperiod) length = len(events["index"]) print(f"Macd events{events}") if length == 0: self.assertEqual(events["order"], []) else: for i in range(length): self.assertEqual(events["order"][i] in orders, True) self.assertEqual(events["index"][i] < b.len_candles, True)