Hello there:
As many of you… I am taking the course on machine learning: linear regression … and I have an hypotesis…
I think that one of the input variables for a linear regression for stimating the prices of the next day… is the premarket volume… Now I want to fetch the premarket volume for a specific stock… from an initial dato to an end date… so I came up with the following code on ibreidgepy using data from interactive brokers in order to do so:
""""""
import sys
import numpy as np
import pandas as pd
import datetime as dt
from datetime import datetime
from datetime import date
from datetime import timedelta
import pytz
import time
import os
from time import sleep
import warnings
warnings.filterwarnings("ignore")
def convertir_fecha(fecha):
fecha_dt = pd.to_datetime(fecha, format='%y-%m-%d')
ny_tz = pytz.timezone('America/New_York')
fecha_dt_ny = ny_tz.localize(fecha_dt.replace(hour=9, minute=30))
return fecha_dt_ny
def initialize(context):
pass
def handle_data(context, data):
# specify the ticker
tickers = ['TMPO']
for i in tickers:
stock = superSymbol(secType='STK', symbol= 'TMPO', currency='USD', exchange='SMART')
df1 = request_historical_data(stock, '1 day', '60 D', datetime.now(pytz.timezone('America/New_York')) ,useRTH=1)
del df1['index']
df1.reset_index(inplace=True)
df1.rename(columns={'index': 'index_column'}, inplace=True)
df1['timestamp']= pd.to_datetime(df1['timestamp'])
df1['timestamp']= df1['timestamp'].dt.date
df1['timestamp'] = pd.to_datetime(df1['timestamp']) + pd.Timedelta(days=1)
dates = df1['timestamp']
dates = dates.apply(convertir_fecha)
lenght = len(dates)
pre_vol =
for s in dates :
data = request_historical_data(stock, '30 mins', '19800 S', s ,useRTH=0)
premarket_vol = data['volume'].sum()
pre_vol.append(premarket_vol)
df1 = df1.assign(PreMKT_Volume = pre_vol)
print (df1)
end()
""""""
This code actually fetches the premarket volume for a specific stock… for the last 60 days… and stores it in an aditional column for the ohlcv data
What do you think about this code ???
Is it good ??? is it bad ???.. How can I make it better…??
I would like to know your comments on order to refine the code… and correct it if it has anny errors… so please tell me
Thanks
Hi Ghery,
The code appears to be functional for fetching premarket volume data. However, there are some potential areas for improvement:
- Data Error Handling: The code fetches premarket volume data using a loop that iterates over a list of dates. However, if there is missing or incomplete data for any of the dates, it may result in errors or incomplete results. It would be advisable to add error handling and data validation to handle such cases.
- Code Organization: The code currently includes all the logic in a single function
handle_data()
. It would be beneficial to consider organizing the code into separate functions or classes for better modularity and maintainability. For example, separate functions for fetching historical data, converting dates to the desired format, and calculating premarket volume could make the code more modular and easier to understand. - Code Readability: It would be beneficial to include comments and documentation to explain the purpose and functionality of the code for better understanding.
- Testing and Debugging: It would be beneficial to thoroughly test the code with different inputs and scenarios to identify and fix any potential issues. Adding debug statements or logging can also help with troubleshooting and debugging in case of errors or unexpected behaviour.
Thanks,
Akshay
ok, Thanks a lot…