-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.py
100 lines (88 loc) · 3.14 KB
/
Tools.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def changeFreq(data: pd.DataFrame, freq: str):
"""
This func is used to change the data freq to get a DataFrameGroupBy object.
"""
data.loc[:, "Time"] = data.loc[:, "date"] + data.loc[:, "Time"].apply(str)
data.loc[:, "Time"] = pd.to_datetime(data.loc[:, 'Time'], format='%Y-%m-%d%H%M%S%f')
return data.groupby(pd.Grouper(key="Time", freq=freq))
def lastPrice(data: pd.DataFrame) -> int:
"""
This func is used to get the newest price in the bar.
"""
if len(data.loc[:, "LastPrice"]) == 0:
return np.nan
return data.loc[:, "LastPrice"].iloc[-1]
def highPrice(data: pd.DataFrame) -> int:
"""
This func is used to get the highest price in the bar.
"""
if len(data.loc[:, "LastPrice"]) == 0:
return np.nan
return data.loc[:, "LastPrice"].max()
def lowPrice(data: pd.DataFrame) -> int:
"""
This func is used to get the lowest price in the bar.
"""
if len(data.loc[:, "LastPrice"]) == 0:
return np.nan
return data.loc[:, "LastPrice"].min()
def turnover(data: pd.DataFrame) -> int:
"""
This func is used to get the turnover in the bar.
"""
if len(data.loc[:, "Turnover"]) == 0:
return np.nan
return data.loc[:, "Turnover"].iloc[-1]
def volume(data: pd.DataFrame) -> int:
"""
This func is used to get the volume in the bar.
"""
if len(data.loc[:, "Volume"]) == 0:
return np.nan
return data.loc[:, "Volume"].iloc[-1]
def openInterest(data: pd.DataFrame) -> int:
"""
This func is used to get the change of openinterest in the bar.
"""
if len(data.loc[:, "OpenInterest"]) == 0:
return np.nan
return data.loc[:, "OpenInterest"].iloc[-1]
def maxBidAskRate(data: pd.DataFrame) -> int:
"""
This func is used to get the max rate of bid ask volume weighted average price in the bar.
the rate of bid ask weighted average price = bid1*volume/ask1*volume
"""
if len(data) == 0:
return np.nan
return ((data.loc[:, "AskPrice1"] * data.loc[:, "AskVol1"]) / (data.loc[:, "BidPrice1"] * data.loc[:, "BidVol1"])).max()
def extractReturn(price: pd.Series) -> pd.Series:
"""
This func is used to get the return in two bars.
"""
returnData = price.diff()/price.shift(1)
returnData.dropna(inplace = True)
return returnData
def extractVolume(volume: pd.Series) -> pd.Series:
"""
This func is used to get the volume in two bars.
"""
returnData = volume.diff()
returnData.dropna(inplace = True)
return returnData
def extractTurnover(turnover: pd.Series) -> pd.Series:
"""
This func is used to get the turnover in two bars.
"""
returnData = turnover.diff()
returnData.dropna(inplace = True)
return returnData
def extractDeltaOpenInterest(openInterest: pd.Series) -> pd.Series:
"""
This func is used to get the change of openinterest in two bars.
"""
returnData = openInterest.diff()/openInterest
returnData.dropna(inplace = True)
return returnData