Фильтрация внебиржевого рынка данных


Это был один месяц, я занимаюсь программированием на Python. Следовательно, мой опыт ограничен. Вот что я сделал до сих пор. Программа фильтрацию только внебиржевого рынка данных по определенным признакам. Может кто сможет оптимизировать и повысить производительность следующий код? Можно ли использовать здесь один класс?

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


144
3
задан 27 февраля 2018 в 06:02 Источник Поделиться
Комментарии