camphong24032002
Test
6b95d78
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from logic.nadaraya import NadarayaWatson
from logic.res_sup import ResSupCluster
from data_core.utils import prepare_data
from collections import Counter
import os
import traceback
app = FastAPI()
class Response(BaseModel):
signal: dict = {}
message: str = None
error: str = None
@app.post("/nadaraya")
def nada(symbol: str = "CEO"):
res = Response()
try:
data = prepare_data(symbol=symbol, date_past=365)
Nada = NadarayaWatson(data=data)
if data.iloc[-1]["open"] <= Nada.lower_bound:
signal = "buy"
elif data.iloc[-1]["open"] >= Nada.upper_bound:
signal = "sell"
else:
signal = "hold"
# update res
res.signal["Nadaraya"] = signal
except:
res.error = traceback.format_exc()
return res
@app.post("/resup")
def ressup(symbol: str = "CEO"):
res = Response()
try:
# Extract Data
print("symbol", symbol)
data = prepare_data(symbol=symbol, date_past=365)
# today price
today_price = data.iloc[-1]["open"]
# fit cluster
ResSup = ResSupCluster(data=data,
n_clusters=3,
is_delete_outlier=True)
print(data)
# buy and sell range
current_week_cluster = Counter(ResSup.data.iloc[-7 :]["Clusters"])
current_day_cluster = current_week_cluster.most_common()[0][0]
support = ResSup.support[current_day_cluster]
resistance = ResSup.resistance[current_day_cluster]
# find buy range
buy_range = (support, support + (resistance - support) * 0.236)
sell_range = (resistance - (resistance - support) * 0.236, resistance)
if buy_range[0] <= today_price <= buy_range[1]:
signal = "buy"
elif sell_range[0] <= today_price <= sell_range[1]:
signal = "sell"
else:
signal = "hold"
res.signal["resup"] = signal
res.message = f"price: {today_price}, resitance: {resistance}, support: {support}"
except:
res.error = traceback.format_exc()
return res
# if __name__ == "__main__":
# uvicorn.run(app, host="127.0.0.1", port=8000, log_level="info")