-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
115 lines (84 loc) · 3.57 KB
/
main.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import talib
import requests
import numpy as np
from fastapi import FastAPI,HTTPException
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="CDLRecognizeAPI",license_info={'name':'find me on linkedin','url':"https://www.linkedin.com/in/hasindu-sithmin-9a1a12209/"})
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount('/static',StaticFiles(directory='static'),name='static')
@app.get("/")
def root():
return FileResponse('static/index.html')
@app.get("/test-report")
def root():
return FileResponse('static/report.html')
def gen_candle(dt):
return {
'time':int(dt[0]/1000)+19800,
'open':float(dt[1]),
'high':float(dt[2]),
'low':float(dt[3]),
'close':float(dt[4])
}
get_ohlc_desc = '''
## use this endpoint to get candlestick data.
[available symbols](/static/symbols.json)
[available interval](/static/intervals.json)
'''
@app.get("/ohlc/{symbol}/{interval}",description=get_ohlc_desc)
def get_ohlc(symbol:str,interval:str):
symbol,interval = symbol.strip().upper(),interval.strip()
with open('static/symbols.json','r') as symbols:
if symbol not in json.load(symbols):
raise HTTPException(status_code=400,detail=f'Invalid symbol :{symbol.lower()}')
with open('static/intervals.json','r') as intervals:
if interval not in json.load(intervals).values():
raise HTTPException(status_code=400,detail=f'Invalid interval :{interval}')
url = f'https://www.binance.com/api/v3/klines?symbol={symbol}&interval={interval}'
res = requests.get(url)
data = res.json()
return [gen_candle(dt) for dt in data]
get_patterns_desc = '''
[available symbols](/static/symbols.json)
[available interval](/static/intervals.json)
[available patterns](/static/pattern.json)
'''
@app.get("/pattern/{symbol}/{interval}/{pattern}",description=get_patterns_desc)
def get_patterns(symbol:str,interval:str,pattern:str):
symbol,interval,pattern = symbol.strip().upper(),interval.strip(),pattern.strip().upper()
with open('static/symbols.json','r') as symbols:
if symbol not in json.load(symbols):
raise HTTPException(status_code=400,detail=f'Invalid symbol :{symbol.lower()}')
with open('static/intervals.json','r') as intervals:
if interval not in json.load(intervals).values():
raise HTTPException(status_code=400,detail=f'Invalid interval :{interval}')
with open('static/pattern.json','r') as patterns:
if pattern not in json.load(patterns).values():
raise HTTPException(status_code=400,detail=f'Invalid pattern :{pattern.lower()}')
url = f'https://www.binance.com/api/v3/klines?symbol={symbol}&interval={interval}'
res = requests.get(url)
data = res.json()
data = [gen_candle(dt) for dt in data]
time,open_,high,low,close = [],[],[],[],[]
for dt in data:
time.append(dt['time'])
open_.append(dt['open'])
high.append(dt['high'])
low.append(dt['low'])
close.append(dt['close'])
result = eval(f'talib.{pattern}(open=np.array(open_),high=np.array(high),low=np.array(low),close=np.array(close))')
bullish = [time[li[0]] for li in np.argwhere(result == 100).tolist()]
bearish = [time[li[0]] for li in np.argwhere(result == -100).tolist()]
return {
'bullish':bullish,
'bearish':bearish
}