Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trades seem to be done in reversed chronological order #1144

Open
jin-95 opened this issue May 27, 2024 · 1 comment
Open

Trades seem to be done in reversed chronological order #1144

jin-95 opened this issue May 27, 2024 · 1 comment

Comments

@jin-95
Copy link
jin-95 commented May 27, 2024

Behavior

Im writing a dollar cost averaging strategy. The code works fine but the trades are done in a reverse chronological order. the dataframe is not reversed.

ticker_symbol = "IWDA.AS"

Get the data

ticker_data = yf.Ticker(ticker_symbol)
df = ticker_data.history(period="max")

class DCA(Strategy):

def init(self):
    self.close = self.data.Close
    self.amount_to_invest = 280  # Example: invest $250 every Monday
    self.day_of_week = self.data.index.to_series().apply(lambda x: x.dayofweek).values
    #self.day_of_week = self.day_of_week.  # Convert to numpy array for indexing
    self.I(lambda: self.day_of_week, name='day_of_week')

def next(self):
    if self.data.index[-1].day_of_week ==0:
        self.buy(size=math.floor(self.amount_to_invest / self.close[-1]))

Initialize the backtest with the dataframe 'df'

bt = Backtest(df, DCA,
cash=10000000,
commission=0,
trade_on_close=False,
exclusive_orders=False)

#Run the backtest
output = bt.run()
print(output)

trades = output["_trades"]
trades

0,3,3750,3754,92.254997,92.07,-0.554993,-0.002005,2024-05-21 00:00:00+02:00,2024-05-27 00:00:00+02:00,6 days 00:00:00
1,3,3745,3754,91.330002,92.07,2.219994,0.008102,2024-05-14 00:00:00+02:00,2024-05-27 00:00:00+02:00,13 days 00:00:00
2,3,3740,3754,90.815002,92.07,3.764992,0.013819,2024-05-07 00:00:00+02:00,2024-05-27 00:00:00+02:00,20 days 00:00:00
3,3,3736,3754,90.010002,92.07,6.179993,0.022886,2024-04-30 00:00:00+02:00,2024-05-27 00:00:00+02:00,27 days 00:00:00
4,3,3731,3754,88.839996,92.07,9.690010,0.036358,2024-04-23 00:00:00+02:00,2024-05-27 00:00:00+02:00,34 days 00:00:00
5,3,3726,3754,89.345001,92.07,8.174995,0.030500,2024-04-16 00:00:00+02:00,2024-05-27 00:00:00+02:00,41 days 00:00:00
6,3,3721,3754,90.370003,92.07,5.099991,0.018812,2024-04-09 00:00:00+02:00,2024-05-27 00:00:00+02:00,48 days 00:00:00
7,3,3713,3754,90.754997,92.07,3.945007,0.014490,2024-03-26 00:00:00+01:00,2024-05-27 00:00:00+02:00,61 days 23:00:00
8,3,3708,3754,89.300003,92.07,8.309990,0.031019,2024-03-19 00:00:00+01:00,2024-05-27 00:00:00+02:00,68 days 23:00:00
9,3,3703,3754,88.730003,92.07,10.019989,0.037642,2024-03-12 00:00:00+01:00,2024-05-27 00:00:00+02:00,75 days 23:00:00
10,3,3698,3754,88.714996,92.07,10.065010,0.037818,2024-03-05 00:00:00+01:00,2024-05-27 00:00:00+02:00,82 days 23:00:00

@coder145
Copy link

probably something to do with the data index, can you share it please having a hard time getting it from yahoo finance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants