Hello quantconnecters,
I have bid/ask data for an instrument (forex) in the form of a .csv file. I need LEAN to buy at the ask price and sell at the bid price (like it would during live execution).
To achieve that, I tried modifying the usual way custom data classes are made : .Reader() returns a QuoteBar object.
from QuantConnect.Python import PythonData
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect import Globals, SubscriptionTransportMedium
import typing
from datetime import datetime
import os
from AlgorithmImports import *
class CustomData(PythonData):
"""
Class to import data that is not coming from Quantconnect servers.
Local .csv data
"""
def GetSource(self, config, date, isLiveMode):
source = os.path.join(Globals.DataFolder,
"forex/MyData/DATA_calib_case_8b.csv")
return SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile)
def Reader(self, config, line, date, isLiveMode):
# If first character is not digit (header : Date, Open, High...), pass
if not (line.strip() and line[0].isdigit()): return None
# New CustomData object
quotebar = QuoteBar()
quotebar.Symbol = config.Symbol
try:
data = line.split(',')
quotebar.Time = datetime.strptime(data[0], '%Y-%m-%d %H:%M:%S')
# .EndTime is automaticaly set.
# Ask
quotebar.Ask.Open = float(data[1])
quotebar.Ask.High = float(data[2])
quotebar.Ask.Low = float(data[3])
quotebar.Ask.Close = float(data[4])
# Bid
quotebar.Bid.Open = float(data[5])
quotebar.Bid.High = float(data[6])
quotebar.Bid.Low = float(data[7])
quotebar.Bid.Close = float(data[8])
# When using .AddForex() and QC's data, .Value has the value of the midpoint Close.
# Try to reproduce the same behavior here
quotebar.Value = quotebar.Close
except ValueError:
# Do nothing
return None
return quotebar
LEAN has no issue with that, the code runs without errors or warnings. The issue is that the orders are always filled at the mid price and not at the bid (or ask) price as they would if I didn't use custom data.
This behavior could be due to the security type being “base type” (instead of Forex type or any other type that makes LEAN sell/buy at bid/ask).
Is there a way to modify the security type and/or something else to indicate to LEAN that orders need to be filled at bid/ask ?
Thank you very much
Fred Painchaud
Hi Noufel,
You are returning a QuoteBar so in that case, I double checked, SecurityType is already QuoteBar:
But your data is added with AddData instead of AddForex. That might be the issue. You could try to format your data into LEAN's forex' format so you can use AddForex to add it.
Fred
Noufel Serroud
Thank you Fred.
For the security type I was refering to Symbol.SecurityType. But great idea, i originaly though about it but dismissed it because it looked like too much trouble lol. Finaly it worked, here are the steps for anyone who is trying to do the same thing :
"Add a market” and then specify that market in .AddForex()
LEAN will hit an error and tell us that there is no market hours for that new market. We can modify the market hour json file to include our new market. That file is in your local lean folder :
Local_Lean_CLI\data\market-hours\market-hours-database.json
in that json file, I copy pasted the entry for Oanda market and renamed the copy : “my_oanda_market”
Last step would be to go in Local_Lean_CLI\data\forex and create a folder named “my_oanda_market”. That is where all the data can be stored with the right format.
Thus far it seems to be working fine.
Also, there is another way using a custom fee model. If LEAN buy/sells at mid price, then one can apply fees proportional to spread/2 on every trade, It would have the same effect as pushing up the buying price form mid to ask and pushing down the selling price from mid to bid.
I have yet to confirm that both techniques give the same results.
Noufel Serroud
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!