| 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: |
| 62 | date = row[0] |
| 63 | time = row[1] |
| 64 | timestamp, bar = self._create_bar(cache, date, time, high_price, low_price, cum_volume, cum_dollar) |
| 65 | list_bars.append(bar) |
| 66 | datetime.append(timestamp) |
| 67 | high_price, low_price, cum_volume, cum_dollar, tick = -np.inf, np.inf, 0, 0, 0 |
| 68 | if self.method == "dollar": |
| 69 | if cum_dollar >= self.threshold: |
| 70 | date = row[0] |
| 71 | time = row[1] |
| 72 | timestamp, bar = self._create_bar(cache, date, time, high_price, low_price, cum_volume, cum_dollar) |
| 73 | list_bars.append(bar) |
| 74 | datetime.append(timestamp) |
| 75 | high_price, low_price, cum_volume, cum_dollar, tick = -np.inf, np.inf, 0, 0, 0 |
| 76 | #print(row[1]) |
| 77 | return datetime, list_bars |
| 78 | |
| 79 | |
| 80 | def _create_bar(self, price_list, date, time, high, low, cum_volume, cum_dollar): |