Login Page - Create Account

Support Board


Date/Time: Mon, 06 May 2024 03:12:38 +0000



Constant CPU Exceptions When Clicking "Add Custom Study" Button

View Count: 1031

[2016-10-05 15:49:09]
User13863 - Posts: 10
Dear SC,

I successfully have coded many custom studies in SC over the years which included many C++ libraries: ,,,etc.

I certainly have made my fair share of mistakes so have encountered and debugged CPU exceptions in the past. If I remember correctly they usually happened after I added a study to the chart. In this case they are happening as soon as I hit the "Add Custom Study" button which I find truly strange. There doesn't seem to be a pattern to when they happen.

I had been doing my development using Visual. I tried repairing it with no luck. Upon seeing your forum note about switching compilers I figured I'd try the built-in compiler but that didn't work either.

I'm sure you guys are busy but if I can't write studies I won't be able to use SC anymore. Please help!


#include <cmath>
#include <algorithm> // std::sort
#include <string>
#include <unordered_map>
#include <vector>
// The top of every source code file must include this line
#include "sierrachart.h"
using namespace std;

SCDLLName("Custom_Studies")

SCSFExport scsf_FXNews(SCStudyGraphRef sc)
{
  SCInputRef Symbol = sc.Input[0];
  SCInputRef DebugDaysMedian = sc.Input[1];
  SCInputRef Debug5MinChanges = sc.Input[2];

  if (sc.SetDefaults)
  {
    sc.FreeDLL = 1; //CHANGE THIS!

    sc.GraphName = "FXNews";
    sc.GraphRegion = 0;
    sc.HideStudy = 1;

    sc.AutoLoop = 1;

    Symbol.Name = "Chart Symbol?";
    Symbol.SetCustomInputStrings("ACa;ACh;AJ;AN;AU;CaCh;CaJ;ChJ;EA;ECa;ECh;EG;EJ;EN;EU;GA;GCa;GCh;GJ;GN;GU;NCa;NCh;NJ;NU;UCa;UCh;UJ");
    Symbol.SetCustomInputIndex(0);
    DebugDaysMedian.Name = "Debug getting days HL median?";
    DebugDaysMedian.SetYesNo(0);
    Debug5MinChanges.Name = "Debug 5 minute changes?";
    Debug5MinChanges.SetYesNo(0);
  }

  int& hasRunOrNot = sc.GetPersistentInt(1);
  if (sc.Index == 0) { hasRunOrNot = -1; }
  if (sc.DownloadingHistoricalData || sc.IsFullRecalculation) { return; }
  if (hasRunOrNot != -1) { return; }
  hasRunOrNot = 0;

  SCString debugStr;

  //Load data from matching daily chart to calculate median of daily high-lows.
  SCGraphData dailyData; sc.GetChartBaseData(Symbol.GetIndex() + 1, dailyData);
  SCDateTimeArray dailyDates; sc.GetChartDateTimeArray(Symbol.GetIndex() + 1, dailyDates);

  vector<float> dayHLs;

  for (int dayI = 0; dayI < dailyDates.GetArraySize(); dayI++) {
    if (dailyDates[dayI].GetDayOfWeek() != SUNDAY) {
      dayHLs.push_back(dailyData[SC_HIGH][dayI]-dailyData[SC_LOW][dayI]);
      if (DebugDaysMedian.GetYesNo()) {
        debugStr.Format("%d/%d/%d ... %6.4f",
          dailyDates[dayI].GetMonth(), dailyDates[dayI].GetDay(), dailyDates[dayI].GetYear(),
          dailyData[SC_HIGH][dayI] - dailyData[SC_LOW][dayI]
        );
        sc.AddMessageToLog(debugStr, 0);
      }
    }
  }
  nth_element(dayHLs.begin(), dayHLs.begin() + dayHLs.size() / 2, dayHLs.end());
  if (DebugDaysMedian.GetYesNo()) { debugStr.Format("Median: %6.4f ... %.1f", dayHLs[dayHLs.size() / 2]); sc.AddMessageToLog(debugStr, 1);}
  float median = dayHLs[dayHLs.size() / 2];
  
  //Get highest movement within each 5 minute period.
  vector<pair<float, int>> movements; float tempMovement = 0; int tempBarI = -1;
  for (int barI = 0; barI < sc.Index-5; barI++){
    if (barI > 0 && (barI % 5 == 0 || barI == sc.Index - 6)) {
      movements.emplace_back(tempMovement, tempBarI);
      tempMovement = 0; tempBarI = -1;
    }
    if (sc.BaseDateTimeIn[barI + 4] - sc.BaseDateTimeIn[barI] < 5 * MINUTES) {      
      if (abs(sc.Close[barI + 4] - sc.Open[barI]) > tempMovement) {
        tempMovement = abs(sc.Close[barI + 4] - sc.Open[barI]); tempBarI = barI;
      }
    }

  }

  if (Debug5MinChanges.GetYesNo()) {
    for (int i = 0; i < movements.size(); i++) {
      if (i > 2000) { break; }
      debugStr.Format("%d/%d/%d %d:%d ... %6.4f",
        sc.BaseDateTimeIn[movements[i].second].GetMonth(), sc.BaseDateTimeIn[movements[i].second].GetDay(), sc.BaseDateTimeIn[movements[i].second].GetYear(),
        sc.BaseDateTimeIn[movements[i].second].GetHour(), sc.BaseDateTimeIn[movements[i].second].GetMinute(), movements[i].first
      );
      sc.AddMessageToLog(debugStr, 0);
    }
  }

  //Sort 5 minute changes.
  sort(movements.begin(), movements.end());

}

