write a vba to calculate heiken candle and if heiken be green candle gives 1 to column q…
- User
write a vba to calculate heiken candle and if heiken be green candle gives 1 to column q and if hiken candle be red column q be -1 .open high and low and close are in column a to d
- Assistant
Below is a VBA code to calculate Heikin Ashi candles and output the values in column Q:
´´´vba Option Explicit
Sub CalculateHeikinAshi() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim haOpen As Double, haHigh As Double, haLow As Double, haClose As Double
Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change "Sheet1" to your sheet name lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow If i = 2 Then haClose = (ws.Cells(i, "A") + ws.Cells(i, "B") + ws.Cells(i, "C") + ws.Cells(i, "D")) / 4 haOpen = (ws.Cells(i, "A") + ws.Cells(i, "D")) / 2 Else haClose = (ws.Cells(i, "A") + ws.Cells(i, "B") + ws.Cells(i, "C") + ws.Cells(i, "D")) / 4 haOpen = (ws.Cells(i - 1, "Q") + ws.Cells(i - 1, "R")) / 2 End If haHigh = Application.WorksheetFunction.Max(ws.Cells(i, "B"), haOpen, haClose) haLow = Application.WorksheetFunction.Min(ws.Cells(i, "C"), haOpen, haClose) ws.Cells(i, "O") = haOpen ws.Cells(i, "P") = haHigh ws.Cells(i, "Q") = haLow ws.Cells(i, "R") = haClose If haClose > haOpen Then ws.Cells(i, "S") = 1 Else ws.Cells(i, "S") = -1 End If Next i ' Set column headers ws.Cells(1, "O") = "HA Open" ws.Cells(1, "P") = "HA High" ws.Cells(1, "Q") = "HA Low" ws.Cells(1, "R") = "HA Close" ws.Cells(1, "S") = "Signal" MsgBox "Heikin Ashi calculation complete.", vbInformation, "Done"