Login Page - Create Account

Support Board


Date/Time: Fri, 18 Oct 2024 04:43:06 +0000



Way to COUNT number of bars ma1 > ma2?

View Count: 211

[2024-06-20 12:53:48]
TopGunTrader - Posts: 165
Hi,

I can't seem to find a way to COUNT when one variable is above another say 20 period MA > 50? If say on 1m chart 20 is over 50 for 30 mins or 60 might want to look for counter trend signals.
[2024-06-20 13:35:48]
John - SC Support - Posts: 35333
Refer to the following:
Spreadsheet Example Formulas and Usage: Formula to Count Bars Since An Event
For the most reliable, advanced, and zero cost futures order routing, use the Teton service:
Sierra Chart Teton Futures Order Routing
[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 - 162 views
imagebar-count-screenshot.png / V - Attached On 2024-06-20 14:47:40 UTC - Size: 105.98 KB - 38 views
[2024-06-20 22:33:00]
TopGunTrader - Posts: 165
Hi,

Thank you so much works flawless!

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account