Login Page - Create Account

Support Board


Date/Time: Sun, 19 May 2024 00:29:18 +0000



Post From: Constant CPU Exceptions When Clicking "Add Custom Study" Button

[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