0

I have read such codes as below in buyandsell strategy

from backtesting import Backtest, Strategy

class BuyAndSellSwitch(Strategy): def init(self): pass def next(self): if self.position.size == 0: self.buy(size=1) else: self.sell(size=1)

bt = Backtest(data, BuyAndSellSwitch, cash=10000) output = bt.run() output

What does the strategy mean on earth?

showkey
  • 95
  • 3
  • 1
    The link you posted looks like it's more about the task of building a framework to backtest strategies, as opposed to coming up with any "good" strategies. The BuyAndSellSwitch you posted looks to be a very simple strategy, likely only implemented for the purposes of testing code, that buys 1 unit if we have no units, and then sells 1 unit if we have 1 unit (in other words, buys then sells then buys then sells then buys then sells then...) – Rylan Jan 17 '24 at 10:28
  • For example if I want to calculate the P&L from buying MSFT on date1 and selling it on date2, I would just call buyandsell on these 2 dates. A very simple code which might be useful to someone. – nbbo2 Jan 17 '24 at 11:14
  • "buy low, sell high";{)} – lehalle Jan 20 '24 at 12:58

0 Answers0