---
# System prepended metadata

title: MACD with 12period EMA and 26period EMA

---

import pandas as pd 
import numpy as np
import math
import matplotlib.pyplot as plt
import warnings #Remove warning message
import seaborn as sns
from sklearn.model_selection import train_test_split
from scipy.stats.stats import pearsonr
warnings.simplefilter("ignore", RuntimeWarning)
warnings.simplefilter('ignore', FutureWarning)

data_file='Group_2_1Daily Crude Oil WTI Futures Historical Data.csv'
#data_file='Group_2_2Daily US Dollar Index Futures Historical Data.csv'

def load_data(fname):
    data=pd.read_csv(fname,parse_dates=['Date'])
    return data

def update_ma(row, rows, n):
    # rows contains history update to yesterday
    if len(rows) < n-1:
        return None
    else:
        price_history =[]
        for i in range(1, n):
            price_history.append(rows[-i]['Price'])
        average = (sum(price_history) + row['Price'])/n
        return average

def update_trend(r):
    try:
        if r['ma5'] > r['ma30']:
            return 'UP'
        else:
            return 'DOWN'
    except:
        return None
    
def get_reversalpts(r,rows):  
    if len(rows) < 30:
        return None
    elif r['trend']!=rows[-1]['trend']:
        if rows[-1]['trend']=='UP':
            return 1 # up to down
        else:   
            return -1 # down to up         
    else:
        return 0

def update_weekday(row):
    t=row['Date']
    return t.day_name()

def update_year(row):
    t=row['Date']
    return t.year

def update_season(r,row):
    t=row['Date']
    if t.month_name()=='March'or t.month_name()=='April'or t.month_name()=='May':
        return 'Spring'

    elif t.month_name()=='June'or t.month_name()=='July'or t.month_name()=='August':
        return 'Summer'

    elif t.month_name()=='September'or t.month_name()=='October'or t.month_name()=='November':
        return 'Autumn'

    else:
        return 'Winter'


def dictionary(file_data1, file_data2):
    zipfile = dict(zip(file_data1,file_data2))
    return zipfile

def dictionary_into_r(r,zipfile):
    try:
        vardata=zipfile[r['year']]
        return vardata
    except:
        return None
    
#Relative Strength Index with period 14days
def RSI(r,row,rows):  
    gain,loss=[],[]
    if r['Price']>=row['Open']:
        gain=r['Price']-row['Open']
        loss=0
    else:
        gain=0
        loss=row['Open']-r['Price']
    
    if len(rows) < 13:
        avegain=None
        aveloss=None
        rsi= None
    
    elif len(rows)<14:
        gain_history =[]
        loss_history =[]
        for i in range(1, 14):
            gain_history.append(rows[-i]['Gain'])
            loss_history.append(rows[-i]['Loss'])
        avegain = (sum(gain_history)+gain)/14
        aveloss = (sum(loss_history)+loss)/14
        rs=avegain/aveloss
        rsi=(100-(100/(1+rs)))
    
    else:       
        avegain=((rows[-1]['avegain']*13)+gain)/14
        aveloss=((rows[-1]['aveloss']*13)+loss)/14
        rs=avegain/aveloss
        rsi=(100-(100/(1+rs)))
    
    return gain, loss,avegain,aveloss,rsi
    
def RateofChangeinPrice(row,rows):
    if len(rows)<1:
        return None  
    else:
        roc=((row['Price']-rows[-1]['Price'])/rows[-1]['Price'])*100
        return roc          

def Duration_of_TrendReversal(i,r,first,j):    
    if r['reversal']== 1 or r['reversal'] == -1:
        if first is True:
            duration=0
            j=i
            first=False        
        else:
            duration=i-j
            j=i
    else:
        duration=0        
    return duration,first,j

def ROCfromLatestReversalPoint(r,row,rows,firstII,latestprice):    
    if len(rows)<30:
        roc=None
        
    elif r['reversal']== 1 or r['reversal'] == -1:
        if firstII is True:
            latestprice=row['Price']
            roc=None
            firstII=False
        
        else:
            roc=(row['Price']-latestprice)/(latestprice)*100
            latestprice=row['Price']
    
    elif firstII is False:
         roc=(row['Price']-latestprice)/(latestprice)*100
         
    else:
        roc=None
    
    return roc,firstII,latestprice 

