File size: 2,346 Bytes
6b95d78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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")