Login Page - Create Account

Support Board


Date/Time: Mon, 06 May 2024 06:27:04 +0000



Post From: Trial user: only able to apply study against current bar

[2019-12-13 04:20:45]
mm3599 - Posts: 13
Hello -

I am trialing SC.
I have written a custom function that is referenced by a custom study that I added to ExampleCustomStudies.cpp.

The study only appears to be passing in the current bar's data and not starting from bar zero.
The logging I have written only shows the last bar's index in the Message Log UI.

/*===============================================================================
MACDP Fast Function
*/
SCFloatArrayRef MACDPFast(SCStudyGraphRef sc, SCBaseDataRef BaseDataIn, SCFloatArrayRef Out, int Index, float Fast)
{
  //fastEMA[0]=((2.0 / (1 + Fast)) * Close[0] + (1 - (2.0 / (1 + Fast))) * fastEMA[1]);
  // fast_part_1_calc = (2.0 / (1 + Fast)) * Close[0]
  // fast_part_2_calc = (1 - (2.0 / (1 + Fast)))
  // fast_part_3_value = Subgraph_MACDP_Fast[1]
  SCString Output;
  sc.FormatString(Output, "Index=%i,Fast=%f", Index,Fast);
  sc.AddMessageToLog(Output, 0);  

  if (Index <= Fast)
    Out[Index] = BaseDataIn[SC_LAST][Index];

    
  else if (Index > Fast)
  {
    float fast_part_1_calc = (2.0 / (1 + Fast)) * BaseDataIn[SC_LAST][Index-1];
    float fast_part_2_calc = (1 - (2.0 / (1 + Fast)));
    Out[Index] = fast_part_1_calc + (fast_part_2_calc * Out[Index - 1]);
  }
  return Out;
  
}

Here's the calling code:
SCSFExport scsf_MACDP(SCStudyGraphRef sc)
{
  
  SCSubgraphRef Subgraph_MACDP = sc.Subgraph[0];
  SCFloatArrayRef Array_MACDP_Fast = sc.Subgraph[0].Arrays[0];

  //SCSubgraphRef Subgraph_SMA_Demin = sc.Subgraph[4];
  
  if (sc.SetDefaults)
  {
    sc.GraphName = "MACDP Rev 1";
    sc.StudyDescription = "MACDP calc test.";
    sc.AutoLoop = 1; // true
    sc.FreeDLL = 0;
    sc.GraphRegion = 0;
    Subgraph_MACDP.Name = "MACDP";
    Subgraph_MACDP.PrimaryColor = RGB(255,0,0); // Red
    Subgraph_MACDP.DrawStyle = DRAWSTYLE_LINE;
    return;
  }
  float fast, slow, smooth, macdp_value;
  fast  = 8.3897;
  slow = 17.5185;
  smooth = 9.0503;
  macdp_value = 0.0;
  
  //calculate the fast value for the macdp
  sc.DataStartIndex = 18;
  MACDPFast(sc, sc.BaseDataIn, Array_MACDP_Fast, sc.Index, fast);

  SCString Output;
  sc.FormatString(Output, "Array_MACDP_Fast[sc.Index]=%f,Array_MACDP_Fast[sc.Index-1]=%f", Array_MACDP_Fast[sc.Index],Array_MACDP_Fast[sc.Index-1]);
  sc.AddMessageToLog(Output, 0);  
}


Here a sample log output:
Chart: ES-201912-GLOBEX 30000 Trades #1 | Study: MACDP Rev 1 | Array_MACDP_Fast[sc.Index]=inf,Array_MACDP_Fast[sc.Index-1]=inf | 2019-12-12 23:17:09.508
Chart: ES-201912-GLOBEX 30000 Trades #1 | Study: MACDP Rev 1 | Index=1317,Fast=8.389700 | 2019-12-12 23:17:10.526