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