def test_get_request_return_None_when_data_is_invaild(self): bnh = StrategyBuyAndHold() bnh.initialize(100, 10) dummy_info = {} bnh.update_trading_info(dummy_info) request = bnh.get_request() self.assertEqual(request, None)
def test_get_request_return_correct_request(self): bnh = StrategyBuyAndHold() bnh.initialize(50000, 100) dummy_info = {} dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["price"], 20000000) self.assertEqual(requests[0]["amount"], 0.0005) self.assertEqual(requests[0]["type"], "buy")
def test_get_request_return_None_when_balance_is_smaller_than_total_value( self): bnh = StrategyBuyAndHold() bnh.initialize(5000, 10) dummy_info = {} dummy_info["closing_price"] = 62000000 bnh.update_trading_info(dummy_info) bnh.balance = 10000 requests = bnh.get_request() self.assertEqual(requests, None)
def test_get_request_return_None_when_balance_is_smaller_than_min_price( self): bnh = StrategyBuyAndHold() bnh.initialize(900, 10) bnh.is_simulation = False dummy_info = {} dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) bnh.balance = 9.5 requests = bnh.get_request() self.assertEqual(requests, None)
def test_get_request_use_balance_when_balance_is_smaller_than_target_budget( self): bnh = StrategyBuyAndHold() bnh.initialize(1000, 10) dummy_info = {} dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) bnh.balance = 100 requests = bnh.get_request() self.assertEqual(requests[0]["price"], 20000) self.assertEqual(requests[0]["amount"], 0.005)
def test_get_request_return_turn_over_when_target_budget_is_too_small_at_simulation( self): bnh = StrategyBuyAndHold() bnh.initialize(100, 100) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["price"], 0) self.assertEqual(requests[0]["amount"], 0)
def test_get_request_return_turn_over_when_balance_is_smaller_than_min_price_at_simulation( self, ): bnh = StrategyBuyAndHold() bnh.initialize(900, 10) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) bnh.balance = 9.5 requests = bnh.get_request() self.assertEqual(requests[0]["price"], 0) self.assertEqual(requests[0]["amount"], 0)
def test_get_request_return_same_datetime_at_simulation(self): bnh = StrategyBuyAndHold() bnh.initialize(1000, 100) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["date_time"], "2020-02-25T15:41:09") dummy_info["date_time"] = "2020-02-25T23:59:59" dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["date_time"], "2020-02-25T23:59:59")
def test_get_request_return_correct_request_with_cancel_requests(self): bnh = StrategyBuyAndHold() bnh.initialize(50000, 100) bnh.waiting_requests["mango_id"] = {"request": {"id": "mango_id"}} bnh.waiting_requests["orange_id"] = {"request": {"id": "orange_id"}} dummy_info = {} dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["id"], "mango_id") self.assertEqual(requests[0]["type"], "cancel") self.assertEqual(requests[1]["id"], "orange_id") self.assertEqual(requests[1]["type"], "cancel") self.assertEqual(requests[2]["price"], 20000000) self.assertEqual(requests[2]["amount"], 0.0005) self.assertEqual(requests[2]["type"], "buy")
def test_ITG_strategy_buy_and_hold_full(self): strategy = StrategyBuyAndHold() self.assertEqual(strategy.get_request(), None) strategy.initialize(50000, 5000) # 거래 정보 입력 - 1 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:51:00", "opening_price": 11288000.0, "high_price": 11304000.0, "low_price": 11282000.0, "closing_price": 11304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 11304000.0, "amount": 0.0008, } self.assertEqual(request[0]["type"], expected_request["type"]) self.assertEqual(request[0]["price"], expected_request["price"]) self.assertEqual(request[0]["amount"], expected_request["amount"]) # 거래 결과 입력 - 정상 체결 됨 strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 11304000.0, "amount": 0.0009, "date_time": "2020-04-30T14:51:00", }, "type": "buy", "price": 11304000.0, "amount": 0.0009, "msg": "success", "balance": 0, "state": "done", "date_time": "2020-04-30T14:51:00", }) self.assertEqual(strategy.balance, 39821) # 거래 정보 입력 - 2 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 11304000.0, "high_price": 21304000.0, "low_price": 11304000.0, "closing_price": 21304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 21304000.0, "amount": 0.0004, } self.assertEqual(request[0]["type"], expected_request["type"]) self.assertEqual(request[0]["price"], expected_request["price"]) self.assertEqual(request[0]["amount"], expected_request["amount"]) # 거래 결과 입력 - 요청되었으나 체결 안됨 self.assertEqual(strategy.balance, 39821) strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 11304000.0, "amount": 0.0009, "date_time": "2020-04-30T14:52:00", }, "type": "buy", "price": 11304000.0, "amount": 0.0009, "msg": "success", "balance": 0, "state": "requested", "date_time": "2020-04-30T14:52:00", }) self.assertEqual(strategy.balance, 39821) last_id = request[0]["id"] # 거래 정보 입력 - 3 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 21304000.0, "high_price": 21304000.0, "low_price": 21304000.0, "closing_price": 21304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 21304000.0, "amount": 0.0004, } self.assertEqual(request[0]["type"], "cancel") self.assertEqual(request[0]["id"], last_id) self.assertEqual(request[1]["type"], expected_request["type"]) self.assertEqual(request[1]["price"], expected_request["price"]) self.assertEqual(request[1]["amount"], expected_request["amount"]) # 거래 결과 입력 - 일부 체결됨 self.assertEqual(strategy.balance, 39821) strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 21304000.0, "amount": 0.0009, "date_time": "2020-04-30T14:52:00", }, "type": "buy", "price": 21304000.0, "amount": 0.0002, "msg": "success", "balance": 0, "state": "done", "date_time": "2020-04-30T14:52:00", }) self.assertEqual(strategy.balance, 35558) # 거래 정보 입력 - 4 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 21304000.0, "high_price": 41304000.0, "low_price": 21304000.0, "closing_price": 41304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 41304000.0, "amount": 0.0002, } self.assertEqual(request[0]["type"], expected_request["type"]) self.assertEqual(request[0]["price"], expected_request["price"]) self.assertEqual(request[0]["amount"], expected_request["amount"]) # 거래 결과 입력 - 정상 체결됨 self.assertEqual(strategy.balance, 35558) strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 41304000.0, "amount": 0.0009, "date_time": "2020-04-30T14:52:00", }, "type": "buy", "price": 41304000.0, "amount": 0.0002, "msg": "success", "balance": 0, "state": "done", "date_time": "2020-04-30T14:52:00", }) self.assertEqual(strategy.balance, 27293) # 거래 정보 입력 - 5 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 41304000.0, "high_price": 61304000.0, "low_price": 41304000.0, "closing_price": 61304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 61304000.0, "amount": 0.0001, } self.assertEqual(request[0]["type"], expected_request["type"]) self.assertEqual(request[0]["price"], expected_request["price"]) self.assertEqual(request[0]["amount"], expected_request["amount"]) # 거래 결과 입력 - 정상 체결됨 self.assertEqual(strategy.balance, 27293) strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 61304000.0, "amount": 0.0009, "date_time": "2020-04-30T14:52:00", }, "type": "buy", "price": 61304000.0, "amount": 0.0002, "msg": "success", "balance": 0, "state": "done", "date_time": "2020-04-30T14:52:00", }) self.assertEqual(strategy.balance, 15026) # 거래 정보 입력 - 6 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 61304000.0, "high_price": 61304000.0, "low_price": 61304000.0, "closing_price": 61304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() expected_request = { "type": "buy", "price": 61304000.0, "amount": 0.0001, } self.assertEqual(request[0]["type"], expected_request["type"]) self.assertEqual(request[0]["price"], expected_request["price"]) self.assertEqual(request[0]["amount"], expected_request["amount"]) # 거래 결과 입력 - 정상 체결됨 self.assertEqual(strategy.balance, 15026) strategy.update_result({ "request": { "id": request[0]["id"], "type": "buy", "price": 61304000.0, "amount": 0.0002, "date_time": "2020-04-30T14:52:00", }, "type": "buy", "price": 61304000.0, "amount": 0.0002, "msg": "success", "balance": 0, "state": "done", "date_time": "2020-04-30T14:52:00", }) self.assertEqual(strategy.balance, 2759) # 거래 정보 입력 - 7 strategy.update_trading_info({ "market": "KRW-BTC", "date_time": "2020-04-30T14:52:00", "opening_price": 61304000.0, "high_price": 61304000.0, "low_price": 61304000.0, "closing_price": 61304000.0, "acc_price": 587101574.8949, "acc_volume": 51.97606868, }) # 거래 요청 정보 생성 request = strategy.get_request() self.assertEqual(request, None) self.assertEqual(strategy.balance, 2759)
def test_update_trading_info_ignore_info_when_not_yet_initialzed(self): bnh = StrategyBuyAndHold() bnh.update_trading_info("mango") self.assertEqual(len(bnh.data), 0)
def test_update_trading_info_append_info_to_data(self): bnh = StrategyBuyAndHold() bnh.initialize(100, 10) bnh.update_trading_info("mango") self.assertEqual(bnh.data.pop(), "mango")