Login Page - Create Account

Support Board


Date/Time: Sun, 28 Apr 2024 15:47:09 +0000



[Programming Help] - Code Correction help (Aim is to avoid consecutive buy/sell signals)

View Count: 518

[2019-09-02 10:37:38]
rvdesh - Posts: 28
/* Please help rectify the error in the code*/

#include "sierrachart.h"

/************************************************************/
SCDLLName("Gap Open Study")

SCSFExport scsf_GapOpenStudy(SCStudyInterfaceRef sc)
{
  SCSubgraphRef BuyEntrySubgraph = sc.Subgraph[0];
  SCSubgraphRef SellEntrySubgraph = sc.Subgraph[2];

  SCInputRef Enabled = sc.Input[0];


  if (sc.SetDefaults)
  {
    // Set the study configuration and defaults.

    sc.GraphName = "Gap Open Delay Study";

    sc.StudyDescription = "Signals based on Gap";

    sc.GraphRegion = 0;

    BuyEntrySubgraph.Name = "Buy Entry";
    BuyEntrySubgraph.DrawStyle = DRAWSTYLE_ARROW_UP;
    BuyEntrySubgraph.PrimaryColor = RGB(0, 255, 0);
    BuyEntrySubgraph.LineWidth = 2;
    BuyEntrySubgraph.DrawZeros = false;

    SellEntrySubgraph.Name = "Sell Entry";
    SellEntrySubgraph.DrawStyle = DRAWSTYLE_ARROW_DOWN;
    SellEntrySubgraph.PrimaryColor = RGB(255, 0, 0);
    SellEntrySubgraph.LineWidth = 2;
    SellEntrySubgraph.DrawZeros = false;

    Enabled.Name = "Enabled";
    Enabled.SetYesNo(1);

    sc.AutoLoop = 1;
    sc.GraphRegion = 0;

    return;
  }

float OpenValue = sc.Open[sc.Index - 1];
float CloseValue = sc.Close[sc.Index - 1];
float PrevLowValue = sc.Low[sc.Index - 2];
float PrevHighValue = sc.High[sc.Index - 2];

bool up = OpenValue < PrevLowValue;
bool dn = OpenValue > PrevHighValue;

/*The next two lines compile but there are too many up/down signals, causing whipsaws. */

bool buysignal = up && !dn[sc.Index - 1];
bool sellsignal = dn && !up[sc.Index - 1];

/*To avoid whipsaws I want buysignal to be true only if open < previous low and there is no sellsignal on the previous bar. Similarly , I want sellsignal to be true only if Open > Previous High and there is no buysignal on the previous bar
But the code is not building but returning error.

bool buysignal = up && !dn[sc.Index - 1];
bool sellsignal = dn && !up[sc.Index - 1];*/

if (buysignal == true)
{
  BuyEntrySubgraph[sc.Index] = sc.Low[sc.Index];
}

else if (sellsignal == true)
{
  SellEntrySubgraph[sc.Index] = sc.High[sc.Index];
}
}

//Please rectify error and help
Date Time Of Last Edit: 2019-09-02 10:39:26
[2019-09-02 11:43:30]
Ackin - Posts: 1865
Hi,
error is

bool dn = OpenValue > PrevHighValue;
and next !dn[sc.Index - 1]

you can't turn a bool variable into an array, variable is only one value




I hope I understand what you need...I choose the fastest and easiest way

1) create new SGs

SCSubgraphRef up = sc.Subgraph[3];
SCSubgraphRef dn = sc.Subgraph[4];

2) change rows up and dn
up[sc.Index] = OpenValue < PrevLowValue ? 1.0f : 0;
dn[sc.Index] = OpenValue > PrevHighValue ? 1.0f : 0;

3) correct condition
if ((up[sc.Index] == 1.0f)&&(dn[sc.Index-1] == 0))
{
BuyEntrySubgraph[sc.Index] = sc.Low[sc.Index];
}
// also change second condition for SELL




I'm not at the computer with c ++ right now, I'm writing it out of my head, but it should work.

Perhaps it helped you.
Date Time Of Last Edit: 2019-09-02 11:58:10
[2019-09-02 12:41:45]
rvdesh - Posts: 28
Thank you very much. It helped me.

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

Login

Login Page - Create Account