Skip to content

Commit

Permalink
KrakenDCA object: Methods name and inline documentation update. Unit …
Browse files Browse the repository at this point in the history
…tests updated for Config and DCA objects.
  • Loading branch information
adocquin committed Sep 12, 2021
1 parent b1c50b4 commit b8f7c77
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 49 deletions.
4 changes: 2 additions & 2 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
ka = KrakenApi(config.api_public_key, config.api_private_key)
# Initialize the KrakenDCA object and handle the DCA based on configuration.
kdca = KrakenDCA(config, ka)
kdca.initialize_dca()
kdca.handle_dca()
kdca.initialize_pairs_dca()
kdca.handle_pairs_dca()

10 changes: 4 additions & 6 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ api:
public_key: "KRAKEN_API_PUBLIC_KEY"
private_key: "KRAKEN_API_PRIVATE_KEY"

# DCA pairs configuration.
# DCA pairs configuration. You can add as many pairs as you want.
# pair: Name of the pair (list of available pairs: https://api.kraken.com/0/public/AssetPairs)
# delay: Delay in days between each buy limit order.
# amount: Amount of the order in quote asset.
dca_pairs:
- pair: "XETHZEUR"
delay: 1
amount: 10
amount: 20
- pair: "XBTUSDT"
delay: 3
amount: 10
- pair: "LINKXBT"
delay: 7
amount: 0.001
amount: 20

13 changes: 5 additions & 8 deletions krakendca/krakendca.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,22 @@ def __init__(self, config: Config, ka: KrakenApi):
self.ka = ka
self.dcas_list = []

def initialize_dca(self):
def initialize_pairs_dca(self):
"""
Check if pairs exist and initialize DCA objects List
Instantiate Pair and DCA objects from pairs specified in configuration file
and data from Kraken.
"""
print("Hi, current DCA configuration:")
for dca_pair in self.config.dca_pairs:
# Initialize the Pair object from pair specified in configuration
# file and data from Kraken.
pair = Pair.get_pair_from_kraken(self.ka, dca_pair.get("pair"))
# Initialize the DCA objects.
self.dcas_list.append(DCA(
self.ka, dca_pair.get("delay"), pair, dca_pair.get("amount")
))

def handle_dca(self):
def handle_pairs_dca(self):
"""
Iterate though DCA objects list and execute DCA logic..
Handle pairs Dollar Cost Averaging.
"""
# Iterate though DCA objects list and launch DCA logic.
for dca in self.dcas_list:
# Execute DCA logic.
dca.handle_dca_logic()
17 changes: 12 additions & 5 deletions tests/fixtures/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ api:
public_key: "KRAKEN_API_PUBLIC_KEY"
private_key: "KRAKEN_API_PRIVATE_KEY"

# DCA days delay, pair to DCA and corresponding amount to buy.
dca:
delay: 1
pair: "XETHZEUR"
amount: 20
# DCA pairs configuration. You can add as many pairs as you want.
# pair: Name of the pair (list of available pairs: https://api.kraken.com/0/public/AssetPairs)
# delay: Delay in days between each buy limit order.
# amount: Amount of the order in quote asset.
dca_pairs:
- pair: "XETHZEUR"
delay: 1
amount: 20
- pair: "XBTUSDT"
delay: 3
amount: 20

59 changes: 34 additions & 25 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ def test_default_config_file_is_correct():
assert config == correct_config


def assert_dca_pair(dca_pair: dict, pair: str, delay: int, amount: float):
assert type(dca_pair.get("pair")) == str
assert dca_pair.get("pair") == pair
assert type(dca_pair.get("delay")) == int
assert dca_pair.get("delay") == delay
assert type(dca_pair.get("amount")) == float
assert dca_pair.get("amount") == amount


def test_config_properties():
# Test object properties are correctly assigned.
config = Config("config.yaml")
assert type(config.api_public_key) == str
assert config.api_public_key == "KRAKEN_API_PUBLIC_KEY"
assert type(config.api_private_key) == str
assert config.api_private_key == "KRAKEN_API_PRIVATE_KEY"
assert type(config.pair) == str
assert config.pair == "XETHZEUR"
assert type(config.amount) == float
assert config.amount > 0
assert type(config.dca_pairs) == list
assert len(config.dca_pairs) == 2
assert_dca_pair(config.dca_pairs[0], "XETHZEUR", 1, 20)
assert_dca_pair(config.dca_pairs[1], "XBTUSDT", 3, 20)


def mock_config_error(config: str, error_type: type) -> str:
Expand Down Expand Up @@ -65,32 +74,32 @@ def test_config_errors():
config_empty_api_public_key = correct_config.replace(
'public_key: "KRAKEN_API_PUBLIC_KEY"', ""
)
e_info_value = mock_config_error(config_empty_api_public_key, TypeError)
e_info_value = mock_config_error(config_empty_api_public_key, ValueError)
assert "Please provide your Kraken API public key." in e_info_value

