SlideShare a Scribd company logo
1 of 2
#CSC521 Final Project
import os
import time
import random
from datetime import date
os.chdir("C:/Users/Desktop")
from nlib import *
import pickle
data = pickle.load(open('clients.pickle.txt'))
#correct stock ticker
for client in data:
if 'WAG' in data[client].keys():
data[client]['WBA']=data[client]['WAG']
del data[client]['WAG']
if 'BRK.B' in data[client].keys():
data[client]['BRK-B']=data[client]['BRK.B']
del data[client]['BRK.B']
else:
pass
symbol=[]
for client in data:
for stock in data[client]:
if stock in symbol:
pass
else:
symbol.append(stock)
stockinfo = PersistentDictionary("stockinfo.sqlite")
for stock in symbol:
if stock in stockinfo:
pass
else:
h=YStock(stock).historical(stop=date(2015,3,31)) #download historiacal
information from Yahoo Finance
stockinfo[stock]=h
s0={}
for stock in symbol:
s0[stock]=stockinfo[stock][-1]["adjusted_close"]
logreturn={} #calculate log return
for stock in symbol:
logreturn[stock]=[day["log_return"] for day in stockinfo[stock]]
# Monte Carlo Simulation
def simulate_once(h,file):
rn=[]
for i in range(h):
n=random.randint(1,250)
rn.append(n)
total=0.0
thisstock=data[client].keys()
for stock in thisstock:
nshare=data[client][stock]
p0=s0[stock]
log=sum(logreturn[stock][-a] for a in rn)
p1=p0*exp(log)
pandl=(p1-p0)
pandl=(p1-p0)*nshare
total +=pandl
return total
def simulate_many(h,file,p,N=1000):
final=[]
for i in range(N):
pl=simulate_once(h,file)
final.append(pl)
final.sort()
final.reverse()
return final[int(p*N)]
def brokerage():
brokerage={}
for stock in symbol:
brokerage[stock]=0
for client in data:
for stock in data[client]:
brokerage[stock]+=data[client][stock]
return brokerage
def onestock(h,file,p,N=1000):
final=[]
for i in range(N):
pl=simulate_once(h,file)
final.append(pl)
final.sort()
final.reverse()
return final
#Value at risk for every client:
for client in data:
print client, simulate_many(7,client,0.99)
#Value at risk for the brokeragehouse:
brokhouse=brokerage()
print 'cumulative',simulate_many(7,brokhouse,0.99)
#histogram for individual stock
AIG=onestock(5,data[client]['AIG'],0.99)
PEP=onestock(5,data[client]['PEP'],0.99)
Canvas(title='99% Var for AIG').hist(AIG).save('AIG.png')
Canvas(title='99% Var for PEP').hist(PEP).save('PEP.png')

More Related Content

What's hot

Workshop 20140522 BigQuery Implementation
Workshop 20140522   BigQuery ImplementationWorkshop 20140522   BigQuery Implementation
Workshop 20140522 BigQuery ImplementationSimon Su
 
MongoDB IoT City Tour EINDHOVEN: Managing the Database Complexity
MongoDB IoT City Tour EINDHOVEN: Managing the Database ComplexityMongoDB IoT City Tour EINDHOVEN: Managing the Database Complexity
MongoDB IoT City Tour EINDHOVEN: Managing the Database ComplexityMongoDB
 
MongoDB.local Paris Keynote
MongoDB.local Paris KeynoteMongoDB.local Paris Keynote
MongoDB.local Paris KeynoteMongoDB
 
Creating your own project's Quality Dashboard
Creating your own project's Quality DashboardCreating your own project's Quality Dashboard
Creating your own project's Quality DashboardVincent Massol
 
Android is not just mobile
Android is not just mobileAndroid is not just mobile
Android is not just mobileKevin McDonagh
 
Faites évoluer votre accès aux données avec MongoDB Stitch
Faites évoluer votre accès aux données avec MongoDB StitchFaites évoluer votre accès aux données avec MongoDB Stitch
Faites évoluer votre accès aux données avec MongoDB StitchMongoDB
 
#SlimScalding - Less Memory is More Capacity
#SlimScalding - Less Memory is More Capacity#SlimScalding - Less Memory is More Capacity
#SlimScalding - Less Memory is More CapacityGera Shegalov
 
Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012Friedger Müffke
 
Services in cf
Services in cfServices in cf
Services in cfdotchev
 
XWiki: The best wiki for developers
XWiki: The best wiki for developersXWiki: The best wiki for developers
XWiki: The best wiki for developersVincent Massol
 

What's hot (11)

Workshop 20140522 BigQuery Implementation
Workshop 20140522   BigQuery ImplementationWorkshop 20140522   BigQuery Implementation
Workshop 20140522 BigQuery Implementation
 
MongoDB IoT City Tour EINDHOVEN: Managing the Database Complexity
MongoDB IoT City Tour EINDHOVEN: Managing the Database ComplexityMongoDB IoT City Tour EINDHOVEN: Managing the Database Complexity
MongoDB IoT City Tour EINDHOVEN: Managing the Database Complexity
 
