Backtrader Community

    • 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/

    how to bring NEXT every tick coming in?

    Indicators/Strategies/Analyzers
    1
    1
    89
    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.
    • G
      goldcar last edited by

      I modify the IBTEST sample as below:

      
      class SmaCross(bt.Strategy):
      
          def __init__(self):
              self.beeapi = self.p.store.main_ctpbee_api
              self.beeapi.strategy = self
          def next(self):
              print('------------------------------------------ next start') #nothing print out here.
          def on_tick(self, tick) -> None:
              print(tick.datetime, tick.last_price) #nothing print out
              print("--------------------------------------------------------on tick in cat")
              pass
      
      if __name__ == '__main__':
          with open('./params.json', 'r') as f:
              ctp_setting = json.load(f)
          cerebro = bt.Cerebro(live=True)
          store = CTPStore(ctp_setting, debug=True)
          data0_name = 'ag2212.SHFE'
          compression = 2
          data0 = store.getdata(dataname=data0_name,
                                timeframe=bt.TimeFrame.Ticks,
                                )
          cerebro.resampledata(data0,
                               timeframe=bt.TimeFrame.Seconds,
                               compression=compression
                               )
      
          cerebro.addstrategy(SmaCross, store=store)
          cerebro.run()
      

      and some of the store part as below:

      class MyCtpbeeApi(CtpbeeApi):
          def __init__(self, name, md_queue=None):
              super().__init__(name)
              self.md_queue = md_queue 
              self.is_position_ok = False
              self.is_account_ok = False
          def on_tick(self, tick: TickData) -> None:
              print(tick.datetime, tick)  #it prints out the incoming datas
              pass
          def on_bar(self, bar: BarData) -> None:
              print("on bar coming") # it works here
              self.md_queue[bar.local_symbol].put(bar)
          def on_init(self, init):
              pass
      
      class MetaSingleton(MetaParams):
          """Metaclass to make a metaclassed class a singleton"""
      
          def __init__(cls, name, bases, dct):
              super(MetaSingleton, cls).__init__(name, bases, dct)
              cls._singleton = None
      
          def __call__(cls, *args, **kwargs):
              if cls._singleton is None:
                  cls._singleton = super(MetaSingleton, cls).__call__(*args, **kwargs)
              return cls._singleton
      
      
      class CTPStore(with_metaclass(MetaSingleton, object)):
          BrokerCls = None  #broker class will auto register
          DataCls = None    #data class will auto register
          @classmethod
          def getdata(cls, *args, **kwargs):
              """Returns `DataCls` with args, kwargs"""
              return cls.DataCls(*args, **kwargs)
      
          @classmethod
          def getbroker(cls, *args, **kwargs):
              """Returns broker with *args, **kwargs from registered `BrokerCls`"""
              return cls.BrokerCls(*args, **kwargs)
      
          def __init__(self, ctp_setting, *args, **kwargs):
              super(CTPStore, self).__init__()
              self.ctp_setting = ctp_setting
              self._cash = 0.0
              self._value = 0.0
              self.q_feed_qlive = dict()
              self.main_ctpbee_api = MyCtpbeeApi("main_ctpbee_api", md_queue=self.q_feed_qlive)
              self.app = CtpBee("ctpstore", __name__, refresh=True)
              self.app.config.from_mapping(ctp_setting)
              self.app.add_extension(self.main_ctpbee_api)
              self.app.start(log_output=True)
              while True:
                  sleep(1)
                  if self.main_ctpbee_api.is_account_ok:
                      break
              print('positions===>', self.main_ctpbee_api.center.positions)
              print('account===>', self.main_ctpbee_api.center.account)
      

      Both has "on tick", but only the one in store class can print out incoming tick datas. in stragecy class ,it doesn't print out the tick data. And, it store class's ON_BAR, it print out bar data no problem, but there is no action in "NEXT" of stragecy class.

      I am new in Python, dont know how to fix it. any help? appreciated!

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