def update_std(r,row, rows, n):
    # rows contains history update to yesterday
    if len(rows) < n-1:
        return None
   
    else:
        SMA=update_ma(row, rows, n=20)
        price_MinusMean =[]
        for i in range(1, n):
            price_MinusMean.append((rows[-i]['Price']-SMA)**2)
        std = math.sqrt((sum(price_MinusMean) + (row['Price']-SMA)**2)/(n-1))
        return std
    
#Bollinger bandwidth[2,20]   
def Bollinger_Bandwidth(r,row,rows,n):  
    if len(rows) < n-1:
        middle_band=None
        upper_band=None
        lower_band=None
        bandwidth=None
    
    else:
        middle_band=update_ma(row, rows, n=20) #SMA(20)
        stdv=r['std20']#20 period std deviation
        upper_band=middle_band+2*stdv
        lower_band=middle_band-2*stdv
        bandwidth=(upper_band-lower_band)/(middle_band)
    
    return middle_band,upper_band,lower_band,bandwidth

def BollMarket_trend(r,row,rows,n): #determine Bull/ Bear using Bollinger band
    if len(rows) < n-1:
        market_trend=None
    
    elif row['Price']>=r['Upper Band']:
        market_trend='Bull'
        
    elif row['Price']<=r['Lower Band']:
        market_trend='Bear'
        
    else:
        market_trend='Neutral'
    
    return market_trend

    
def get_ema(r,row,rows,n):
     if len(rows) < n-1:
        return None
    
     elif len(rows)<n:
        price_history =[]
        for i in range(1,n):
            price_history.append(rows[-i]['Price'])
        mean = (sum(price_history) + row['Price'])/n
        return mean
        
     else:
        k=2/(n+1)
        ema=(row['Price']*k)+(rows[-1]['EMA%s'%n]*(1-k))
        return ema
    
# MACD with 12period EMA and 26period EMA    
def MACD(r,rows): 
     if len(rows) < 25:
        macd=None     
     else:
        macd=r['EMA12']-r['EMA26']             
     return macd

def Open_PriorClose(row,rows): # get difference between today open and prior close
    if len(rows) < 1:
        return None    
    else:
        diff=row['Open']-rows[-1]['Price']
        return diff

def Stochastic_Oscillator(i,row,rows,data):    
    
    if len(rows) < 13:
        Min=None
        Max=None
        K=None        
    else:
        high_history =[]
        low_history=[]
        for j in range(1, 14):
            high_history.append(data.High[i-j])
            low_history.append(data.Low[i-j])
        high_history.append(row['High'])
        low_history.append(row['Low'])
        Min=min(low_history)
        Max=max(high_history)
        K=(row['Price']-Min)/(Max-Min)*100        
    
    if len(rows)<15:
        sto=None
        
    else:
        k_history=[]
        for i in range(1, 3):
            k_history.append(rows[-i]['K%'])
        sto=(sum(k_history)+K)/3
        
    return K,sto

#Rate of change of candlestick body size
def ROC_candlestick_body(r,row,rows):
    
    if row['Open']>=r['Price']:
        size=row['Open']-r['Price']
    
    else:
        size=r['Price']-row['Open']    
        
    if len(rows)<1:
        roc=None
    
    elif rows[-1]['CandleSize']==0:
        roc=0
        
    else:        
        roc=(size-rows[-1]['CandleSize'])/(rows[-1]['CandleSize'])
    
    return size,roc

#Average true range(14periods)
def ATR(row,rows): 
   
    if len(rows)<1:
        TR=row['High']-row['Low']
        
    else: 
        TR=max(row['High']-row['Low'],abs(row['High']-rows[-1]['Price']),abs(row['Low']-rows[-1]['Price']))
    
    if len(rows)<13:
        ATR=None
    
    elif len(rows)<14:
        TR_history=[]
        for i in range(1, 14):
            TR_history.append(rows[-i]['TR'])
        ATR=(sum(TR_history)+TR)/14
    
    else:
        ATR=((rows[-1]['ATR']*13)+TR)/14
        
    return TR,ATR

