#include "sierrachart.h"

SCDLLName("ReadFromOtherChart");
/*==================================================================================*/
SCSFExport scsf_ReadFromOtherChart(SCStudyInterfaceRef sc)
{
	auto& RefStudy = sc.Input[0];
	auto& RefData = sc.Subgraph[0];

	if (sc.SetDefaults)
	{
		sc.GraphName = "Read From Other Chart";
		sc.AutoLoop = 0;
		sc.ScaleRangeType = SCALE_AUTO;
		sc.DisplayStudyInputValues = 0;

		RefStudy.Name = "Referenced Study";
		RefStudy.SetChartStudyValues(0, 0);

		RefData.Name = "Referenced Study Data";
		RefData.DrawStyle = DRAWSTYLE_LINE;
		RefData.PrimaryColor = RGB(0, 255, 0);
		RefData.DrawZeros = 1;
		RefData.LineWidth = 1;

		return;
	}

	if (RefStudy.GetChartNumber() == 0)
		return;

	// Example works correctly only if chart time frames are the same
	// Here we only read the first subgraph of the referenced study
	
	auto refData = SCGraphData{};

	sc.GetStudyArraysFromChartUsingID(RefStudy.GetChartNumber()
									, RefStudy.GetStudyID()
									, refData);

	if (refData.GetArraySize() == 0
		|| refData[0].GetArraySize() == 0)
		return;

	// Copy the data from the referenced study to the current study
	for (auto i = sc.UpdateStartIndex; i < sc.ArraySize; ++i)
		RefData[i] = refData[0][i];
}

/*==================================================================================*/
SCSFExport scsf_OtherChartStudy(SCStudyInterfaceRef sc)
{
	auto& Length = sc.Input[0];
	auto& Data = sc.Subgraph[0];

	if (sc.SetDefaults)
	{
		sc.GraphName = "Other Chart Study";
		sc.AutoLoop = 0;
		sc.ScaleRangeType = SCALE_AUTO;
		sc.DisplayStudyInputValues = 0;

		Length.Name = "Length";
		Length.SetInt(14);
		Length.SetIntLimits(1, 1000);

		Data.Name = "Study Data";
		Data.DrawStyle = DRAWSTYLE_LINE;
		Data.PrimaryColor = RGB(0, 255, 0);
		Data.DrawZeros = 1;
		Data.LineWidth = 1;

		return;
	}

	for (auto i = sc.UpdateStartIndex; i < sc.ArraySize; ++i)
	{
		sc.MovingAverage(sc.Close,
						Data,
						MovAvgTypeEnum::MOVAVGTYPE_EXPONENTIAL,
						i,
						Length.GetInt());
	}
}

/*==================================================================================*/