You need to enable JavaScript in order to visit our site!
 
Logo PTMC
NAVIGATION
CANCEL
 
or via PTMC account
Restore password
or via PTMC account
Logo PTMC
NAVIGATION
or via PTMC account
Restore password
or via PTMC account
or via PTMC account
Restore password
or via PTMC account
Mac Mac 07.04 2017

用 Level 2 報價建立交易腳本

除了最佳買賣價之外,PTMC 還能夠協助用戶使用 level 2 的報價,以下檔案是文章中會用到的範例檔:

CustomTickAggregator_en.cs

Level2Test.cs

EUR/USD 報價矩陣

MATRIX EUR/USD in Protrader

6E[Z3] 市場深度

Market Depth in Protrader

存取 level 2 報價

Level 2 報價可以透過以下功能存取:

int GetLevel2Count(string symbol, int fieldType) is the number of available market depth levels for a given instrument

int GetLevel2Count(int fieldType) is the same thing, current instrument

GetLevel2Price(string symol, int fieldType, int offset) is a price of speficied instrument, type, and level

GetLevel2Price(int fieldType, int offset) is the same thing, for current instrument

string GetLevel2MMID(string symol, int fieldType, int offset) is an ECN or exchange where there was a set order

GetLevel2MMID(int fieldType, int offset) is the same thing, for current instrument

GetLevel2Size(string symol, int fieldType, int offset) is an instrument volume at the given level

GetLevel2Size(int fieldType, int offset) is the same thing, for current instrument

GetLevel2VWAPByQuantity(string symol, int fieldType, double amount) is the weighted price of a total quantity across all required volume levels

GetLevel2VWAPByQuantity(int fieldType, double amount) is the same thing, for current instrument

使用這些功能可以幫助您的技術分析更加「立體」,並且可以使用一些之前沒辦法使用的造市者策略。例如,您可以用在即時的交易量分布、根據低流動性商品報價再客製一個商品、或是分析商品的市場深度方向等等。而要最大化這類型的策略效益,你必須要有超低延遲的報價及下單速度。

CustomTickAggregator 指標就是一個使用 level 2 資訊做成的簡單趨勢指標。跟一般圖表的不同在於,這項指標可以單純觀察非時間序列的價格變化,將微小的價格變化過濾掉,凸顯重大的變動及市場反轉。

這種過濾條件的演算基礎是,一個上升的趨勢,通常會從一個成長的買家數量(買價)開始,再經跟著成長的賣家數量(賣價)證實,反之亦然。最後我們會得到一個指標,一個隨著賣價上升、買價下跌的線型指標。

然而,如果我們稍微用最好的買賣價來調校這項指標,那它就是一個對市場價格波動非常敏感的指標,因此我們使用市場深度某些區間的平均價格來調整。此外我們也增加了交易量的限制,為的是在某個交易量以上時,指標才會有所反應,避免掉非流動性市場的假訊號。


以下我們按部就班來拆解這個指標的程式碼結構:

輸入變數

//the number of levels to average is by default 5
[InputParameter("Quantity Price Levels for Averaging",0)]
public int QuantityPriceLevelsForAveraging = 5;
//minimal level of volume that indicator will react to
[InputParameter("Minimum Volume Threshold",1)]
public int MinimumVolumeThreshold = 0;

計算 VWAP:

for (int i = 0; i<QuantityPriceLevelsForAveraging; i++)
{
    //summarization of Ask prices at each level
    summAskVolume += GetLevel2Price(ASK, i)*GetLevel2Size(ASK, i);
    //summarization volume of Ask
    askVolume += GetLevel2Size(ASK, i);
    //summarization of Bid prices at each level
    summBidVolume += GetLevel2Price(BID, i)*GetLevel2Size(BID, i);
    //summarization volume of Bid
    bidVolume += GetLevel2Size(BID, i);}
//Average Ask price
double VolumeWeightedAverageAsk = (askVolume != 0.0) ? 
summAskVolume/askVolume:0.0;
Average Bid price
double VolumeWeightedAverageBid = (bidVolume != 0.0) ? 
summBidVolume/bidVolume:0.0;

計算指標線:

//If the average Ask is not equal to zero and greater than the indicator line the volume of Ask is greater than the minimum threshold,
//then we displace the indicator line backspace and assign current value of average ask price
if(GetValue(0, 0) > VolumeWeightedAverageAsk
&& VolumeWeightedAverageAsk != 0.0 && askVolume > MinimumVolumeThreshold)
{
    for( int i = inWindowCount; i >= 1; i-- )
    {
        SetValue(0, i, GetValue(0, i-1));
    }
    SetValue(0, 0, VolumeWeightedAverageAsk);
}
//If the average Bid is not equal to zero and greater than the indicator line the volume of Bid is greater than the minimum threshold,
//then we displace the indicator line backspace and assign current value of average bid price
if(GetValue(0, 0) < VolumeWeightedAverageBid
&& VolumeWeightedAverageBid != 0.0 && bidVolume > MinimumVolumeThreshold)
{
    for( int i = inWindowCount; i >= 1; i-- )
    {
        SetValue(0, i, GetValue(0, i-1));
    }
    SetValue(0, 0, VolumeWeightedAverageBid);
}

啟動指標後,開始讀取 level 2 資料:

public override void Init()
{
    //subscribe to Level 2 events
    NewLevel2Quote += new NewLevel2QuoteHandler(NewLevel2QuoteComes);
    PriceSubscribe(QUOTE_LEVEL2);
}

從圖表上刪除指標後,停止讀取 level 2 資料:

public override void Complete()
{
    //unsubscribing from the events before removing indicator level 2
    NewLevel2Quote -= new NewLevel2QuoteHandler(NewLevel2QuoteComes);
    PriceUnsubscribe(QUOTE_LEVEL2);
}

優化:

//receiving a number of visible bars from the chart
int inWindowCount = (WindowBarsPerChart() > 300) ? 300:WindowBarsPerChart();

當圖表上出現一個新的 bar 時,將指標線向前移動一個位置:

if(Count > prevCount)
{
    prevCount = Count;
    for(int i = 0; i < inWindowCount; i++)
    {
       SetValue(0, i, GetValue(0, i+1));
    }
}

自訂 tick 合併圖表 - EUR/USD 1 分圖

Custom tick aggregator, EUR/USD 1 minute chart

結論

除了在 level 2 市場深度中找到 VWAP 價格外,您還可以嘗試使用 GetLevel2VWAPByQuantity() 指標來獲取指定量的平均價格。這個例子可以在 Level2Test.cs 代碼中找到。

歡迎大家一起來討論 level 2 報價的相關指標!

Discussion
Join PTMC community to post your comments
No comments yet. Be the first.
PTMC 是一個專業的交易平台,結合了強大的圖表和分析工具,並可在不同的金融市場進行交易。 它是由 PFSOFT UK LTD 開發的,該公司是全球銀行和經紀商交易技術提供商
© 2024. PTMC 為基於 Protrader 技術的專業交易平台
地址
臺北市大安區羅斯福路3段273號5樓
聯絡我們
電話: +886-2-2367-8583
E-mail: service@kcdatanet.com
社群
© 2024. PTMC 為基於 Protrader 技術的專業交易平台