backtesting.py / print out exit_price

65 Views Asked by At
from backtesting import Backtest, Strategy
from backtesting.test import GOOG
from backtesting.lib import crossover
import talib

class RsiOscillator(Strategy):

    upper_bound = 75
    lower_bound = 35
    rsi_window = 12
    
    def init(self):
        self.rsi = self.I(talib.RSI, self.data.Close, self.rsi_window)

    def next(self):
        if not self.position:
            if crossover(self.lower_bound, self.rsi):
                self.buy()    

        elif crossover(self.rsi, self.upper_bound):
            self.position.close()
            
            if self.closed_trades:
                exit_price = [x.exit_price for x in self.closed_trades]
                print(exit_price[-1])

I'm trying to print out the exit price of the trade but only way of me doing it this weird exit_price[-1] method and it also misses the exit price of last trade so can anyone help me out with this? (No such index works instead of (-1) exit_price[th index])

0

There are 0 best solutions below