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

Setting Open = High = Low = Close



  • Hi,

    My data source has OHLC values, how can I make it so Backtrader only looks at the "Close" column, i.e. Backtrader sets Open = High = Low = Close.

    I am using a Pandas custom data loader, is the following appropriate?

    class CustomBTStockLoader(btfeeds.PandasData):
    
        params = (
                     ('openinterest', None),
                     ('open', 'Close'),
                     ('high', 'Close'),
                     ('low', 'Close'),
                 ) 
        datafields = btfeeds.PandasData.datafields
    

    ... and then later on:

    data = CustomBTStockLoader(dataname=datarange) #allows load additional "Opportunity" column
                data.plotinfo.plot=False
                cerebro.adddata(data, name=ticker)
    

    Thank you!



  • @backtrader the above appears to work as I would expect. Can you please confirm this is a reasonable approach and is indeed setting O = H = L = Close?

    Thanks!



  • Your approach will ruin indicators which use full OHLC values and limit/stop order execution.

    If you want orders to be executed on close than probably better to use cheat-on-close and market orders and original OHLC data.

    So depends on your goals.



  • hmm..

    Thanks @ab_trader.

    So using the above, when my sell orders complete, they seem to be using the next days close price.

    Does Backtrader use the next day's open when completing a sell on the current day? This shouldn't happen?

    NB: I do NOT have "Cheat on open" configured.

    cheers, CWE


  • administrators

    @cwse said in Setting Open = High = Low = Close:

    So using the above, when my sell orders complete, they seem to be using the next days close price.

    Well, your only input is the close price, so it seems reasonable that some close price will be used for the matching.

    @cwse said in Setting Open = High = Low = Close:

    Does Backtrader use the next day's open when completing a sell on the current day?

    No idea what you mean with "next day's open when completing a sell on the current day".

    @cwse said in Setting Open = High = Low = Close:

    This shouldn't happen?

    Why? We still don't know what you do. So it may actually be the intended behavior.

    @ab_trader said in Setting Open = High = Low = Close:

    to use cheat-on-close

    It would seem like if you ignored this advice from @ab_trader

    NB: I do NOT have "Cheat on open" configured.

    Why should this play a role? This functionality gives you the chance to do something before the next bar is officially given to you, but doesn't affect price matching.