I have below code and is working fine when I am executing from console. Now I want convert the code constructor which will help to call this and get data.
from kiteconnect import KiteConnect
import pandas as pd
from datetime import datetime
from dateutil.relativedelta import relativedelta
import math
import numpy as np
import talib
import json
# Setting Spyder environmental variables
pd.set_option("display.max_columns", 30)
pd.set_option("display.max_rows", 500)
pd.set_option("display.width", 1000)
with open('C:/AlgoTrade/TradeApp/config/brokerapp.json') as f:
data = json.load(f)
access_token = data['accessToken']
api = data['appKey']
kite=KiteConnect(api_key=api)
kite.set_access_token(access_token)
End_Time = datetime.now().date()
Start_Time = End_Time + relativedelta(days=-30)
# Function to calculate DATR
def DATR():
BF_DATR = pd.DataFrame(kite.historical_data(260105,Start_Time,End_Time,"day",False,True))
atr = (talib.ATR(BF_DATR["high"], BF_DATR["low"], BF_DATR["close"], timeperiod = 14)).tail(1)
atr2p = math.ceil(atr * .02)
return atr, atr2p
DATR, DATRP = DATR()
print("DATR : %2d, 2 percentage of DATR : %3d" % (DATR, DATRP))
# Find explosive candles in 5 mins range
class Zones:
def __init__(self, data):
self.data = data
def candle_type(self):
"""this column will be true if the candle is up """
self.data['candle_type'] = np.where(self.data['open'] < self.data['close'], "Bullish", "Bearish")
def body_spread(self):
"""the body of the candle"""
self.data['body'] = (self.data['close'] - self.data['open']).abs()
self.data['spread'] = self.data['high'] - self.data['low']
def is_exciting_candle(self):
self.data['candle_size'] = np.where(((self.data['body'] > (0.55*self.data['spread'])) & (self.data['body'] > DATRP)), "Exciting", "Basing")
def clean(self):
"""removes unneeded columns"""
self.data.drop(['volume','oi', 'body', 'spread'], axis=1, inplace= True)
def run_all(self):
"""run all the methods"""
self.candle_type()
self.body_spread()
self.is_exciting_candle()
self.clean()
Start_Time = End_Time + relativedelta(days=-100)
data = pd.DataFrame(kite.historical_data(260105,Start_Time,End_Time,"5minute",False,True))
data['date'] = pd.to_datetime(data['date']).apply(lambda x: x.replace(tzinfo=None))
sample = Zones(data=data)
sample.run_all()
data = sample.data
print(data)
Now I need to convert above code to constructor and call from another function. Which should return the pandas data frame.
Expected out put is, when we call the constructor from another function it should return the data.
BANKNIFTYDATA = zone.data()
print(BANKNIFTYDATA)
date open high low close candle_type candle_size
0 2022-08-04 09:15:00 38111.05 38230.55 38111.05 38166.65 Bullish Basing
1 2022-08-04 09:20:00 38165.00 38165.00 38079.90 38098.80 Bearish Exciting
2 2022-08-04 09:25:00 38099.90 38157.50 38096.75 38113.05 Bullish Basing
3 2022-08-04 09:30:00 38114.45 38141.20 38086.10 38108.85 Bearish Basing
4 2022-08-04 09:35:00 38106.10 38115.00 38051.50 38066.40 Bearish Exciting
5 2022-08-04 09:40:00 38060.65 38111.75 38054.00 38081.95 Bullish Basing
6 2022-08-04 09:45:00 38081.80 38151.40 38080.75 38133.60 Bullish Exciting
7 2022-08-04 09:50:00 38133.85 38170.20 38131.05 38161.75 Bullish Exciting
8 2022-08-04 09:55:00 38163.20 38166.40 38112.10 38129.30 Bearish Exciting
9 2022-08-04 10:00:00 38131.70 38141.15 38093.10 38117.65 Bearish Basing
I got my answer..
class NiftybankData:
def __init__(self):
End_Time = datetime.now().date()
Start_Time = End_Time + relativedelta(days=-100)
def data(self):
data = pd.DataFrame(kite.historical_data(260105,Start_Time,End_Time,"5minute",False,True))
data['date'] = pd.to_datetime(data['date']).apply(lambda x: x.replace(tzinfo=None))
data['candle_type'] = np.where(data['open'] < data['close'], "Bullish", "Bearish")
data['body'] = (data['close'] - data['open']).abs()
data['spread'] = data['high'] - data['low']
data['candle_size'] = np.where(((data['body'] > (0.55*data['spread'])) & (data['body'] > DATRP)), "Exciting", "Basing")
data.drop(['volume','oi', 'body', 'spread'], axis=1, inplace= True)
return data
NiftyBank = NiftybankData().data()
print(NiftyBank)
Related
Im preparing a python code , to screen stocks from the SP500 , DOW and Nasdaq.
SP500 and DOW importing data stocks is working properly , but when I try to import Nasdaq always get similar error, related to timestamp.
See below:
My code:
import talib
from yahoo_fin.stock_info import get_data
import yahoo_fin.stock_info as si
from datetime import datetime
list = si.tickers_nasdaq()
# Get current date and time
now = datetime.now().strftime("%m_%d_%Y_%I_%M_%S")
end_date = datetime.now().strftime('%Y-%m-%d')
start_date = (datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d')
# Create file to save results
f = open(f'C:/Users/fco_j/OneDrive/Escritorio/CHAT GPT/Python/Reports/dow_results_{now}.csv', 'w')
# print table header to file
f.write('Ticker, ClosePrice, SMA200, SMA20, RSI, RelVol\n')
# Define cache_data function
def cache_data(data, stock):
data.to_pickle(f'C:/Users/fco_j/OneDrive/Escritorio/CHAT GPT/Python/Pickle/{stock}.pkl')
for stock in list:
# Download historical data for past year
data = si.get_data(stock, start_date=start_date, end_date=end_date)
last_price = data["close"][-1]
# Get 150 and 20 simple moving averages using Talib
sma150 = talib.SMA(data['close'], timeperiod=150)[-1]
sma20 = talib.SMA(data['close'], timeperiod=20)[-1]
rsi = talib.RSI(data['close'], timeperiod=14)
# Calculate Relative Volume
rel_vol = data['volume'] / talib.SMA(data['volume'].values.astype(float), timeperiod = 50)
# Cache data
cache_data(data, stock)
# Filter stocks with relative volume (time period 20) over 1
if last_price > sma150 and last_price > sma20 and rsi[-1] > 50 and rel_vol[-1] > 1:
# Print results to file
f.write(f"{stock},{last_price},{sma150},{sma20},{rsi[-1]},{rel_vol[-1]}\n")
f.close()
The error:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11208/2663596324.py in
26 for stock in dow_list:
27 # Download historical data for past year
---> 28 data = si.get_data(stock, start_date=start_date, end_date=end_date)
29 last_price = data["close"][-1]
30 # Get 150 and 20 simple moving averages using Talib
~\anaconda3\envs\PyFinance\lib\site-packages\yahoo_fin\stock_info.py in get_data(ticker, start_date, end_date, index_as_date, interval, headers)
98
99 # get the date info
--> 100 temp_time = data["chart"]["result"][0]["timestamp"]
101
102 if interval != "1m":
KeyError: 'timestamp'
The code is working with si.tickers_dow() and si.tickers_sp500() , but not with si.tickers_nasdaq() .
Not sure if a dataframe issue.
Code:
from datetime import date
from datetime import timedelta
from nsepy import get_history
import pandas as pd
end1 = date.today()
start1 = end1 - timedelta(days=25)
exp_date1 = date(2022,8,25)
exp_date2 = date(2022,9,29)
# stock = ['HDFCLIFE']
stock = ['RELIANCE','HDFCBANK','INFY','ICICIBANK','HDFC','TCS','KOTAKBANK','LT','SBIN','HINDUNILVR','AXISBANK',
'ITC','BAJFINANCE','BHARTIARTL','ASIANPAINT','HCLTECH','MARUTI','TITAN','BAJAJFINSV','TATAMOTORS',
'TECHM','SUNPHARMA','TATASTEEL','M&M','WIPRO','ULTRACEMCO','POWERGRID','HINDALCO','NTPC','NESTLEIND',
'GRASIM','ONGC','JSWSTEEL','HDFCLIFE','INDUSINDBK','SBILIFE','DRREDDY','ADANIPORTS','DIVISLAB','CIPLA',
'BAJAJ-AUTO','TATACONSUM','UPL','BRITANNIA','BPCL','EICHERMOT','HEROMOTOCO','COALINDIA','SHREECEM','IOC']
target_stocks = []
# oi_change = []
for stock in stock:
stock_jan = get_history(symbol=stock,
start=start1,
end=end1,
futures=True,
expiry_date=exp_date1)
stock_feb = get_history(symbol=stock,
start=start1,
end=end1,
futures=True,
expiry_date=exp_date2)
delivery_per_age = get_history(symbol=stock,
start=start1,
end=end1)
symbol_s = get_history(symbol=stock,
start=start1,
end=end1)
oi_combined = pd.concat([stock_jan['Change in OI'] + stock_feb['Change in OI']])
total_oi = pd.concat([stock_jan['Open Interest']+stock_feb['Open Interest']])
delivery_vol = pd.concat([delivery_per_age['Deliverable Volume']])
# delivery_per = pd.concat([delivery_per_age['%Deliverble']*100])
na_me = pd.concat([symbol_s['Symbol']])
close = pd.concat([delivery_per_age['Close']])
df = pd.DataFrame(na_me)
df['TOTAL_OPN_INT'] = total_oi
df['OI_COMBINED'] = oi_combined
df['%_CHANGE'] = ((df['OI_COMBINED'] / df['TOTAL_OPN_INT']) * 100).__round__(2)
df['AVG_OI_COMBINED'] = df['OI_COMBINED'].rolling(5).mean()
# df['DELIVERY_VOL'] = delivery_vol
# df['AVG_DELIVERY_VOL'] = df['DELIVERY_VOL'].rolling(5).mean()
# df['DELIVERY_PER'] = delivery_per
# df['AVG_DELIVERY_%'] = df['DELIVERY_PER'].rolling(5).mean()
df['_CLOSE_PRICE_'] = close
pd.set_option('display.max_columns',8)
pd.set_option('display.width',200)
# print(df)
cond = ((df.loc[df.index[-5:-1], '%_CHANGE'].agg(min) > 0) |(df.loc[df.index[-6:-1], '%_CHANGE'].agg(min) > 0)) & (df.loc[df.index[-1], '%_CHANGE'] < 0)
if(cond):
target_stocks.append(df)
print(target_stocks)
PRODUCT:
[ Symbol TOTAL_OPN_INT OI_COMBINED %_CHANGE AVG_OI_COMBINED _CLOSE_PRICE_
Date
2022-07-19 HINDUNILVR 1015800 313200 30.83 NaN 2567.95
2022-07-20 HINDUNILVR 1617900 602100 37.21 NaN 2604.50
2022-07-21 HINDUNILVR 2355000 737100 31.30 NaN 2607.45
2022-07-22 HINDUNILVR 3671400 1316400 35.86 NaN 2640.60
2022-07-25 HINDUNILVR 5421300 1749900 32.28 943740.0 2623.60
2022-07-26 HINDUNILVR 6886200 1464900 21.27 1174080.0 2547.10
2022-07-27 HINDUNILVR 8522700 1636500 19.20 1380960.0 2581.95
2022-07-28 HINDUNILVR 10300200 1777500 17.26 1589040.0 2620.10
2022-07-29 HINDUNILVR 10250100 -50100 -0.49 1315740.0 2637.40
2022-08-01 HINDUNILVR 10237200 -12900 -0.13 963180.0 2593.00
2022-08-02 HINDUNILVR 10178700 -58500 -0.57 658500.0 2635.25
2022-08-03 HINDUNILVR 10208400 29700 0.29 337140.0 2626.35
2022-08-04 HINDUNILVR 10289700 81300 0.79 -2100.0 2627.95
2022-08-05 HINDUNILVR 10334100 44400 0.43 16800.0 2645.40
2022-08-08 HINDUNILVR 10350000 15900 0.15 22560.0 2650.35
2022-08-10 HINDUNILVR 10422900 72900 0.70 48840.0 2642.80
2022-08-11 HINDUNILVR 10432800 9900 0.09 44880.0 2613.70
2022-08-12 HINDUNILVR 10378200 -54600 -0.53 17700.0 2594.95]
Process finished with exit code 0.
Problem:
When I ran the code on 12-aug I got this output as displayed above which is a list. So how can I convert that list of target_stocks into pandas dataframe.
when I tried using df2 = pd.Dataframe(target_stocks) it is throwing an error must pass 2-d input. shape(4,18,16).
You are appending a dataframe to an empty list. This method does not work for dataframes. Instead of having target_stocks = [] make it target_stocks = pd.DataFrame() (an empty dataframe). Then change:
if(cond):
target_stocks.append(df)
to
if(cond):
target_stocks = pd.concat([target_stocks, df])
To add a blank row at the end of the dataframe if the condition is met, add the code below. This finds the length of your data frame and adds a blank row (created by placing an empty value in every column):
target_stocks.loc[len(target_stocks)]=['']*len(target_stocks.columns)
All together:
from datetime import date
from datetime import timedelta
from nsepy import get_history
import pandas as pd
end1 = date.today()
start1 = end1 - timedelta(days=25)
exp_date1 = date(2022,8,25)
exp_date2 = date(2022,9,29)
# stock = ['HDFCLIFE']
stock = ['RELIANCE','HDFCBANK','INFY','ICICIBANK','HDFC','TCS','KOTAKBANK','LT','SBIN','HINDUNILVR','AXISBANK',
'ITC','BAJFINANCE','BHARTIARTL','ASIANPAINT','HCLTECH','MARUTI','TITAN','BAJAJFINSV','TATAMOTORS',
'TECHM','SUNPHARMA','TATASTEEL','M&M','WIPRO','ULTRACEMCO','POWERGRID','HINDALCO','NTPC','NESTLEIND',
'GRASIM','ONGC','JSWSTEEL','HDFCLIFE','INDUSINDBK','SBILIFE','DRREDDY','ADANIPORTS','DIVISLAB','CIPLA',
'BAJAJ-AUTO','TATACONSUM','UPL','BRITANNIA','BPCL','EICHERMOT','HEROMOTOCO','COALINDIA','SHREECEM','IOC']
target_stocks = pd.DataFrame()
# oi_change = []
for stock in stock:
stock_jan = get_history(symbol=stock,
start=start1,
end=end1,
futures=True,
expiry_date=exp_date1)
stock_feb = get_history(symbol=stock,
start=start1,
end=end1,
futures=True,
expiry_date=exp_date2)
delivery_per_age = get_history(symbol=stock,
start=start1,
end=end1)
symbol_s = get_history(symbol=stock,
start=start1,
end=end1)
oi_combined = pd.concat([stock_jan['Change in OI'] + stock_feb['Change in OI']])
total_oi = pd.concat([stock_jan['Open Interest']+stock_feb['Open Interest']])
delivery_vol = pd.concat([delivery_per_age['Deliverable Volume']])
# delivery_per = pd.concat([delivery_per_age['%Deliverble']*100])
na_me = pd.concat([symbol_s['Symbol']])
close = pd.concat([delivery_per_age['Close']])
df = pd.DataFrame(na_me)
df['TOTAL_OPN_INT'] = total_oi
df['OI_COMBINED'] = oi_combined
df['%_CHANGE'] = ((df['OI_COMBINED'] / df['TOTAL_OPN_INT']) * 100).__round__(2)
df['AVG_OI_COMBINED'] = df['OI_COMBINED'].rolling(5).mean()
# df['DELIVERY_VOL'] = delivery_vol
# df['AVG_DELIVERY_VOL'] = df['DELIVERY_VOL'].rolling(5).mean()
# df['DELIVERY_PER'] = delivery_per
# df['AVG_DELIVERY_%'] = df['DELIVERY_PER'].rolling(5).mean()
df['_CLOSE_PRICE_'] = close
pd.set_option('display.max_columns',8)
pd.set_option('display.width',200)
# print(df)
cond = ((df.loc[df.index[-5:-1], '%_CHANGE'].agg(min) > 0) |(df.loc[df.index[-6:-1], '%_CHANGE'].agg(min) > 0)) & (df.loc[df.index[-1], '%_CHANGE'] < 0)
if(cond):
target_stocks = pd.concat([target_stocks, df])
target_stocks.loc[len(target_stocks)]=['']*len(target_stocks.columns)
target_stocks
Output:
I am making a tkinter app where upon calling the function Add_Task_Timing(), it creates a temporary dataframe inside a method of the class Task. The following is format of the code:
# Makes a global dataframe
Task_Dataframe = pd.DataFrame()
Task_Dataframe.loc[0, 'Task_Name'] = [None]
Task_Dataframe.loc[0, 'Start_Time'] = [None]
Task_Dataframe.loc[0, 'End_Time'] = [None]
Task_Dataframe.loc[0, 'Duration'] = [None]
Task_Dataframe.loc[0, 'Team_Member'] = [None]
class Task():
def __init__(self,task_name, start_time, end_time):
# THIS METHOD CREATES ATTRIBUTES OF THE TASK
self.task_name = task_name
self.start_time = start_time
self.end_time = end_time
def Add_Task_Timing (self):
# THIS METHOD ADDS THE TASK TO THE DATAFRAME
# column_names = ["Task_Name", "Start_Time","End_Time","Duration", "Team_Member"]
temporary_df = pd.DataFrame()
temporary_df.loc[0, 'Task_Name'] = [task_name]
temporary_df.loc[0, 'Start_Time'] = [start_time]
temporary_df.loc[0, 'End_Time'] = [end_time]
temporary_df.loc[0, 'Duration'] = [end_time - start_time]
temporary_df.loc[0, 'Team_Member'] = [None]
print(temporary_df)
Task_Dataframe.join(temporary_df, lsuffix='_MAIN', rsuffix='_TEMP')
return Task_Dataframe
My only problem is that the manipulation inside the method Add_Task_Timing() does not happen to the global dataframe Task_Dataframe. For some reason the output by returning the method Add_Task_Timing() shows:
Task_Name Start_Time End_Time Duration Team_Member
0 NaN NaN NaN NaN NaN
I understand that for some reason the method is not updating the values defined for the global dataframe. I have tried concat(),join(), append(). None seem to do the trick.
I added the following lines:
global Task_Dataframe
Task_Dataframe = Task_Dataframe.join(temporary_df, lsuffix='_M', rsuffix='_T')
When the temporary_df is:
Task_Name Start_Time End_Time Duration Team_Member
0 tasking 23.0 43.0 20.0 NaN
The result of temporary_df is:
Task_Name_Main Start_Time_Main End_Time_Main Duration_Main ... Start_Time_Temp End_Time_Temp Duration_Temp Team_Member_Temp
0 NaN NaN NaN NaN ... 43.0 23.0 20.0 NaN
I am still trying to figure out why it won't update the values in the global dataframe Task_Dataframe.
This code below gets us the messages count for every 30 days from the time period of message sent.
This code gets us:(In-detail)
1.Amazon first mail to my mail with a particular phase(here first order).
2.Convert that epoch format into time date and using timedelta And getting count of mails sent in the interval of 30 days.
The output for this code will be like this:
Amazon first order:
1534476682000
Amazon total orders between 2018-08-01 and 2018-09-01: 20
Amazon total orders between 2018-09-01 and 2018-10-01: 11
Amazon total orders between 2018-10-01 and 2018-11-01: 15
Amazon total orders between 2018-11-01 and 2018-12-01: 7
Amazon total orders between 2018-12-01 and 2019-01-01: 19
Amazon total orders between 2019-01-01 and 2019-02-01: 23
Amazon total orders between 2019-02-01 and 2019-03-01: 12
Code:
#amazonfirstorder
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from dateutil.relativedelta import relativedelta
from datetime import datetime
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
results = service.users().messages().list(userId='me', q='from:auto-confirm#amazon.in subject:(your amazon.in order of )',labelIds = ['INBOX']).execute()
messages = results.get('messages', [])
print('\nFilpkart first order:')
if not messages:
print (" ")
else:
print (" ")
msg = service.users().messages().get(userId='me', id=messages[-1]['id']).execute()
#print(msg['snippet'])
a=(msg['internalDate'])
ts = int(a)
ts /= 1000
year=int(datetime.utcfromtimestamp(ts).strftime('%Y'))
month=int(datetime.utcfromtimestamp(ts).strftime('%m'))
#print(year)
#print(month)
print(msg['internalDate'])
log_results = []
start_date = datetime(year,month,1)
#start_date = datetime(2016,1,1)
end_date = datetime.today()
increment = relativedelta(months=1)
target_date = start_date + increment
while target_date <= end_date:
timestamp_after = int(start_date.timestamp()) # timestamp of start day
timestamp_before = int(target_date.timestamp()) # timestamp of start day + 30 days
query = f'from:(auto-confirm#amazon.in) subject:(your amazon.in order of ) after:{timestamp_after} before:{timestamp_before}'
results = service.users().messages().list(userId='me', q=query, labelIds=['INBOX']).execute()
messages = results.get('messages', [])
orders = len(messages)
start_date_str = start_date.strftime('%Y-%m-%d')
target_date_str = target_date.strftime('%Y-%m-%d')
print(f"\nFlipkart total orders between {start_date.strftime('%Y-%m-%d')} and {target_date.strftime('%Y-%m-%d')}: {orders}")
log_results.append(dict(start=start_date_str, end=target_date_str, orders=orders))
# update interval
start_date += increment
target_date += increment
return log_results
if __name__ == '__main__':
log_results = main()
Now i have two problems :
First
How to save the output of that code into csv file.
Second:
The above code gets us 30 days mails count,What i need is i need the count of mails i received before 12.00 PM in month-wise and after 12 PM in month-wise and save them in csv.
OUTPUT i need for 2nd Problem :
Amazon total orders between 2018-09-01 and 2018-10-01 before 12:00 PM : 11
Amazon total orders between 2018-10-01 and 2018-11-01 before 12:00 PM : 15
Amazon total orders between 2018-11-01 and 2018-12-01 before 12:00 PM : 7
Amazon total orders between 2018-12-01 and 2019-01-01 before 12:00 PM : 19
Amazon total orders between 2018-09-01 and 2018-10-01 after 12:00 PM : 3
Amazon total orders between 2018-10-01 and 2018-11-01 after 12:00 PM : 6
Amazon total orders between 2018-11-01 and 2018-12-01 after 12:00 PM : 88
Amazon total orders between 2018-12-01 and 2019-01-01 after 12:00 PM : 26
Similar to what already proposed, but in this case you would calculate the increment as exactly one month instead of 30 days (see the use of relativedelta instead of timedelta):
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from dateutil.relativedelta import relativedelta
from datetime import datetime
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
log_results = []
start_date = datetime(2016, 1, 1)
end_date = datetime.today()
increment = relativedelta(months=1)
target_date = start_date + increment
while target_date <= end_date:
timestamp_after = int(start_date.timestamp()) # timestamp of start day
timestamp_before = int(target_date.timestamp()) # timestamp of start day + 30 days
query = f'from:(auto-confirm#amazon.in) subject:(your amazon.in order of ) after:{timestamp_after} before:{timestamp_before}'
results = service.users().messages().list(userId='me', q=query, labelIds=['INBOX']).execute()
messages = results.get('messages', [])
orders = len(messages)
start_date_str = start_date.strftime('%Y-%m-%d')
target_date_str = target_date.strftime('%Y-%m-%d')
print(f"\nAmazon total orders between {start_date.strftime('%Y-%m-%d')} and {target_date.strftime('%Y-%m-%d')}: {orders}")
log_results.append(dict(start=start_date_str, end=target_date_str, orders=orders))
# update interval
start_date += increment
target_date += increment
return log_results
if __name__ == '__main__':
log_results = main()
# Write to csv
import pandas as pd
df = pd.DataFrame(log_results)
df.to_csv('orders.csv')
You will just need to loop through the dates with the interval you want.
The code below retrieves the messages of a user from a particular period of time, such as the month messages count.
You will need help to automate it to retrieve messages count for every 30 days.
For example, this code gets the messages from Jan 1, 2016 to Jan 30, 2016.
So from Jan 1, 2016 to Jan 1, 2019 you will need to automate it in a regular interval of 30 days.
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time
from dateutil.relativedelta import relativedelta
from datetime import datetime
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
end_date = datetime(2019, 1, 1)
interval = relativedelta(months=1)
current = datetime(2016, 1, 1) # init to the start date
while current < end_date + interval:
after = current.timestamp()
before = (current + interval).timestamp()
query = 'from:(auto-confirm#amazon.in) subject:(your amazon.in order of ) after:{} before:{}'.format(after, before)
results = service.users().messages().list(userId='me', q=query, labelIds = ['INBOX']).execute()
messages = results.get('messages', [])
print("\namazon total orders in {}: {}".format(current.strftime('%B %Y'), len(messages)))
current += interval
if __name__ == '__main__':
main()
I'm struggling with the results of a ScannerSubscription.
For example, if I request:
qqq_id = 0
subscript = ScannerSubscription()
subscript.numberOfRows(15)
subscript.m_scanCode = 'HIGH_OPEN_GAP'
subscript.m_instrument = 'STK'
subscript.m_averageOptionVolumeAbove = ''
subscript.m_couponRateAbove = ''
subscript.m_couponRateBelow = ''
subscript.m_abovePrice = '5'
subscript.m_belowPrice = ''
subscript.m_marketCapAbove = ''
subscript.m_marketCapBelow = ''
subscript.m_aboveVolume = '100000'
subscript.m_stockTypeFilter = 'ALL'
subscript.locationCode('STK.US.MAJOR')
tws_conn.reqScannerSubscription(qqq_id, subscript)
tws_conn.reqScannerParameters()
I received a scannerData response like this:
<scannerData reqId=0, rank=0, contractDetails=<ib.ext.ContractDetails.ContractDetails object at 0x00000000036EFA58>, distance=None, benchmark=None, projection=None, legsStr=None>
etc...
But I cannot retrieve the result values, for example:
reqScannerParameters() xml result specifies <colId>390</colId> as the colId for the Gap value:
<ScanType>
<displayName>Top Close-to-Open % Gainers</displayName>
<scanCode>HIGH_OPEN_GAP</scanCode>
<instruments>STK,STOCK.NA,STOCK.EU,STOCK.HK,FUT.US,FUT.HK,FUT.EU,FUT.NA</instruments>
<absoluteColumns>false</absoluteColumns>
<Columns varName="columns">
<Column>
<colId>390</colId>
<name>Gap</name>
<display>true</display>
<section>m</section>
<displayType>DATA</displayType>
</Column>
How do I retrieve the GAP value?
Is this even possible ?
Now I'm sure you're supposed to request data after getting the contract.
import pandas as pd
scans = 15
res = pd.DataFrame(index = range(scans), columns = ['sym','open','close','calc']).fillna(0)
msgs = []
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from ib.ext.TickType import TickType as tt
def tickPrice(msg):
global scans
if msg.field in [tt.OPEN, tt.CLOSE]:
res.loc[msg.tickerId,tt.getField(msg.field)] = msg.price
op = res.loc[msg.tickerId,'open']
cl = res.loc[msg.tickerId,'close']
if op > 0 and cl > 0 and res.loc[msg.tickerId,'calc'] == 0:
res.loc[msg.tickerId,'calc'] = ((op-cl)*100/cl)
con.cancelMktData(msg.tickerId)
scans -= 1
if scans == 0:
print(res)
con.disconnect()
def snapshot(msg):
res.loc[msg.rank,'sym'] = msg.contractDetails.m_summary.m_symbol
#tt.OPEN (14) isn't coming with snapshot
con.reqMktData(str(msg.rank), msg.contractDetails.m_summary, "", False)
def watcher(msg):
#print (msg)
msgs.append(msg)
def scanData(msg):
snapshot(msg)
def scanDataEnd(msg):
con.cancelScannerSubscription(qqq_id)
con = ibConnection(port=7497, clientId=888)
con.registerAll(watcher)
con.unregister(watcher, message.scannerData)
con.register(scanData, message.scannerData)
con.unregister(watcher, message.scannerDataEnd)
con.register(scanDataEnd, message.scannerDataEnd)
con.unregister(watcher, message.tickPrice)
con.register(tickPrice, message.tickPrice)
con.connect()
from ib.ext.ScannerSubscription import ScannerSubscription
qqq_id = 0
subscript = ScannerSubscription()
subscript.numberOfRows(15)
subscript.m_scanCode = 'HIGH_OPEN_GAP'
subscript.m_instrument = 'STK'
subscript.m_averageOptionVolumeAbove ='0'
subscript.m_abovePrice = '5'
subscript.m_aboveVolume = '100000'
con.reqScannerSubscription(qqq_id, subscript)
res at 1 pm est =
sym open close calc
0 TAC 4.95 4.25 16.470588
1 CTRP 44.80 40.99 9.294950
2 IIIN 39.26 36.58 7.326408
3 LFC 14.60 13.63 7.116654
4 ACH 11.59 10.87 6.623735
5 KALV 9.01 8.38 7.517900
6 OMER 13.25 12.75 3.921569
7 DWTI 68.00 66.50 2.255639
8 WLDN 23.75 23.43 1.365770
9 BZQ 19.67 18.73 5.018687
10 JNUG 6.55 6.43 1.866252
11 GXP PRB 50.78 49.80 1.967871
12 AU 10.85 10.59 2.455146
13 USLV 13.07 12.81 2.029664
14 CBD 16.60 16.03 3.555833
I don't know why they don't come in rank order??