0% found this document useful (0 votes)
3 views

Super Simple Pcr Vba Module

The document outlines a PCR analysis module for backtesting NIFTY options in Excel, featuring a one-touch data refresh button, management of PCR analysis tabs, and integration with an NSE data feed. It includes functions for importing CSV data, creating and updating analysis sheets, and formatting data for visualization. The module also provides market sentiment analysis based on PCR values and updates the analysis sheet with the latest data and trends.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Super Simple Pcr Vba Module

The document outlines a PCR analysis module for backtesting NIFTY options in Excel, featuring a one-touch data refresh button, management of PCR analysis tabs, and integration with an NSE data feed. It includes functions for importing CSV data, creating and updating analysis sheets, and formatting data for visualization. The module also provides market sentiment analysis based on PCR values and updates the analysis sheet with the latest data and trends.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Option Explicit

' ===== PCR ANALYSIS MODULE FOR NIFTY BACKTESTING.xlsx =====


' This module provides:
' 1. One-touch data refresh button
' 2. PCR Analysis tab creation and management
' 3. Conditional formatting and visualization tools
' 4. Integration with NSE data feed

' ===== GLOBALS =====


Public Const PCR_SHEET_NAME As String = "PCR Analysis"
Public Const DATA_SHEET_NAME As String = "PCR_Data"
Public Const SCRIPT_PATH As String = "C:\Users\DOGA\Documents\NIFTY PCR\
very_simple_excel_integration.py"
Public Const CSV_PATH As String = "C:\Users\DOGA\Documents\NIFTY PCR\data\
pcr_data.csv"

' Window state constants


Public Const WINDOW_NORMAL = 1
Public Const WINDOW_MINIMIZED = 2
Public Const WINDOW_MAXIMIZED = 3

' ===== MAIN FUNCTIONS =====

Sub RefreshPCRData()
' One-touch refresh button implementation
' Fetches fresh PCR data and updates analysis

' Show status


Application.StatusBar = "Fetching PCR data from NSE..."

' Disable screen updating to prevent flicker


Application.ScreenUpdating = False

' Check if PCR Analysis sheet exists, create if not


If Not SheetExists(PCR_SHEET_NAME) Then
CreatePCRAnalysisSheet
End If

' Check if data sheet exists


If Not SheetExists(DATA_SHEET_NAME) Then
' Create data sheet if it doesn't exist
ThisWorkbook.Sheets.Add.Name = DATA_SHEET_NAME
End If

' Run Python script to fetch data