#sum of reversal point before 20 days period
def sum_ofreversal(r,rows): 
    if len(rows)<50:
        return None
    
    else:
        counter=0
        for i in range(1, 21):
            if rows[-i]['reversal']== 1 or rows[-i]['reversal']== -1:
                counter+=1
            
        return counter
    
#Chaikin Money Flow(Period,20)
def CMF(row,rows):    
    MFM=((row['Price']-row['Low'])-(row['High']-row['Price']))/(row['High']-row['Low']) #Money Flow Multiplier
    MFV=MFM*row['Vol.']     #Money Flow Volume
    if len(rows) < 19:
        CMF= None
    else:
        MFV_history =[]
        vol_history=[]
        for i in range(1, 20):
            MFV_history.append(rows[-i]['MFV'])
            vol_history.append(rows[-i]['Volume'])
        sumMFV = sum(MFV_history) + MFV
        sumVol=sum(vol_history)+row['Vol.']
        CMF=sumMFV/sumVol
    
    return MFM,MFV,CMF
        
# Williams %R / Williams Percent Range 
def william(n, r, rows):
    n_price = []
    if len(rows) < n:
        return None
    else:
        for i in range (1,n):
            n_price.append(rows [-i]['Price'])
        highest_high = max(n_price)
        lowest_low = min(n_price)
        close = r['Price']
        william = (highest_high - close)/ (highest_high - lowest_low)
        return william

def typicalprice(row):      #for CCI and MFI
    TP1 = (row['High'] + row['Low'] + row['Price'])
    meanTP1 = np.mean(TP1)
    return meanTP1

def typicalpriceCCI(n,rows):     #for CCI
    t_price = []
    if len(rows) < n:
        return None
    else:
        for i in range (1,n):
            t_price.append(rows[-i]['Typical Price'])
        typicalpriceCCI = sum(t_price)
        return typicalpriceCCI
 
def maCCI(n,rows):        #for CCI
    ma = []
    if len(rows) < n:
        return None
    else:
        for i in range (1,n):
            if rows[-i]['Typical Price for CCI'] is None:
                ma.append(0)
            else:
                ma.append(rows[-i]['Typical Price for CCI'])
        maCCI = sum(ma)/n
        return maCCI

def meandeviation(n,rows):       #for CCI
    md=[]
    if len(rows) < n:
        return None
    else:
        for i in range (1,n):
            if rows[-i]['Typical Price for CCI'] is None or rows[-i]['MA for CCI'] is None:
                md.append(0)
            else:
                md.append(abs(rows[-i]['Typical Price for CCI'])-(rows[-i]['MA for CCI']))
        meandeviation=(sum(md))/n
    return meandeviation

#Commodity Channel Index
def CCI(r,constant):
    if r['Typical Price for CCI'] == None or r['MA for CCI'] == None or r['Mean Deviation'] == None:
        return None
    else: 
        CCI = (r['Typical Price for CCI']-r['MA for CCI'])/(constant * r['Mean Deviation'])
        return CCI

def typicalprice_trend(r,rows):     #for MFI
    if len(rows) > 0:
        if r['Typical Price'] < rows[-1]['Typical Price']:
            return -1
        elif r['Typical Price'] > rows[-1]['Typical Price']:
            return 1
        else:
            return None
    else:
        return None

def rawmoneyflow(r,row):       #for MFI
    rmf = r['Typical Price'] * row['Vol.']
    return rmf     

def moneyflow_ratio(n,r,rows):       #for MFI
    positivemf=[]
    negativemf=[]
    
    days = len(rows)
    if days < n:
        return None
    else:
        for i in range (1,n):
            if rows[-i]['Typical Price Trend'] == 1:
                positivemf.append(rows[-i]['Typical Price'])
            elif rows[-i]['Typical Price Trend'] == -1:
                negativemf.append(rows[-i]['Typical Price'])
            else:
                pass
                
        positive=sum(positivemf)
        negative=sum(negativemf)
        mfratio=positive/negative
        return mfratio

#Money Flow Index
def MFI(r):
    if r['Money Flow Ratio'] == None:
        return 0
    else:
        mfi = 100 -(100/(1 + r['Money Flow Ratio']))  
        return mfi
     
