Home >  > 使用Python进行程序化交易(一)

使用Python进行程序化交易(一)

Algorithmic Trading Using Python
01:37 OK

项目介绍:
利用iex api获取S&P 500的相关信息,并以excel文件的形式储存到本地。

学到的知识点:
1.将账户、Token等敏感信息储存到文件中,然后在程序中读取出来。
2.将列表分为几十个一个的“块”。
3.批量从API获取数据,大大提高获取数据的速度。
4.对用户输入的数据进行类型判断。
5.将数据储存到excel文件时进行格式化,美化表格。

一、申请iex api
在https://iexcloud.io/docs/api/申请一个Token,并且将token储存到secrets.py文件中。secrets.py文件的内容为:

IEX_CLOUD_API_TOKEN = 'Tpk_059b97af715d417d9f49f50b51b1cxxx'

二、获取单个股票信息

import numpy as np #The Numpy numerical computing library
import pandas as pd #The Pandas data science library
import requests #The requests library for HTTP requests in Python
import xlsxwriter #The XlsxWriter libarary for 
import math #The Python math module

from secrets import IEX_CLOUD_API_TOKEN  #import Token from secrets.py file 

stocks = pd.read_csv('sp_500_stocks.csv')
my_columns = ['Ticker', 'Price','Market Capitalization', 'Number Of Shares to Buy']

symbol='AAPL'
api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
data = requests.get(api_url).json()
final_dataframe = pd.DataFrame(columns = my_columns)
final_dataframe = final_dataframe.append(
                                        pd.Series(['AAPL', 
                                                   data['latestPrice'], 
                                                   data['marketCap'], 
                                                   'N/A'], 
                                                  index = my_columns), 
                                        ignore_index = True)
print(final_dataframe)

运行结果

三、批量获取股票信息

import numpy as np #The Numpy numerical computing library
import pandas as pd #The Pandas data science library
import requests #The requests library for HTTP requests in Python
import xlsxwriter #The XlsxWriter libarary for 
import math #The Python math module

from secrets import IEX_CLOUD_API_TOKEN  #import Token from secrets.py file 

stocks = pd.read_csv('sp_500_stocks.csv')
my_columns = ['Ticker', 'Price','Market Capitalization', 'Number Of Shares to Buy']  #设定标题栏



# Function sourced from 
# https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


symbol_groups = list(chunks(stocks['Ticker'], 100))
symbol_strings = []
for i in range(0, len(symbol_groups)):
    symbol_strings.append(','.join(symbol_groups[i]))
#     print(symbol_strings[i])

final_dataframe = pd.DataFrame(columns = my_columns)

for symbol_string in symbol_strings:
#     print(symbol_strings)
    batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch/?types=quote&symbols={symbol_string}&token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(batch_api_call_url).json()
    for symbol in symbol_string.split(','):
        final_dataframe = final_dataframe.append(
                                        pd.Series([symbol, 
                                                   data[symbol]['quote']['latestPrice'], 
                                                   data[symbol]['quote']['marketCap'], 
                                                   'N/A'], 
                                                  index = my_columns), 
                                        ignore_index = True)


# print(final_dataframe)
portfolio_size = input("Enter the value of your portfolio:")

#判断输入的数据是否是数字
try:
    val = float(portfolio_size)
except ValueError:
    print("That's not a number! \n Try again:")
    portfolio_size = input("Enter the value of your portfolio:")

position_size = float(portfolio_size) / len(final_dataframe.index)
for i in range(0, len(final_dataframe['Ticker'])-1):
    final_dataframe.loc[i, 'Number Of Shares to Buy'] = math.floor(position_size / final_dataframe['Price'][i])

writer = pd.ExcelWriter('recommended_trades.xlsx', engine='xlsxwriter')
final_dataframe.to_excel(writer, sheet_name='Recommended Trades', index = False)

background_color = '#0a0a23'
font_color = '#ffffff'

string_format = writer.book.add_format(
        {
            'font_color': font_color,
            'bg_color': background_color,
            'border': 1
        }
    )

dollar_format = writer.book.add_format(
        {
            'num_format':'$0.00',
            'font_color': font_color,
            'bg_color': background_color,
            'border': 1
        }
    )

integer_format = writer.book.add_format(
        {
            'num_format':'0',
            'font_color': font_color,
            'bg_color': background_color,
            'border': 1
        }
    )

column_formats = { 
                    'A': ['Ticker', string_format],
                    'B': ['Price', dollar_format],
                    'C': ['Market Capitalization', dollar_format],
                    'D': ['Number of Shares to Buy', integer_format]
                    }

for column in column_formats.keys():
    writer.sheets['Recommended Trades'].set_column(f'{column}:{column}', 20, column_formats[column][1])
    writer.sheets['Recommended Trades'].write(f'{column}1', column_formats[column][0], string_format)

writer.save()

运行结果

source: https://www.youtube.com/watch?v=xfzGZB4HhEE

本文暂无标签