MCPcopy Create free account
hub / github.com/VivekPa/AIAlpha / BaseBars

Class BaseBars

data_processor/base_bars.py:4–83  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import numpy as np
3
4class BaseBars:
5 def __init__(self, file_path, output_path, method, threshold, batch_size=20000000):
6 self.file_path = file_path
7 self.output_path = output_path
8 self.method = method
9 self.threshold = threshold
10 self.batch_size = batch_size
11 self.cache = []
12
13 def batch_run(self, verbose=True):
14 header = True
15 if verbose:
16 print(f'Reading data in batches of {self.batch_size}')
17
18 count = 0
19 cols = ['date', 'time', 'open', 'high', 'low', 'close', 'volume']
20
21 #list_bars = []
22
23 for batch in pd.read_csv(self.file_path, chunksize=self.batch_size, index_col=0):
24 if verbose:
25 print(f'Sampling batch {count}')
26 datetime, list_bars = self._sample(batch)
27 full_bars = pd.concat([pd.DataFrame(datetime), pd.DataFrame(list_bars)], axis=1)
28 full_bars.columns = cols
29 #print(type(list_bars[2][3]))
30 #list_bars.columns = cols
31 full_bars.to_csv(self.output_path, header=header, index=False, mode='a')
32 header = False
33
34
35 def _sample(self, data):
36 high_price, low_price, cum_volume, cum_dollar, tick = -np.inf, np.inf, 0, 0, 0
37 cache = []
38 #cols = ['date', 'time', 'open', 'high', 'low', 'close', 'volume']
39 datetime = []
40 list_bars = []
41 #list_bars = pd.DataFrame(columns=cols)
42 for row in data.values:
43 if high_price < row[2]:
44 high_price = row[2]
45 if low_price > row[2]:
46 low_price = row[2]
47 tick += 1
48 cum_volume += row[3]
49 cum_dollar += row[2]*row[3]
50 cache.append(row[2])
51
52 if self.method == "tick":
53 if tick == self.threshold:
54 date = row[0]
55 time = row[1]
56 timestamp, bar = self._create_bar(cache, date, time, high_price, low_price, cum_volume, cum_dollar)
57 list_bars.append(bar)
58 datetime.append(timestamp)
59 high_price, low_price, cum_volume, cum_dollar, tick = -np.inf, np.inf, 0, 0, 0
60 if self.method == "volume":
61 if cum_volume >= self.threshold:

Callers 2

run.pyFile · 0.90
bar_sample.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected