-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (54 loc) · 2.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, render_template, request
import requests
import numpy as np
app = Flask(__name__)
COINGECKO_API_URL = "https://api.coingecko.com/api/v3"
def get_top_cryptos():
url = (f"{COINGECKO_API_URL}/coins/markets?vs_currency=usd&order=market_cap_desc"
f"&per_page=10&page=1&sparkline=true&price_change_percentage=1h,24h,7d")
response = requests.get(url).json()
return response
def get_crypto_data(crypto_name):
url = f"{COINGECKO_API_URL}/coins/{crypto_name.lower()}/market_chart?vs_currency=usd&days=30"
response = requests.get(url).json()
prices = [price[1] for price in response.get("prices", [])]
return prices
def fibonacci_projection(prices):
high = max(prices)
low = min(prices)
levels = [0.236, 0.382, 0.618]
projections = {level: low + (high - low) * level for level in levels}
return projections
def fibonacci_expected_changes(prices):
projections = fibonacci_projection(prices)
current_price = prices[-1]
expected_changes = {}
for level, proj in projections.items():
percentage_change = ((proj - current_price) / current_price) * 100
expected_changes[level] = percentage_change
return expected_changes
@app.route('/')
def home():
return render_template('index.html')
@app.route('/crypto', methods=['GET', 'POST'])
def crypto():
top_cryptos = get_top_cryptos()
analysis = None
if request.method == 'POST':
crypto_name = request.form.get('crypto_name')
try:
prices = get_crypto_data(crypto_name)
expected_changes = fibonacci_expected_changes(prices)
current_price = prices[-1]
analysis_lines = [f"Current price of {crypto_name.title()}: ${current_price:.2f}"]
for level, change in expected_changes.items():
direction = "increase" if change > 0 else "decrease"
analysis_lines.append(
f"At Fibonacci level {level:.3f}, expected {direction} of {abs(change):.2f}%."
)
analysis = " ".join(analysis_lines)
except Exception as e:
analysis = f"Error fetching data. Please check the cryptocurrency name. ({e})"
return render_template('crypto.html', cryptos=top_cryptos, analysis=analysis)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)