how to use CountIF?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Ram
Posts: 90
Joined: 25 Nov 2014
Has thanked: 17 times
Been thanked: 5 times

how to use CountIF?

Postby Ram » 01 Dec 2014

Dear All,

I'm looking for a way of getting the number of times a certain price was crossed over from last bar, I know how to get this done using loops but as I need to scan many prices I prefer to use CountIF instead of loops inside loops..

Can anyone pls point me on how to do this?

Thanks much in advance!
Best,
Ram

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: how to use CountIF?

Postby Henry MultiСharts » 01 Dec 2014

Hello Ram,

It works the same way as in the PowerLanguage. You can find the description and examples in the Functions & Reserved Words Reference guide.

Ram
Posts: 90
Joined: 25 Nov 2014
Has thanked: 17 times
Been thanked: 5 times

Re: how to use CountIF?  [SOLVED]

Postby Ram » 04 Dec 2014

for anyone interested in a working example:

Code: Select all

Imports System
Imports System.Drawing
Imports PowerLanguage
Imports PowerLanguage.Indicator
Imports PowerLanguage.Function
Imports System.Linq

Namespace PowerLanguage.Indicator
Public Class countIF
Inherits IndicatorObject

Public Sub New(ByVal _ctx As Object)
MyBase.New(_ctx)
End Sub

Protected Overrides Sub CalcBar()
If Bars.LastBarOnChart Then
MyPrice = 2000
' count how many times the HIGH was above MyPrice
MyCount = Me.criteria.CountIF(ExecInfo.MaxBarsBack)
'print out the results
Output.WriteLine(MyCount)
StopCalc()
End If
End Sub

Protected Overrides Sub Create()
End Sub

Protected Overrides Sub StartCalc()
ExecInfo.MaxBarsBack = Bars.FullSymbolData.Count
'criteria for HIGH level count example
Me.m_criteria = New Lambda(Of Boolean)(Function(_bb) PublicFunctions.DoubleGreater(MyBase.Bars.High.Item(_bb), MyPrice))
End Sub

Private ReadOnly Property criteria As ISeries(Of Boolean)
Get
Return Me.m_criteria
End Get
End Property

Private m_criteria As ISeries(Of Boolean)

Private MyPrice as Double
Private MyCount as Double

End Class
End Namespace


Return to “MultiCharts .NET”