In [1]:
%matplotlib inline
# Imports
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Jupyter import *
from datetime import datetime, timedelta
import pandas as pd

# Create an instance
qb = QuantBook()
In [9]:
# forex_pair = 'EURUSD'
forex_pair = 'GBPUSD'
start_time = DateTime(2019,1,1,0,0,0)
# end_time = DateTime(2019,8,23,0,0,0)
end_time = DateTime.Now
In [10]:
# qb.AddForex(forex_pair, market=Market.Oanda) # Subscribe to the instrument.
qb.AddForex(forex_pair, market=Market.FXCM) # Subscribe to the instrument.

prices = qb.History([forex_pair], start_time, end_time, Resolution.Hour).loc[forex_pair][['open','high','low','close']]

prices.head()
# prices.head(50)
# prices.tail(50)
Out[10]:
open high low close
time
2019-01-02 01:00:00 1.274300 1.275780 1.274080 1.275600
2019-01-02 02:00:00 1.275605 1.277320 1.275210 1.275540
2019-01-02 03:00:00 1.275520 1.276350 1.274760 1.275195
2019-01-02 04:00:00 1.275185 1.276085 1.269575 1.271130
2019-01-02 05:00:00 1.271135 1.271950 1.268770 1.268900
In [11]:
fill_forward_times = []
for i in range(2, len(prices)-1):
    if ((prices.iloc[i]['open'] == prices.iloc[i-1]['open']) and not (prices.iloc[i-1]['open'] == prices.iloc[i-2]['open'])) \
            and ((prices.iloc[i]['high'] == prices.iloc[i-1]['high']) and not (prices.iloc[i-1]['high'] == prices.iloc[i-2]['high'])) \
            and ((prices.iloc[i]['low'] == prices.iloc[i-1]['low']) and not (prices.iloc[i-1]['low'] == prices.iloc[i-2]['low'])) \
            and ((prices.iloc[i]['close'] == prices.iloc[i-1]['close']) and not (prices.iloc[i-1]['close'] == prices.iloc[i-2]['close'])):
        fill_forward_times.append(prices.index[i-1])
        
In [12]:
# Times of the bar that is filled forward.
fill_forward_times
# len(fill_forward_times)
Out[12]:
[Timestamp('2019-05-09 01:00:00'),
 Timestamp('2019-05-22 01:00:00'),
 Timestamp('2019-05-31 01:00:00'),
 Timestamp('2019-06-03 02:00:00'),
 Timestamp('2019-06-06 01:00:00'),
 Timestamp('2019-06-11 01:00:00'),
 Timestamp('2019-06-13 01:00:00'),
 Timestamp('2019-07-01 02:00:00'),
 Timestamp('2019-07-05 01:00:00'),
 Timestamp('2019-07-08 02:00:00'),
 Timestamp('2019-07-17 01:00:00'),
 Timestamp('2019-07-18 01:00:00'),
 Timestamp('2019-07-19 01:00:00'),
 Timestamp('2019-07-19 17:00:00'),
 Timestamp('2019-07-23 01:00:00'),
 Timestamp('2019-07-24 02:00:00'),
 Timestamp('2019-07-26 01:00:00'),
 Timestamp('2019-07-26 17:00:00'),
 Timestamp('2019-08-01 01:00:00'),
 Timestamp('2019-08-02 02:00:00'),
 Timestamp('2019-08-05 02:00:00'),
 Timestamp('2019-08-09 01:00:00'),
 Timestamp('2019-08-12 02:00:00'),
 Timestamp('2019-08-14 01:00:00'),
 Timestamp('2019-08-15 01:00:00'),
 Timestamp('2019-08-16 02:00:00'),
 Timestamp('2019-08-19 02:00:00'),
 Timestamp('2019-08-20 02:00:00'),
 Timestamp('2019-08-21 02:00:00'),
 Timestamp('2019-08-22 02:00:00'),
 Timestamp('2019-08-23 02:00:00')]
In [13]:
# Print the prices from the row before fill forward through the next 24 rows.
start_row = prices.index.get_loc('2019-05-31 01:00:00')
# start_row = prices.index.get_loc(fill_forward_times[1])
prices.iloc[start_row-1:start_row+24]
Out[13]:
open high low close
time
2019-05-31 00:00:00 1.261220 1.261250 1.261050 1.261125
2019-05-31 01:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 02:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 03:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 04:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 05:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 06:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 07:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 08:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 09:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 10:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 11:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 12:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 13:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 14:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 15:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 16:00:00 1.261130 1.261150 1.260410 1.260775
2019-05-31 17:00:00 1.261130 1.261150 1.260410 1.260775
2019-06-02 18:00:00 1.261695 1.263440 1.261685 1.263100
2019-06-02 19:00:00 1.263105 1.263985 1.262670 1.263560
2019-06-02 20:00:00 1.263635 1.263915 1.263085 1.263490
2019-06-02 21:00:00 1.263485 1.263930 1.263410 1.263905
2019-06-02 22:00:00 1.263900 1.264790 1.263820 1.264725
2019-06-02 23:00:00 1.264715 1.265370 1.264490 1.265090
2019-06-03 00:00:00 1.265095 1.265535 1.264725 1.265425
In [ ]: