Developing an investment strategy based on stock movements

Below is my code

import pandas as pd
import math
import csv

fund = 10000
print("investment",fund)

pval = 0
oldportfolio = []

dts = ["06 Feb 2017", "07 Feb 2017", "08 Feb 2017", "09 Feb 2017", "10 Feb 2017", "13 Feb 2017", "14 Feb 2017", "15 Feb 2017", "16 Feb 2017", "17 Feb 2017",
"20 Feb 2017", "21 Feb 2017", "22 Feb 2017", "23 Feb 2017", "27 Feb 2017"]
for dt in dts:
files = ["stocklistcustom.csv"]
for file in files:
i = 0
filecount = len(df)
result = []
while i < filecount:
# while i < 10:
name = df[i]
mcsym = df[i]
i = i + 1
filepath = "data/nse/his/" + mcsym + ".csv"
try:
endrow = sp[sp == dt].index + 1
parray = []
tarray = []
starray = []
intdate = []
p1 = 0
p2 = 0
p3 = 0
p4 = 0
j = 0
mavg15 = ''
mavg60 = ''
olddiff = 0
days = 2
strtrow = endrow - days - 60
for k in range (strtrow, endrow):
date = sp[k]
price = float(sp[k])
k = k + 1
parray.append(price)
j = j + 1
strtavg = j - 15
mavg15 = sum(parray[strtavg:j]) / 15
strtavg = j - 60
mavg60 = sum(parray[strtavg:j]) / 60
if j > 59:
diff = mavg60 - mavg15
if diff < 0 and olddiff > 0:
trigger = 1
intdate.append(date)
else:
trigger = 0
tarray.append(trigger)
olddiff = diff
# sell criteria
if j == (days + 60):
pricep = (price - p1) * 100 / p1
p1p = (p1 - p2) * 100 / p2
p2p = (p2 - p3) * 100 / p3
p3p = (p3 - p4) * 100 / p4
if pricep < -5 or pricep > 8:
sell = 1
if price < p1 and p1 < p2 and p2 < p3:
sell = 1
else:
sell = 0
p4 = p3
p3 = p2
p2 = p1
p1 = price
if sum(tarray) > 0:
if sell > 0:
result.append([name,mcsym,"sell",price])
except:
pass

# print(result)

output = "output/triggers/"+dt+"trigger.csv"
with open(output, "wb") as f:
writer = csv.writer(f)
writer.writerows(result)
print(output,"exported")

# Code for calculating investment
portfolio = []
for row in result:
if row == "sell" and len(oldportfolio) > 0:
pindex = 0
for buys in oldportfolio:
bindex = 0
for stock in buys:
if row == stock:
sellqty = stock
sellp = row
sellval = sellqty * sellp
purchasep = stock
sellcost = purchasep * sellqty
print(dt,"selling",row,row,sellp,sellqty,sellval)
# print(oldportfolio)
del oldportfolio[pindex][bindex]
# print(oldportfolio)
fund = fund + sellval
pval = pval - sellcost
bindex = bindex + 1
pindex = pindex + 1
# print("op", oldportfolio)
# print(dt,"fund after selling",fund)
buycount = sum(1 for row in result if row==("buy"))
if buycount > 0:
maxinvest = fund / buycount
for row in result:
if row == "buy":
name = row
price = row
qty = math.floor(maxinvest / price)
if qty > 0:
val = qty * price
portfolio.append([name,price,qty,val])
fund = fund - val

# print("portfolio",portfolio)
pval = pval + sum(row for row in portfolio)
print(dt,"cash",fund,"portfolio value",pval,"total",fund+pval)
oldportfolio.append(portfolio)

print(oldportfolio)

It computes the value of portfolio on each day after implementing a specific buy and sell strategy.

But it takes huge time in execution. How to speed it up?

• Can you provide example data? (in/out) – Bryce Guinta Apr 27 '17 at 7:44