Login Page - Create Account

Support Board


Date/Time: Sun, 29 Dec 2024 11:25:29 +0000



[User Discussion] - Read parameters from a file and set variables

View Count: 1537

[2016-01-19 19:01:57]
ertrader - Posts: 681
I have created a functioning example of how to read in parameters from a file. The .cpp and text file are attached. I have searched the support board and documentation and could not find a good example so I created one.

To get the code working:
1) Place the text file with parameters in the data folder or where your custom .dlls get compiled to
2) Compile the .cpp and attach to a graph as any other study

4 MA's will be drawn as an example.

My question is:
In regards to reading in values from a file and setting variables, is the method I have coded the correct and most efficient way for the SC environment?

In particular:
1) I am using global variables to pass multiple values (integers w,x,y,z read from the file and then the global variables are updated)
2) I am using iostream to read data that is space delimited. A close file command is not needed with iostream as best I can tell.

I'm not a c++ expert so am looking for suggestions.

Best regards
Date Time Of Last Edit: 2016-01-19 23:14:05
attachmenttextfile.txt - Attached On 2016-01-19 18:52:34 UTC - Size: 13 B - 333 views
attachmentReadFileExample.cpp - Attached On 2016-01-19 19:04:24 UTC - Size: 3.11 KB - 422 views
[2016-01-19 19:08:03]
ertrader - Posts: 681
#include <iostream>
#include <fstream>
#include "sierrachart.h"

// global variables
float g_Len1, g_Len2, g_Len3, g_Len4, g_Len5;

// function to read data from a file
float ReadFile(SCStudyInterfaceRef sc);

SCDLLName("ReadFileExample")
SCSFExport scsf_ReadFileExample(SCStudyGraphRef sc)
{
// Use MA's as examples of passing parameters
  SCSubgraphRef MA1         = sc.Subgraph[0];
  SCSubgraphRef MA2         = sc.Subgraph[1];
  SCSubgraphRef MA3         = sc.Subgraph[2];  
  SCSubgraphRef MA4         = sc.Subgraph[3];

//used for testing  
  SCSubgraphRef Values1      = sc.Subgraph[4];
  SCSubgraphRef Values2      = sc.Subgraph[5];    

// Create input for MA type
  SCInputRef INmatype        = sc.Input[0];
  
if(sc.SetDefaults)
{
sc.GraphName="Read File Example";
sc.StudyDescription="Read File Example and set Variables";

sc.DrawZeros = 0;
sc.AutoLoop = true;
sc.GraphRegion = 0;
    sc.FreeDLL=0; // 1 = dll is freed up, this allows debugging. Change back to 0 when code is complete. sc.FreeDLL to FALSE (0)
    sc.ValueFormat = 4;
    sc.MaintainAdditionalChartDataArrays = 1;    
    //sc.CalculationPrecedence = LOW_PREC_LEVEL;    
    
  MA1.Name="MA1";
MA1.DrawStyle = DRAWSTYLE_LINE;
MA1.LineWidth = 1;
  MA1.PrimaryColor = COLOR_YELLOW;
  
  MA2.Name="MA2";
MA2.DrawStyle = DRAWSTYLE_LINE;
MA2.LineWidth = 1;
MA2.PrimaryColor = COLOR_GREEN;
    
MA3.Name="MA3";
MA3.DrawStyle = DRAWSTYLE_LINE;
MA3.LineWidth = 1;
MA3.PrimaryColor = COLOR_RED;
    
MA4.Name="MA4";
MA4.DrawStyle = DRAWSTYLE_LINE;
MA4.LineWidth = 1;
MA4.PrimaryColor = COLOR_BLUE;

  INmatype.Name = "Moving Average Type";
  INmatype.SetMovAvgType(MOVAVGTYPE_WEIGHTED);

// used for testing    
    Values1.Name="Values1";
    Values1.DrawStyle = DRAWSTYLE_LINE;
    Values1.LineWidth = 1;
    Values1.PrimaryColor = COLOR_BLUE;
    Values1.DrawZeros = true;    

    Values2.Name="Values2";
    Values2.DrawStyle = DRAWSTYLE_LINE;
    Values2.LineWidth = 1;
    Values2.PrimaryColor = COLOR_BLUE;
    Values2.DrawZeros = true;      
return;
}

// create integers for 4 MA lengths
int iMA1, iMA2, iMA3, iMA4;

//read from file only on first bar so the file is not opened multiple times
  if (sc.Index == 0) ReadFile(sc);

// set the MA variables = to the global variables from the file
  iMA1 = g_Len1;
  iMA2 = g_Len2;
  iMA3 = g_Len3;
  iMA4 = g_Len4;  

// draw the MA's  for demonstration
  sc.MovingAverage(sc.BaseDataIn[SC_LAST], MA1, INmatype.GetMovAvgType(), iMA1);
  sc.MovingAverage(sc.BaseDataIn[SC_LAST], MA2, INmatype.GetMovAvgType(), iMA2);
  sc.MovingAverage(sc.BaseDataIn[SC_LAST], MA3, INmatype.GetMovAvgType(), iMA3);
  sc.MovingAverage(sc.BaseDataIn[SC_LAST], MA4, INmatype.GetMovAvgType(), iMA4);

// used for testing
  Values1[sc.Index] = g_Len1;
  Values2[sc.Index] = g_Len2;
}

float ReadFile(SCStudyInterfaceRef sc)
{
  std::ifstream input("textfile.txt");
  int w,x,y,z;
  input >> w >> x >> y >> z;
  
  // set the global variables to local variables so the parameters are passed
  g_Len1 = w;
  g_Len2 = x;
  g_Len3 = y;
  g_Len4 = z;    
}

Date Time Of Last Edit: 2016-01-19 23:16:24

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

Login

Login Page - Create Account