"""Website example: bundled GOOG daily bars, 0.2% commission per execution."""

from openbacktesting import Backtest, Strategy
from openbacktesting.test import GOOG


class SmaCross(Strategy):
    fast = 10
    slow = 20

    def init(self):
        self.fast_sma = self.ta.sma(self.close, length=self.fast)
        self.slow_sma = self.ta.sma(self.close, length=self.slow)

    def next(self):
        if self.ta.is_crossover(self.fast_sma, self.slow_sma):
            self.position.close()
            self.buy(percent=95)
        elif self.ta.is_crossunder(self.fast_sma, self.slow_sma):
            self.position.close()
            self.sell(percent=95)


if __name__ == "__main__":
    bt = Backtest(GOOG, SmaCross, cash=10_000, commission=0.002,
                  end_policy="close_at_last_close")
    result = bt.run()
    print(result[["Return [%]", "# Trades", "Max. Drawdown [%]"]])
