Filtering OTC market data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
2












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()






share|improve this question





















  • Can you post a few lines of the data you're parsing?
    – Tony
    Feb 28 at 18:33
















up vote
3
down vote

favorite
2












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()






share|improve this question





















  • Can you post a few lines of the data you're parsing?
    – Tony
    Feb 28 at 18:33












up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





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()






share|improve this question













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()








share|improve this question












share|improve this question




share|improve this question








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
















  • 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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation