見出し画像

カスタム増減率

前期比較してますかっ!(・∀・)
今回はカスタムファンダメンタルを改造した増減比較バージョンです
カスタムファンダメンタルに入力でき履歴のある項目なら多分大丈夫です

前期比営業CF一番増えたとこどこだろうと思って作ったので、
デフォルトは前年度比、営業CFになってます
入力項目を都度変えてもいいですし、コピーして項目部分を変えて営業利益増減とか自分の必要なものを作る事も簡単にできると思います
見出しは敢えてカスタム用にFundValueにしておきました
デフォルトはValue1=今期、Value2=前期、Value3=差額、Value4=%です

※ スタイルプロパティはバリュー1、2、3は数値、4は%です
※ 決算のタイミング等によって今期と前期の比較がまだ出来ない銘柄も
  あるので注意が必要です
※ スキャナーでスキャンする時は連結/単独の両方しないと
  全銘柄のスキャンをした事にならないので注意が必要です
※ スキャナーは最後に実行した設定基準のままなので一々変えるの面倒な  人は専用のプログラムを作ったり、連結用と単独用で2つスキャナー   作った方が楽だと思います(´д`;)

スキャナーの連単変更部分はスキャン基準の折り畳みを開いて
ここのConsolidationLevelです、1=連結、1以外が単独
ほんとこの項目はウザい(°_°)

プログラム

using elsystem;
using tsdata.marketdata;
using elsystem.collections;

inputs:
	string FundField( "CR_OPCF" ) [DisplayName = "FundField", ToolTip = "Fundamental field.  Enter the name of the fundamental field for which data is to be obtained."],
	int MonthsReported( 12 ) [DisplayName = "MonthsReported", ToolTip = "Enter the number of months in the reporting period for which data is desired.  Fundamental data usually covers a 3, 6 or 12 month period."],
	int ConsolidationLevel( 1 ) [DisplayName = "ConsolidationLevel", ToolTip = "Enter 1 if consolidated data is desired; enter any other value for non-consolidated data."],
	int AccountingStandard( 0 ) [DisplayName = "AccountingStandard", ToolTip = "Enter 1 for Japanese accounting standard; enter 2 for Securities and Exchange Commission (SEC) accounting standard; enter 3 for International Financial Reporting Standards (IFRS) accounting standard; enter 0 to use IFRS, SEC, or Japanese accounting standards, in that order, depending on which is available."],
	int PeriodsAgo1( 0 ) [DisplayName = "PeriodsAgo1", ToolTip = "Enter the number of fundamental periods ago of the desired value.  Enter 0 for the current value, 1 for one period ago, etc."],
	int PeriodsAgo2( 1 ) [DisplayName = "PeriodsAgo2", ToolTip = "Enter the number of fundamental periods ago of the desired value.  Enter 0 for the current value, 1 for one period ago, etc."],
	double ValueToPlotOnError( 0 ) [DisplayName = "ValueToPlotOnError", ToolTip = "Enter a value to be plotted if an error occurs retrieving the fundamental value."],
	UpColor( UpColorDefault ) [DisplayName = "上昇色", ToolTip = "上昇時"], 
	DnColor( DownColorDefault ) [DisplayName = "下降色", ToolTip = "下降時"] ;

variables:
	intrabarpersist bool InAChart( false ),
	intrabarpersist string ConsLevel( "Consolidated" ),
	intrabarpersist string AcctStdInputString( "" ),
	Dictionary DataDict( null ),
	FundamentalQuotesProvider FQP( null ),
	intrabarpersist bool OkayToPlot( false ),
	double FundFieldValue1( 0 ),
	double FundFieldValue2( 0 );

constants:
	string MonthsReportedKey( "MonthsReported" ),
	string ConsolidatedKey( "ConsolidationLevel" ),
	string AccountingKey( "AccountingStandard" ),
	string ValuesAdder( "_Values" ),
	string ConsolidatedValue( "Consolidated" ),
	string NonConsolidatedValue( "NonConsolidated" ),
	string IFRSString( "IFRS" ),
	string SECString( "SEC" ),
	string JSTDString( "JSTD" );
{ -------------------------------------------------- }	
method void CreateFundamentalQuotesProvider()
begin
	FQP = new FundamentalQuotesProvider();
	FQP.Symbol = Symbol;
	FQP.Fields += FundField;
	FQP.Realtime = false;
	FQP.TimeZone = tsdata.common.TimeZone.local;
	FQP.LoadProvider();
	Value99 = FQP.Count;
end;{ CreateFundamentalQuotesProvider }
{ -------------------------------------------------- }
method bool GetQuoteAsOfDate( string FieldName, DateTime tempdt, int PeriodsAgo1, out double QuoteVal )
variables:  
	int Counter,
	bool QuoteFound,
	bool SetVariable,
	DateTime VectDateTime,
	Vector QuoteDateTimeVector,
	Vector QuoteValuesVector;
begin
	if FieldName = "" or tempdt = NULL or DataDict = NULL or PeriodsAgo1 < 0 then return false;
	QuoteDateTimeVector = DataDict[FieldName] astype Vector;
	QuoteValuesVector = DataDict[FieldName + ValuesAdder] astype Vector;
	QuoteFound = false;
	SetVariable = false;
	for Counter = QuoteDateTimeVector.Count - 1 downto 0 begin
		SetVariable = false;
		VectDateTime = QuoteDateTimeVector[Counter] astype DateTime;
		if tempdt >= VectDateTime then begin
			SetVariable = true;
		end else begin
			break;
		end;	
		if SetVariable then begin
			if Counter + PeriodsAgo1 <= QuoteDateTimeVector.Count - 1 then begin			
				QuoteVal = QuoteValuesVector[Counter + PeriodsAgo1] astype double;
				QuoteFound = true;
			end;
		end;
	end;{ for }
	return QuoteFound;
end;{ GetQuoteAsOfDate method }
{ -------------------------------------------------- }
method void LoadFundDataVectors( string ifundField )
variables:  
	int Counter, 
	Vector dateVect, 
	Vector valuesVect,
	FundamentalQuote fq;
begin
	if DataDict = NULL then DataDict = new Dictionary();
	dateVect = new Vector();
	valuesVect = new Vector();
	DataDict.Add( ifundField, dateVect astype Vector );
	DataDict.Add( ifundField + ValuesAdder, valuesVect astype Vector );
	fq = FQP[ifundField];
	for Counter = 0 to fq.Count - 1 begin
		dateVect.push_back( fq.PostDate[Counter] astype DateTime );
		valuesVect.push_back( fq.DoubleValue[Counter] astype double );
	end;{ for }
end;{ LoadFundDataVectors method }
{ -------------------------------------------------- }
method void LoadFundDataVectorsWithFilters( string ifundField )
variables:  
	int Counter, 
	Vector dateVect, 
	Vector valuesVect,
	FundamentalQuote fq,
	string fqAcctStd,
	string LastAcctStd,
	bool LoadThisQuote,
	bool ReplaceLastValue,
	bool NumMonthsOkay,
	bool ConsLevelOkay,
	DateTime LastDateTime;
begin
	if DataDict = null then DataDict = new Dictionary();
	dateVect = new Vector();
	valuesVect = new Vector();
	DataDict.Add( ifundField, dateVect astype Vector );
	DataDict.Add( ifundField + ValuesAdder, valuesVect astype Vector );
	fq = FQP[ifundField];
	LastDateTime = DateTime.Create( 1900, 1, 1 );
	for Counter = 0 to fq.Count - 1 begin
		LoadThisQuote = false;
		ReplaceLastValue = false;
		fqAcctStd = fq.ExtendedProperties[Counter][AccountingKey].StringValue;
		NumMonthsOkay = fq.ExtendedProperties[Counter][MonthsReportedKey].IntegerValue = MonthsReported;
		ConsLevelOkay = fq.ExtendedProperties[Counter][ConsolidatedKey].StringValue = ConsLevel;
		if NumMonthsOkay and ConsLevelOkay and ( AccountingStandard = 0 or fqAcctStd = AcctStdInputString ) then begin	
			if fq.PostDate[Counter] <> LastDateTime then begin
				LoadThisQuote = true;
			end else begin
				switch ( fqAcctStd ) begin
					case IFRSString:
						ReplaceLastValue = true;
					case SECString:
						if LastAcctStd <> IFRSString then
							ReplaceLastValue = true;
					case JSTDString:
						if LastAcctStd <> IFRSString and LastAcctStd <> SECString then ReplaceLastValue = true;
				end;{ switch }
			end;
		end;
		if LoadThisQuote then begin
			dateVect.push_back( fq.PostDate[Counter] astype DateTime );
			valuesVect.push_back( fq.DoubleValue[Counter] astype double );
			LastAcctStd = fqAcctStd;
			LastDateTime = fq.PostDate[Counter];
		end else if ReplaceLastValue then begin
			valuesVect[Counter-1] = fq.DoubleValue[Counter] astype double;
			LastAcctStd = fqAcctStd;
		end;
	end;{ for }
end;{ LoadFundDataVectorsWithFilters method }
{ -------------------------------------------------- }
method string GetAcctStdString()
variables:  string AcctStdString;
begin
	switch ( AccountingStandard ) begin
		case 1: { JSTD } AcctStdString = JSTDString;
		case 2: { SEC } AcctStdString = SECString;
		case 3: { IFRS } AcctStdString = IFRSString;
		default: AcctStdString = "";
	end;{ switch }
	return AcctStdString;
end;{ GetAcctStdString }
{ -------------------------------------------------- }
once begin
	InAChart = GetAppInfo( aiApplicationType ) = cChart;
	CreateFundamentalQuotesProvider();
	if FQP.HasQuoteData( FundField ) and ( FQP[FundField].Type = DataType.doubleval or FQP[FundField].Type = DataType.integerval ) then begin
		OkayToPlot = true;
		AcctStdInputString = GetAcctStdString();
		if ConsolidationLevel = 1 then ConsLevel = ConsolidatedValue else ConsLevel = NonConsolidatedValue;
		if FQP[FundField].ExtendedProperties[0].Contains( MonthsReportedKey ) then LoadFundDataVectorsWithFilters( FundField ) else LoadFundDataVectors( FundField );
	end;
end;{ once }
{ -------------------------------------------------- }
if OkayToPlot then begin
	if GetQuoteAsOfDate( FundField, BarDateTime, PeriodsAgo1, FundFieldValue1 ) then
		Plot1( FundFieldValue1, !( "FundValue1" ) );
	if GetQuoteAsOfDate( FundField, BarDateTime, PeriodsAgo2, FundFieldValue2 ) then
		Plot2( FundFieldValue2, !( "FundValue2" ) );
	Value1 = FundFieldValue1 - FundFieldValue2;
		Plot3( Value1, !( "FundValue3" ) );
	if FundFieldValue2 <> 0 then Value2 = FundFieldValue1 / FundFieldValue2 - 1;
//マイナス変換
	if FundFieldValue1 > FundFieldValue2 and Value2 < 0 then Value2 = Value2 * ( -1 );
	if FundFieldValue1 < FundFieldValue2 and Value2 > 0 then Value2 = Value2 * ( -1 );
		Plot4( Value2, !( "FundValue4" ) );

end else begin
	Plot1( ValueToPlotOnError, !( "FundValue1" ) );
end;
//上下色分け
if Plot3 <= 0 then SetPlotColor( 3, DnColor ) else SetPlotColor( 3, UpColor );
if Plot4 <= 0 then SetPlotColor( 4, DnColor ) else SetPlotColor( 4, UpColor );


サポートされると喜んでアイスを買っちゃいます!٩(๑❛ᴗ❛๑)۶