Viva 20.11.2013 google analytics
Viva 20.11.2013 google analyticsViva 20.11.2013 google analytics
Viva 20.11.2013 google analytics
 
MongoDB.local Paris Keynote
MongoDB.local Paris KeynoteMongoDB.local Paris Keynote
MongoDB.local Paris Keynote
 
Creating your own project's Quality Dashboard
Creating your own project's Quality DashboardCreating your own project's Quality Dashboard
Creating your own project's Quality Dashboard
 
Android is not just mobile
Android is not just mobileAndroid is not just mobile
Android is not just mobile
 
Faites évoluer votre accès aux données avec MongoDB Stitch
Faites évoluer votre accès aux données avec MongoDB StitchFaites évoluer votre accès aux données avec MongoDB Stitch
Faites évoluer votre accès aux données avec MongoDB Stitch
 
#SlimScalding - Less Memory is More Capacity
#SlimScalding - Less Memory is More Capacity#SlimScalding - Less Memory is More Capacity
#SlimScalding - Less Memory is More Capacity
 
Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012
 
Services in cf
Services in cfServices in cf
Services in cf
 
XWiki: The best wiki for developers
XWiki: The best wiki for developersXWiki: The best wiki for developers
XWiki: The best wiki for developers
 

Similar to final

Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaVMware Tanzu
 
Patterns and Practices for Event Design With Adam Bellemare | Current 2022
Patterns and Practices for Event Design With Adam Bellemare | Current 2022Patterns and Practices for Event Design With Adam Bellemare | Current 2022
Patterns and Practices for Event Design With Adam Bellemare | Current 2022HostedbyConfluent
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile servicesAymeric Weinbach
 
Viki Big Data Meetup 2013_10
Viki Big Data Meetup 2013_10Viki Big Data Meetup 2013_10
Viki Big Data Meetup 2013_10ishanagrawal90
 
Re:Invent 2018 Database Announcements
Re:Invent 2018 Database AnnouncementsRe:Invent 2018 Database Announcements
Re:Invent 2018 Database AnnouncementsSteven Ensslen
 
FleetDB: A Schema-Free Database in Clojure
FleetDB: A Schema-Free Database in ClojureFleetDB: A Schema-Free Database in Clojure
FleetDB: A Schema-Free Database in ClojureMark McGranaghan
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Dan Robinson
 
PWA night vol.11 20191218
PWA night vol.11 20191218PWA night vol.11 20191218
PWA night vol.11 20191218bitpart
 
FleetDB A Schema-Free Database in Clojure
FleetDB A Schema-Free Database in ClojureFleetDB A Schema-Free Database in Clojure
FleetDB A Schema-Free Database in Clojureelliando dias
 
XilinxのxsimでSoftware Driven Verification.pdf
XilinxのxsimでSoftware  Driven Verification.pdfXilinxのxsimでSoftware  Driven Verification.pdf
XilinxのxsimでSoftware Driven Verification.pdfMr. Vengineer
 
Super-NetOps Source of Truth
Super-NetOps Source of TruthSuper-NetOps Source of Truth
Super-NetOps Source of TruthJoel W. King
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSONChris Saxon
 
Deep dive into N1QL: SQL for JSON: Internals and power features.
Deep dive into N1QL: SQL for JSON: Internals and power features.Deep dive into N1QL: SQL for JSON: Internals and power features.
Deep dive into N1QL: SQL for JSON: Internals and power features.Keshav Murthy
 
Introducing N1QL: New SQL Based Query Language for JSON
Introducing N1QL: New SQL Based Query Language for JSONIntroducing N1QL: New SQL Based Query Language for JSON
Introducing N1QL: New SQL Based Query Language for JSONKeshav Murthy
 
[MongoDB.local Bengaluru 2018] Keynote
[MongoDB.local Bengaluru 2018] Keynote[MongoDB.local Bengaluru 2018] Keynote
[MongoDB.local Bengaluru 2018] KeynoteMongoDB
 
How to create an Angular builder
How to create an Angular builderHow to create an Angular builder
How to create an Angular builderMaurizio Vitale
 
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022Lowering the Barrier to Stream Processing With Alex Morley | Current 2022
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022HostedbyConfluent
 

Similar to final (20)

Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
 
Patterns and Practices for Event Design With Adam Bellemare | Current 2022
Patterns and Practices for Event Design With Adam Bellemare | Current 2022Patterns and Practices for Event Design With Adam Bellemare | Current 2022
Patterns and Practices for Event Design With Adam Bellemare | Current 2022
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile services
 
Viki Big Data Meetup 2013_10
Viki Big Data Meetup 2013_10Viki Big Data Meetup 2013_10
Viki Big Data Meetup 2013_10
 
Serverless
ServerlessServerless
Serverless
 
