Looking for ways to integrate FXCM data into QSTrader and one problem is the PriceHandler has a built-in 'price lag' which amplifies on higher times frames.
Take the following example:
Current Short Position
Sold EUR, Bought USD
EURUSD
10K lot
Price 1.12035
Based on how the Position object is coded and with zero commission, this would translate to 10000 * 1.12035 = 11200.35USD
My account currency is GBP, so on each iteration, the Portfolio should update equity in GBP, not USD. This is easy, just make sure that GBPUSD is included in the backtest data and make the conversion.
GBPUSD = 1.23456
11200.35 \ 1.23456
Position Value 9072.34GBP
If using say Daily data, the values used to calculate the exchange rate would be 1 Day behind, because the Portfolio is calling the PriceHandler for the last known value and of course the most current value has not been iterated through the event loop.
To make sure the PriceHandler has all the values stored with the same timestamp. I have implemented the following code below.
The code will call next until the date is greater than the last. The last greater than date is saved and only placed into the queue on the next iteration.
def __init__(self):
.....
.....
self.last_row = None
self.row_saved = False
.....
[snip]
def _iter_nextrow(self):
"""
Get next row from the pandas DataFrame
"""
try:
return next(self.bar_stream)
except StopIteration:
self.continue_backtest = False
return None, None
def place_into_queue(self, index, row):
"""
Place the next FXCMBarEvent onto the event queue
"""
# Obtain all elements of the bar from the dataframe
ticker = row["Ticker"]
period = 86400 # Seconds in a day
# Create the tick event for the queue
bev = self._create_event(index, period, ticker, row)
# Store event
self._store_event(bev)
# Send event to queue
self.events_queue.put(bev)
def stream_next(self):
"""
Stream all bars that have the same date.
"""
# No row saved on system startup
if self.row_saved:
# Get last saved row and reset
index, row = self.last_row
self.place_into_queue(index, row)
self.row_saved = False
# System starts here on first run
while True:
index, row = self._iter_nextrow()
if row is None:
return
if self.last_row is not None:
if index > self.last_row[0]:
# Save last row for later
self.last_row = index, row
self.row_saved = True
return
# Update last row
self.last_row = index, row
self.place_into_queue(index, row)
Hi,
Looking for ways to integrate FXCM data into QSTrader and one problem is the
PriceHandler
has a built-in 'price lag' which amplifies on higher times frames.Take the following example:
Current Short Position
Based on how the
Position
object is coded and with zero commission, this would translate to10000 * 1.12035 = 11200.35USD
My account currency is GBP, so on each iteration, the
Portfolio
should updateequity
in GBP, not USD. This is easy, just make sure that GBPUSD is included in the backtest data and make the conversion.If using say
Daily
data, the values used to calculate the exchange rate would be 1 Day behind, because thePortfolio
is calling thePriceHandler
for the last known value and of course the most current value has not been iterated through the event loop.To make sure the
PriceHandler
has all the values stored with the same timestamp. I have implemented the following code below.The code will call
next
until the date is greater than the last. The last greater than date is saved and only placed into the queue on the next iteration.Some
Event
log output to visualise.Any ideas or comments please?
Many Thanks
James