MiaaaFan / Classifying-and-Predicting-Stock-Market-States-Using-HMM-and-XGBoost

26 stars 10 forks source link

Incorrect Buy and Hold Strategy #2

Open geofffoster opened 2 years ago

geofffoster commented 2 years ago

If you buy on 2018-01-03 at 1595, and sell 2021-12-30 at 2795 you have clearly doubled your money. I think you have an error in this strategy. Using a generic bt byandhold as below gives me a near doubling

class BuyAndHold(bt.Strategy): def start(self):

set the starting cash

    self.val_start = self.broker.get_cash() 
def nextstart(self):
    # Buy stocks with all the available cash
    size = int(self.val_start / self.data)
    self.buy(size=size)
def stop(self):
    # calculate the actual returns
    self.roi = (self.broker.get_value() / self.val_start) - 1.0
    print("ROI: %.2f, Cash: %.2f" % (100.0 * self.roi, self.broker.get_value()))