Below is an example script in Python that uses the CoinGecko API to fetch the current price of Bitcoin and a script that implements Dollar-Cost Averaging (DCA) method to buy Bitcoin at regular intervals.
User
write with code script
Assistant
Certainly! Here's the complete code script that includes both the "Get Price of Bitcoin" and "Dollar-Cost Averaging" functionality:
import requests
import datetime
def get_bitcoin_price():
try:
response = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
data = response.json()
bitcoin_price = data['bitcoin']['usd']
return bitcoin_price
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
def dca_script(initial_investment, investment_interval, num_of_investments):
bitcoin_price = get_bitcoin_price()
if bitcoin_price is not None:
total_invested = initial_investment
for i in range(num_of_investments):
invested_amount = initial_investment + (i * investment_interval)
total_invested += invested_amount
bitcoin_purchased = invested_amount / bitcoin_price
print(f"On {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, invest ${invested_amount:.2f} to buy {bitcoin_purchased:.8f} BTC.")
# Implement code to make purchase and track investments in your preferred exchange/platform
bitcoin_price = get_bitcoin_price()
if bitcoin_price is not None:
print(f"Current price of Bitcoin: ${bitcoin_price}")
dca_script(initial_investment=100, investment_interval=50, num_of_investments=5)
Get Price of Bitcoin:
import requests
def get_bitcoin_price():
try:
response = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
data = response.json()
bitcoin_price = data['bitcoin']['usd']
return bitcoin_price
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
bitcoin_price = get_bitcoin_price()
if bitcoin_price is not None:
print(f"Current price of Bitcoin: ${bitcoin_price}")
This script uses the requests library to send an HTTP GET request to the CoinGecko API and fetches the current price of Bitcoin in USD.
Dollar-Cost Averaging Script:
import datetime
def dca_script(initial_investment, investment_interval, num_of_investments):
bitcoin_price = get_bitcoin_price()
if bitcoin_price is not None:
total_invested = initial_investment
for i in range(num_of_investments):
invested_amount = initial_investment + (i * investment_interval)
total_invested += invested_amount
bitcoin_purchased = invested_amount / bitcoin_price
print(f"On {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, invest ${invested_amount:.2f} to buy {bitcoin_purchased:.8f} BTC.")
# Implement code to make purchase and track investments in your preferred exchange/platform
dca_script(initial_investment=100, investment_interval=50, num_of_investments=5)
This script uses the dca_script function that takes the initial investment amount, investment interval, and the number of investments as inputs. It calls the get_bitcoin_price function to get the current price of Bitcoin. Then, it iterates over the number of investments specified and calculates the invested amount, Bitcoin purchased, and prints the details of each investment.
Note: The script above (as well as any financial investment) should be used with caution and it's advisable to do thorough research and consult with financial advisors before making any investment decisions.
This script begins by defining the get_bitcoin_price function, which uses the CoinGecko API to fetch the current price of Bitcoin in USD.
Then, the dca_script function is defined, which takes inputs such as initial investment amount, investment interval, and the number of investments. Inside this function, it fetches the current Bitcoin price, calculates the invested amount, Bitcoin purchased, and prints the details of each investment using the datetime module.
Finally, the main part of the script calls the get_bitcoin_price function to get the current Bitcoin price and prints it. It then calls the dca_script function with the desired investment parameters to demonstrate the Dollar-Cost Averaging functionality.
Remember to install the requests library (pip install requests) before running the script.