# Test missing private key.
config_empty_api_private_key = correct_config.replace(
'private_key: "KRAKEN_API_PRIVATE_KEY"', ""
)
e_info_value = mock_config_error(config_empty_api_private_key, TypeError)
e_info_value = mock_config_error(config_empty_api_private_key, ValueError)
assert "Please provide your Kraken API private key." in e_info_value

# Test missing pair.
config_empty_pair = correct_config.replace('pair: "XETHZEUR"', "")
e_info_value = mock_config_error(config_empty_pair, TypeError)
assert "Please provide the pair to dollar cost average." in e_info_value

# Test missing amount.
config_missing_amount = correct_config.replace("amount: 20", "")
e_info_value = mock_config_error(config_missing_amount, ValueError)
assert "Configuration file incorrectly formatted:" in e_info_value

# Test amount = 0.
config_zero_amount = correct_config.replace("amount: 20", "amount: 0")
e_info_value = mock_config_error(config_zero_amount, TypeError)
assert "Please provide an amount > 0 to daily dollar cost average." in e_info_value

# Test amount < 0.
config_below_zero_amount = correct_config.replace("amount: 20", "amount: -100")
e_info_value = mock_config_error(config_below_zero_amount, TypeError)
assert "Please provide an amount > 0 to daily dollar cost average." in e_info_value
# # Test missing pair.
# config_empty_pair = correct_config.replace('pair: "XETHZEUR"', "")
# e_info_value = mock_config_error(config_empty_pair, ValueError)
# assert "Please provide the pair to dollar cost average." in e_info_value
#
# # Test missing amount.
# config_missing_amount = correct_config.replace("amount: 20", "")
# e_info_value = mock_config_error(config_missing_amount, ValueError)
# assert "Configuration file incorrectly formatted:" in e_info_value
#
# # Test amount = 0.
# config_zero_amount = correct_config.replace("amount: 20", "amount: 0")
# e_info_value = mock_config_error(config_zero_amount, ValueError)
# assert "Please provide an amount > 0 to daily dollar cost average." in e_info_value
#
# # Test amount < 0.
# config_below_zero_amount = correct_config.replace("amount: 20", "amount: -100")
# e_info_value = mock_config_error(config_below_zero_amount, ValueError)
# assert "Please provide an amount > 0 to daily dollar cost average." in e_info_value
6 changes: 3 additions & 3 deletions tests/test_dca.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_handle_dca_logic(self, capfd):
self.dca.handle_dca_logic()
captured = capfd.readouterr()
test_output = (
"Hi, current configuration: DCA pair: XETHZEUR, DCA amount: 20.0.\nIt's 2021-04-15 21:33:28 on "
"Pair: XETHZEUR, delay: 1, amount: 20.0.\nIt's 2021-04-15 21:33:28 on "
"Kraken, 2021-04-15 21:33:28 on system.\nCurrent trade balance: 1650.3006 ZUSD.\nPair balances: "
"39.728 ZEUR, 0.109598362 XETH.\nDidn't DCA already today.\nCurrent XETHZEUR ask price: "
"2083.16.\nCreate a 19.9481ZEUR buy limit order of 0.00957589XETH at 2083.16ZEUR.\nFee "
Expand All @@ -62,7 +62,7 @@ def test_handle_dca_logic_error(self, capfd):
self.dca.handle_dca_logic()
captured = capfd.readouterr()
test_output = (
"Hi, current configuration: DCA pair: XETHZEUR, DCA amount: 20.0.\nIt's 2021-04-16 18:54:53 on "
"Pair: XETHZEUR, delay: 1, amount: 20.0.\nIt's 2021-04-16 18:54:53 on "
"Kraken, 2021-04-16 18:54:53 on system.\nCurrent trade balance: 16524.7595 ZUSD.\nPair "
"balances: 359.728 ZEUR, 0.128994332 XETH.\nAlready DCA.\n"
)
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_send_buy_limit_order(self, capfd):
self.dca.send_buy_limit_order(order)
captured = capfd.readouterr()
test_output = (
"Hi, current configuration: DCA pair: XETHZEUR, DCA amount: 20.0.\nCreate a 19.9481ZEUR buy "
"Pair: XETHZEUR, delay: 1, amount: 20.0.\nCreate a 19.9481ZEUR buy "
"limit order of 0.01029256XETH at 1938.11ZEUR.\nFee expected: 0.0519ZEUR (0.26% taker "
"fee).\nTotal price expected: 0.01029256XETH for 20.0ZEUR.\nOrder successfully created.\nTXID: "
"OUHXFN-RTP6W-ART4VP\nDescription: buy 0.01029256 ETHEUR @ limit 1938.11\n"
Expand Down

0 comments on commit b8f7c77

Please sign in to comment.