見出し画像

信用買残・信用売残

PFのJTちゃんが空売りめちゃ増えてたので、信用のインジもあったなと思い見てみたら、既存のだといつからなのかは分からなかったので、履歴を少し見れるようなのを作ってみました

先々週比は、先々週と先週の比較、先週比は先週と今週の比較です
発表は1週間遅れなので今週と言っているのは実際は先週の実績データです

本当は1つにまとめたかったけど、見出しが買いとか売りとか個々に入れるとあれなので、買いと売りと分けて3つになりました
こういうのは幅取り過ぎなのでアプリでシンボルリンクさせて1か月程見れる方がいいですね、見れればいいか的なので取り敢えずこれで(・∀・)

任意の新しいインジケータを3つ作成します
・既存のインジケータの信用残、信用融資残・貸株残、信用倍率に、
 貸借の項目を追加しています
・既存のインジケータの信用買残先週比に、先週と先々週の買い残と、
 先々週との比率を追加しています
・既存のインジケータの信用売残先週比に、先週と先々週の売り残と、
 先々週との比率を追加しています

プログラム(信用取引)

using tsdata.marketdata;
using elsystem;

variables:
	FundamentalQuotesProvider FQP( null ),
	DateTime OldestTypeDateTime( null ),
	DateTime OldestLongDateTime( null ),
	DateTime OldestShortDateTime( null ),
	DateTime OldestBorrowedDataDateTime( null ),
	DateTime OldestLoanedDataDateTime( null ),
	intrabarpersist bool OkayToPlotType( false ),
	intrabarpersist bool OkayToPlotLong( false ),
	intrabarpersist bool OkayToPlotShort( false ),
	intrabarpersist bool OkayToPlotBorrowed( false ),
	intrabarpersist bool OkayToPlotLoaned( false ),
	double OpenType( 0 ),
	double OpenLong( 0 ),
	double OpenShort( 0 ),
	double MarginBalanceRatio( 0 ),
	double Borrowed( 0 ),
	double Loaned( 0 );

constants:
	string TypeFieldName( "CB_LBTR" ),								//信用/貸借取引のタイプ
	string LongFieldName( "CB_MTBCSB" ),							//一般および制度信用取引の買残高
	string ShortFieldName( "CB_MTBCSS" ),							//一般および制度信用取引の売残高
	string BorrowedFieldName( "OLE_Borrowed_Shares_Outstanding" ),	//融資残
	string LoanedFieldName( "OLE_Loaned_Shares_Outstanding" );		//貸株残
	
method void CreateFundamentalQuotesProvider()
begin
	FQP = new FundamentalQuotesProvider();
	FQP.Symbol = Symbol;
	FQP.Fields += TypeFieldName;
	FQP.Fields += LongFieldName;
	FQP.Fields += ShortFieldName;
	FQP.Fields += BorrowedFieldName;
	FQP.Fields += LoanedFieldName;
	FQP.Realtime = false;
	FQP.TimeZone = tsdata.common.TimeZone.local;
	FQP.LoadProvider();
	Value99 = FQP.Count; { force provider to load }
end;

once begin
	CreateFundamentalQuotesProvider();

	if FQP.HasQuoteData( TypeFieldName ) then begin
		OkayToPlotType = true;
		OldestTypeDateTime = FQP[TypeFieldName].PostDate[FQP[TypeFieldName].Count-1];
	end;
	
	if FQP.HasQuoteData( LongFieldName ) then begin
		OkayToPlotLong = true;
		OldestLongDateTime = FQP[LongFieldName].PostDate[FQP[LongFieldName].Count-1];
	end;
	
	if FQP.HasQuoteData( ShortFieldName ) then begin
		OkayToPlotShort = true;
		OldestShortDateTime = FQP[ShortFieldName].PostDate[FQP[ShortFieldName].Count-1];
	end;

	if FQP.HasQuoteData( BorrowedFieldName ) then begin
		OkayToPlotBorrowed = true;
		OldestBorrowedDataDateTime = FQP[BorrowedFieldName].PostDate[FQP[BorrowedFieldName].Count-1];
	end;
	
	if FQP.HasQuoteData( LoanedFieldName ) then begin
		OkayToPlotLoaned = true;
		OldestLoanedDataDateTime = FQP[LoanedFieldName].PostDate[FQP[LoanedFieldName].Count-1];
	end;
end;{ once }

if OkayToPlotType and BarDateTime >= OldestTypeDateTime then begin
	OpenType = FQP[TypeFieldName].DoubleValue[BarDateTime];
	switch( OpenType )begin
		case 1: Plot1( "貸借", !( "貸 借" ) );SetPlotColor( 1, DarkBrown );
		case 2: Plot1( "融資", !( "貸 借" ) );SetPlotColor( 1, White );
		case 3: Plot1( "", !( "貸 借" ) );SetPlotColor( 1, Black );
	end;
end;

if OkayToPlotLong and BarDateTime >= OldestLongDateTime then begin
	OpenLong = FQP[LongFieldName].DoubleValue[BarDateTime];
	Plot2( OpenLong, !( "買 残" ) );
end;

if OkayToPlotShort and BarDateTime >= OldestShortDateTime then begin
	OpenShort = FQP[ShortFieldName].DoubleValue[BarDateTime];
	Plot3( OpenShort, !( "売 残" ) );
end;

if OpenShort <> 0 then MarginBalanceRatio = OpenLong / OpenShort;
	Plot4( MarginBalanceRatio, !( "倍 率" ) );

