0% found this document useful (0 votes)
11 views5 pages

PCR Analysis Vba Module

The document outlines a VBA script for analyzing PCR data in an Excel workbook, including data formatting and trend chart creation. It includes functionalities for conditional formatting based on PCR values, a refresh button for updating data, and instructions for using the module. The script enhances the PCR Analysis tab with current values, market sentiment analysis, and visual representations of trends.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

PCR Analysis Vba Module

The document outlines a VBA script for analyzing PCR data in an Excel workbook, including data formatting and trend chart creation. It includes functionalities for conditional formatting based on PCR values, a refresh button for updating data, and instructions for using the module. The script enhances the PCR Analysis tab with current values, market sentiment analysis, and visual representations of trends.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

wsData.Cells(startRow + i, 1).

Value
wsAnalysis.Cells(31 + i, 1).NumberFormat = "dd-mmm-yyyy hh:mm"

' Weekly PCR


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

' Overall PCR


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

' Weekly Put OI


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

' Weekly Call OI


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

' Overall Put OI


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

' Overall Call OI


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

' Signal
Dim rowPCR As Double
rowPCR = 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
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

' 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
.SetSourceData Source:=wsData.Range("A" & startRow & ":A" & lastRow & ",C"
& startRow & ":C" & lastRow & ",G" & startRow & ":G" & 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