Date Time Of Last Edit: 2016-10-05 21:03:38
[2016-10-05 17:09:17]
Sierra Chart Engineering - Posts: 104368
We are checking on this. There may have been a problem in the recent releases where the sc structure has changed.

Just give us a couple of hours to look into this.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2016-10-05 17:24:20]
User13863 - Posts: 10
Thanks for your reply! I'll await more information.
[2016-10-05 18:02:19]
Sierra Chart Engineering - Posts: 104368
The problem is not within Sierra Chart. We have verified that.

Your code apparently does have some problem with it if it is causing an exception.

This is not going to solve the underlying problem but this block of code needs a return statement at the end:
if (sc.SetDefaults)
{
sc.FreeDLL = 1; //CHANGE THIS!


sc.GraphName = "FXNews";
sc.GraphRegion = 0;
sc.HideStudy = 1;

sc.AutoLoop = 1;

Symbol.Name = "Chart Symbol?";
Symbol.SetCustomInputStrings("ACa;ACh;AJ;AN;AU;CaCh;CaJ;ChJ;EA;ECa;ECh;EG;EJ;EN;EU;GA;GCa;GCh;GJ;GN;GU;NCa;NCh;NJ;NU;UCa;UCh;UJ");
Symbol.SetCustomInputIndex(0);
DebugDaysMedian.Name = "Debug getting days HL median?";
DebugDaysMedian.SetYesNo(0);
Debug5MinChanges.Name = "Debug 5 minute changes?";
Debug5MinChanges.SetYesNo(0);

return;//this is needed

}

Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2016-10-05 18:03:36
[2016-10-05 22:22:39]
User13863 - Posts: 10
I'm so thankful for your response as my problem was the missing return! I must have accidentally deleted it and I don't think I ever would have thought to check for it. I should have known that if SC was giving me a CPU exception upon hitting the "Add Custom Study" button (before I ever added it to the chart) that it was something wrong in the basic structure of the study.

Anyway your platform is great--better than Ninjatrader and MarketDelta among others--and you guys continue to add awesome features (e.g. historical market depth on chart). Thanks!
[2016-10-05 22:28:48]
Sierra Chart Engineering - Posts: 104368
OK good.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

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

Login

Login Page - Create Account