def candlestick_pattern(row,rows):
    if row['Open']>row['Price'] and ((row['Price']-row['Low'])/(row['High']-row['Low']))>=0.75:    
        pattern1='Hammer'
    
    elif row['Price']>row['Open'] and ((row['Open']-row['Low'])/(row['High']-row['Low']))>=0.65: 
        pattern1='Hammer'
    
    elif row['Open']>row['Price'] and ((row['High']-row['Open'])/(row['High']-row['Low']))>=0.7:     
        pattern1='ShootingStar'
    
    elif row['Price']>row['Open'] and ((row['High']-row['Price'])/(row['High']-row['Low']))>=0.8:  
        pattern1='ShootingStar'
             
    else:
        pattern1='Neutral 1'
                
    if len(rows)<1:
        pattern2=None
    
    elif (rows[-1]['Open']>rows[-1]['Price']) and (row['Price']>rows[-1]['Open']) and (rows[-1]['Price']>=row['Open']) and ((row['Price']-row['Open'])>(rows[-1]['Open']-rows[-1]['Price'])):
        pattern2='Bull 2'
    
    elif (rows[-1]['Price']>rows[-1]['Open']) and (row['Open']>rows[-1]['Price']) and (rows[-1]['Open']>=row['Price']) and ((row['Open']-row['Price'])>(rows[-1]['Price']-rows[-1]['Open'])) :                       
        pattern2='Bear 2'
    
    else:
        pattern2='Neutral 2'
           
    if len(rows)<1:
        pattern3=None
        
    elif (rows[-1]['Open']>rows[-1]['Price']) and (row['Price']>row['Open']) and (row['Price']>=(0.5*(rows[-1]['Open']+rows[-1]['Price']))):
        pattern3='PiercingLine'
            
    elif (rows[-1]['Price']>rows[-1]['Open']) and (row['Open']>row['Price']) and (row['Price']>=(0.5*(rows[-1]['Open']+rows[-1]['Price']))):
        pattern3='DarkCloudCover'
       
    else:
        pattern3='Neutral 3'     
     
   
    if len(rows)<3:
        pattern4=None 
        
    elif (rows[-3]['Open']>rows[-3]['Price']) and (rows[-2]['Price']>rows[-2]['Open']) and (rows[-1]['Price']>rows[-2]['Price']) and (row['Price']>rows[-1]['Price']):
        pattern4='3WhiteSoldier'
        
    elif (rows[-3]['Price']>rows[-3]['Open']) and (rows[-2]['Open']>rows[-2]['Price']) and (rows[-2]['Price']>rows[-1]['Price']) and (rows[-1]['Price']>row['Price']):
        pattern4='3BlackCrows'
        
    else:
        pattern4='Neutral_4'
    
    return pattern1, pattern2, pattern3, pattern4 


def CreateDummies(data):    
    for i in dummies:
        x = pd.get_dummies(data[i])
        data = pd.concat([data,x],axis=1)
    return data 
        
def RemoveNone(df):    
    #Remove incomplete row from snapshot
    df=df.where((pd.notnull(df)), None)
    df = df[df.astype(str).ne('None').all(1)]
    df = df.reset_index(drop=True)
    return df       


