-
Notifications
You must be signed in to change notification settings - Fork 1
/
InvestEX.py
55 lines (41 loc) · 1.76 KB
/
InvestEX.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
from datetime import datetime
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/get_price_data', methods=['GET'])
def get_price_data():
crypto = request.args.get('crypto')
time = request.args.get('time')
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency=usd&days={time}"
response = requests.get(url)
data = response.json()
if 'prices' in data:
prices = [p[1] for p in data['prices']]
else:
error = f"Error: could not retrieve price data for {crypto} with time {time}"
return jsonify(error=error), 404
return jsonify(prices=prices), 200
@app.route('/predict_price', methods=['GET'])
def predict_price():
crypto = request.args.get('crypto')
time = request.args.get('time')
days = int(request.args.get('days', '7'))
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency=usd&days={time}"
response = requests.get(url)
data = response.json()
if 'prices' in data:
prices = [p[1] for p in data['prices']]
else:
error = f"Error: could not retrieve price data for {crypto} with time {time}"
return jsonify(error=error), 404
if len(prices) >= days:
moving_average = sum(prices[-days:]) / days
next_price = moving_average * 2 - prices[-days]
return jsonify(predicted_price=next_price), 200
else:
error = f"Error: not enough data available to make prediction for {crypto} with time {time}"
return jsonify(error=error), 404
if __name__ == '__main__':
app.run(debug=True)