For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Option Strategy - Buy / Sell order on Custom data feed



  • Hi guys,

    I am kinda stuck on the following option strategy implementation. I have created my own bt.feeds, whereby I have added several lines where each new line correspond to an option strike.

    Sample dict of the bt.feeds below:

    {'lines': ('St_240', 'St_250', 'St_260', 'St_270', 'St_280', 'St_290').....
    
    'params': (('datetime', 0), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('volume', -1), ('openinterest', -1), ('St_240', 1), ('St_250', 2), ('St_260', 3), ('St_270', 4), ('St_280', 5), ('St_290', 6)......
    
    'datafields': ['datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest', 'St_240', 'St_250', 'St_260', 'St_270', 'St_280', 'St_290',......]}
    

    The above is a GenericCSVData feeds, and I pass as parameter a csv with below output (each strike has its own implied vol. I still need to go back from implied vols to prices, but for the sake of my problem let's assume the below are prices..)..

                St_240  St_250    St_260   ...     St_900  St_910  St_1000
    asOfDate                               ...                            
    2017-06-08     NaN     NaN       NaN   ...        NaN     NaN      NaN
    2017-06-09     NaN     NaN  0.257653   ...        NaN     NaN      NaN
    2017-06-12     NaN     NaN  0.252866   ...        NaN     NaN      NaN
    2017-06-13     NaN     NaN       NaN   ...        NaN     NaN      NaN
    2017-06-14     NaN     NaN  0.258900   ...        NaN     NaN      NaN
    

    Ideally, I would be able to use these set of prices and buy / sell any available strike (therefore being able to buy St_240 at x price and being able to buy St_760 at y price).

    How would I achieve that when issuing orders? If I specify my data when issuing buy / sell orders to be:

    self.order = self.buy(self.datas[0])
    

    it just won't pick up the specific strike (and I believe that bt would just be looking for the close price of the data, which I do not have)

    However, if I try to specify the buy order strike with the below, for say strike 510:

    self.order = self.buy(self.datas[0].St_510)
    

    i get the below error:

      File "C:/Users/xxx/PycharmProjects/OptionBacktester/BacktesterOptionV1.py", line 79, in <module>
        cerebro.run()
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
        self._runonce(runstrats)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1695, in _runonce
        strat._oncepost(dt0)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\strategy.py", line 311, in _oncepost
        self.nextstart()  # only called for the 1st value
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 342, in nextstart
        self.next()
      File "C:/Users/xxx/PycharmProjects/OptionBacktester/BacktesterOptionV1.py", line 60, in next
        self.order = self.buy(self.datas[0].St_510)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\strategy.py", line 930, in buy
        size = size if size is not None else self.getsizing(data, isbuy=True)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\strategy.py", line 1469, in getsizing
        return self._sizer.getsizing(data, isbuy=isbuy)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\sizer.py", line 51, in getsizing
        comminfo = self.broker.getcommissioninfo(data)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\broker.py", line 80, in getcommissioninfo
        if data._name in self.comminfo:
    AttributeError: 'LineBuffer' object has no attribute '_name'
    

    Question is, what would be the best practice to tell bt to trade on a different line than close? is it even possible with bt as it is to trade multiple prices on the same datafeed?

    It may look confusing what I am trying to achieve, so if that's case, I can try to clarify what I am trying to achieve.

    Many thanks in advance!


  • administrators

    The broker cannot be magically made aware of what you mean with the extra lines in your data feed.

    @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    self.order = self.buy(self.datas[0].St_510)
    

    self.datas[0].St_510 is not a data feed, it is just one of the lines. And you actually buy a data feed and not its lines.

    @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    what would be the best practice to tell bt to trade on a different line than close?

    The broker doesn't trade on the close. The broker uses the standard OHLC components to decide what's the best matching price (taking into account the order type and the constraints which derive from it). The broker trades on a data feed.

    @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    is it even possible with bt as it is to trade multiple prices on the same datafeed?

    This is a misconception (for sure nowhere to be found in the docs or any other reference) as explained above. You need a custom broker.



  • @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    is it even possible with bt as it is to trade multiple prices on the same datafeed?

    This is a misconception (for sure nowhere to be found in the docs or any other reference) as explained above. You need a custom broker.

    Yes, I asked the question badly and in the wrong way. What I meant (which you understood correctly) was if there was a way to pass directly the buy / sell orders given my data structure.

    Given that I need a custom broker, could you clarify the following doubt that just came up.

    My end goal is doing some backtesting on some delta hedging strategies on derivatives based on some internal models we have in the office. Given that I would need to build a new custom broker to handle the trading of different strikes, but I would also need to be able to trade futures / equities, would the platform be able to handle two brokers (i.e. the normal one that Bt uses (OHLC) and my custom one on the same strategy)? Or would you recommend building a new single totally different broker that can handle both the trading of futures (delta 1 products) and options?

    What would you consider the best approach?

    Many thanks again!



  • You may have different bt compatible feeds for each strike. OHLC columns can be set to single price or to OHLC prices if you have them. In this case you can buy/sell using existing broker. Since these feeds will be standard feeds, than you can use equities and futures along with the options feeds.



  • @ab_trader said in Option Strategy - Buy / Sell order on Custom data feed:

    You may have different bt compatible feeds for each strike. OHLC columns can be set to single price or to OHLC prices if you have them. In this case you can buy/sell using existing broker. Since these feeds will be standard feeds, than you can use equities and futures along with the options feeds.

    @ab_trader
    I see, so having a new data for each strike with the OHLC (will probably just be bid / ask)? And then feed a new data with the futures prices? Is this what you meant?

    Thanks!



  • Yes.

    With the bid/ask data it will be harder to implement, since bt is acting on the OHLC prices.



  • @ab_trader Thanks a bunch!


  • administrators

    @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    would the platform be able to handle two brokers (i.e. the normal one that Bt uses (OHLC) and my custom one on the same strategy)?

    No. It's not meant to work like that.

    @emilpuiu said in Option Strategy - Buy / Sell order on Custom data feed:

    What would you consider the best approach?

    I do really believe your use case is not meant to be, unless you produce something like a continuous future for each of the option strike prices. The problem being that depending on where the future is, you are interested in some strike prices and not in others. But all of them have to be in the engine for them to be considered for a potential purchase(after having cleared the hurdle which is adapting the different strike prices as different data feeds)

    This was actually one of the 1st use cases which was presented by a Quant firm which wanted to cooperate. But backtrader isn't meant for such scenarios.