Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kraken.spot.Trade.create_order: Ability to use floats as trade amounts or prices #94

Closed
andyDoucette opened this issue May 18, 2023 · 3 comments · Fixed by #95
Closed
Assignees
Labels
Could Something that could be cool bit is not really important enhancement New feature or request Spot Topic related to Spot trading
Milestone

Comments

@andyDoucette
Copy link

The Kraken create_order API only allows string amounts and volumes, since it strictly enforces how many decimal points can be used in a way that is different per trade pair.

Python-Kraken-SDK's create_order on the other hand, says it allows float arguments, which is really nice as it seems to abstract away this pesky detail. However, if you try to use it with arbitrary float arguments, for example float(1/9), the Kraken API gives an error that there is an invalid number of decimal places provided.

So, we "kinda" support float arguments, but not really because they have to be a specific subset of float arguments that have the precice number of decimal places provided.

I'm not entirely sure what the "best" behavior would be. On one hand, I would like to be able to pass .5000000001 to create_order, and it knows to truncate it to .500000000. On the other hand, it might be considered "wrong" to place any order that isn't precicely what was asked for.

Maybe there can be a 'truncate' flag to create_order that truncates the amounts appropriately? Maybe by default it's not enabled, just so we don't confuse anyone, but a simple truncate=True would truncate the amounts to the correct number of decimal places.

I'm using this code currently as a work-around. It could be adapted pretty easily to create this Truncate feature, but before I offer a pull request, I want to make sure we agree this is an improvement.

truncate.py

from math import floor

def truncate(amount, amount_type, pair):
    '''
    Kraken only allows volume and price amounts to be specified with a specific number of decimal places, and these
    varry depending on the currency pair used.
    
    This function converts an amount of a specific type and pair to a string that uses the correct number of decimal
    places.
    
    Inputs:
        amount          - The floating point amount we want to represent.
        amount_type     - What the amount represents.  Either ('price'|'volume')
        pair            - The currency pair the amount is in reference to.
    
    Outputs:
        amount_str      - A string representation of the amount.
    '''
    
    tradeable_asset_pairs=tradeable_asset_pairs_get()['result']
    
    #Extract the data we need for this from the tradeable_asset_pairs:
    pair_decimals=tradeable_asset_pairs[pair]['pair_decimals']
    lot_decimals=tradeable_asset_pairs[pair]['lot_decimals']
    
    if amount_type=='price':
        decimals=pair_decimals
    elif amount_type=='volume':
        decimals=lot_decimals
    else:
        assert 0, f'Invalid {amount_type=}.'
    
    amount_rounded=floor(amount*10**decimals)/10**decimals
    
    #Convert with the right number of decimal places:
    amount_str='{:.{}f}'.format(amount_rounded, decimals)
    
    return amount_str

tradeable_asset_pairs_get.py ( this is referenced by another issue too)

from .timed_lru_cache import timed_lru_cache
import requests

@timed_lru_cache(seconds=120, maxsize=100)
def tradeable_asset_pairs_get():
    'Gets information about all of the currency conversions we can do.'
    
    response=requests.get('https://api.kraken.com/0/public/AssetPairs')
    assert response.status_code ==200, 'Error getting tradeable asset pairs.'
    
    return response.json()
@andyDoucette
Copy link
Author

Plus, knowing how I work, I will probably get distracted by other priorities before I make a pull request, so at least you have the code don't end up doing that.

@btschwertfeger
Copy link
Owner

That sounds reasonable. I also had a function like this for my strategies but never thought it would be good within this module. I will adapt your example and test it out. - Thanks!

@btschwertfeger btschwertfeger added enhancement New feature or request Could Something that could be cool bit is not really important Spot Topic related to Spot trading labels May 18, 2023
@btschwertfeger btschwertfeger self-assigned this May 18, 2023
@btschwertfeger btschwertfeger added this to the v1.3.0 milestone May 18, 2023
@andyDoucette
Copy link
Author

andyDoucette commented May 18, 2023 via email

@btschwertfeger btschwertfeger changed the title Trade().create_order: Ability to use floats as trade amounts or prices kraken.spot.Trade.create_order: Ability to use floats as trade amounts or prices May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Could Something that could be cool bit is not really important enhancement New feature or request Spot Topic related to Spot trading
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants