Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    cerebro.addbroker()?

    General Code/Help
    3
    4
    454
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kav last edited by kav

      Hi all,

      I have created a custom broker by modifying a couple of functions from
      /backtrader/brokers/bbroker.py
      (calling the new file mybbroker.py and putting it in my own code directory, -- not in the same folder as the original bbroker.py).

      Now my question is: how can I call this one to be used instead?

      I tried to add a line

      import mybroker
      

      on top of the code, and though the import happens, it's still the original broker that gets used when I do:

      import backtrader
      import mybroker
      
      cerebro   = backtrader.Cerebro(maxcpus = 1)
      cerebro.addobserver(backtrader.observers.Broker)
      cerebro.addstrategy(StrategyBaseTradingplusplus)        
      data      = PandasDataStrat1(dataname     = dataframe, 
                                           datetime     = -1, 
                                           open         = -1, 
                                           high         = -1, 
                                           low          = -1, 
                                           close        = -1,
                                           volume       = -1,
                                           openinterest = None)
      cerebro.adddata(data)
              
      cerebro.broker.setcash(100000.0)
      cerebro.broker.setcommission(commission = 0.2, 
                                    margin     = 1000.0, 
                                    mult       = 100.0)
      cerebro.broker.set_filler(backtrader.broker.filler.FixedBarPerc(perc = 100.0))
                     
      cerebro.run()
      
      

      So my question is: How to use my custom broker?

      1 Reply Last reply Reply Quote 0
      • M
        momentum last edited by

        cerebro.setbroker(<instance of your broker class>)
        
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by backtrader

          Why don't you use what's already documented?

          Example: Docs - Live Trading - Oanda v1.0

          The Cerebro reference says what setbroker does (can also be reached via the broker property): Docs - Cerebro

          1 Reply Last reply Reply Quote 0
          • K
            kav last edited by

            Thanks, worked like a charm. As usual, thank you for the prompt answer and the references;)

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors