TikZ 中的价格和数量网格

TikZ 中的价格和数量网格

语境

我想直观地解释价格的动态。

  • 人们愿意以一定的价格、一定的数量出售
  • 人们愿意为一定数量支付一定价格,出价

当买入价交叉(=)卖出价时,进行交易。

使用链来显示价格链最初是有意义的,但尽管我阅读了有关链的章节,我仍然对这个库感到困惑。

所以我决定回到下面的基本节点定义。结果部分完成了工作(它显示了价格网格),但是一旦使用我的代码,我就遇到了缩放问题,因为节点是绝对定义的。当价格描述为 100 时,我的定位没问题。但显然,当价格较低时,我有一个缩放效果,所有节点几乎都重叠了。

我的问题

我需要提高我的图表的可扩展性。

它应该如何工作?

价格边界由 4 个输入决定

  1. \bid
  2. \offer
  3. \Nprices
  4. \tick

  • \Ubound = \offer + (\Nprices - 1)* \tick
  • \Lbound = \bid - (\Nprices - 1)* \tick

我使用了 Excel 电子表格,因为我想获得相同的渲染效果。

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

示例 1

在此处输入图片描述

导致

在此处输入图片描述

价格的中央列(主链?)定义为\Ubound\Lbound。出价链从 开始\bid,在左侧下方延伸。报价链从 开始,\offer在右侧上方延伸。主链上的灰色区域对应于\bid和之间可能出现的价格\offer

示例 2

\tick现在是 0.5,我可以获得更多价格之间的粒度。

在此处输入图片描述

在此处输入图片描述

示例 3

\bid=\offer

在此处输入图片描述

在此处输入图片描述

到目前为止我得到了以下结果 在此处输入图片描述 在此处输入图片描述

渲染效果还不错

在此处输入图片描述

渲染不正常

在此处输入图片描述

渲染不正常

在此处输入图片描述

手动yscale=5使用

%My variables
\def\bid{10}            %Bid price          % 99 euros but any stock price
\def\offer{10}          %Offer price        % 101 euros but any stock price 
\def\Qbid{50}           %Bid Quantity       % random numbers, not essential,
\def\Qoffer{50}         %Offer Quantity     % random numbers, not essential 
\def\tick{0.1}          %Tick for prices    % tick between 2 prices, = 0.10,0.25,0.50,1, 5, 10...
\def\Nprices{3}         % number of bids or offers in the chain of prices and quantities

在此处输入图片描述

我需要什么帮助?

  1. 我是否需要切换到链(叹气)来解决可扩展性问题?

  2. 感谢 schrodinger'scat,我有一个(仍然非常)手动的解决方案,用于整数价格。但是,如果价格不是整数,我会遇到循环问题。

  3. 我想突出显示最佳出价(最高出价)和最佳报价(最低报价),并将其他设置为color=gray

  4. 图表的下一步将包括展示向买入价 = 卖出价收敛的想法,并可能在最后得到一个漂亮的动画。

  5. 我当然愿意接受任何提高可读性的建议。

    \documentclass[tikz]{standalone}
    \usetikzlibrary {chains,scopes,fit,backgrounds}
    \usepackage{eurosym}
    \begin{document}
    
    %I define the conditions of what I want to highlight
    \newif\ifShowBid        \ShowBidtrue
    \newif\ifShowOffer      \ShowOffertrue
    \newif\ifShowSpread     \ShowSpreadtrue
    
    %My variables
    \def\bid{100}           %Bid price          % 99 euros but any stock price
    \def\offer{100}         %Offer price        % 101 euros but any stock price 
    \def\Qbid{50}           %Bid Quantity       % random numbers, not essential,
    \def\Qoffer{50}         %Offer Quantity     % random numbers, not essential 
    \def\tick{1}            %Tick for prices    % tick between 2 prices, = 0.10,0.25,0.50,1, 5, 10...
    \def\Nprices{3}         % number of bids or offers in the chain of prices and quantities
    
    \tikzset{
    Pbid/.style={
        rounded corners,draw=blue,fill=blue!20,text=blue,text width = 3em,align=center},
    Poffer/.style={
        Pbid,draw=red,fill=red!20,text=red!70},
    Qbid/.style={
        rectangle,fill=blue!20,draw=none,text=blue,text width = 2em,align=center},
    Qoffer/.style={
        Qbid,text=red,fill=red!20},
    PriceDiscovery/.style={, fill= gray, ultra thick, rounded corners,opacity=0.5,draw=red, line width =3 pt},
    }
    
    \begin{tikzpicture}[node distance=2mm and 0.3cm]
    
    \foreach 
        [evaluate={
                \Nbid   = \bid - \i* \tick;                 %  list of bids
                \Noffer = \offer + \i* \tick;               %  list of offers
                \Qbid   = int(\Qbid - 10 *\i* rand);        %  list of Qbids
                \Qoffer = int(\Qoffer - 10 *\i* rand);      %  list of Qoffers      
                }] \i in {0,...,\Nprices}
    {
        \node[Pbid]                 (bid-\i)    at (0,\Nbid)    {\euro{\Nbid}};
        \node[Poffer]               (offer-\i)  at (1.5,\Noffer)    {\euro{\Noffer}};
        \node[Qbid,left=of bid-\i]  (Qbid-\i)                   {$\times \Qbid$};
        \node[Qoffer,right=of offer-\i] (Qoffer-\i)             {$\times \Qoffer$}; 
    }
    \ifnum \bid=\offer
    \node[fit=(Qbid-0)(Qoffer-0), draw,fill= red!50, rounded corners,opacity=0.5,label=right:{Trading level}] {};
    \ShowBidfalse
    \ShowOfferfalse
    \ShowSpreadfalse
    \fi
    % I need to highlight different parts of the graph
    % 1- the whole set of bids and offers
    \ifShowSpread
        \begin{scope}[on background layer]
        \node[PriceDiscovery, fit=(Qbid-0.south west)(Qoffer-0.north east),label=right:{Best market}] {Price discovery area};
        \end{scope}
    \fi
    % 2- the whole set of offers
    \ifShowOffer
        \begin{scope}[on background layer]
        \node[fit=(Qoffer-0)(offer-\Nprices), fill= red!50, rounded corners,opacity=0.5,label=right:{Offers}] {};
        \end{scope}
    \fi
    % 3- the whole set of bids
    \ifShowBid
        \begin{scope}[on background layer]
        \node[fit=(Qbid-0)(bid-\Nprices), fill= blue!50, rounded corners,opacity=0.5,label=left:{Bids}] {};
        \end{scope}
    \fi
    \end{tikzpicture}
    \end{document}
    

和编辑版本

\documentclass[tikz]{standalone}
\usetikzlibrary {chains,scopes,fit,backgrounds}
        \usetikzlibrary{calc}
        \usetikzlibrary{shapes}
\usepackage{eurosym}
\usepackage{verbatim}
\usepackage{xstring}

\begin{document}

\tikzset{
    Pbid/.style={
        rounded corners,draw=blue,fill=blue!20,text=blue,text width = 3em,align=center},
    Poffer/.style={
        Pbid,draw=red,fill=red!20,text=red!70},
    Qbid/.style={
        rectangle,fill=blue!20,draw=none,text=blue,text width = 2em,align=center},
    Qoffer/.style={
        Qbid,text=red,fill=red!20},
    PriceDiscovery/.style={, fill= gray, ultra thick, rounded corners,opacity=0.5,draw=red, line width =3 pt},
}


%I define the conditions of what I want to highlight
\newif\ifShowBid        %\ShowBidtrue
\newif\ifShowOffer      %\ShowOffertrue
\newif\ifShowSpread     \ShowSpreadtrue
\newif\ifShowPGrid      %\ShowPGridtrue

%My variables
\def\bid{25}        % Bid price         % 99 euros but any stock price
\def\offer{25}      % Offer price       % 101 euros but any stock price 
\def\Qbid{50}       % Bid Quantity      % random numbers, not essential,
\def\Qoffer{50}     % Offer Quantity    % random numbers, not essential 
\def\tick{1}        % Tick for prices   % tick between 2 prices, = 0.10,0.25,0.50,1, 5, 10...
\def\Nprices{4}     % number of bids or offers in the chain of prices and quantities

\def\X{1}           % Same column = 0, otherwise symmetry
\def\Y{0.6}         % yscale factor

%I'll use {\LBound,...,\UBound} later, if \tick is not integer, pb with \pgfmathtruncatemacro
\pgfmathtruncatemacro\LBound{\bid   - \Nprices * \tick}
\pgfmathtruncatemacro\UBound{\offer + \Nprices * \tick}
\pgfmathtruncatemacro\a{\LBound + \tick}
\ifnum \bid=\offer
    \pgfmathtruncatemacro\LSpread{\bid}
    \pgfmathtruncatemacro\USpread{\offer}
