66import env
77
88
9- def get_balance (kraken_ticker ):
9+ def get_balance (asset_id ):
1010 balances = util .kraken_request ('/0/private/Balance' )
1111 balance = 0
12- if f' { kraken_ticker } ' in balances :
13- balance = float (balances [f' { kraken_ticker } ' ])
12+ if asset_id in balances :
13+ balance = float (balances [asset_id ])
1414 return balance
1515
1616
17- def get_pair_details (quote_curency , base_currency , base_kraken_ticker ):
18- response = util .kraken_request ('/0/public/AssetPairs' , {'pair' : f'{ quote_curency } { base_currency } ' })[f'{ quote_curency } { base_kraken_ticker } ' ]
19- order_min = float (response ['ordermin' ])
20- trade_decimals = int (response ['pair_decimals' ])
17+ def get_pair_details (quote_curency , base_currency ):
18+ pair_details_result = util .kraken_request ('/0/public/AssetPairs' , {'pair' : f'{ quote_curency } { base_currency } ' })
19+ pair_id = next (iter (pair_details_result ))
20+ pair_details = pair_details_result [pair_id ]
21+ order_min = float (pair_details ['ordermin' ])
22+ trade_decimals = int (pair_details ['pair_decimals' ])
2123 return order_min , trade_decimals
2224
2325
24- def prepare_and_make_trade (balance , quote_currency , base_currency , base_kraken_ticker , payload ):
26+ def prepare_and_make_trade (balance , quote_currency , base_currency , payload ):
2527 # Get trading pair details for a direct conversion
26- order_min , trade_decimals = get_pair_details (quote_currency , base_currency , base_kraken_ticker )
28+ order_min , trade_decimals = get_pair_details (quote_currency , base_currency )
2729
2830 # If we continue, then we got the order_min and trade_decimals for the pair without issues
2931 # We ensure we have more than the minimum to trade
3032 if balance > order_min :
3133 # We get the current orderbook to calculate the mid market price
32- orderbook = util .kraken_request ('/0/public/Depth?count=1' , {'pair' : f'{ quote_currency } { base_currency } ' })[f'{ quote_currency } { base_kraken_ticker } ' ]
34+ orderbook_result = util .kraken_request ('/0/public/Depth?count=1' , {'pair' : f'{ quote_currency } { base_currency } ' })
35+ pair_id = next (iter (orderbook_result ))
36+ orderbook = orderbook_result [pair_id ]
3337
3438 # We make the trade
3539 mid_market_price = float (orderbook ['bids' ][0 ][0 ]) + ((float (orderbook ['asks' ][0 ][0 ]) - float (orderbook ['bids' ][0 ][0 ])) / 2 ) # Example 212.55+((212.72-212.55)/2) = 212.635
@@ -42,7 +46,7 @@ def prepare_and_make_trade(balance, quote_currency, base_currency, base_kraken_t
4246 print (f'{ quote_currency } balance { balance } is below the minimum of { order_min } ; skipping' )
4347
4448
45- def attempt_sell (asset , settlement_currency , settlement_kraken_ticker ):
49+ def attempt_sell (asset , settlement_currency ):
4650 payload = {
4751 'ordertype' : 'limit' ,
4852 'timeinforce' : 'IOC' # Immediately fill order to extent possible, then cancel
@@ -54,32 +58,32 @@ def attempt_sell(asset, settlement_currency, settlement_kraken_ticker):
5458 payload ['type' ] = 'sell'
5559 payload ['pair' ] = f'{ asset } { settlement_currency } '
5660 payload ['volume' ] = balance
57- prepare_and_make_trade (balance , asset , settlement_currency , settlement_kraken_ticker , payload )
61+ prepare_and_make_trade (balance , asset , settlement_currency , payload )
5862 except Exception as e :
5963 print ('prepare_and_make_trade error: ' , e )
6064 # If we are here, then there is an issue getting the pair details and executing the trade
6165 # Either there is a Kraken error, or this trading pair does not exist
6266 print (f'Attempting to sell indirectly via other assets' )
63- for intermediary_currency , intermediary_kraken_ticker in [( 'USD' , 'ZUSD' ), ( ' USDT' , 'USDT' ), ( ' USDC' , 'USDC' ), ( ' EUR' , 'ZEUR' ), ( ' XBT', 'XXBT' ) ]:
67+ for intermediary_currency in ['USD' , 'USDT' , 'USDC' , 'EUR' , 'XBT' ]:
6468 # NOTE: it's possible for the conversion to be "stuck" in the intermediary currency if there is not enough to convert out of
6569 # Remove any intermediary currencies that you don't want value to get potentially stuck in
6670 # Conversion fees apply: https://www.kraken.com/features/fee-schedule#spot-crypto
6771 # Fees are lower for trades between stablecoins/FX, such as USDT/EUR
6872 try :
69- get_pair_details (asset , intermediary_currency , intermediary_kraken_ticker ) # Using asset as quote currency and intermediary as base currency
70- get_pair_details (settlement_currency , intermediary_currency , intermediary_kraken_ticker ) # Using settlement currency as quote currency and intermediary as base currency
73+ get_pair_details (asset , intermediary_currency ) # Using asset as quote currency and intermediary as base currency
74+ get_pair_details (settlement_currency , intermediary_currency ) # Using settlement currency as quote currency and intermediary as base currency
7175 # If we made it here, then a route through the intermediary currency exists (we would have gotten an error with get_pair_details if it didn't)
7276 # Start the first trade, selling the asset for the base intermediary currency
7377 payload ['type' ] = 'sell'
7478 payload ['pair' ] = f'{ asset } { intermediary_currency } '
7579 payload ['volume' ] = balance
76- prepare_and_make_trade (balance , asset , intermediary_currency , intermediary_kraken_ticker , payload )
80+ prepare_and_make_trade (balance , asset , intermediary_currency , payload )
7781 intermediary_balance = get_balance (intermediary_currency )
7882 # Start the second trade, buying the quote settlement currency with the base intermediary currency balance
7983 payload ['type' ] = 'buy'
8084 payload ['pair' ] = f'{ settlement_currency } { intermediary_currency } '
8185 payload ['volume' ] = intermediary_balance
82- prepare_and_make_trade (balance , intermediary_kraken_ticker )
86+ prepare_and_make_trade (balance , settlement_currency , intermediary_currency , payload )
8387 except :
8488 print (f'No path found through { intermediary_currency } ' )
8589 continue
@@ -94,19 +98,12 @@ def attempt_sell(asset, settlement_currency, settlement_kraken_ticker):
9498 while 1 :
9599 # First, get the settlement currency formatted correctly
96100 settlement_currency = env .SETTLEMENT_CURRENCY
97- # https://support.kraken.com/hc/en-us/articles/360001206766-Bitcoin-currency-code-XBT-vs-BTC
98- if settlement_currency in ['AUD' , 'CAD' , 'EUR' , 'GBP' , 'JPY' , 'USD' ]:
99- settlement_kraken_ticker = 'Z' + settlement_currency
100- elif settlement_currency in ['ETC' , 'ETH' , 'LTC' , 'MLN' , 'REP' , 'XBT' , 'XDG' , 'XLM' , 'XMR' , 'XRP' , 'ZEC' ]:
101- settlement_kraken_ticker = 'X' + settlement_currency
102- else :
103- settlement_kraken_ticker = settlement_currency
104101
105102 # Then, initiate the selling process for each supported asset unless it's already the settlement currency
106103 for asset in ['XBT' , 'LTC' , 'XMR' ]:
107104 try :
108105 if settlement_currency != asset :
109- attempt_sell (asset , settlement_currency , settlement_kraken_ticker )
106+ attempt_sell (asset , settlement_currency )
110107 else :
111108 print (f'Not converting { asset } since it is already in the settlement currency' )
112109 except Exception as e :
0 commit comments