Exemplo n.º 1
0
def create_file(pair, granularity, api):
    candle_count = 2000
    time_step = INCREMENTS[granularity] * candle_count

    end_date = utils.get_utc_dt_from_string("2020-12-31 23:59:59")
    date_from = utils.get_utc_dt_from_string("2018-01-01 00:00:00")

    candle_dfs = []

    date_to = date_from
    while date_to < end_date:
        date_to = date_from + dt.timedelta(minutes=time_step)
        if date_to > end_date:
            date_to = end_date

        code, df = api.fetch_candles(pair,
                                     granularity=granularity,
                                     date_from=date_from,
                                     date_to=date_to,
                                     as_df=True)
        if df is not None and df.empty == False:
            candle_dfs.append(df)
        elif code != 200:
            print("ERROR", pair, granularity, date_from, date_to)
            break
        date_from = date_to

    final_df = pd.concat(candle_dfs)
    final_df.drop_duplicates(subset='time', inplace=True)
    final_df.sort_values(by='time', inplace=True)
    final_df.to_pickle(utils.get_his_data_filename(pair, granularity))
    print(
        f"{pair} {granularity} {final_df.iloc[0].time}  {final_df.iloc[-1].time}"
    )
Exemplo n.º 2
0
def create_file(pair, granularity, api):
    candle_count = 2000
    time_step = INCREMENTS[granularity] * candle_count

    end_date = utils.get_utc_dt_from_string("2020-12-31 23:59:59")
    date_from = utils.get_utc_dt_from_string("2018-01-01 00:00:00")

    candle_dfs = []

    date_to = date_from
    while date_to < end_date:
        date_to = date_from + dt.timedelta(minutes=time_step)
        if date_to > end_date:
            date_to = end_date

        date_from = date_to
Exemplo n.º 3
0
    def candles_to_df(cls, json_data):
        prices = ['mid', 'bid', 'ask']
        ohlc = ['o', 'h', 'l', 'c']

        our_data = []
        for candle in json_data:
            if candle['complete'] == False:
                continue
            new_dict = {}
            new_dict['time'] = candle['time']
            new_dict['volume'] = candle['volume']
            for price in prices:
                for oh in ohlc:
                    new_dict[f"{price}_{oh}"] = float(candle[price][oh])
            our_data.append(new_dict)
        df = pd.DataFrame.from_dict(our_data)
        df["time"] = [parse(x) for x in df.time]
        return df


if __name__ == "__main__":
    api = OandaAPI()
    #api.save_instruments()
    date_from = utils.get_utc_dt_from_string("2019-05-05 18:00:00")
    date_to = utils.get_utc_dt_from_string("2019-05-10 18:00:00")
    res, df = api.fetch_candles("EUR_USD",
                                date_from=date_from,
                                date_to=date_to,
                                as_df=True)
    print(df.info())