Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinkoff-invest-volume-analysis-robot-master/utils/parse_util.py
5929 views
1
from datetime import datetime
2
from typing import Dict
3
4
import pandas as pd
5
6
from utils.format_util import quotation_to_float
7
8
9
def processed_data(trade):
10
try:
11
if trade is None:
12
return
13
14
price = quotation_to_float(trade.price)
15
data = pd.DataFrame.from_records([
16
{
17
"figi": trade.figi,
18
"direction": trade.direction,
19
"price": price,
20
"quantity": trade.quantity,
21
"time": pd.to_datetime(str(trade.time), utc=True)
22
}
23
])
24
25
return data
26
except Exception as ex:
27
return ex
28
29
30
def parse_date(str_date: str):
31
try:
32
return datetime.strptime(str_date, "%Y-%m-%d %H:%M:%S.%f%z")
33
except ValueError:
34
pass
35
36
try:
37
return datetime.strptime(str_date, "%Y-%m-%d %H:%M:%S%z")
38
except ValueError:
39
pass
40
41
return None
42
43
44
def get_float_from_dict(dictionary: Dict, key: str) -> float:
45
if key in dictionary:
46
return float(dictionary.get(key))
47
return 0
48
49
50
def get_int_value(dictionary: Dict, key: str) -> int:
51
if key in dictionary:
52
return int(dictionary.get(key))
53
return 0
54
55
56
def get_datetime_value(dictionary: Dict, key: str):
57
if key in dictionary:
58
return pd.to_datetime(dictionary.get(key))
59
return None
60
61