camphong24032002 commited on
Commit
78c8ffa
·
1 Parent(s): 51a04f7

test: asyncio

Browse files
Files changed (3) hide show
  1. routes/data.py +1 -1
  2. services/indicator.py +7 -7
  3. utils/config.py +17 -1
routes/data.py CHANGED
@@ -55,7 +55,7 @@ async def update_entire_rsi():
55
  )
56
  async def update_entire_macd():
57
  asyncio.create_task(Indicator.update_entire_macd())
58
- return None
59
 
60
 
61
  @router.post(
 
55
  )
56
  async def update_entire_macd():
57
  asyncio.create_task(Indicator.update_entire_macd())
58
+ return {"message": "Updated"}
59
 
60
 
61
  @router.post(
services/indicator.py CHANGED
@@ -4,11 +4,11 @@ import pandas as pd
4
  import numpy as np
5
  from vnstock import longterm_ohlc_data
6
  from pymongo import MongoClient, ASCENDING, DESCENDING
7
- from utils.config import DATE_FORMAT, VN100_URL
8
  from .utils import Utility
9
  import os
10
- # from dotenv import load_dotenv
11
- # load_dotenv()
12
 
13
  PREFIX = "https://www.hsx.vn/Modules/Chart/StaticChart/"
14
  TIME = {"1m": 2, "3m": 3, "6m": 4, "1y": 5, "2y": 6, "5y": 7}
@@ -41,7 +41,7 @@ class Indicator:
41
  newest_record = collection.find_one(sort=[("_id", ASCENDING)])
42
  lst_symbols = list(newest_record.keys())[2:]
43
  elif source == "hsx":
44
- data = requests.get(VN100_URL).json()
45
  data = data["rows"]
46
  for value in data:
47
  lst_symbols.append(value["cell"][2].strip())
@@ -292,7 +292,7 @@ class Indicator:
292
  cnt += 1
293
  url = PREFIX + INDICATORS["RSI"] \
294
  + f"?stockSymbol={symbol}&rangeSelector=3&periods=14"
295
- data = requests.get(url).json()
296
  rsi_df = pd.DataFrame(data["SeriesColection"][0]["Points"])
297
  if get_time:
298
  lst_values["time"] = \
@@ -362,7 +362,7 @@ class Indicator:
362
  return stoch_rsi_k.rolling(window=d).mean()
363
 
364
  @staticmethod
365
- def update_entire_macd() -> None:
366
  global updating_macd
367
  if updating_macd:
368
  return
@@ -386,7 +386,7 @@ class Indicator:
386
  cnt += 1
387
  url = PREFIX + INDICATORS["MACD"] \
388
  + f"?stockSymbol={symbol}&rangeSelector=4&fastPeriod=12&slowPeriod=26&signalPeriod=9"
389
- data = requests.get(url).json()
390
  rsi_df = pd.DataFrame(
391
  data["SeriesColection"][0]["Points"]).tail(20)
392
  if get_time:
 
4
  import numpy as np
5
  from vnstock import longterm_ohlc_data
6
  from pymongo import MongoClient, ASCENDING, DESCENDING
7
+ from utils.config import DATE_FORMAT, VN100_URL, HSX_HEADER
8
  from .utils import Utility
9
  import os
10
+ from dotenv import load_dotenv
11
+ load_dotenv()
12
 
13
  PREFIX = "https://www.hsx.vn/Modules/Chart/StaticChart/"
14
  TIME = {"1m": 2, "3m": 3, "6m": 4, "1y": 5, "2y": 6, "5y": 7}
 
41
  newest_record = collection.find_one(sort=[("_id", ASCENDING)])
42
  lst_symbols = list(newest_record.keys())[2:]
43
  elif source == "hsx":
44
+ data = requests.get(VN100_URL, headers=HSX_HEADER).json()
45
  data = data["rows"]
46
  for value in data:
47
  lst_symbols.append(value["cell"][2].strip())
 
292
  cnt += 1
293
  url = PREFIX + INDICATORS["RSI"] \
294
  + f"?stockSymbol={symbol}&rangeSelector=3&periods=14"
295
+ data = requests.get(url, headers=HSX_HEADER).json()
296
  rsi_df = pd.DataFrame(data["SeriesColection"][0]["Points"])
297
  if get_time:
298
  lst_values["time"] = \
 
362
  return stoch_rsi_k.rolling(window=d).mean()
363
 
364
  @staticmethod
365
+ async def update_entire_macd() -> None:
366
  global updating_macd
367
  if updating_macd:
368
  return
 
386
  cnt += 1
387
  url = PREFIX + INDICATORS["MACD"] \
388
  + f"?stockSymbol={symbol}&rangeSelector=4&fastPeriod=12&slowPeriod=26&signalPeriod=9"
389
+ data = requests.get(url, headers=HSX_HEADER).json()
390
  rsi_df = pd.DataFrame(
391
  data["SeriesColection"][0]["Points"]).tail(20)
392
  if get_time:
utils/config.py CHANGED
@@ -1,5 +1,21 @@
1
  DATE_FORMAT = "%Y-%m-%d"
2
- tcbs_headers = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
4
  'DNT': '1',
5
  'Accept-language': 'vi',
 
1
  DATE_FORMAT = "%Y-%m-%d"
2
+ HSX_HEADER = {
3
+ "Accept": "*/*",
4
+ "Accept-Encoding": "gzip, deflate, br",
5
+ "Accept-Language": "en-US,en;q=0.9",
6
+ "Connection": "keep-alive",
7
+ "Host": "www.hsx.vn",
8
+ "Referer": "https://www.hsx.vn/modules/Chart/StaticChart/technicalchart?fid=138445b111554631a5e8a322f10337ee",
9
+ "Sec-Ch-Ua": "\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"",
10
+ "Sec-Ch-Ua-Mobile": "?0",
11
+ "Sec-Ch-Ua-Platform": "\"Windows\"",
12
+ "Sec-Fetch-Dest": "empty",
13
+ "Sec-Fetch-Mode": "cors",
14
+ "Sec-Fetch-Site": "same-origin",
15
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
16
+ "X-Requested-With": "XMLHttpRequest",
17
+ }
18
+ TCBS_HEADER = {
19
  'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
20
  'DNT': '1',
21
  'Accept-language': 'vi',