forked from kevinlawler/High-Frequency-Trading-Model-with-IB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockData.py
100 lines (76 loc) · 3.17 KB
/
StockData.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
#######################################
# Author: James Ma
# Email stuff here: [email protected]
#######################################
from ChartDataSet import ChartDataSet
import pandas as pd
class StockData:
is_storing_long_term = False
is_storing_short_term = False
is_bootstrap_completed = False
def __init__(self, contract):
self.contract = contract
self.historical_lt_chart_data_set = ChartDataSet()
self.historical_st_chart_data_set = ChartDataSet()
self.position = 0
self.market_price = 0
self.market_value = 0
self.average_cost = 0
self.unrealized_pnl = 0
self.realized_pnl = 0
self.account_name = ""
self.is_order_filled = False
def set_bootstrap_is_completed(self):
self.is_bootstrap_completed = True
def get_is_bootstrap_completed(self):
return self.is_bootstrap_completed
def get_stock_contract(self):
return self.contract
def set_is_storing_long_term(self):
self.is_storing_long_term = True
def set_is_storing_short_term(self):
self.is_storing_short_term = True
def set_finished_storing(self):
self.is_storing_long_term = False
self.is_storing_short_term = False
def is_waiting_for_storing(self):
return self.is_storing_long_term or self.is_storing_short_term
def add_historical_data_point(self, price, date):
if self.is_storing_long_term:
self.historical_lt_chart_data_set.add_tick(price, date)
elif self.is_storing_short_term:
self.historical_st_chart_data_set.add_tick(price, date)
def add_incoming_tick(self, price, date):
self.historical_st_chart_data_set.add_tick(price, date)
def get_historical_chart_data_set(self):
return self.historical_lt_chart_data_set
def get_historical_short_term_chart_data_set(self):
return self.historical_st_chart_data_set
def get_long_term_prices(self):
return self.historical_lt_chart_data_set.get_prices()
def get_short_term_prices(self):
return self.historical_st_chart_data_set.get_prices()
def get_long_term_mean(self):
pdseries = pd.Series(self.get_long_term_prices())
return pdseries.values.mean()
def get_short_term_mean(self):
pdseries = pd.Series(self.get_short_term_prices())
return pdseries.values.mean()
def get_short_term_std(self):
return pd.Series(self.get_short_term_prices()).pct_change().std()*100
def get_long_term_std(self):
return pd.Series(self.get_long_term_prices()).pct_change().std()*100
def update_position(self
, position, market_price, market_value, average_cost, unrealized_pnl, realized_pnl, account_name):
self.position = position
self.market_price = market_price
self.market_value = market_value
self.average_cost = average_cost
self.unrealized_pnl = unrealized_pnl
self.realized_pnl = realized_pnl
self.account_name = account_name
def on_send_order(self, position):
self.position += position
self.is_order_filled = False
def get_position(self):
return self.position