def main():
    data=load_data(data_file)
    data2=pd.read_csv('Group_2_Central Government Debt (Percent of GDP).csv')
    data3=pd.read_csv('Group_2_GDP US.csv')
    data4=pd.read_csv('Group_2_Interest Rate US.csv')
    data5=pd.read_csv('Group_2_Inflation Rate US.csv')
    data6=pd.read_csv('Group_2_Unemployment Rate.csv')
    data7=pd.read_csv('Group_2_Net Trade In Goods.csv')
    
    data2_dict = dictionary(data2['Year'], data2['United States'])
    data3_dict = dictionary(data3['Year'], data3['GDP US (Billions in Dollars)'])
    data4_dict = dictionary(data4['Year'], data4['Interest Rate US (%)'])
    data5_dict = dictionary(data5['Year'], data5['Inflation Rate US (%)'])
    data6_dict = dictionary(data6['Year'], data6['Unemployment Rate (%)'])
    data7_dict = dictionary(data7['Year'], data7['Net Trade in Goods United States (Billions in Dollars)'])

    rows=[]#containers for each row
    first=True
    firstII=True
    j=[]
    latestprice=[]
    
    for i, row in data.iterrows():
        r={}
        r['Price'],r['Open'],r['Volume']=row['Price'],row['Open'],row['Vol.']
        r['ma5'] = update_ma(row, rows, n=5)
        r['ma30'] = update_ma(row, rows, n=30)
        r['trend'] = update_trend(r)
        r['reversal']=get_reversalpts(r,rows)
        r['Weekday']=update_weekday(row)
        r['year']=update_year(row)
        r['Season']=update_season(r,row)
        r['Gain'],r['Loss'],r['avegain'],r['aveloss'],r['RSI']= RSI(r,row,rows)
        r['Rate of Change in Price%']=RateofChangeinPrice(row,rows)
        r['Duration'],first,j=Duration_of_TrendReversal(i,r,first,j)
        r['ROCfromLatestReversalPoint%'],firstII,latestprice=ROCfromLatestReversalPoint(r,row,rows,firstII,latestprice)
        r['std20']=update_std(r,row, rows, n=20)#for finding Bollinger and as a variable(volatility)
        r['Middle Band'],r['Upper Band'],r['Lower Band'],r['Bandwidth']=Bollinger_Bandwidth(r,row,rows,n=20)
        r['Bollinger Market Trend']=BollMarket_trend(r,row,rows,n=20)
        r['EMA12']=get_ema(r,row,rows,n=12)
        r['EMA26']=get_ema(r,row,rows,n=26)
        r['MACD']=MACD(r,rows)
        r['Dif bet Open& Prior Close']=Open_PriorClose(row,rows)
        r['K%'],r['StoOsci%']=Stochastic_Oscillator(i,row,rows,data)
        r['CandleSize'],r['ROC of Candlestick size']=ROC_candlestick_body(r,row,rows)
        r['TR'],r['ATR']=ATR(row,rows)
        r['Sum of reversal']=sum_ofreversal(r,rows)
        r['MFM'],r['MFV'],r['CMF']=CMF(row,rows)
        r['William']=william(14,r,rows)
        r['Typical Price']=typicalprice(row)
        r['Typical Price for CCI']=typicalpriceCCI(20,rows)
        r['MA for CCI']=maCCI(20,rows)
        r['Mean Deviation']=meandeviation(20,rows)
        r['CCI']=CCI(r,constant=0.015)
        r['Raw Money Flow']=rawmoneyflow(r,row)
        r['Typical Price Trend']=typicalprice_trend(r,rows)
        r['Money Flow Ratio']=moneyflow_ratio(20,r,rows)
        r['MFI']= MFI(r)
        r['Pattern 1'], r['Pattern 2'], r['Pattern 3'], r['Pattern 4']=candlestick_pattern(row,rows)
        r['Government Debt United States']=dictionary_into_r(r,data2_dict)
        r['GDP United States(Billions in Dollars)']=dictionary_into_r(r,data3_dict)
        r['Interest Rate United States']=dictionary_into_r(r,data4_dict)
        r['Inflation Rate United States']=dictionary_into_r(r,data5_dict)
        r['Unemployment Rate United States']=dictionary_into_r(r,data6_dict)
        r['Net Trade in Goods United States']=dictionary_into_r(r,data7_dict)

        rows.append(r)
        df=pd.DataFrame(rows)
        df.drop(['Open','EMA12','EMA26','Gain','Loss','avegain','aveloss','K%','TR','CandleSize','Typical Price','Typical Price for CCI','MA for CCI','Mean Deviation','Raw Money Flow','Typical Price Trend','Money Flow Ratio','MFM','MFV'],axis=1,inplace=True)
    return data,df

def extractdata(df2):
    
    reversaldata = pd.concat([df2[df2.reversal == -1], df2[df2.reversal== 1]])
    nonreversaldata=df2[df2.reversal== 0].sample(n=len(reversaldata),random_state=10)
    extracteddata=pd.concat([reversaldata,nonreversaldata]).reset_index(drop=True)
    del extracteddata['Volume'], extracteddata['trend'],extracteddata['Pattern 1'],extracteddata['Pattern 2'],extracteddata['Pattern 3'],extracteddata['Pattern 4'],extracteddata['year'], extracteddata['ma30'],extracteddata['ma5']
    col=['ATR','Bandwidth', 'CCI','CMF','Dif bet Open& Prior Close','Duration','MACD','MFI','Price','ROC of Candlestick size', 'ROCfromLatestReversalPoint%', 'RSI','Rate of Change in Price%','StoOsci%','Sum of reversal','William',  'reversal','std20' ]
    for i in col:
        extracteddata[i]=pd.to_numeric(extracteddata[i], errors='coerce')
    return reversaldata,nonreversaldata,extracteddata

def Visualisation(extracteddata,data,df):
    
     #to check missing values
# =============================================================================
#      fig,ax=plt.subplots()
#      sns.heatmap(extracteddata.isnull(),cbar=False,yticklabels=False,cmap = 'viridis')
# =============================================================================
    
    cols=['ATR','Bandwidth', 'CCI','CMF','Dif bet Open& Prior Close','Duration','MACD','MFI','Price','ROC of Candlestick size', 'ROCfromLatestReversalPoint%', 'RSI','Rate of Change in Price%','StoOsci%','Sum of reversal','William', 'reversal','std20' ]
    visualdata=extracteddata[cols]
    l = visualdata.columns.values
    number_of_columns=18/2
    number_of_rows = ((len(l)/2)-1)/number_of_columns
    plt.figure(figsize=(1.5*number_of_columns,5*number_of_rows))
    for i in range(0,9):
        plt.figure(1).suptitle('Boxplot 1',fontsize=14)  #to identify outliers
        plt.subplot(number_of_rows + 1,number_of_columns,i+1)
        sns.set_style('whitegrid')
        sns.boxplot(visualdata[l[i]],color='green',orient='v')
        plt.tight_layout()
    
    plt.figure(figsize=(1.5*number_of_columns,5*number_of_rows))
    for i in range(0,9):
        plt.figure(2).suptitle('Boxplot 2',fontsize=14)
        plt.subplot(number_of_rows + 1,number_of_columns,i+1)
        sns.set_style('whitegrid')
        sns.boxplot(visualdata[l[i+9]],color='green',orient='v')
        plt.tight_layout()
    
    for i in range(0,9):
        plt.figure(3).suptitle('Distribution Plot 1',fontsize=14) #to determine skewness
        plt.subplot(3,3,i+1)
        sns.distplot(visualdata[l[i]],kde=True)
        plt.tight_layout()
    
    for i in range(0,9): 
        plt.figure(4).suptitle('Distribution Plot 2',fontsize=14)
        plt.subplot(3,3,i+1)
        sns.distplot(visualdata[l[i+9]],kde=True)   
        plt.tight_layout()

    cols2=['ATR','Bandwidth','Dif bet Open& Prior Close','Duration','Price','ROC of Candlestick size','Rate of Change in Price%','William', 'reversal','std20' ]
    visualdata2=extracteddata[cols2]
    rets2=visualdata2.pct_change()
    corr=rets2.corr(method='pearson')
    plt.figure(5).suptitle('Heat Map',fontsize=14)
    sns.heatmap(corr,xticklabels=corr.columns.values,yticklabels=corr.columns.values,annot=True,annot_kws={'size':12})
    heat_map=plt.gcf()
    heat_map.set_size_inches(14,10)
    plt.xticks(fontsize=8)
    plt.yticks(fontsize=10)
    plt.show()
    
    plt.figure(6).suptitle('Bollinger Market Trend Barchart',fontsize=14)
    sns.countplot(x="reversal", hue="Bollinger Market Trend", data=extracteddata)
    plt.show()
    
