def testStateInit(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 15, 20, 10, 20, 10) state = State(candle, 5, 100) self.assertEqual(state.position, candle.close)
def setUp(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 200, 200, 100, 150, 100) self.state = State(candle, 5, 100) self.distance = 1 self.tp = 2 self.sl = 2 self.trl = 2 self.min_seq = 3 self.use_mac = True logger = Logger(0, "TST_SYM", 30) self.intelligence = Intelligence(0, 0, logger, self.distance, self.tp, self.sl, self.trl, self.min_seq, self.use_mac)
def __init__(self, id, output, logger, init_candles, atr_len, ma_len, continuation=1, reversal=2): self.id = id self.output = [int(x) for x in output.split(",")] self.logger = logger self.logger.log_info(self.name, "Starting...") # increment atr_len by 1 so first # can be used to calculate the TR self.atr_len = atr_len + 1 self.ma_len = ma_len self.continuation = continuation self.reversal = reversal # check number of candles is enough to build if len(init_candles) < max(self.atr_len, self.ma_len): error_message = "%d is not enough to build model" % len( init_candles) logger.log_fail(self.name, error_message) raise Exception(error_message) # the number of candles needed to create ATR and MA self.retention = max(self.atr_len, self.ma_len) self.candles = init_candles[0:self.retention] atr = utils.get_candles_atr(init_candles[-self.atr_len:]) ma = utils.get_candles_ema(init_candles[-self.ma_len:]) self.state = State(init_candles[self.retention - 1], atr, ma, self.continuation, self.reversal) for candle in init_candles[self.retention:]: self.input(candle) startup_str = "Started successfully with: parameters: atr_len=%d, ma_len=%d continuation=%d, reversal=%d" \ %(atr_len, ma_len, continuation, reversal) self.logger.log_info(self.name, startup_str)
class IntelligenceTestCase(unittest.TestCase): def setUp(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 200, 200, 100, 150, 100) self.state = State(candle, 5, 100) self.distance = 1 self.tp = 2 self.sl = 2 self.trl = 2 self.min_seq = 3 self.use_mac = True logger = Logger(0, "TST_SYM", 30) self.intelligence = Intelligence(0, 0, logger, self.distance, self.tp, self.sl, self.trl, self.min_seq, self.use_mac) def testBuyOrders(self): # no order for sequence of 1 order = self.intelligence.input(self.state) self.assertIsNone(order) # order for sequence of 3 and above ma self.state.sequence = 3 self.state.ma = self.state.position - 1 order = self.intelligence.input(self.state) self.assertIsNotNone(order) self.assertEqual(order.direction, 1) self.assertEqual(order.open, self.state.level(self.distance)) self.assertEqual(order.tp, self.state.level(self.distance + self.tp)) self.assertEqual(order.sl, self.state.level(self.distance - self.sl)) self.assertEqual(order.trl, order.open - order.sl) # no order for sequence of 3 and below ma self.state.ma = self.state.position + 1 order = self.intelligence.input(self.state) self.assertIsNone(order) # no take profit self.intelligence.tp = 0 self.state.ma = self.state.position - 1 order = self.intelligence.input(self.state) self.assertIsNotNone(order) def testSellOrders(self): # order for sequence of -3 and below ma self.state.sequence = -3 self.state.ma = self.state.position + 1 order = self.intelligence.input(self.state) self.assertIsNotNone(order) self.assertEqual(order.direction, -1) self.assertEqual(order.tp, self.state.level(-self.distance - self.tp)) self.assertEqual(order.sl, self.state.level(-self.distance + self.sl)) self.assertEqual(order.trl, order.sl - order.open) # No order for sequence of -3 and above ma self.state.ma = self.state.position - 1 order = self.intelligence.input(self.state) self.assertIsNone(order)
def testStateDownSequenceUpdateContinuation(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 200, 200, 100, 150, 100) state = State(candle, 5, 100) candle.close -= 5 state.update(candle, 5, 100) self.assertEqual(state.sequence, -2) candle.close = state.position - 11 state.update(candle, 5, 100) self.assertEqual(state.sequence, -4) candle.close = state.position - 4 state.update(candle, 5, 100) self.assertEqual(state.sequence, -4) candle.close = state.position - 9 state.update(candle, 5, 100) self.assertEqual(state.sequence, -5) candle.close = state.position - 17 state.update(candle, 5, 100) self.assertEqual(state.sequence, -8)
def testStateUpSequenceUpdateReversal(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 150, 200, 100, 200, 100) state = State(candle, 5, 100) candle.close -= 5 state.update(candle, 5, 100) self.assertEqual(state.sequence, 1) candle.close = state.position - 9 state.update(candle, 5, 100) self.assertEqual(state.sequence, 1) candle.close = state.position - 11 state.update(candle, 5, 100) self.assertEqual(state.sequence, -1) candle = Candle(now, 150, 200, 100, 200, 100) state = State(candle, 5, 100) candle.close = state.position - 100 state.update(candle, 5, 100) self.assertEqual(state.sequence, -19)
def testStateUpSequenceUpdateContinuation(self): now = str( datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000000000Z")) candle = Candle(now, 150, 200, 100, 200, 100) state = State(candle, 5, 100) candle.close += 5 state.update(candle, 5, 100) self.assertEqual(state.sequence, 2) candle.close = state.position + 11 state.update(candle, 5, 100) self.assertEqual(state.sequence, 4) candle.close = state.position + 4 state.update(candle, 5, 100) self.assertEqual(state.sequence, 4) candle.close = state.position + 9 state.update(candle, 5, 100) self.assertEqual(state.sequence, 5) candle.close = state.position + 17 state.update(candle, 5, 100) self.assertEqual(state.sequence, 8) candle = Candle(now, 150, 200, 100, 200, 100) state = State(candle, 5, 100) candle.close = state.position + 100 state.update(candle, 5, 100) self.assertEqual(state.sequence, 21)