-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoMovAvgStrat.py
68 lines (55 loc) · 2.48 KB
/
TwoMovAvgStrat.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
import pandas as pd
import yfinance as yf
from utils import send_message
# import matplotlib.pyplot as plt
#---------------------------------------------------------------
SYMBOL = "^NSEI" # any symbol from yahoo finance
data = yf.download(SYMBOL,period ="3d",interval = "15m") # getting data
#---------------------------------------------------------------
# Simple Moving Average
def SWMA(data, ndays):
SMA = pd.Series(data['Close'].rolling(ndays).mean(), name = 'SMA')
data = data.join(SMA)
# print(data)
return data
# Compute the 50-day SMA
n = 50 # n gives time period
SMA = SWMA(data,n) # calling function
SMA_data = SMA['SMA'] # getting only SMA data with time
SMA_data_list = SMA_data.values.tolist()
#----------------------------------------------------------------
# Exponentially-weighted Moving Average
def EWMA(data, ndays):
EMA = pd.Series(data['Close'].ewm(span = ndays, min_periods = ndays - 1).mean(),
name = 'EWMA_' + str(ndays))
data = data.join(EMA)
# print(data)
return data
#----------------------------------------------------------------
# Compute the 5-day EWMA
ew = 5
EWMA = EWMA(data,ew) # type: ignore
EWMA_data = EWMA['EWMA_5'] # getting only SMA data with time # type: ignore
EWMA_data_list = EWMA_data.values.tolist() # converting data into list
# getting time
index_time = data.index
index_time_tolist = index_time.tolist() #converting to list
for i in range(0,len(SMA_data_list)-1):
if EWMA_data_list[i] == 'NaN' and SMA_data_list[i] == 'NaN': continue
else :
# EWMA_data_list[i] = int(EWMA_data_list[i])
# SMA_data_list[i] = int(SMA_data_list[i])
#For Bullish
if SMA_data_list[i] > EWMA_data_list[i] and EWMA_data_list[i+1] > SMA_data_list[i+1] :
print("Bullish Market.")
print("From : " + str(SMA_data_list[i+1])+ '\nTime : ' + str(index_time_tolist[i+1]))
print()
send_message(" Bullish Market.\n" + "From : " + str(SMA_data_list[i+1])+ '\nTime : ' + str(index_time_tolist[i+1]))
# else : print("No Bulish ")
#For Bearish
if(EWMA_data_list[i] > SMA_data_list[i] and SMA_data_list[i+1] > EWMA_data_list[i+1]) :
print("Bearish Market.")
print("From : " + str(EWMA_data_list[i+1]) +'\nTime : '+ str(index_time_tolist[i+1]))
print()
send_message(" Bearish Market.\n"+ "From : " + str(EWMA_data_list[i+1]) +'\nTime : '+ str(index_time_tolist[i+1]))
# else : print("No Bearish ")