Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 08:42:25 +0000



Post From: Way to COUNT number of bars ma1 > ma2?

[2024-06-20 14:59:06]
emmanuel - Posts: 57
Here's another method.

I attached a custom study you can use to count the number of bars during a "signal" of your choice. A signal is simply a subgraph which produces a non-zero value.

In the attached screenshot I used the Spreadsheet formula study to produce a true/false (1 or 0) signal when the SMA 20 is above the SMA 50. The formula is: ID1.SG1 > ID2.SG1

ID1 is the identifier for the SMA 20, and SG1 is the first (and only) subgraph that's produced by the Moving Average - Simple study. You can see these identifiers in Analysis -> Studies.

I then configured the custom study provided, Bar Count During Signal study, to use the Spreadsheet formula study as its input.

You'd have to compile the study yourself. The instructions are at How to Build an Advanced Custom Study from Source Code

NOTE: The Spreadsheet formula study is limited to 2,000 bars by default, which isn't much on a 1 MIN. chart; You may need to increase this number since it limits how far back the counting can occur.


#include "sierrachart.h"
SCDLLName("Bar Count During Signal Study")

SCSFExport scsf_TemplateFunction(SCStudyInterfaceRef sc) {
SCSubgraphRef count = sc.Subgraph[0];
SCInputRef signalInput = sc.Input[0];
SCFloatArray signal;

  if(sc.SetDefaults) {
    sc.GraphName = "Bar Count During Signal Study";
sc.StudyDescription = "This study counts the number of bars while the given subgraph value is non-zero. The subgraph produced by this study contains the count.";
    sc.AutoLoop = 1;

signalInput.Name = "Subgraph to use as the signal to count.";
signalInput.SetChartStudySubgraphValues(1,1, 0);

    count.Name = "Count";
    count.DrawStyle = DRAWSTYLE_LINE;
    count.PrimaryColor = RGB (0, 255, 0);
count.DrawZeros = 1;
    
    return;
  }

sc.GetStudyArrayFromChartUsingID(signalInput.GetChartStudySubgraphValues(), signal);

if(signal[sc.Index] != 0) {
count[sc.Index] = count[sc.Index - 1] + 1;
}
}

attachmentBarCountDuringSignal.cpp - Attached On 2024-06-20 14:46:35 UTC - Size: 993 B - 205 views
imagebar-count-screenshot.png / V - Attached On 2024-06-20 14:47:40 UTC - Size: 105.98 KB - 55 views