// TWAP BETA_1 // The top of every source code file must include this line #include "sierrachart.h" // For reference, refer to this page: // https://www.sierrachart.com/index.php?page=doc/doc_CreatingDLLs.html // This line is required. Change the text within the quote // marks to what you want to name your group of custom studies. SCDLLName("TWAP") //This is the basic framework of a study function. SCSFExport scsf_TWAP(SCStudyInterfaceRef sc) { SCSubgraphRef TWAP = sc.Subgraph[1]; SCSubgraphRef TimeDiff = sc.Subgraph[2]; SCSubgraphRef PTime = sc.Subgraph[3]; // price * time SCSubgraphRef CPTime = sc.Subgraph[4]; // cumulative price*time SCSubgraphRef CTime = sc.Subgraph[5]; // cumulative time SCSubgraphRef Time = sc.Subgraph[6]; // cumulative time SCInputRef Time1 = sc.Input[1]; // Section 1 - Set the configuration variables and defaults if (sc.SetDefaults) { sc.GraphName = "TWAP"; // During development set this flag to 1, so the DLL can be rebuilt without restarting Sierra Chart. When development is completed, set it to 0 to improve performance. sc.FreeDLL = 1; sc.AutoLoop = 0; //Auto looping is enabled. sc.MaintainAdditionalChartDataArrays=1; TWAP.Name = "TWAP"; TWAP.DrawStyle = DRAWSTYLE_LINE; TWAP.PrimaryColor = RGB (0, 255, 0); Time1.Name = "Start Cash Session Time"; Time1.SetTime(HMS_TIME(9,30,0)); return; } // Section 2 - Do data processing here long i = 0; // refs to persistent vars float& cum = sc.PersistVars->f1; // int& InTime = sc.PersistVars->i1; // start time of cash session int& lastIndex = sc.PersistVars->i2; // lastIndex processed int& lastTime = sc.PersistVars->i3; // lastIndex processed if (sc.UpdateStartIndex == 0) { // init state i.e. persist vars InTime=Time1.GetTime(); lastIndex = -1; lastTime=0; } for (int i = sc.UpdateStartIndex; i < sc.ArraySize; i++) { if (i == lastIndex) continue; // block handling the same tick multiple times // starting TWAP at cash session or new day if (i == 0 || (sc.BaseDateTimeIn[i-1].GetDate() != sc.BaseDateTimeIn[i].GetDate()) || (sc.BaseDateTimeIn[i].GetTime()==InTime)) { TWAP[i]=0; TimeDiff[i]=0; CPTime[i]=0; PTime[i]=0; CTime[i]=0; lastTime=sc.BaseDateTimeIn[i].GetTime(); } else { TimeDiff[i]=sc.BaseDateTimeIn[i].GetTime()-lastTime; // seconds difference between ticks lastTime= sc.BaseDateTimeIn[i].GetTime(); if (TimeDiff[i]<=0) { TimeDiff[i]=1; } PTime[i]=sc.Close[i]*TimeDiff[i]; // price * time CTime[i] = CTime[i-1]+TimeDiff[i]; CPTime[i] =CPTime[i-1]+PTime[i]; TWAP[i]=CPTime[i]/CTime[i]; } lastIndex = i; } }