Re:Invent 2018 Database Announcements
Re:Invent 2018 Database AnnouncementsRe:Invent 2018 Database Announcements
Re:Invent 2018 Database Announcements
 
FleetDB: A Schema-Free Database in Clojure
FleetDB: A Schema-Free Database in ClojureFleetDB: A Schema-Free Database in Clojure
FleetDB: A Schema-Free Database in Clojure
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
 
PWA night vol.11 20191218
PWA night vol.11 20191218PWA night vol.11 20191218
PWA night vol.11 20191218
 
KPMG - TASK 1.pdf
KPMG - TASK 1.pdfKPMG - TASK 1.pdf
KPMG - TASK 1.pdf
 
FleetDB A Schema-Free Database in Clojure
FleetDB A Schema-Free Database in ClojureFleetDB A Schema-Free Database in Clojure
FleetDB A Schema-Free Database in Clojure
 
XilinxのxsimでSoftware Driven Verification.pdf
XilinxのxsimでSoftware  Driven Verification.pdfXilinxのxsimでSoftware  Driven Verification.pdf
XilinxのxsimでSoftware Driven Verification.pdf
 
Super-NetOps Source of Truth
Super-NetOps Source of TruthSuper-NetOps Source of Truth
Super-NetOps Source of Truth
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSON
 
Deep dive into N1QL: SQL for JSON: Internals and power features.
Deep dive into N1QL: SQL for JSON: Internals and power features.Deep dive into N1QL: SQL for JSON: Internals and power features.
Deep dive into N1QL: SQL for JSON: Internals and power features.
 
Introducing N1QL: New SQL Based Query Language for JSON
Introducing N1QL: New SQL Based Query Language for JSONIntroducing N1QL: New SQL Based Query Language for JSON
Introducing N1QL: New SQL Based Query Language for JSON
 
[MongoDB.local Bengaluru 2018] Keynote
[MongoDB.local Bengaluru 2018] Keynote[MongoDB.local Bengaluru 2018] Keynote
[MongoDB.local Bengaluru 2018] Keynote
 
How to create an Angular builder
How to create an Angular builderHow to create an Angular builder
How to create an Angular builder
 
Platform BigData 2.pptx
Platform BigData 2.pptxPlatform BigData 2.pptx
Platform BigData 2.pptx
 
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022Lowering the Barrier to Stream Processing With Alex Morley | Current 2022
Lowering the Barrier to Stream Processing With Alex Morley | Current 2022
 

final

  • 1. #CSC521 Final Project import os import time import random from datetime import date os.chdir("C:/Users/Desktop") from nlib import * import pickle data = pickle.load(open('clients.pickle.txt')) #correct stock ticker for client in data: if 'WAG' in data[client].keys(): data[client]['WBA']=data[client]['WAG'] del data[client]['WAG'] if 'BRK.B' in data[client].keys(): data[client]['BRK-B']=data[client]['BRK.B'] del data[client]['BRK.B'] else: pass symbol=[] for client in data: for stock in data[client]: if stock in symbol: pass else: symbol.append(stock) stockinfo = PersistentDictionary("stockinfo.sqlite") for stock in symbol: if stock in stockinfo: pass else: h=YStock(stock).historical(stop=date(2015,3,31)) #download historiacal information from Yahoo Finance stockinfo[stock]=h s0={} for stock in symbol: s0[stock]=stockinfo[stock][-1]["adjusted_close"] logreturn={} #calculate log return for stock in symbol: logreturn[stock]=[day["log_return"] for day in stockinfo[stock]] # Monte Carlo Simulation def simulate_once(h,file): rn=[] for i in range(h): n=random.randint(1,250) rn.append(n) total=0.0
  • 2. thisstock=data[client].keys() for stock in thisstock: nshare=data[client][stock] p0=s0[stock] log=sum(logreturn[stock][-a] for a in rn) p1=p0*exp(log) pandl=(p1-p0) pandl=(p1-p0)*nshare total +=pandl return total def simulate_many(h,file,p,N=1000): final=[] for i in range(N): pl=simulate_once(h,file) final.append(pl) final.sort() final.reverse() return final[int(p*N)] def brokerage(): brokerage={} for stock in symbol: brokerage[stock]=0 for client in data: for stock in data[client]: brokerage[stock]+=data[client][stock] return brokerage def onestock(h,file,p,N=1000): final=[] for i in range(N): pl=simulate_once(h,file) final.append(pl) final.sort() final.reverse() return final #Value at risk for every client: for client in data: print client, simulate_many(7,client,0.99) #Value at risk for the brokeragehouse: brokhouse=brokerage() print 'cumulative',simulate_many(7,brokhouse,0.99) #histogram for individual stock AIG=onestock(5,data[client]['AIG'],0.99) PEP=onestock(5,data[client]['PEP'],0.99) Canvas(title='99% Var for AIG').hist(AIG).save('AIG.png') Canvas(title='99% Var for PEP').hist(PEP).save('PEP.png')