Created
July 28, 2022 00:44
-
-
Save rhettre/ac171045a32dc4e7c0b37d69543779e8 to your computer and use it in GitHub Desktop.
InvestAnswers DCA Steroids ATH Script
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 | |
import requests | |
GEMINI_PUBLIC_KEY = "" | |
GEMINI_PRIVATE_KEY = "" | |
symbol = "BTCUSD" | |
tick_size = 8 | |
quote_currency_price_increment = 2 | |
buy_amount = 100 | |
GLASSNODE_API_KEY = '' | |
GLASSNODE_TOKEN = "BTC" | |
# make API request | |
resp = requests.get('https://api.glassnode.com/v1/metrics/market/price_drawdown_relative', | |
params={'a': GLASSNODE_TOKEN, 'api_key': GLASSNODE_API_KEY}) | |
distance_from_ath = round(resp.json()[-1]['v']*-1,3) | |
def _setBuyFactor(dist_ath): | |
print(f"Distance from ATH: {dist_ath}") | |
if dist_ath >= .35 and dist_ath < .4: | |
buy_factor = 1.66 | |
elif dist_ath >= .4 and dist_ath < .45: | |
buy_factor = 2.64 | |
elif dist_ath >= .45 and dist_ath <.5: | |
buy_factor = 4.26 | |
elif dist_ath >= .5 and dist_ath < 1: | |
buy_factor = 2 | |
else: | |
buy_factor = 0 | |
print(f"Buy Factor: {buy_factor}") | |
return buy_factor | |
def _buyCrypto(pub_key, priv_key): | |
buy_factor = _setBuyFactor(distance_from_ath) | |
print(buy_factor) | |
if buy_factor > 0: | |
#establish connection with Gemini | |
trader = gemini.PrivateClient(pub_key, priv_key) | |
#get BTC spot price | |
symbol_spot_price = float(trader.get_ticker(symbol)['ask']) | |
#factor for getting quick fill on limit order | |
factor = 0.999 | |
#calculate execution price | |
execution_price = str(round(symbol_spot_price*factor,quote_currency_price_increment)) | |
#calculate amount using the buy factor | |
amount = round((buy_amount*buy_factor*0.998)/float(execution_price),tick_size) | |
#place limit buy order for 0.999x ask price | |
buy = trader.new_order(symbol, str(amount), execution_price, "buy", ["maker-or-cancel"]) | |
print(f'Maker Buy: {buy}') | |
else: | |
print("No buy because buy factor was 0.") | |
def lambda_handler(event, context): | |
_buyCrypto(GEMINI_PUBLIC_KEY, GEMINI_PRIVATE_KEY) | |
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