#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];		

// Creat 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
	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;		
}