3
\$\begingroup\$

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()
\$\endgroup\$
  • \$\begingroup\$ Can you post a few lines of the data you're parsing? \$\endgroup\$ – Tony Feb 28 '18 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.