佐治亚理工学院 计算投资公开课第五周作业 市场仿真器

26 篇文章 0 订阅
3 篇文章 0 订阅

Computational Investing, Part I 
by Dr. Tucker Balch

这门课的讲授者给出了一个开源的python包QuantSoftware ToolKit,方便教学,虽然效率不高,但已经足够了。


前几周的作业都比较简单,因此没有发上来。这次要求给出一个市场仿真器,根据order给出各日的账户金额。
除此之外,可以分析相应的投资方案的各种参数,如日均回报率等,由于时间关系没有做。

本题要求使用命令行传参数,如:
python marketsim.py 1000000 orders.csv values.csv
使用sys模块的argv即可。

另外,QSTK中的close其实是 Adjusted Close ,而actual_close则是 actual close。
这里的调整是针对分红、股票分拆等进行调整。

程序内容为:
import pandas as pd
import numpy as np
import math
import copy
import QSTK.qstkutil.qsdateutil as du
import datetime as dt
import QSTK.qstkutil.DataAccess as da
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkstudy.EventProfiler as ep


#get order
#sys.argv to get comman parameter
na_data = np.loadtxt('orders2.csv',dtype=np.str,delimiter=',')
#dtype={'names':('year','month','day','equity','buorsell','count'),    'formats':('i4','i4','i4','S5','S5','i4')},
na_dates=np.int_(na_data[:,0:3])
order=na_data[:,3:6]
ls_symbols=set(order[:,0])

#get equity price
dt_start = dt.datetime(2011, 1, 1)
dt_end = dt.datetime(2011, 12, 31)
ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16))

dataobj = da.DataAccess('Yahoo')

#why close?
#close for Adjusted Close ;actual_close for actual close
ls_keys = 'close'#['open', 'high', 'low', 'close', 'volume', 'actual_close']
ldf_data = dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)

#calc portfolio
currentCash=1000000
currentEquity=dict()
byOrSellDict={'Buy':1,'Sell':-1}

#dateInd=0
#currentDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16)
#orders=[dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16),
#    [order[dateInd,0],order[dateInd,1],int(order[dateInd,2])] for dateInd in range(na_data.shape[0])]
orders={}
for dateInd in range(na_data.shape[0]):
    tmpDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16)
    if tmpDate in orders.keys():
        orders[tmpDate].append([order[dateInd,0],order[dateInd,1],int(order[dateInd,2])])
    else:orders[tmpDate]=[[order[dateInd,0],order[dateInd,1],int(order[dateInd,2])]]



for i in ldt_timestamps:
    if i in orders.keys():
        for singleOrder in orders[i]:
            equity=singleOrder[0]
            byOrSell=singleOrder[1]
            count=singleOrder[2]
            if equity in currentEquity.keys():
                currentEquity[equity]+=count*byOrSellDict[byOrSell]
            else:currentEquity[equity]=count*byOrSellDict[byOrSell]
            currentCash+=-ldf_data[equity][i]*count*byOrSellDict[byOrSell]
    
            print '----------------------',i,equity,byOrSell,count
            print currentEquity
            
            #dateInd+=1
            #currentDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16)
    
    #calc portfolia value
    portfValue=currentCash
    for tmpEqui in currentEquity.keys():
        portfValue+=ldf_data[tmpEqui][i]*currentEquity[tmpEqui]
    print i,portfValue
    



题目要求为

Overview
In this project you will create a basic market simulator that accepts trading orders and keeps track of a portfolio's value and saves it to a file. You will also create another program that assesses the performance of that portfolio.
To Do
Part 1: Create a market simulation tool, marketsim.py that takes a command line like this:
python marketsim.py 1000000 orders.csv values.csv
Where the number represents starting cash and orders.csv is a file of orders organized like this:
Year
Month
Day
Symbol
BUY or SELL
Number of Shares
For example:
2008, 12, 3, AAPL, BUY, 130
2008, 12, 8, AAPL, SELL, 130
2008, 12, 5, IBM, BUY, 50
Your simulator should calculate the total value of the portfolio for each day using adjusted closing prices (cash plus value of equities) and print the result to the file values.csv. The contents of the values.csv file should look something like this:
2008, 12, 3, 1000000
2008, 12, 4, 1000010
2008, 12, 5, 1000250
...
Part 2: Create a portfolio analysis tool, analyze.py, that takes a command line like this:
python analyze.py values.csv \$SPX
The tool should read in the daily values (cumulative portfolio value) from values.csv and plot them. It should use the symbol on the command line as a benchmark for comparison (in this case $SPX). Using this information, analyze.py should:
Plot the price history over the trading period.
Your program should also output:
Standard deviation of daily returns of the total portfolio
Average daily return of the total portfolio
Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio
Cumulative return of the total portfolio
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
尊敬的招生委员会: 我非常荣幸能够为申请佐治亚理工大学计算机科学系的XXX学生写一封推荐信。我是XXX在XXX大学计算机科学系的导师,我在与他的交往中见证了他在计算机科学方面的才华和潜力。 XXX是一名非常聪明、勤奋、有天赋的学生。在他的本科学习中,他在课程中表现出色,通过自学和参加课外活动,他积累了丰富的计算机知识,并且在课程中获得了很高的分数。他不仅在理论方面表现出色,还在实践中表现得非常不错。在参加编程比赛时,他的表现非常优异,多次获得奖项。他还参与了一些开源项目,并在其中发挥了重要作用。 XXX在学术研究方面也表现出色。他参加了我的研究小组,并参与了几个项目。他的工作非常出色,为项目的成功做出了很大的贡献。他在研究中展现了非常强的创造力和领导能力。 我认为,佐治亚理工大学的计算机科学系是全球最优秀的计算机科学系之一。我相信,XXX将在这里受到最好的教育和挑战,并有机会与最优秀的学生和教授共事。他将为学院带来非常宝贵的贡献,并在未来成为一个非常有才华的计算机科学家。 综上所述,我强烈推荐XXX加入佐治亚理工大学计算机科学系。如果您有任何疑问,请随时与我联系。 祝贺您和您的团队,祝您们一切顺利! 此致, 敬礼! XXX导师 XXX大学计算机科学系
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值