-
Notifications
You must be signed in to change notification settings - Fork 3
/
timeseries.py
48 lines (34 loc) · 1.47 KB
/
timeseries.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
import numpy as np
import pandas as pd
class TimeSeries():
def __init__(self, status):
self.resample_interval = '5s'
self.prices = pd.DataFrame()
self.buys = pd.DataFrame()
self.sells = pd.DataFrame()
self.close_wins = pd.DataFrame()
self.close_loses = pd.DataFrame()
self.status = status
self.resampled_prices = None
def add_tick_event(self, event):
self.prices.loc[event.time, event.instrument] = event.bid
self.resampled_prices = self.prices.resample(
self.resample_interval,
how='last',
fill_method="ffill")
if len(self.resampled_prices) > 1000:
self.resampled_prices.drop(self.resampled_prices.index[[0]])
if len(self.prices) > 1000:
self.prices.drop(self.prices.index[[0]])
def add_buy_event(self, event):
self.buys.loc[event.time, event.instrument] = event.bid
def add_sell_event(self, event):
self.sells.loc[event.time, event.instrument] = event.bid
def add_close_win_event(self, event):
self.close_wins.loc[event.time, event.instrument] = event.bid
def add_close_lose_event(self, event):
self.close_loses.loc[event.time, event.instrument] = event.bid
def get_latest_ts_as_df(self, period):
return self.resampled_prices.tail(period)
def get_latest_ts_as_array(self, period, event):
return np.asarray(self.get_latest_ts_as_df(period)[event.instrument])