As I continue to learn more of #PythonProgramming , the more I discover its features, data structures and modules that simplify the task of handling any data. In my case the data is stock market historical data from @NSEIndia website... (1/n)
Here, was my old piece of #python code of extracting OHLC (Open, High, Low, Close price) values of currency futures historical data from the CSV file. I was using string functions to clean the csv file & pull OHLC values into a dictionary with date (a string) as key... (2/n)
I did two things to the old code... A.) I made this section of code into a reusable function that takes a CSV file as an argument and returns a dictionary with OHLC values. B.) I discovered built-in #Python csv module that handles all CSV files. Used it here to clean the data.
Since the function is reusable & saved in separate python file, I can just use it in any program by importing it like a module. Here I call the function 'nsetasks.ohlc(fhandle)' in a #python program to retrieve OHLC values to find daily range of the price (High minus Low).. (4/n)
Sorting by date was not possible by using date as a string from the #NSE website's csv file. I discovered the built-in #python datetime module. It's function strptime() takes any date string to create a 'date' object which can be used as key in dictionary... (5/n)
Using datetime module not only solved my problem of sorting OHLC values by Date, it also allows dates to be compared and operations performed based on dates. This opens up doors to sequentially #backtest #tradingstrategies... (6/n)
I'll continue to work on this code & share further updates on my #PythonProgramming learning journey to use it for #backtesting simple #trading strategies on #NSE historical data files. I am sure I'll discover even better ways to handle data in Python. (7/7)
Anyone following this thread, @theBuoyantMan just suggested that @pandas_dev would be a suitable framework to handle such timeseries data (like daily stock prices). I shall check it out once I finish dabbling with my elementary #PythonProgramming skills in these experiments.
You can follow @nishantr.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: