EMA(Exponential Moving Average) Formulae to Code Technical Indicators from Scratch in Python:
Follow Instructions from the Above Video then go for the Code Input.
Import the Libraries:
import pandas as pd
from fyers_apiv3 import fyersModel
from datetime import datetime,timedelta
import talib
import matplotlib.pyplot as plt
import sys
Call App ID and Access Token for Authorization and Set the Required Input:
client_id=open('app_id.txt','r').read()
token=open('access_token.txt','r').read().strip()
fyers=fyersModel.FyersModel(client_id=client_id,token=token,log_path='C:/Users/your_folder/Desktop/fyers')
days=1
symbol='NSE:NIFTYBANK-INDEX'
interval='1'
now=datetime.now()
from_date=now-timedelta(days=days)
from_date=datetime.strftime(from_date,'%Y-%m-%d')
to_date=datetime.strftime(now,'%Y-%m-%d')
print(now,symbol,interval,from_date,to_date)
Call the Historical Data Function by Using Fyers API:
def historical_data(symbol,interval,from_date,to_date):
data={'symbol':symbol,
'resolution':interval,
'date_format':'1',
'range_from':from_date,
'range_to':to_date,
'cont_flag':'1'
}
response=pd.DataFrame(fyers.history(data=data)['candles'],columns=['date','open','high','low','close','volume'])
return response
df=historical_data(symbol,interval,from_date,to_date)
Define the Exponential Moving Average Function:
def EMA(prices,period):
ema=[]
alpha=2/(period+1)
print(alpha)
print((1-alpha))
ema.append(sum(prices[:period])/period)
for price in prices[period:]:
ema_prev=ema[-1]
new_ema=price*alpha+ema_prev*(1-alpha)
ema.append(new_ema)
return ema
print(pd.DataFrame(EMA(df['close'],9)))
Show Full Code Input:
import pandas as pd
from fyers_apiv3 import fyersModel
from datetime import datetime,timedelta
import talib
import matplotlib.pyplot as plt
import sys
client_id=open('app_id.txt','r').read()
token=open('access_token.txt','r').read().strip()
fyers=fyersModel.FyersModel(client_id=client_id,token=token,log_path='C:/Users/your_folder/Desktop/fyers')
days=1
symbol='NSE:NIFTYBANK-INDEX'
interval='1'
now=datetime.now()
from_date=now-timedelta(days=days)
from_date=datetime.strftime(from_date,'%Y-%m-%d')
to_date=datetime.strftime(now,'%Y-%m-%d')
print(now,symbol,interval,from_date,to_date)
def historical_data(symbol,interval,from_date,to_date):
data={'symbol':symbol,
'resolution':interval,
'date_format':'1',
'range_from':from_date,
'range_to':to_date,
'cont_flag':'1'
}
response=pd.DataFrame(fyers.history(data=data)['candles'],columns=['date','open','high','low','close','volume'])
return response
df=historical_data(symbol,interval,from_date,to_date)
def EMA(prices,period):
ema=[]
alpha=2/(period+1)
print(alpha)
print((1-alpha))
ema.append(sum(prices[:period])/period)
for price in prices[period:]:
ema_prev=ema[-1]
new_ema=price*alpha+ema_prev*(1-alpha)
ema.append(new_ema)
return ema
print(pd.DataFrame(EMA(df['close'],9)))
Convert Formulae to Code Technical Indicators ATR in Python:
Follow Instructions from the Above Video then go for the Code Input.
import pandas as pd
from fyers_apiv3 import fyersModel
from datetime import datetime,timedelta
import numpy as np
client_id=open('app_id.txt','r').read()
token=open('access_token.txt','r').read().strip()
fyers=fyersModel.FyersModel(client_id=client_id,token=token,log_path='C:/Users/VIKASH/Desktop/fyersv3')
days=4
symbol='NSE:NIFTY50-INDEX'
interval='1'
now=datetime.now()
from_date=now-timedelta(days=days)
from_date=datetime.strftime(from_date,'%Y-%m-%d')
to_date=datetime.strftime(now,'%Y-%m-%d')
print(now,symbol,interval,from_date,to_date)
def historical_data(symbol,interval,from_date,to_date):
data={'symbol':symbol,
'resolution':interval,
'date_format':'1',
'range_from':from_date,
'range_to':to_date,
'cont_flag':'1'
}
response=pd.DataFrame(fyers.history(data=data)['candles'],columns=['date','open','high','low','close','volume'])
return response
df=historical_data(symbol,interval,from_date,to_date)
def TR(df):
df['pc']=df['close'].shift(1)
df['hl']=df['high']-df['low']
df['hpc']=np.abs(df['high']-df['pc'])
df['lpc']=np.abs(df['low']-df['pc'])
df['tr']=df[['hl','hpc','lpc']].max(axis=1)
return df['tr']
def ATR(tr,period=14):
df['True']=tr
df['atr']=df['True'].ewm(span=period).mean()
return df['atr']
print(ATR(TR(df),period=14))