Created
July 28, 2022 00:32
-
-
Save rhettre/479a5d939d7136d959dde34a25ce594f to your computer and use it in GitHub Desktop.
Automatically Buy Dips with Low Limit Orders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import gemini | |
public_key = "" | |
private_key = "" | |
symbol = "BTCUSD" | |
tick_size = 8 | |
quote_currency_price_increment = 2 | |
#update symbol based on what crypto/fiat pair you want to buy. Default is BTCUSD, change to BTCEUR for Euros or ETHUSD for Ethereum (for example) - see all possibilities down in symbols and minimums link | |
#update tick_size and quote_currency_price_increment based on what crypto-pair you are buying. BTC is 8 - in the doc it says 1e-8 you want the number after e-. Or in the case of .01 you want 2 (because .01 is 1e-2) | |
#Check out the API link below to see what you need for your pair | |
#https://docs.gemini.com/rest-api/#symbols-and-minimums | |
def _buyBitcoin(pub_key, priv_key,buy_size,factor=0.999): | |
# Set up a buy for 0.999 times the current price add more decimals for a higher price and faster fill, if the price is too close to spot your order won't post. | |
# Lower factor makes the order cheaper but fills quickly (0.5 would set an order for half the price and so your order could take months/years/never to fill) | |
trader = gemini.PrivateClient(pub_key, priv_key) | |
symbol_spot_price = float(trader.get_ticker(symbol)['ask']) | |
print(symbol_spot_price) | |
#to set a limit order at a fixed price (ie. $55,525) set execution_price = "55525.00" or execution_price = str(55525.00) | |
execution_price = str(round(symbol_spot_price*factor,quote_currency_price_increment)) | |
#set amount to the most precise rounding (tick_size) and multiply by 0.998 for fee inclusion - if you make an order for $20.00 there should be $19.96 coin bought and $0.04 (0.20% fee) | |
amount = round((buy_size*0.998)/float(execution_price),tick_size) | |
#execute maker buy with the appropriate symbol, amount, and calculated price | |
buy = trader.new_order(symbol, str(amount), execution_price, "buy", ["maker-or-cancel"]) | |
print(f'Maker Buy: {buy}') | |
def lambda_handler(event, context): | |
#with no fourth parameter, the factor will default to 0.999 | |
_buyBitcoin(public_key, private_key,20) | |
#with a fourth parameter the factor will be set to whatever you specify in this case 0.95 (or 5% lower than spot price) | |
#everytime you call _buyBitcoin a new order will be posted | |
_buyBitcoin(public_key,private_key,20,0.95) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('End of script') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buy-bitcoin lambda_function.py, buys $20 of Bitcoin on execution once you provide API keys from Gemini and sets lower limit orders when you lower the factor parameter