Why this 'n' is equal to this

Course Name: Price Action Trading Strategies Using Python, Section No: 8, Unit No: 5, Unit type: Notebook

Hello, while calculating the 'n', in the CAGR cell, if the candle difference is more less than 1 (intraday data) n became equal to 8, which is the number of columns for the last day of trading?

Ksn3C2k.png (1196×309) (imgur.com)

Can you explain the whole reasoning for this?

Hi Daniel,



CAGR, as the name itself suggests is the annualised value. If the data is daily time period data, then you obviously consider 252 days in a year, and 252 datapoints.



But if it is intraday, then we need to know how many datapoints are there in a year, thus we are calculating the 'n' depending on the number of trading candles.



If candle_difference is 1 or more, np.floor(candle_difference) gives you the integer part of candle_difference, effectively converting any fractional part to the nearest lower integer.



For example, if candle_difference is 1.8, np.floor(candle_difference) will give 1. Daily data will give n as 1.



If candle_difference is less than 1, it means you have multiple trading candles per day. In this case, you calculate n as the number of trading candles in the last day of the dataset.



This distinction is important because the calculation of CAGR involves knowing whether your data is daily or more frequent (intraday).



The .shape attribute in Python is used to get the dimensions of an array or DataFrame. For a DataFrame, .shape returns a tuple representing the dimensions of the DataFrame, where the first element of the tuple is the number of rows and the second element is the number of columns.



In the context of the code:



n = data.loc[datetime.datetime.strftime(data.index[-1].date(), '%Y-%m-%d')].shape[0]



Here's what .shape[0] specifically does:



data.loc[datetime.datetime.strftime(data.index[-1].date(), '%Y-%m-%d')] is used to select rows in the DataFrame data based on a specific date.

.shape is then applied to this selected subset, giving you the dimensions of this subset.

Finally, .shape[0] extracts the first element of the tuple returned by .shape, which corresponds to the number of rows in the subset.

So, in simple terms, .shape[0] in this context gives you the number of rows in data that correspond to the specific date extracted from data.index[-1].



I hope this helps.



No, please recheck again as shape is returning in the first index columns and in the second the rows, but as this is a single row the number of rows is None. Check the following screenshot:


So you're not saying that you're going to use the number of candles in a day as "n" but you're saying to python to use the number of columns as "n", so the calculation will be mistaken.

We need to change the way we calculate the number of candles contained in a day when an intraday OHLCV data is passed.

PS: maybe I'm wrong… Is it possible I'm seeing only 7 columns a none row because the data is daily. Would it work when intraday data is used?

PS2: my bad! You're right, I tried with intraday data and line of code tells you how many rows in the day do you have! The code is working perfectly!



Hello Daniel,



That is great to know. Happy learning.



Thanks.