Dim command As String
command = "python """ & SCRIPT_PATH & """"

' Run the command using Shell


Dim returnValue As Variant
returnValue = Shell("cmd.exe /c " & command, WINDOW_MINIMIZED)

' Wait for script to complete


Application.Wait Now + TimeValue("00:00:05")

' Import the CSV data into the Excel sheet


ImportCSVData
' Update PCR analysis
UpdatePCRAnalysis

' Reset status


Application.StatusBar = False
Application.ScreenUpdating = True

' Show completion message


MsgBox "PCR data updated successfully!", vbInformation, "PCR Analysis"
End Sub

Sub ImportCSVData()
' Imports CSV data into the PCR_Data sheet

Dim wsData As Worksheet

' Get or create the data sheet


On Error Resume Next
Set wsData = ThisWorkbook.Sheets(DATA_SHEET_NAME)
On Error GoTo 0

If wsData Is Nothing Then


ThisWorkbook.Sheets.Add.Name = DATA_SHEET_NAME
Set wsData = ThisWorkbook.Sheets(DATA_SHEET_NAME)
End If

' Clear existing data


wsData.Cells.Clear

' Check if CSV file exists


If Dir(CSV_PATH) = "" Then
MsgBox "CSV data file not found at: " & CSV_PATH, vbExclamation
Exit Sub
End If

' Open the CSV file


Dim fso As Object
Dim textStream As Object
Dim line As String
Dim values() As String
Dim row As Long

Set fso = CreateObject("Scripting.FileSystemObject")


Set textStream = fso.OpenTextFile(CSV_PATH, 1) ' 1 = ForReading

' Read CSV data line by line


row = 1
Do While Not textStream.AtEndOfStream
line = textStream.ReadLine
values = Split(line, ",")

' Write values to the sheet


Dim col As Long
For col = 0 To UBound(values)
wsData.Cells(row, col + 1).Value = values(col)
Next col

row = row + 1
Loop
' Close the text stream
textStream.Close
Set textStream = Nothing
Set fso = Nothing

' Format the data sheet


If row > 1 Then
wsData.Rows(1).Font.Bold = True
wsData.Columns.AutoFit
End If
End Sub

Sub CreatePCRAnalysisSheet()
' Creates a new PCR Analysis sheet with proper formatting

' Add a new sheet if it doesn't exist


Dim ws As Worksheet

' Check if sheet exists already


On Error Resume Next
Set ws = ThisWorkbook.Sheets(PCR_SHEET_NAME)
On Error GoTo 0

If ws Is Nothing Then
' Create new sheet
Set ws = ThisWorkbook.Sheets.Add
ws.Name = PCR_SHEET_NAME
End If

' Set up the PCR Analysis sheet


With ws
' Clear any existing content
.Cells.Clear

' Add title


.Range("A1").Value = "NIFTY OPTIONS PCR ANALYSIS"
.Range("A1:H1").Merge
.Range("A1").Font.Size = 16
.Range("A1").Font.Bold = True
.Range("A1").HorizontalAlignment = xlCenter

' Add last updated timestamp


.Range("A2").Value = "Last Updated:"
.Range("B2").Value = Now()
.Range("B2").NumberFormat = "dd-mmm-yyyy hh:mm:ss"

' Add refresh button (text)


.Range("G2").Value = "REFRESH DATA"
.Range("G2:H2").Merge
.Range("G2").Font.Bold = True
.Range("G2").HorizontalAlignment = xlCenter
.Range("G2").Interior.Color = RGB(0, 176, 80)
.Range("G2").Font.Color = RGB(255, 255, 255)

' Add section headers


.Range("A4").Value = "CURRENT PCR VALUES"
.Range("A4:H4").Merge
.Range("A4").Font.Bold = True
.Range("A4").Interior.Color = RGB(91, 155, 213)
.Range("A4").Font.Color = RGB(255, 255, 255)

' Current PCR metrics


.Range("A5").Value = "Weekly PCR:"
.Range("A6").Value = "Overall PCR:"
.Range("A7").Value = "PCR Trend:"

' Add summary area


.Range("A9").Value = "MARKET SIGNALS"
.Range("A9:H9").Merge
.Range("A9").Font.Bold = True
.Range("A9").Interior.Color = RGB(91, 155, 213)
.Range("A9").Font.Color = RGB(255, 255, 255)

' Market signal metrics


.Range("A10").Value = "Market Sentiment:"
.Range("A11").Value = "Signal Strength:"
.Range("A12").Value = "Reversal Risk:"

' Add chart section


.Range("A14").Value = "PCR TREND ANALYSIS"
.Range("A14:H14").Merge
.Range("A14").Font.Bold = True
.Range("A14").Interior.Color = RGB(91, 155, 213)
.Range("A14").Font.Color = RGB(255, 255, 255)

' Add data table section


.Range("A29").Value = "RECENT PCR DATA"
.Range("A29:H29").Merge
.Range("A29").Font.Bold = True
.Range("A29").Interior.Color = RGB(91, 155, 213)
.Range("A29").Font.Color = RGB(255, 255, 255)

' Column headers for data table


.Range("A30").Value = "Date"
.Range("B30").Value = "Weekly PCR"
.Range("C30").Value = "Overall PCR"
.Range("D30").Value = "Weekly Put OI"
.Range("E30").Value = "Weekly Call OI"
.Range("F30").Value = "Overall Put OI"
.Range("G30").Value = "Overall Call OI"
.Range("H30").Value = "Signal"

' Format column headers


.Range("A30:H30").Font.Bold = True
.Range("A30:H30").Interior.Color = RGB(198, 224, 180)

' Add notes section


.Range("A45").Value = "NOTES"
.Range("A45:H45").Merge
.Range("A45").Font.Bold = True
.Range("A45").Interior.Color = RGB(91, 155, 213)
.Range("A45").Font.Color = RGB(255, 255, 255)

' Add notes content


.Range("A46").Value = "PCR Interpretation:"
.Range("A47").Value = "PCR > 1.2: Bearish sentiment (too many puts,
potential reversal to upside)"
.Range("A48").Value = "PCR 1-1.2: Slightly bearish"
.Range("A49").Value = "PCR 0.8-1: Slightly bullish"
.Range("A50").Value = "PCR < 0.8: Bullish sentiment (too many calls,
potential reversal to downside)"

' Size columns appropriately


.Columns("A:H").AutoFit

' Create named ranges for easier reference


On Error Resume Next
ThisWorkbook.Names.Add Name:="WeeklyPCR", RefersTo:=.Range("B5")
ThisWorkbook.Names.Add Name:="OverallPCR", RefersTo:=.Range("B6")
ThisWorkbook.Names.Add Name:="PCRTrend", RefersTo:=.Range("B7")

ThisWorkbook.Names.Add Name:="MarketSentiment", RefersTo:=.Range("B10")


ThisWorkbook.Names.Add Name:="SignalStrength", RefersTo:=.Range("B11")
ThisWorkbook.Names.Add Name:="ReversalRisk", RefersTo:=.Range("B12")
On Error GoTo 0

' Assign refresh button macro


AssignRefreshButtonMacro
End With
End Sub

Sub UpdatePCRAnalysis()
' Updates the PCR Analysis sheet with latest data

' Get the PCR Analysis sheet


Dim wsAnalysis As Worksheet
Dim wsData As Worksheet

On Error Resume Next


Set wsAnalysis = ThisWorkbook.Sheets(PCR_SHEET_NAME)
Set wsData = ThisWorkbook.Sheets(DATA_SHEET_NAME)
On Error GoTo 0

' Check if sheets exist


If wsAnalysis Is Nothing Then
CreatePCRAnalysisSheet
Set wsAnalysis = ThisWorkbook.Sheets(PCR_SHEET_NAME)
End If

If wsData Is Nothing Then


MsgBox "Data sheet not found. Please refresh data first.", vbExclamation
Exit Sub
End If

' Check if data exists


Dim lastRow As Long
lastRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row

If lastRow <= 1 Then


wsAnalysis.Range("B5").Value = "No data"
wsAnalysis.Range("B6").Value = "No data"
Exit Sub
End If

' Get the latest data


' Based on CSV format:
' 0: Timestamp
' 1: Date
' 2: Weekly_PCR
' 3: Weekly_Put_OI
' 4: Weekly_Call_OI
' 5: Overall_PCR
' 6: Overall_Put_OI
' 7: Overall_Call_OI

Dim latestWeeklyPCR As Double


Dim latestOverallPCR As Double

latestWeeklyPCR = CDbl(wsData.Cells(lastRow, 3).Value) ' Weekly PCR in column


3
latestOverallPCR = CDbl(wsData.Cells(lastRow, 6).Value) ' Overall PCR in
column 6

' Update the current PCR values


wsAnalysis.Range("B5").Value = latestWeeklyPCR
wsAnalysis.Range("B6").Value = latestOverallPCR

' Format PCR values


wsAnalysis.Range("B5").NumberFormat = "0.000"
wsAnalysis.Range("B6").NumberFormat = "0.000"

' Determine trend (if at least 2 data points)


Dim pcrTrend As String
pcrTrend = "Neutral"

If lastRow > 2 Then


Dim previousWeeklyPCR As Double
previousWeeklyPCR = CDbl(wsData.Cells(lastRow - 1, 3).Value)

If latestWeeklyPCR > previousWeeklyPCR + 0.1 Then


pcrTrend = "Rising ↑"
wsAnalysis.Range("B7").Font.Color = RGB(0, 128, 0) ' Green
ElseIf latestWeeklyPCR < previousWeeklyPCR - 0.1 Then
pcrTrend = "Falling ↓"
wsAnalysis.Range("B7").Font.Color = RGB(255, 0, 0) ' Red
Else
pcrTrend = "Neutral →"
wsAnalysis.Range("B7").Font.Color = RGB(0, 0, 0) ' Black
End If
End If

wsAnalysis.Range("B7").Value = pcrTrend

' Update last updated timestamp


wsAnalysis.Range("B2").Value = Now()

' Determine market signals


Dim sentiment As String
Dim signalStrength As String
Dim reversalRisk As String

' Sentiment based on latest Weekly PCR


If latestWeeklyPCR > 1.5 Then
sentiment = "Extremely Bearish"
wsAnalysis.Range("B10").Font.Color = RGB(192, 0, 0) ' Dark Red
signalStrength = "Strong"
reversalRisk = "High"
ElseIf latestWeeklyPCR > 1.2 Then
sentiment = "Bearish"
wsAnalysis.Range("B10").Font.Color = RGB(255, 0, 0) ' Red
signalStrength = "Moderate"
reversalRisk = "Moderate"
ElseIf latestWeeklyPCR > 1 Then
sentiment = "Slightly Bearish"
wsAnalysis.Range("B10").Font.Color = RGB(255, 128, 128) ' Light Red
signalStrength = "Weak"
reversalRisk = "Low"
ElseIf latestWeeklyPCR >= 0.8 Then
sentiment = "Neutral"
wsAnalysis.Range("B10").Font.Color = RGB(0, 0, 0) ' Black
signalStrength = "Neutral"
reversalRisk = "Low"
ElseIf latestWeeklyPCR >= 0.6 Then
sentiment = "Slightly Bullish"
wsAnalysis.Range("B10").Font.Color = RGB(0, 176, 80) ' Light Green
signalStrength = "Weak"
reversalRisk = "Low"
ElseIf latestWeeklyPCR >= 0.4 Then
sentiment = "Bullish"
wsAnalysis.Range("B10").Font.Color = RGB(0, 128, 0) ' Green
signalStrength = "Moderate"
reversalRisk = "Moderate"
Else
sentiment = "Extremely Bullish"
wsAnalysis.Range("B10").Font.Color = RGB(0, 100, 0) ' Dark Green
signalStrength = "Strong"
reversalRisk = "High"
End If

' Update market signals


wsAnalysis.Range("B10").Value = sentiment
wsAnalysis.Range("B11").Value = signalStrength
wsAnalysis.Range("B12").Value = reversalRisk

' Update data table


' Copy last 15 rows of data (or all if less than 15)
Dim dataRows As Long
dataRows = Application.WorksheetFunction.Min(15, lastRow - 1)

Dim startRow As Long


startRow = lastRow - dataRows + 1

' Clear existing data


wsAnalysis.Range("A31:H45").ClearContents

' Copy data


Dim i As Long
For i = 0 To dataRows - 1
' Make sure startRow + i is not less than 2 (to skip header)
If startRow + i >= 2 Then
' Date (column 2 in CSV data)
wsAnalysis.Cells(31 + i, 1).Value = wsData.Cells(startRow + i, 2).Value
wsAnalysis.Cells(31 + i, 1).NumberFormat = "dd-mmm-yyyy hh:mm"
' Weekly PCR (column 3 in CSV data)
wsAnalysis.Cells(31 + i, 2).Value = wsData.Cells(startRow + i, 3).Value
wsAnalysis.Cells(31 + i, 2).NumberFormat = "0.000"

' Overall PCR (column 6 in CSV data)


wsAnalysis.Cells(31 + i, 3).Value = wsData.Cells(startRow + i, 6).Value
wsAnalysis.Cells(31 + i, 3).NumberFormat = "0.000"

' Weekly Put OI (column 4 in CSV data)


wsAnalysis.Cells(31 + i, 4).Value = wsData.Cells(startRow + i, 4).Value
wsAnalysis.Cells(31 + i, 4).NumberFormat = "#,##0"

' Weekly Call OI (column 5 in CSV data)


wsAnalysis.Cells(31 + i, 5).Value = wsData.Cells(startRow + i, 5).Value
wsAnalysis.Cells(31 + i, 5).NumberFormat = "#,##0"

' Overall Put OI (column 7 in CSV data)


wsAnalysis.Cells(31 + i, 6).Value = wsData.Cells(startRow + i, 7).Value
wsAnalysis.Cells(31 + i, 6).NumberFormat = "#,##0"

' Overall Call OI (column 8 in CSV data)


wsAnalysis.Cells(31 + i, 7).Value = wsData.Cells(startRow + i, 8).Value
wsAnalysis.Cells(31 + i, 7).NumberFormat = "#,##0"

' Signal
Dim rowPCR As Double
rowPCR = CDbl(wsData.Cells(startRow + i, 3).Value)

If rowPCR > 1.2 Then


wsAnalysis.Cells(31 + i, 8).Value = "Bearish"
wsAnalysis.Cells(31 + i, 8).Font.Color = RGB(255, 0, 0)
ElseIf rowPCR > 1 Then
wsAnalysis.Cells(31 + i, 8).Value = "Slightly Bearish"
wsAnalysis.Cells(31 + i, 8).Font.Color = RGB(255, 128, 128)
ElseIf rowPCR >= 0.8 Then
wsAnalysis.Cells(31 + i, 8).Value = "Neutral"
wsAnalysis.Cells(31 + i, 8).Font.Color = RGB(0, 0, 0)
ElseIf rowPCR >= 0.6 Then
wsAnalysis.Cells(31 + i, 8).Value = "Slightly Bullish"
wsAnalysis.Cells(31 + i, 8).Font.Color = RGB(0, 176, 80)
Else
wsAnalysis.Cells(31 + i, 8).Value = "Bullish"
wsAnalysis.Cells(31 + i, 8).Font.Color = RGB(0, 128, 0)
End If
End If
Next i

' Create PCR trend chart


CreatePCRTrendChart

' Apply conditional formatting to the PCR values


ApplyConditionalFormattingToPCR
End Sub

Sub CreatePCRTrendChart()
' Creates or updates the PCR trend chart

Dim wsAnalysis As Worksheet


Dim wsData As Worksheet
On Error Resume Next
Set wsAnalysis = ThisWorkbook.Sheets(PCR_SHEET_NAME)
Set wsData = ThisWorkbook.Sheets(DATA_SHEET_NAME)
On Error GoTo 0

' Check if sheets exist


If wsAnalysis Is Nothing Or wsData Is Nothing Then
Exit Sub
End If

' Delete existing chart if it exists


Dim chartObj As ChartObject
For Each chartObj In wsAnalysis.ChartObjects
chartObj.Delete
Next chartObj

' Get data count


Dim lastRow As Long
lastRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row

' Need at least 2 data points for a chart


If lastRow <= 2 Then
Exit Sub
End If

' Determine how many data points to use (max 20)


Dim dataPoints As Long
dataPoints = Application.WorksheetFunction.Min(20, lastRow - 1)

' Define chart data range


Dim startRow As Long
startRow = lastRow - dataPoints + 1

' Make sure startRow is at least 2 to skip header


If startRow < 2 Then startRow = 2

' Create chart


Dim chartObj1 As ChartObject
Set chartObj1 = wsAnalysis.ChartObjects.Add(Left:=wsAnalysis.Range("A15").Left,
_
Width:=350, _
Top:=wsAnalysis.Range("A15").Top + 20,
_
Height:=200)

' Set up chart


With chartObj1.Chart
.ChartType = xlLine

' Use columns based on CSV format:


' Column 2: Date
' Column 3: Weekly PCR
' Column 6: Overall PCR
.SetSourceData Source:=wsData.Range("B" & startRow & ":B" & lastRow & ",C"
& startRow & ":C" & lastRow & ",F" & startRow & ":F" & lastRow)

.HasTitle = True
.ChartTitle.Text = "PCR Trend Analysis"
' Format axes
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Date"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "PCR Value"

' Add horizontal line at PCR = 1 (neutral level)


Dim neutralLine As Series
Set neutralLine = .SeriesCollection.NewSeries
neutralLine.Name = "Neutral Line"
neutralLine.Values = Array(1, 1)
neutralLine.XValues =
Array(.Axes(xlCategory).MinimumScale, .Axes(xlCategory).MaximumScale)
neutralLine.Format.Line.DashStyle = msoLineDash
neutralLine.Format.Line.ForeColor.RGB = RGB(255, 0, 0)

' Set series names


.SeriesCollection(1).Name = "Weekly PCR"
.SeriesCollection(2).Name = "Overall PCR"

' Format series colors


.SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(0, 112, 192)
.SeriesCollection(2).Format.Line.ForeColor.RGB = RGB(0, 176, 80)

' Add markers


.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
.SeriesCollection(2).MarkerStyle = xlMarkerStyleDiamond

' Add legend


.HasLegend = True
.Legend.Position = xlLegendPositionBottom
End With
End Sub

Sub ApplyConditionalFormattingToPCR()
' Applies conditional formatting to PCR values

Dim wsAnalysis As Worksheet

On Error Resume Next


Set wsAnalysis = ThisWorkbook.Sheets(PCR_SHEET_NAME)
On Error GoTo 0

If wsAnalysis Is Nothing Then


Exit Sub
End If

' Clear existing conditional formatting


wsAnalysis.Range("B5:B6").FormatConditions.Delete
wsAnalysis.Range("B31:B45").FormatConditions.Delete
wsAnalysis.Range("C31:C45").FormatConditions.Delete

' Weekly PCR value


With wsAnalysis.Range("B5").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlGreater, Formula1:="1.2")
.Interior.Color = RGB(255, 199, 206) ' Light red
End With
With wsAnalysis.Range("B5").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlLess, Formula1:="0.8")
.Interior.Color = RGB(198, 239, 206) ' Light green
End With

' Overall PCR value


With wsAnalysis.Range("B6").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlGreater, Formula1:="1.2")
.Interior.Color = RGB(255, 199, 206) ' Light red
End With

With wsAnalysis.Range("B6").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlLess, Formula1:="0.8")
.Interior.Color = RGB(198, 239, 206) ' Light green
End With

' Weekly PCR values in table


With wsAnalysis.Range("B31:B45").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlGreater, Formula1:="1.2")
.Interior.Color = RGB(255, 199, 206) ' Light red
End With

With wsAnalysis.Range("B31:B45").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlLess, Formula1:="0.8")
.Interior.Color = RGB(198, 239, 206) ' Light green
End With

' Overall PCR values in table


With wsAnalysis.Range("C31:C45").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlGreater, Formula1:="1.2")
.Interior.Color = RGB(255, 199, 206) ' Light red
End With

With wsAnalysis.Range("C31:C45").FormatConditions.Add(Type:=xlCellValue,
Operator:=xlLess, Formula1:="0.8")
.Interior.Color = RGB(198, 239, 206) ' Light green
End With
End Sub

Sub AssignRefreshButtonMacro()
' Assigns the refresh macro to the refresh button text cell

Dim wsAnalysis As Worksheet

On Error Resume Next


Set wsAnalysis = ThisWorkbook.Sheets(PCR_SHEET_NAME)
On Error GoTo 0

If wsAnalysis Is Nothing Then


Exit Sub
End If

' Create a button shape if it doesn't exist


Dim shp As Shape
Dim shapeExists As Boolean

shapeExists = False
For Each shp In wsAnalysis.Shapes
If shp.Name = "RefreshButton" Then
shapeExists = True
Exit For
End If
Next shp

If Not shapeExists Then


Set shp = wsAnalysis.Shapes.AddShape(msoShapeRectangle, _
wsAnalysis.Range("G2").Left, _
wsAnalysis.Range("G2").Top, _
wsAnalysis.Range("G2:H2").Width, _
wsAnalysis.Range("G2").Height)
shp.Name = "RefreshButton"
shp.Fill.ForeColor.RGB = RGB(0, 176, 80)
shp.Line.ForeColor.RGB = RGB(0, 112, 52)
shp.TextFrame2.TextRange.Characters.Text = "REFRESH PCR DATA"
shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 255, 255)
shp.TextFrame2.TextRange.Font.Bold = msoTrue
shp.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
shp.TextFrame2.VerticalAnchor = msoAnchorMiddle
End If

' Assign macro to the button


wsAnalysis.Shapes("RefreshButton").OnAction = "RefreshPCRData"
End Sub

' ===== HELPER FUNCTIONS =====

Function SheetExists(sheetName As String) As Boolean


' Checks if a sheet exists in the workbook

Dim ws As Worksheet

On Error Resume Next


Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0

SheetExists = Not ws Is Nothing


End Function

' ===== INSTRUCTIONS =====


'
' HOW TO USE THIS MODULE:
'
' 1. Open NIFTY BCKTESTING.xlsx
' 2. Press Alt+F11 to open the VBA Editor
' 3. Insert > Module and paste this entire code
' 4. Save as a macro-enabled workbook (.xlsm)
' 5. Run the "RefreshPCRData" macro to:
' - Create the PCR Analysis sheet
' - Fetch the latest PCR data
' - Update the analysis
'
' The one-touch button for refreshing data will be automatically created
' on the PCR Analysis tab.
'
' To refresh data:
' - Simply click the "REFRESH PCR DATA" button on the PCR Analysis tab
'
' The PCR Analysis tab provides:
' - Current PCR values with trend indicators
' - Market sentiment analysis
' - PCR trend chart
' - Recent PCR data table with signal indicators
' - Explanatory notes
'

You might also like