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/

    Cutting A Startegy

    Indicators/Strategies/Analyzers
    2
    5
    231
    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.
    • aviyam ivgi
      aviyam ivgi last edited by

      Hey,
      I'm writing a strategy that reads backtest signals. The signals are for hourly trade and each strategy contains only one signal. When I run all the strategies, They open the trade for the signal, close it and then I want to stop running this strategy.
      Is there a way to stop strategy in the middle of it's running? I tried to raise error and return and it not worked

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Let me summarize what backtrader is not:

        • An interactive shell
        • A debugger
        • Matlab (yes, I know you can stop execution and inspect variables)
        • A money printing machine
        • A coffee machine

        Now, focusing on reality:

        • backtrader runs (it's a design choice) things in the main thread (the only exception is data receiving events from live data sources/brokers, or else ... events in live trading wouldn't be a reality)

        • As such ... when your code in next is running you are free to do whatever you want, for example.: stop execution by diverting the execution path to the module/function/object of your choice.

        aviyam ivgi 1 Reply Last reply Reply Quote 1
        • aviyam ivgi
          aviyam ivgi @backtrader last edited by

          @backtrader
          Thank you for the quick response but I didn't got you.
          My strategy for example:
          `
          import backtrader as bt

          class SignalStrategy(bt.Strategy):
          params = dict(
          signal=None,
          )

          def notify_trade(self, trade):
              if trade.isclosed:
                  # END THE STRATEGY RUN
          
          def next(self):
             if self.datetime.datetime() >= self.p.signal.start_time:
                  if self.p.signal.order_type == OrderType.Buy:
                      main_order = self.buy(data=self.p.signal.symbol, price=self.p.signal.rate, exectype=bt.Order.Market,
                                              transmit=False)
                      if self.p.signal.stop_loss:
                          self.sell(data=self.p.signal.symbol, price=self.p.signal.stop_loss, size=main_order.size,
                                      exectype=bt.Order.Stop, transmit=False, parent=main_order)
                      if self.p.signal.take_profit:
                          self.sell(data=self.p.signal.symbol, price=self.p.signal.take_profit, size=main_order.size,
                                      exectype=bt.Order.Limit, transmit=True, parent=main_order)
                  elif self.p.signal.order_type == OrderType.Sell:
                      main_order = self.sell(data=self.p.signal.symbol, price=self.p.signal.rate, exectype=bt.Order.Market,
                                              transmit=False)
                      if self.p.signal.stop_loss:
                          self.buy(data=self.p.signal.symbol, price=self.p.signal.stop_loss, size=main_order.size,
                                   exectype=bt.Order.Stop, transmit=False, parent=main_order)
                      if self.p.signal.take_profit:
                          self.buy(data=self.p.signal.symbol, price=self.p.signal.take_profit, size=main_order.size,
                                   exectype=bt.Order.Limit, transmit=True, parent=main_order)
          

          '

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            From the top of every page of the forum

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

            @aviyam-ivgi said in Cutting A Startegy:

            Thank you for the quick response but I didn't got you.

            Your code is irrelevant. You want to pause/halt execution and do something. What I said is that you can do whatever you want in next , there is no time requirement for a return from next. You can yield execution to your own code, in which you can if you wish implement an inspector.

            1 Reply Last reply Reply Quote 0
            • aviyam ivgi
              aviyam ivgi last edited by

              when I write yield in my next() function the strategy doesn't even get to next function

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