if OkayToPlotBorrowed and BarDateTime >= OldestBorrowedDataDateTime then begin
	Borrowed = FQP[BorrowedFieldName].DoubleValue[BarDateTime];
	Plot5( Borrowed, !( "融資残" ) );
end;

if OkayToPlotLoaned and BarDateTime >= OldestLoanedDataDateTime then begin
	Loaned = FQP[LoanedFieldName].DoubleValue[BarDateTime];
	Plot6( Loaned, !( "貸株残" ) );
end;

プログラム(信用買残)

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

variables:
	FundamentalQuotesProvider FQP( null ),
	Dictionary DataDict( null ),
	intrabarpersist bool OkayToPlot( false ),
	double MarginBuyBalanceCurrent( 0 ),
	double MarginBuyBalancePrevious( 0 ),
	double MarginBuyBalanceBeforelast( 0 ),
	double MarginBuyBalanceRatio( 0 ),
	double MarginBuyBalanceRatio2( 0 );

constants:
	string FundFieldName( "CB_MTBCSB" ),
	string ValuesAdder( "_Values" );
	
method void CreateFundamentalQuotesProvider()
begin
	FQP = new FundamentalQuotesProvider();
	FQP.Symbol = Symbol;
	FQP.Fields += FundFieldName;
	FQP.Realtime = false;
	FQP.TimeZone = tsdata.common.TimeZone.local;
	FQP.LoadProvider();
	Value99 = FQP.Count; { force provider to load }
end;

method bool GetQuoteAsOfDate( string FieldName, DateTime tempdt, int PeriodsAgo, 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 PeriodsAgo < 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 + PeriodsAgo <= QuoteDateTimeVector.Count - 1 then begin			
				QuoteVal = QuoteValuesVector[Counter + PeriodsAgo] astype double;
				QuoteFound = true;
			end;
		end;
	end;
	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;
end;

once begin
	CreateFundamentalQuotesProvider();
	if FQP.HasQuoteData( FundFieldName ) then begin
		OkayToPlot = true;
		LoadFundDataVectors( FundFieldName );
	end;
end;

if OkayToPlot 
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 0, MarginBuyBalanceCurrent ) 
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 1, MarginBuyBalancePrevious )
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 2, MarginBuyBalanceBeforelast ) then begin
	if MarginBuyBalancePrevious <> 0 then begin
		MarginBuyBalanceRatio = MarginBuyBalanceCurrent / MarginBuyBalancePrevious;
		Plot1( MarginBuyBalanceRatio, !( "先週比" ) );
	end;
		Plot2( MarginBuyBalancePrevious, !( "先週" ) );
	if MarginBuyBalanceBeforelast <> 0 then begin
		MarginBuyBalanceRatio2 = MarginBuyBalancePrevious / MarginBuyBalanceBeforelast;
		Plot3( MarginBuyBalanceRatio2, !( "先々週比" ) );
	end;
		Plot4( MarginBuyBalanceBeforelast, !( "先々週" ) );
end;

プログラム(信用売残)

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

variables:
	FundamentalQuotesProvider FQP( null ),
	Dictionary DataDict( null ),
	intrabarpersist bool OkayToPlot( false ),
	double MarginSellBalanceCurrent( 0 ),
	double MarginSellBalancePrevious( 0 ),
	double MarginSellBalanceBeforelast( 0 ),
	double MarginSellBalanceRatio( 0 ),
	double MarginSellBalanceRatio2( 0 );

constants:
	string FundFieldName( "CB_MTBCSS" ),
	string ValuesAdder( "_Values" );
	
method void CreateFundamentalQuotesProvider()
begin
	FQP = new FundamentalQuotesProvider();
	FQP.Symbol = Symbol;
	FQP.Fields += FundFieldName;
	FQP.Realtime = false;
	FQP.TimeZone = tsdata.common.TimeZone.local;
	FQP.LoadProvider();
	Value99 = FQP.Count; { force provider to load }
end;

method bool GetQuoteAsOfDate( string FieldName, DateTime tempdt, int PeriodsAgo, 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 PeriodsAgo < 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 + PeriodsAgo <= QuoteDateTimeVector.Count - 1 then begin			
				QuoteVal = QuoteValuesVector[Counter + PeriodsAgo] astype double;
				QuoteFound = true;
			end;
		end;
	end;
	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;
end;

once begin
	CreateFundamentalQuotesProvider();
	if FQP.HasQuoteData( FundFieldName ) then begin
		OkayToPlot = true;
		LoadFundDataVectors( FundFieldName );
	end;
end;

if OkayToPlot 
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 0, MarginSellBalanceCurrent ) 
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 1, MarginSellBalancePrevious )
	and GetQuoteAsOfDate( FundFieldName, BarDateTime, 2, MarginSellBalanceBeforelast ) then begin
	if MarginSellBalancePrevious <> 0 then begin
		MarginSellBalanceRatio = MarginSellBalanceCurrent / MarginSellBalancePrevious;
		Plot1( MarginSellBalanceRatio, !( "先週比" ) );
	end;
		Plot2( MarginSellBalancePrevious, !( "先週" ) );
	if MarginSellBalanceBeforelast <> 0 then begin
		MarginSellBalanceRatio2 = MarginSellBalancePrevious / MarginSellBalanceBeforelast;
		Plot3( MarginSellBalanceRatio2, !( "先々週比" ) );
	end;
		Plot4( MarginSellBalanceBeforelast, !( "先々週" ) );
end;

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