# =============================================================================
#     cols3=['MFI', 'ROCfromLatestReversalPoint%', 'RSI','StoOsci%']
#     visualdata3=extracteddata[cols3]
#     rets3=visualdata3.pct_change()
#     sns.pairplot(rets3,height=2).fig.suptitle('Scatter Plot',fontsize=14)
#     plt.show()
# =============================================================================
    
    #Sum of Reversal and Rate of Change from Latest Reversal Point%
    fig, ax = plt.subplots()
    ax.scatter(extracteddata['Sum of reversal'], extracteddata['ROCfromLatestReversalPoint%'])
    ax.set_xlabel('SumOfReversal')
    ax.set_ylabel('ROCfromLatestReversalPoint')
    ax.set_title('Relationship between SumOfReversal & ROCfromLatestReversalPoint')
    plt.show()

    #Standard deviation 20 and Rate of Change in Price%
    fig, ax = plt.subplots()
    ax.scatter(extracteddata['std20'], extracteddata['Rate of Change in Price%'])
    ax.set_xlabel('Std20')
    ax.set_ylabel('RateofChangeinPrice')
    ax.set_title('Relationship between Std20 & RateofChangeinPrice')
    plt.show()
 
    #RSI and MFI
    fig, ax = plt.subplots()
    ax.scatter(extracteddata.RSI, extracteddata.MFI, c= np.random.rand(len(extracteddata)), alpha=0.5)
    ax.set_xlabel('RSI', fontsize=15)
    ax.set_ylabel('MFI', fontsize=15)
    ax.set_title('RSI and MFI')
    ax.grid(True)
    fig.tight_layout()
    plt.show()
    
    #Season
    plt.figure(10).suptitle('Reversal Point of Season',fontsize=14)
    sns.countplot(x='Season',hue='reversal', data=extracteddata)
    plt.show()
    
    #Weekday
    plt.figure(11).suptitle('Reversal Point of Weekday',fontsize=14)
    sns.countplot(x='Weekday',hue='reversal', data=extracteddata)
    plt.show()
     
    #Unemployment Rate, Inflation Rate and Net Trade in Goods
    plt.figure(12)
    x = extracteddata['Inflation Rate United States']
    y = extracteddata['Unemployment Rate United States']
    z = extracteddata['Net Trade in Goods United States']
    plt.subplot(211)
    plt.scatter(y, x, color = 'r')
    plt.xlabel('Unemployment Rate (%)')
    plt.ylabel('Inflation Rate (%)')
    plt.title('Relationship between Unemployment Rate and Inflation Rate')
    plt.subplot(212)
    plt.scatter(y, z, color = 'g')
    plt.xlabel('Unemployment Rate (%)')
    plt.ylabel('Net Trade in Goods (Billions in Dollars)')
    plt.title('Relationship between Unemployment Rate and Net Trade in Goods')
    plt.tight_layout()
    plt.show()

    #Inflation Rate and Interest Rate
    fig,ax=plt.subplots()
    ax.plot(extracteddata['Interest Rate United States'], extracteddata['Inflation Rate United States'], '^',alpha=0.5)
    ax.set_title('Relationship between Interest Rate and Inflation Rate')            
    ax.set_xlabel("Interest Rate")
    ax.set_ylabel("Inflation Rate")
    plt.show()
 
    #ATR and Bandwidth
    fig,ax=plt.subplots()
    plt.plot(data.Date,df.ATR,color='yellow',label ='ATR')
    plt.plot(data.Date,df.Bandwidth,color='red',label ='Bandwidth')
    plt.title('Relationsip between ATR and Bandwidth')            
    plt.xlabel("Years")
    plt.ylabel("Value of ATR and Bandwidth")
    plt.legend()
    plt.show()
    
    #Reversal Points
    fig,ax=plt.subplots()
    plt.plot(data.Date,df.ma5,color='yellow',label='ma5')
    plt.plot(data.Date,df.ma30,color='red',label='ma30')
    plt.plot(data.Date,data.Price,label='Price')
    for i in range (len(df.Price)):
        if df.reversal[i] == 1 or df.reversal[i] == -1:
            plt.plot(data.Date[i], data.Price[i], marker='o', color='purple')
    plt.legend()
    plt.show()
    
   
data,df=main()
dummies=['Bollinger Market Trend','Pattern 1','Pattern 2','Pattern 3','Pattern 4']
df = CreateDummies(df)
df2=RemoveNone(df)
reversaldata,nonreversaldata,extracteddata=extractdata(df2)
Visualisation(extracteddata,data,df)

def removenone(extracteddata):
    extracteddata=extracteddata.where((pd.notnull(extracteddata)), None)
    extracteddata = extracteddata[extracteddata.astype(str).ne('None').all(1)]
    extracteddata = extracteddata.reset_index(drop=True)
    return extracteddata

def testsplit(x,y):
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1)
    return x_train, x_test, y_train, y_test

def normalisation():
    normalisation = sklearn.preprocessing.normalise()
    return normalisation