-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (91 loc) · 2.77 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import yfinance as yf
import numpy as np
import pandas as pd
import yfinance as yf
import datetime as dt
import matplotlib.pyplot as plt
import mplfinance as mpf
start = '2016-01-01' # format: YYYY-MM-DD
end = dt.datetime.now() # today
stock='CDNA'
df = yf.download(stock,start, end, interval='1d')
df.head()
short_sma= 20
long_sma = 50
SMAs=[short_sma, long_sma]
for i in SMAs:
df["SMA_"+str(i)]= df.iloc[:,4].rolling(window=i).mean()
position = 0 # 1 means we have already entered poistion, 0 means not already entered
counter = 0
percentChange = [] # empty list to collect %changes
for i in df.index:
SMA_short = df['SMA_20']
SMA_long = df['SMA_50']
close = df['Adj Close'][i]
if (SMA_short[i] > SMA_long[i]): # line 9
print('Up trend')
if (position == 0):
buyP = close # buy price
position = 1 # turn position
print("Buy at the price" + str(buyP))
elif (SMA_short[i] < SMA_long[i]):
print('Down trend')
if (position == 1): # have a poistion in down trend
position = 0 # selling position
sellP = close # sell price
print("Sell at the price" + str(sellP))
perc = (sellP / buyP - 1) * 100
percentChange.append(perc) # line 23
if (counter == df["Adj Close"].count() - 1 and position == 1):
position = 0
sellP = close
print("Sell at the price" + str(sellP))
perc = (sellP / buyP - 1) * 100
percentChange.append(perc)
counter += 1
print(percentChange)
gains=0
numGains=0
losses=0
numLosses=0
totReturn=1
for i in percentChange:
if(i>0):
gains+=i
numGains+=1
else:
losses+=i
numLosses+=1
totReturn = totReturn*((i/100)+1)
totReturn=round((totReturn-1)*100,2)
print("This statistics is from "+str(df.index[0])+" up to now with "+str(numGains+numLosses)+" trades:")
print("SMAs used: "+str(SMAs))
print("Total return over "+str(numGains+numLosses)+ " trades: "+ str(totReturn)+"%" )
if (numGains>0):
avgGain=gains/numGains
maxReturn= str(max(percentChange))
else:
avgGain=0
maxReturn='unknown'
if(numLosses>0):
avgLoss=losses/numLosses
maxLoss=str(min(percentChange))
ratioRR=str(-avgGain/avgLoss) # risk-reward ratio
else:
avgLoss=0
maxLoss='unknown'
ratioRR='inf'
print("Average Gain: "+ str(avgGain))
print("Average Loss: "+ str(avgLoss))
print("Max Return: "+ maxReturn)
print("Max Loss: "+ maxLoss)
print("Gain/loss ratio: "+ ratioRR)
if(numGains>0 or numLosses>0):
batAvg=numGains/(numGains+numLosses)
else:
batAvg=0
print("Batting Avg: "+ str(batAvg))
mpf.plot(df, type = 'ohlc',figratio=(16,6),
mav=(short_sma,long_sma),
volume=True, title= str(stock),
style='default')