File size: 2,362 Bytes
de6e775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List

from meta.config import BINANCE_BASE_URL
from meta.config import TIME_ZONE_BERLIN
from meta.config import TIME_ZONE_JAKARTA
from meta.config import TIME_ZONE_PARIS
from meta.config import TIME_ZONE_SELFDEFINED
from meta.config import TIME_ZONE_SHANGHAI
from meta.config import TIME_ZONE_USEASTERN
from meta.config import USE_TIME_ZONE_SELFDEFINED
from meta.data_processors._base import _Base

# from basic_processor import _Base


## The code of this file is used in website, not locally.
class Quantconnect(_Base):
    def __init__(
        self,
        data_source: str,
        start_date: str,
        end_date: str,
        time_interval: str,
        **kwargs,
    ):
        super().__init__(data_source, start_date, end_date, time_interval, **kwargs)

    # def data_fetch(start_time, end_time, stock_list, resolution=Resolution.Daily) :
    #     #resolution: Daily, Hour, Minute, Second
    #     qb = QuantBook()
    #     for stock in stock_list:
    #         qb.AddEquity(stock)
    #     history = qb.History(qb.Securities.Keys, start_time, end_time, resolution)
    #     return history

    def download_data(
        self, ticker_list: List[str], save_path: str = "./data/dataset.csv"
    ):
        # self.time_zone = calc_time_zone(ticker_list, TIME_ZONE_SELFDEFINED, USE_TIME_ZONE_SELFDEFINED)

        # start_date = pd.Timestamp(start_date, tz=self.time_zone)
        # end_date = pd.Timestamp(end_date, tz=self.time_zone) + pd.Timedelta(days=1)
        qb = QuantBook()
        for stock in ticker_list:
            qb.AddEquity(stock)
        history = qb.History(
            qb.Securities.Keys,
            self.start_date,
            self.end_date,
            self.time_interval,
        )
        self.dataframe = history

        self.save_data(save_path)

        print(
            f"Download complete! Dataset saved to {save_path}. \nShape of DataFrame: {self.dataframe.shape}"
        )

    # def preprocess(df, stock_list):
    #     df = df[['open','high','low','close','volume']]
    #     if_first_time = True
    #     for stock in stock_list:
    #         if if_first_time:
    #             ary = df.loc[stock].values
    #             if_first_time = False
    #         else:
    #             temp = df.loc[stock].values
    #             ary = np.hstack((ary,temp))
    #     return ary