Building a Technical Analysis Dashboard for NEPSE Stocks with Real-Time Data

Building a Technical Analysis Dashboard for NEPSE Stocks with Real-Time Data

As a developer or data scientist in Nepal, creating a reliable and efficient technical analysis dashboard for the NEPSE stock market can be a daunting task, especially when dealing with real-time data and complex technical indicators. In this post, we'll address this challenge by providing a step-by-step guide to building a production-ready dashboard, utilizing real-time data from the Yahoo Finance API and implementing key technical indicators to predict stock market trends.

Key Takeaways

  • Fetch real-time stock prices from the Yahoo Finance API to power your dashboard.
  • Implement technical indicators such as Moving Averages and Relative Strength Index (RSI) to analyze stock market trends.
  • Utilize a library like Dash to create an interactive and user-friendly dashboard for visualizing technical indicators and stock prices.

The Problem

Many developers and data scientists in Nepal struggle to create a reliable and efficient technical analysis dashboard for the NEPSE stock market, often due to the lack of real-time data and limited understanding of technical indicators. This post addresses this pain point by providing a step-by-step guide to building a production-ready dashboard.

Data and Sources

We'll be using real-time data from the Yahoo Finance API, specifically the historical stock prices of NEPSE-listed companies. You can access the API documentation here. Data accessed on 2024-09-16.

Loading the Data

To fetch the real-time stock prices, we'll use the `requests` library to send a GET request to the Yahoo Finance API.

import requests
import pandas as pd

def load_data(ticker):
    url = f"https://query1.finance.yahoo.com/v7/finance/quote?symbols={ticker}"
    response = requests.get(url)
    data = response.json()
    df = pd.DataFrame(data['quoteResponse']['result'])
    return df

The Core Logic

We'll implement two key technical indicators: Moving Averages and Relative Strength Index (RSI). The Moving Average indicator will help us identify trends, while the RSI indicator will help us identify overbought and oversold conditions.

import pandas as pd

def calculate_moving_average(df, window):
    df['MA'] = df['regularMarketPrice'].rolling(window=window).mean()
    return df

def calculate_rsi(df, window):
    delta = df['regularMarketPrice'].diff(1)
    up, down = delta.copy(), delta.copy()
    up[up < 0] = 0
    down[down > 0] = 0
    roll_up = up.rolling(window).mean()
    roll_down = down.rolling(window).mean().abs()
    rs = roll_up / roll_down
    rsi = 100.0 - (100.0 / (1.0 + rs))
    df['RSI'] = rsi
    return df

Putting It Together

We'll use the Dash library to create an interactive and user-friendly dashboard for visualizing the technical indicators and stock prices.

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H1('NEPSE Stock Market Dashboard'),
    dcc.Dropdown(
        id='ticker-dropdown',
        options=[{'label': 'NEPSE', 'value': 'NEPSE'}],
        value='NEPSE'
    ),
    dcc.Graph(id='stock-price-graph'),
    dcc.Graph(id='ma-graph'),
    dcc.Graph(id='rsi-graph')
])

@app.callback(
    Output('stock-price-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_stock_price_graph(ticker):
    df = load_data(ticker)
    fig = px.line(df, x='date', y='regularMarketPrice')
    return fig

@app.callback(
    Output('ma-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_ma_graph(ticker):
    df = load_data(ticker)
    df = calculate_moving_average(df, 50)
    fig = px.line(df, x='date', y='MA')
    return fig

@app.callback(
    Output('rsi-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_rsi_graph(ticker):
    df = load_data(ticker)
    df = calculate_rsi(df, 14)
    fig = px.line(df, x='date', y='RSI')
    return fig

if __name__ == '__main__':
    app.run_server()

Complete Script

The full runnable script combining all steps:

#!/usr/bin/env python3
import requests
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px

def load_data(ticker):
    url = f"https://query1.finance.yahoo.com/v7/finance/quote?symbols={ticker}"
    response = requests.get(url)
    data = response.json()
    df = pd.DataFrame(data['quoteResponse']['result'])
    return df

def calculate_moving_average(df, window):
    df['MA'] = df['regularMarketPrice'].rolling(window=window).mean()
    return df

def calculate_rsi(df, window):
    delta = df['regularMarketPrice'].diff(1)
    up, down = delta.copy(), delta.copy()
    up[up < 0] = 0
    down[down > 0] = 0
    roll_up = up.rolling(window).mean()
    roll_down = down.rolling(window).mean().abs()
    rs = roll_up / roll_down
    rsi = 100.0 - (100.0 / (1.0 + rs))
    df['RSI'] = rsi
    return df

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H1('NEPSE Stock Market Dashboard'),
    dcc.Dropdown(
        id='ticker-dropdown',
        options=[{'label': 'NEPSE', 'value': 'NEPSE'}],
        value='NEPSE'
    ),
    dcc.Graph(id='stock-price-graph'),
    dcc.Graph(id='ma-graph'),
    dcc.Graph(id='rsi-graph')
])

@app.callback(
    Output('stock-price-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_stock_price_graph(ticker):
    df = load_data(ticker)
    fig = px.line(df, x='date', y='regularMarketPrice')
    return fig

@app.callback(
    Output('ma-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_ma_graph(ticker):
    df = load_data(ticker)
    df = calculate_moving_average(df, 50)
    fig = px.line(df, x='date', y='MA')
    return fig

@app.callback(
    Output('rsi-graph', 'figure'),
    [Input('ticker-dropdown', 'value')]
)
def update_rsi_graph(ticker):
    df = load_data(ticker)
    df = calculate_rsi(df, 14)
    fig = px.line(df, x='date', y='RSI')
    return fig

if __name__ == '__main__':
    app.run_server()

Expected Output

When you run the script, you should see an interactive dashboard with three graphs: stock price, moving average, and RSI. You can select the ticker symbol from the dropdown menu to view the corresponding data.

Limitations and Tradeoffs

This dashboard has several limitations, including the use of a single data source (Yahoo Finance API) and the implementation of only two technical indicators (Moving Averages and RSI). Additionally, the dashboard does not account for other market factors that may affect stock prices, such as economic indicators or company performance. In a production environment, you would want to consider using multiple data sources, implementing additional technical indicators, and incorporating other market factors to create a more comprehensive dashboard.

Frequently Asked Questions

What is the purpose of the Moving Averages indicator?

The Moving Averages indicator helps identify trends in the stock market by calculating the average price of a stock over a certain period of time.

What is the purpose of the RSI indicator?

The RSI indicator helps identify overbought and oversold conditions in the stock market by measuring the magnitude of recent price changes.

Can I customize the dashboard to display additional technical indicators?

Yes, you can customize the dashboard to display additional technical indicators by implementing new functions and callbacks in the Dash application.

What I'd Change

In a production environment, I would focus on improving the dashboard's scalability and reliability by using a more robust data source, implementing additional technical indicators, and incorporating other market factors. I would also consider using a more advanced visualization library, such as Plotly or Bokeh, to create more interactive and engaging visualizations. Additionally, I would prioritize security and authentication to ensure that only authorized users can access the dashboard and view sensitive market data.

إرسال تعليق

Hi! How can we help you? Send us a message and we'll get back to you.