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



  • 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


  • administrators

    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.



  • @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)
    

    '


  • administrators

    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.



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


Log in to reply
 

});