-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathbt_datafeed_postgres.py
87 lines (73 loc) · 3.39 KB
/
bt_datafeed_postgres.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
# from __future__ import (absolute_import, division, print_function,
# unicode_literals)
import datetime
from backtrader.feed import DataBase
from backtrader import date2num
from sqlalchemy import create_engine
class PostgreSQL_Daily(DataBase):
params = (
('dbHost', None),
('dbUser', None),
('dbPWD', None),
('dbName', None),
('ticker', 'EUR_USD'),
('fromdate', datetime.datetime.min),
('todate', datetime.datetime.max),
('name', ''),
)
def __init__(self):
self.engine = create_engine('postgresql+psycopg2://'+self.p.dbUser+':'+ self.p.dbPWD +'@'+ self.p.dbHost +'/'+ self.p.dbName)
# self.engine = psycopg2.connect(host=self.p.dbHost, database=self.p.dbName, user=self.p.dbUser, password=self.p.dbPWD)
def start(self):
self.conn = self.engine.connect()
sql = "select a.date_price date, a.close_price as close, a.high_price high, a.open_price open, a.low_price low from daily_data a inner join symbol b on a.stock_id = b.id where b.ticker='"+ self.p.ticker + "' and a.date_price between '"+self.p.fromdate.strftime("%Y-%m-%d")+"' and '"+self.p.todate.strftime("%Y-%m-%d")+"' order by date ASC"
self.result = self.conn.execute(sql)
def stop(self):
#self.conn.close()
self.engine.dispose()
def _load(self):
one_row = self.result.fetchone()
if one_row is None:
return False
self.lines.datetime[0] = date2num(one_row[0])
self.lines.open[0] = float(one_row[1])
self.lines.high[0] = float(one_row[2])
self.lines.low[0] = float(one_row[3])
self.lines.close[0] = float(one_row[4])
# self.lines.volume[0] = int(one_row[5])
self.lines.openinterest[0] = -1
return True
class PostgreSQL_Minute(DataBase):
params = (
('dbHost', None),
('dbUser', None),
('dbPWD', None),
('dbName', None),
('ticker', 'EUR_USD'),
('fromdate', datetime.datetime.min),
('todate', datetime.datetime.max),
('name', ''),
)
def __init__(self):
self.engine = create_engine('postgresql+psycopg2://'+self.p.dbUser+':'+ self.p.dbPWD +'@'+ self.p.dbHost +'/'+ self.p.dbName)
print(self.engine)
# self.engine = psycopg2.connect(host=self.p.dbHost, database=self.p.dbName, user=self.p.dbUser, password=self.p.dbPWD)
def start(self):
self.conn = self.engine.connect()
sql = "select a.date_price date, a.close_price as close, a.high_price high, a.open_price open, a.low_price low from minute_data a inner join symbol b on a.stock_id = b.id where b.ticker='"+ self.p.ticker + "' and a.date_price between '"+self.p.fromdate.strftime("%Y-%m-%d")+"' and '"+self.p.todate.strftime("%Y-%m-%d")+"' order by date ASC"
self.result = self.conn.execute(sql)
def stop(self):
#self.conn.close()
self.engine.dispose()
def _load(self):
one_row = self.result.fetchone()
if one_row is None:
return False
self.lines.datetime[0] = date2num(one_row[0])
self.lines.open[0] = float(one_row[1])
self.lines.high[0] = float(one_row[2])
self.lines.low[0] = float(one_row[3])
self.lines.close[0] = float(one_row[4])
# self.lines.volume[0] = int(one_row[5])
self.lines.openinterest[0] = -1
return True