Skip to content

Instantly share code, notes, and snippets.

@rhettre
Created July 28, 2022 00:40
Show Gist options
  • Save rhettre/3e997bce08d27ba40b8a08073042d51c to your computer and use it in GitHub Desktop.
Save rhettre/3e997bce08d27ba40b8a08073042d51c to your computer and use it in GitHub Desktop.
Buy Bitcoin Fear and Sell Bitcoin Greed Index
import json
import gemini
import requests
public_key=""
private_key=""
#Fear and Greed indicator: Set to True if you want to include Fear and Greed data
INCLUDE_FEAR_AND_GREED = True
#If the Fear and Greed indicator is below FEAR_FLOOR, multiply amount purchased by FEAR_MULTIPLIER
FEAR_FLOOR = 20
FEAR_MULTIPLIER = 1.5
#If the Fear and Greed indicator is above GREED_CEILING, mulitply amount purchased by GREED_MULITPLIER
GREED_CEILING = 80
GREED_MULTIPLIER = 0.5
def get_fear_and_greed_index():
response = requests.get("https://api.alternative.me/fng/")
print("got fear and greed value")
json_data = json.loads(response.text)
fear_and_greed_value = int(json_data['data'][0]['value'])
fear_and_greed_classification = json_data['data'][0]['value_classification']
return dict(value=fear_and_greed_value, classification=fear_and_greed_classification)
def _buyBitcoin(buy_size,pub_key, priv_key, fear_and_greed):
# Set up a buy for the current price
trader = gemini.PrivateClient(pub_key, priv_key)
factor = 0.999
price = str(round(float(trader.get_ticker("BTCUSD")['ask'])*factor,2))
if(fear_and_greed):
print(f"fear and greed value: {get_fear_and_greed_index()['value']}")
if(get_fear_and_greed_index()['value'] < FEAR_FLOOR):
buy_size *= FEAR_MULTIPLIER
if(get_fear_and_greed_index()['value'] > GREED_CEILING):
buy_size *= GREED_MULTIPLIER
#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*.998)/float(price),8)
#execute maker buy, round to 8 decimal places for precision, multiply price by 2 so your limit order always gets fully filled
buy = trader.new_order("BTCUSD", str(amount), price, "buy", ["maker-or-cancel"])
print(f'Maker Buy: {buy}')
def lambda_handler(event, context):
buy_size = 20
_buyBitcoin(buy_size, public_key, private_key, INCLUDE_FEAR_AND_GREED)
return {
'statusCode': 200,
'body': json.dumps(f'Buy order placed for {buy_size} dollars of BTC ')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment