Filtering OTC market data
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
It's been one month I do programming in python. Hence, my experience is a bit limited. Here is what I've done so far. The program is only filtering OTC market data according to certain features. Could anyone be able to refactor and improve the performance of the following code? Is it possible to use a single class here?
import pandas as pd
import requests
import json
import csv
token =
"access_token": "abcdefghijklmnopABCDEFGHIJKLMNOP1",
"api_server": "https://api05.iq.questrade.com/",
"expires_in": 1800,
"refresh_token": "QrStUvWxYz_0123456789abcdefgHIJK",
"token_type": "Bearer"
uri = "v1/symbols?names=".format(token.get('api_server'))
headers = 'Authorization': 'Bearer '.format(token.get('access_token'))
# Reformat the CSV file to keep only the symbols
path = 'OTCBB.csv'
df = pd.read_csv(path, usecols=['Symbol'])
df['Symbol'] = df['Symbol'].map(lambda x: x.lstrip('.OB').rstrip('.OB'))
df.to_csv(path, index=False)
def batch(batch_size=100, count=0):
"""
Build chunks of batch_size by default =100
"""
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
result =
string = ''
for index, row in enumerate(rows[1:]):
if count >= batch_size or index == len(rows[1:])-1:
result.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string,row))
else:
string = ','.join((string,row))
count += 1
return result
batch = batch()
def symbolIds():
"""
Get the OTC symbolIds
"""
another_listing =
for i in batch:
rt = requests.get(''.join((uri,i)), headers=headers)
another_listing.extend(rt.json().get('symbols'))
return [element['symbolId'] for element in another_listing]
symbolIds = symbolIds()
def batch_symbolIds(batch_size=100, count=0):
"""
Build symbolIds chunks
"""
batch_symbolIds =
string = ''
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
for index, row in enumerate(symbolIds[1:]):
if count >= batch_size or index == len(rows[1:])-1:
batch_symbolIds.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string, str(row)))
else:
string = ','.join((string, str(row)))
count += 1
return batch_symbolIds
batch_symbolIds = batch_symbolIds()
def raw_output():
"""
Gathering information, i.e., volume, bid, ask, bidsize, asksize
"""
result =
uri2 = "v1/markets/quotes?ids=".format(token.get('api_server'))
for elem in batch_symbolIds:
rt = requests.get(''.join((uri2, elem)), headers=headers)
result.extend(rt.json().get('quotes'))
return result
raw_output = raw_output()
def filtering_otc_stocks():
"""
Filter the OTC market data according to
certain features, i.e. volume, BAS, bid, ask, ...
"""
for elem in raw_output:
try:
chg = (elem['lastTradePrice'] - elem['openPrice']) / elem['openPrice']*100
except:
chg = 0
elem.update("% chg": chg)
columns = ["symbol", "% chg", "volume", "bidPrice", "bidSize", "askPrice", "askSize",
"lastTradePrice", "lastTradeSize", "lowPrice", "highPrice", "openPrice"]
df = pd.DataFrame(raw_output, columns=columns)
df = df.loc[(df["lastTradePrice"] <= 5) & (df["lastTradePrice"] >= 0.001)] #LastPrice must be less or equal to 5$
df = df.loc[(df.volume >= 500000) & (df.volume <= 50000000)]
df = df.sort_values('% chg', ascending=False)
df.to_csv('OTCList.csv', index=False)
if __name__ == "__main__":
filtering_otc_stocks()
python beginner json csv pandas
add a comment |Â
up vote
3
down vote
favorite
It's been one month I do programming in python. Hence, my experience is a bit limited. Here is what I've done so far. The program is only filtering OTC market data according to certain features. Could anyone be able to refactor and improve the performance of the following code? Is it possible to use a single class here?
import pandas as pd
import requests
import json
import csv
token =
"access_token": "abcdefghijklmnopABCDEFGHIJKLMNOP1",
"api_server": "https://api05.iq.questrade.com/",
"expires_in": 1800,
"refresh_token": "QrStUvWxYz_0123456789abcdefgHIJK",
"token_type": "Bearer"
uri = "v1/symbols?names=".format(token.get('api_server'))
headers = 'Authorization': 'Bearer '.format(token.get('access_token'))
# Reformat the CSV file to keep only the symbols
path = 'OTCBB.csv'
df = pd.read_csv(path, usecols=['Symbol'])
df['Symbol'] = df['Symbol'].map(lambda x: x.lstrip('.OB').rstrip('.OB'))
df.to_csv(path, index=False)
def batch(batch_size=100, count=0):
"""
Build chunks of batch_size by default =100
"""
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
result =
string = ''
for index, row in enumerate(rows[1:]):
if count >= batch_size or index == len(rows[1:])-1:
result.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string,row))
else:
string = ','.join((string,row))
count += 1
return result
batch = batch()
def symbolIds():
"""
Get the OTC symbolIds
"""
another_listing =
for i in batch:
rt = requests.get(''.join((uri,i)), headers=headers)
another_listing.extend(rt.json().get('symbols'))
return [element['symbolId'] for element in another_listing]
symbolIds = symbolIds()
def batch_symbolIds(batch_size=100, count=0):
"""
Build symbolIds chunks
"""
batch_symbolIds =
string = ''
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
for index, row in enumerate(symbolIds[1:]):
if count >= batch_size or index == len(rows[1:])-1:
batch_symbolIds.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string, str(row)))
else:
string = ','.join((string, str(row)))
count += 1
return batch_symbolIds
batch_symbolIds = batch_symbolIds()
def raw_output():
"""
Gathering information, i.e., volume, bid, ask, bidsize, asksize
"""
result =
uri2 = "v1/markets/quotes?ids=".format(token.get('api_server'))
for elem in batch_symbolIds:
rt = requests.get(''.join((uri2, elem)), headers=headers)
result.extend(rt.json().get('quotes'))
return result
raw_output = raw_output()
def filtering_otc_stocks():
"""
Filter the OTC market data according to
certain features, i.e. volume, BAS, bid, ask, ...
"""
for elem in raw_output:
try:
chg = (elem['lastTradePrice'] - elem['openPrice']) / elem['openPrice']*100
except:
chg = 0
elem.update("% chg": chg)
columns = ["symbol", "% chg", "volume", "bidPrice", "bidSize", "askPrice", "askSize",
"lastTradePrice", "lastTradeSize", "lowPrice", "highPrice", "openPrice"]
df = pd.DataFrame(raw_output, columns=columns)
df = df.loc[(df["lastTradePrice"] <= 5) & (df["lastTradePrice"] >= 0.001)] #LastPrice must be less or equal to 5$
df = df.loc[(df.volume >= 500000) & (df.volume <= 50000000)]
df = df.sort_values('% chg', ascending=False)
df.to_csv('OTCList.csv', index=False)
if __name__ == "__main__":
filtering_otc_stocks()
python beginner json csv pandas
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
It's been one month I do programming in python. Hence, my experience is a bit limited. Here is what I've done so far. The program is only filtering OTC market data according to certain features. Could anyone be able to refactor and improve the performance of the following code? Is it possible to use a single class here?
import pandas as pd
import requests
import json
import csv
token =
"access_token": "abcdefghijklmnopABCDEFGHIJKLMNOP1",
"api_server": "https://api05.iq.questrade.com/",
"expires_in": 1800,
"refresh_token": "QrStUvWxYz_0123456789abcdefgHIJK",
"token_type": "Bearer"
uri = "v1/symbols?names=".format(token.get('api_server'))
headers = 'Authorization': 'Bearer '.format(token.get('access_token'))
# Reformat the CSV file to keep only the symbols
path = 'OTCBB.csv'
df = pd.read_csv(path, usecols=['Symbol'])
df['Symbol'] = df['Symbol'].map(lambda x: x.lstrip('.OB').rstrip('.OB'))
df.to_csv(path, index=False)
def batch(batch_size=100, count=0):
"""
Build chunks of batch_size by default =100
"""
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
result =
string = ''
for index, row in enumerate(rows[1:]):
if count >= batch_size or index == len(rows[1:])-1:
result.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string,row))
else:
string = ','.join((string,row))
count += 1
return result
batch = batch()
def symbolIds():
"""
Get the OTC symbolIds
"""
another_listing =
for i in batch:
rt = requests.get(''.join((uri,i)), headers=headers)
another_listing.extend(rt.json().get('symbols'))
return [element['symbolId'] for element in another_listing]
symbolIds = symbolIds()
def batch_symbolIds(batch_size=100, count=0):
"""
Build symbolIds chunks
"""
batch_symbolIds =
string = ''
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
for index, row in enumerate(symbolIds[1:]):
if count >= batch_size or index == len(rows[1:])-1:
batch_symbolIds.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string, str(row)))
else:
string = ','.join((string, str(row)))
count += 1
return batch_symbolIds
batch_symbolIds = batch_symbolIds()
def raw_output():
"""
Gathering information, i.e., volume, bid, ask, bidsize, asksize
"""
result =
uri2 = "v1/markets/quotes?ids=".format(token.get('api_server'))
for elem in batch_symbolIds:
rt = requests.get(''.join((uri2, elem)), headers=headers)
result.extend(rt.json().get('quotes'))
return result
raw_output = raw_output()
def filtering_otc_stocks():
"""
Filter the OTC market data according to
certain features, i.e. volume, BAS, bid, ask, ...
"""
for elem in raw_output:
try:
chg = (elem['lastTradePrice'] - elem['openPrice']) / elem['openPrice']*100
except:
chg = 0
elem.update("% chg": chg)
columns = ["symbol", "% chg", "volume", "bidPrice", "bidSize", "askPrice", "askSize",
"lastTradePrice", "lastTradeSize", "lowPrice", "highPrice", "openPrice"]
df = pd.DataFrame(raw_output, columns=columns)
df = df.loc[(df["lastTradePrice"] <= 5) & (df["lastTradePrice"] >= 0.001)] #LastPrice must be less or equal to 5$
df = df.loc[(df.volume >= 500000) & (df.volume <= 50000000)]
df = df.sort_values('% chg', ascending=False)
df.to_csv('OTCList.csv', index=False)
if __name__ == "__main__":
filtering_otc_stocks()
python beginner json csv pandas
It's been one month I do programming in python. Hence, my experience is a bit limited. Here is what I've done so far. The program is only filtering OTC market data according to certain features. Could anyone be able to refactor and improve the performance of the following code? Is it possible to use a single class here?
import pandas as pd
import requests
import json
import csv
token =
"access_token": "abcdefghijklmnopABCDEFGHIJKLMNOP1",
"api_server": "https://api05.iq.questrade.com/",
"expires_in": 1800,
"refresh_token": "QrStUvWxYz_0123456789abcdefgHIJK",
"token_type": "Bearer"
uri = "v1/symbols?names=".format(token.get('api_server'))
headers = 'Authorization': 'Bearer '.format(token.get('access_token'))
# Reformat the CSV file to keep only the symbols
path = 'OTCBB.csv'
df = pd.read_csv(path, usecols=['Symbol'])
df['Symbol'] = df['Symbol'].map(lambda x: x.lstrip('.OB').rstrip('.OB'))
df.to_csv(path, index=False)
def batch(batch_size=100, count=0):
"""
Build chunks of batch_size by default =100
"""
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
result =
string = ''
for index, row in enumerate(rows[1:]):
if count >= batch_size or index == len(rows[1:])-1:
result.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string,row))
else:
string = ','.join((string,row))
count += 1
return result
batch = batch()
def symbolIds():
"""
Get the OTC symbolIds
"""
another_listing =
for i in batch:
rt = requests.get(''.join((uri,i)), headers=headers)
another_listing.extend(rt.json().get('symbols'))
return [element['symbolId'] for element in another_listing]
symbolIds = symbolIds()
def batch_symbolIds(batch_size=100, count=0):
"""
Build symbolIds chunks
"""
batch_symbolIds =
string = ''
with open(path, 'r') as csvfile:
rows=[row.strip('n') for row in csvfile]
for index, row in enumerate(symbolIds[1:]):
if count >= batch_size or index == len(rows[1:])-1:
batch_symbolIds.append(string)
string = ''
count = 0
if count == 0:
string = ''.join((string, str(row)))
else:
string = ','.join((string, str(row)))
count += 1
return batch_symbolIds
batch_symbolIds = batch_symbolIds()
def raw_output():
"""
Gathering information, i.e., volume, bid, ask, bidsize, asksize
"""
result =
uri2 = "v1/markets/quotes?ids=".format(token.get('api_server'))
for elem in batch_symbolIds:
rt = requests.get(''.join((uri2, elem)), headers=headers)
result.extend(rt.json().get('quotes'))
return result
raw_output = raw_output()
def filtering_otc_stocks():
"""
Filter the OTC market data according to
certain features, i.e. volume, BAS, bid, ask, ...
"""
for elem in raw_output:
try:
chg = (elem['lastTradePrice'] - elem['openPrice']) / elem['openPrice']*100
except:
chg = 0
elem.update("% chg": chg)
columns = ["symbol", "% chg", "volume", "bidPrice", "bidSize", "askPrice", "askSize",
"lastTradePrice", "lastTradeSize", "lowPrice", "highPrice", "openPrice"]
df = pd.DataFrame(raw_output, columns=columns)
df = df.loc[(df["lastTradePrice"] <= 5) & (df["lastTradePrice"] >= 0.001)] #LastPrice must be less or equal to 5$
df = df.loc[(df.volume >= 500000) & (df.volume <= 50000000)]
df = df.sort_values('% chg', ascending=False)
df.to_csv('OTCList.csv', index=False)
if __name__ == "__main__":
filtering_otc_stocks()
python beginner json csv pandas
edited Feb 27 at 22:13
asked Feb 27 at 18:53
J.Doe
162
162
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33
add a comment |Â
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188473%2ffiltering-otc-market-data%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you post a few lines of the data you're parsing?
â Tony
Feb 28 at 18:33