\else
    \pgfmathtruncatemacro\LSpread{\bid   + \tick}
    \pgfmathtruncatemacro\USpread{\offer - \tick}
\fi

%% 
%\IfInteger{\tick}
%   \pgfmathsetmacro\LBound{\bid   - \Nprices * \tick}
%   \pgfmathsetmacro\UBound{\offer + \Nprices * \tick}
%   \pgfmathsetmacro\a{\LBound + \tick}
%   
%   \ifnum \bid=\offer
%       \pgfmathsetmacro\LSpread{\bid}
%       \pgfmathsetmacro\USpread{\offer}
%   \else
%       \pgfmathsetmacro\LSpread{\bid   + \tick}
%       \pgfmathsetmacro\USpread{\offer - \tick}
%       \pgfmathsetmacro\b{\LSpread + \tick}
%   \fi


\begin{tikzpicture}[yscale=\Y,node distance=10mm and 0.25cm]
            
    % Price axis
    \ifShowPGrid    
    \foreach  \i in {\LBound,...,\UBound}
    \node[gray] (P-\i)  at (-\X-3,\i)   {\euro{\i}};
    \fi 
        
    %   % Prices available in the discovery area
    %   \foreach  \i in {\LSpread,\b,...,\USpread}
    %   \node[gray] (A)     at (0,\i)   {\euro{\i}};
            
        
        
    \foreach 
    [evaluate={
            \Nbid   = \bid - \i* \tick;                 %  list of bids
            \Noffer = \offer + \i* \tick;               %  list of offers
            \Qbid   = int(\Qbid - 10 *\i* rand);        %  list of Qbids
            \Qoffer = int(\Qoffer - 10 *\i* rand);      %  list of Qoffers      
        }] \i in {0,...,\Nprices}
    {
        \node[Pbid]                 (bid-\i)    at (-\X,\Nbid)  {\euro{\Nbid}};
        \node[Poffer]               (offer-\i)  at (\X,\Noffer)     {\euro{\Noffer}};
        \node[Qbid,left=of bid-\i]  (Qbid-\i)                   {$\times \Qbid$};
        \node[Qoffer,right=of offer-\i] (Qoffer-\i)             {$\times \Qoffer$}; 
    }
            
    \ifnum \bid=\offer
        \node[fit=(Qbid-0)(Qoffer-0), draw,fill= red!50, rounded corners,opacity=0.5,label=right:{Trading level}] {};
                        
        \node[star,star points=7,star point ratio=0.4,fill=red,opacity=0.8] at ($(bid-0)!0.5!(offer-0)$) {};
        \ShowBidfalse
        \ShowOfferfalse
        \ShowSpreadfalse
    \else  %%%% I'll do that manually in a poor way but it could be implied from \tick \bid and \offer
                        
        \def\a{\bid+\tick}
        \def\b{\offer-\tick}
                        
                        
    \fi
            
            
    % I need to highlight different parts of the graph
    % 1- the whole set of bids and offers
    \ifShowSpread
    
    \foreach  \i in {\LSpread,...,\USpread}
    \node[gray] (PD-\i)     at (0,\i)   {\euro{\i}};
    
    \begin{scope}[on background layer]
        \node[PriceDiscovery, fit=(bid-0.south west)(offer-0.north east),] {}; %label=right:{Price discovery area}
    \end{scope}
    \fi
    % 2- the whole set of offers
    \ifShowOffer
    \begin{scope}[on background layer]
        \node[fit=(Qoffer-0)(offer-\Nprices), fill= red!50, rounded corners,opacity=0.5,] {}; %label=right:{Offers}
    \end{scope}
    \fi
    % 3- the whole set of bids
    \ifShowBid
    \begin{scope}[on background layer]
        \node[fit=(Qbid-0)(bid-\Nprices), fill= blue!50, rounded corners,opacity=0.5,] {}; %label=left:{Bids}
    \end{scope}
    \fi
            
            
    %Fleches
    %\draw [thick, -latex] (offer-0.west) to [bend left] (offer-1.west);
    %\draw [thick, -latex] (offer-1.west) to [bend left] (offer-2.west);
    %\draw [thick, -latex] (offer-2.west) to [bend left] (offer-3.west);
    %Fleches
    %\draw [thick, -latex] (bid-0.east) to [bend left] (bid-1.east);
    %\draw [thick, -latex] (bid-1.east) to [bend left] (bid-2.east);
    %\draw [thick, -latex] (bid-2.east) to [bend left] (bid-3.east);
            
            
\end{tikzpicture}
\end{document}

相关内容