Decay?是一個設置,用于確定要反映多少過去的位置。正如我們之前詳細介紹的那樣,Decay 值越高,Alpha 周轉率越低。但是,請注意,Alpha 的夏普比率可能會隨著信息延遲而降低。
創建 Alpha 時,頭寸可能會集中在特定股票中。在這種情況下,您可以使用?Truncation,它限制了單個股票可以具有的最大權重。在 TOP3000 域中,通常使用大約 0.01(1%) 的值。但是,在像 TOP200 這樣的較小域中,擁有更大的最大位置可能會更好。(限制了單個股票的權重)
巴氏殺菌確定是將不在宇宙中的種群包括在計算中,還是將它們保留為 NaN。
Unit Handling?在仿真過程中檢測不匹配的數據字段單位并發出警告。它僅提供警告而不影響實際仿真,因此只有 Verify (驗證) 選項可用。
NaN Handling?是一個選項,用于選擇是否將數據字段中的缺失值 (NaN) 替換為 0。
Test Period?允許將 PnL 的子集隱藏為驗證期。可以通過單擊 'Show test period' 按鈕來查看此 PnL。使用此功能,您可以檢查隱藏時段的表現,并根據該時段的表現評估 Alpha 的穩健性
Logical operators?evaluate expressions and return true or false values. In BRAIN, true equals 1 and false equals 0.
邏輯運算符計算表達式并返回 true 或 false 值。在 BRAIN 中,true 等于 1,false 等
Time series 運算符執行與特定股票的過去 d 日值相關的作。例如,ts_mean(x,d) 計算 x 在 d 天內的平均值。
橫截面運算符在特定時間點比較或處理目標庫存的值。例如,rank(x) 在特定時間對 x 值進行排序,并將其從 0 分配到 1。
Vector Operators??📐 向量運算符
搜索數據字段時,您可能會找到矢量類型的數據字段?。它們不是每天每個股票只有一個值,而是存儲多個值(以向量格式)。要將這些轉換為 Alpha 位置,您需要將它們轉換為單個代表值,如 mean 或 median。這些運算符用于此目的。
Great job getting familiar with the platform! 👏 Now let's look at what kinds of Alphas we can create. 😁
熟悉平臺真是太棒了!👏 現在讓我們看看我們可以創建哪些類型的 Alpha。😁
PV data?has information related to price and volume. Since it includes price itself, which is essential for predicting stock prices, it's one of the most useful data types when first creating Alphas.
PV 數據包含與價格和數量相關的信息。由于它包括價格本身,這對于預測股票價格至關重要,因此它是首次創建 Alpha 時最有用的數據類型之一。
PV 數據包括股票價格 – 開盤價、最高價、最低價、收盤價和其他交易相關信息,例如交易量和市值。這些值在 K 線圖中得到了很好的表示。
- 開盤價是當天股市開盤時的第一個交易價格。
- Close?is the last traded price when the stock market ends for the day.
收盤價是當天股市收盤時的最后交易價格。 - High?is the highest price traded during the day.
High?是當天交易的最高價格。 - Low?is the lowest price traded during the day.
Low?是當天交易的最低價格。
?
Volume?indicates the number of shares investors transacted that day.
交易量表示投資者當天交易的股票數量。
You can use the?adv20?data field to access the 20-day average volume. If you want to calculate the average for a different number of days, you can use?ts_mean(volume,N).
您可以使用?adv20?數據字段訪問 20 天的平均交易量。如果要計算不同天數的平均值,可以使用?ts_mean(volume,N)。
📋 VWAP (成交量加權平均價格)
Additionally,?VWAP?can represent a day's stock price, which is the volume-weighted average price. Since low-volume trades might give a false picture of other price indicators like closing price,?VWAP?can be a better measure of that day's price. Let's look at this table:
此外,VWAP?可以代表一天的股票價格,即成交量加權平均價格。由于小批量交易可能會錯誤地反映其他價格指標(如收盤價),因此?VWAP?可以更好地衡量當天的價格。讓我們看看這個表格:
Here, the VWAP is 11, resulting from dividing the sum of price times volume by the total volume sum. In formula terms, it's?sum(price*volume)/sum(volume).
這里,VWAP 是 11,由價格乘以交易量之和除以總交易量總和得出。用公式來說,它是?sum(price*volume)/sum(volume)。
- perform well, while stocks that have performed poorly will continue to do so.
動量?- 假設過去表現良好的股票將繼續表現良好,而表現不佳的股票將繼續表現良好。- Momentum effect?typically appears over longer periods (several months or more).
動量效應通常出現在較長時間(幾個月或更長時間)內。
- Momentum effect?typically appears over longer periods (several months or more).
- Reversion?-The hypothesis is that if something increases today, it will fall tomorrow. And if something decreases today, it will increase tomorrow. This something can be anything: price, volume, correlation between two things or the other indicators/variables that you can think of while developing your alpha.
回歸?- 假設是如果某物今天增加,明天就會下降。如果某件事今天減少,明天就會增加。這可以是任何東西:價格、交易量、兩件事之間的相關性或您在開發 alpha 時可以想到的其他指標/變量。- Reversion effect?appears over shorter periods (days or weeks).
回歸效應在較短的時間段(幾天或幾周)內顯示。
- Reversion effect?appears over shorter periods (days or weeks).
我們應該嘗試使用?VWAPP?實現一個回歸 Alpha?嗎?VWAP 是當天按交易量加權的平均價格,而收盤價是最后交易價格。通過比較 VWAP 和收盤價,我們可以了解最后交易價格與當天平均值的比較情況。例如,如果收盤價遠高于 VWAP,我們可以解釋為價格在收盤附近上漲。反之亦然。
Since?reversion?theory assumes prices return to their mean, we can implement an Alpha using the formula "vwap/close". This takes long positions when closing prices fall below VWAP, and short positions when they rise above it.
由于回歸理論假設價格回到其均值,因此我們可以使用公式 “vwap/close” 實現 Alpha。當收盤價低于 VWAP 時,它需要多頭頭寸,當收盤價高于 VWAP 時,它需要多頭頭寸。
When you simulate this, you'll notice that while the Sharpe ratio is high, the turnover is excessive. According to submission criteria, turnover should be below 70% for submission. You can adjust turnover by applying operators (ex. trade_when) or changing settings (ex. Decay).
當您模擬此情況時,您會注意到,雖然夏普比率很高,但周轉率過高。根據提交標準,提交的營業額應低于 70%。您可以通過應用運算符(例如 trade_when)或更改設置(例如 Decay)來調整周轉率。
技術分析是一種分析方法,旨在根據歷史價格和交易量數據預測未來價格走勢。它基于市場參與者的心理和行為模式重復的假設,使用各種指標,例如圖表模式、價格趨勢、動量指標和移動平均線。以下是您可以在 Alpha 探索中使用的一些代表性指標:
- RSI (Relative Strength Index):?A momentum indicator showing the degree of price increase/decrease
RSI (相對強弱指數):?顯示價格上漲/下跌程度的動量指標 - Bollinger Bands:?Sets upper/lower bands using standard deviation around a moving average
Bollinger Bands(布林帶):?使用移動平均線周圍的標準差設置上限/下限 - MACD (Moving Average Convergence Divergence):?A trend indicator using the difference between short-term and long-term moving averages
MACD (Moving Average Convergence Divergence) (移動平均收斂散度):?使用短期和長期移動平均線之間差值的趨勢指標
CLV = ((Close - Low) - (High - Close)) / (High - Low)
CLV = ((收盤價 - 最低價) - (最高價 - 收盤價)) / (最高價 - 最低價)
The?CLV?value ranges from?1 to -1, with these characteristics:
CLV?值的范圍從?1 到 -1,具有以下特征:
- CLV close to 1 means closing price is near the high
CLV 接近 1 表示收盤價接近高點 - CLV close to -1 means closing price is near the low
CLV 接近 -1 意味著收盤價接近低點
CLV?是一個捕捉一天(短期)價格變化的指標。因此,它可能更適合在 PV Alpha 的兩個主要分支(動量和反轉)之間創建回歸 Alpha。因此,以下想法可能是有效的:
When CLV is low (close to -1) → Buy position
當 CLV 較低(接近 -1)時→買入頭寸
When CLV is high (close to 1) → Sell position
當 CLV 較高(接近 1)時 → 賣出頭寸
提示:?CLV 表達式的范圍是 -1 到 1。
Write an alpha that allocates more positions to stocks with higher volume.
編寫一個 alpha,將更多頭寸分配給交易量更高的股票。
DECAY 4
clv = ((close-low)-(high-close))/(high-low);
-clv * rank(volume)
答:?
DECAY 4
clv = ((close-low)-(high-close))/(high-low);
-clv * rank(volume)
使用所有四個數據字段創建夏普比率高于 1.4 的 Alpha:?最高價、最低價、收盤價、成交量?
。
Momentum effect manifests differently depending on the time perspective:
動量效應根據時間視角的不同而表現不同:
- Short-term?(less than 1 month): Reversion effect is stronger
短期?(不到 1 個月):回歸效應更強 - Medium-term?(quarter to year): Traditional momentum effect is most pronounced
中期?(季度至年):傳統動量效應最為明顯 - Long-term?(over 12 months): Effect tends to gradually weaken
長期?(超過 12 個月):效果趨于逐漸減弱
The momentum effect can be implemented in various ways. The simplest method is calculating recent price increases. Shall we calculate an asset's price increase over one year? Since BRAIN only considers business days, a year's length is typically represented as 250 days (the number of business days in a year). We can measure the change in x over a year using the time series operator?ts_delta(close,250)/ts_delay(close,250).
動量效應可以通過多種方式實現。最簡單的方法是計算最近的價格上漲。我們應該計算資產一年內的價格上漲嗎?由于 BRAIN 僅考慮工作日,因此一年的長度通常表示為 250 天(一年中的工作日數)。我們可以使用時間序列運算符?ts_delta(close,250)/ts_delay(close,250)?來衡量一年中 x 的變化。
但是,單獨使用此公式不會產生良好的 Alpha 結果。一年期回報率受近期回報率的顯著影響,包括短期回歸效應。要在 BRAIN 中將動量效應實現為 Alpha,我們需要使用不同的方法或添加條件。
因此,為了更清楚地看到動量效應,我們可以使用以下方法:
- Apply delay to one-year returns to mitigate short-term reversion effects.
對一年期申報表應用延遲,以減輕短期回歸效應。- ts_delay(ts_delta(close,250)/ts_delay(close,250),10)
ts_delay(ts_delta(收盤,250)/ts_delay(收盤,250),10)
- ts_delay(ts_delta(close,250)/ts_delay(close,250),10)
- Count the number of days with price increases over one year.
計算一年內價格上漲的天數。- ts_sum(returns>0? 1:0, 250)?*
ts_sum(返回>0? 1:0, 250)?*
- ts_sum(returns>0? 1:0, 250)?*
*?<condition>? <if_true>:<if_false>?returns if_true value when the condition is true, and if_false value when it's not.?if_else(<condition>,<if_true>,<if_false>)?means the same thing.
*?<condition>? <if_true>:<if_false>?在條件為 true 時返回 if_true 值,在條件不為 true 時返回 if_false 值。if_else(<condition>,<if_true>,<if_false>)?表示相同的內容。
?ts_sum(if_else(greater(returns,0),1,0),250)
解析?ts_sum(if_else(greater(returns,0),1,0),250)
這個表達式是金融時間序列分析中的一個復合函數,用于統計過去 250 天內收益率為正的天數。下面我來逐步拆解它的邏輯:
函數拆解與執行流程
1.?內層函數?greater(returns,0)
- 功能:判斷每日收益率是否大于 0。
- 輸入:
returns
?是每日收益率序列。 - 輸出:布爾值序列,收益率 > 0 時為?
True
,否則為?False
。
2.?中間函數?if_else(...)
- 功能:根據布爾值進行條件賦值。
- 邏輯:
plaintext
if returns[i] > 0:賦值為 1 else:賦值為 0
- 輸出:由?
1
?和?0
?組成的序列,其中?1
?代表當天收益率為正。
3.?外層函數?ts_sum(...,250)
- 功能:計算過去 250 天內的累積和。
- 滑動窗口:對每個時間點?
t
,統計?[t-249, t]
?區間內所有?1
?的總和。 - 輸出:每個時間點對應的過去 250 天內正收益天數。
數學表達
對于時間序列?returns = [r?, r?, ..., r?]
,該函數計算:
plaintext
result[t] = ∑_{i=t-249}^{t} I(r_i > 0)
其中?I(r_i > 0)
?是指示函數,當?r_i > 0
?時為 1,否則為 0。
Python 實現示例
python
運行
import numpy as npdef ts_sum(if_else_result, window=250):"""計算滑動窗口內的累積和"""return np.convolve(if_else_result, np.ones(window), mode='valid')def greater(returns, threshold):"""判斷序列中每個元素是否大于閾值"""return returns > thresholddef if_else(condition, value_if_true, value_if_false):"""根據條件進行賦值"""return np.where(condition, value_if_true, value_if_false)# 示例數據:生成1000天的隨機收益率
np.random.seed(42)
returns = np.random.normal(0.0005, 0.01, 1000) # 假設日均值0.05%,標準差1%# 計算正收益天數
condition = greater(returns, 0)
binary_result = if_else(condition, 1, 0)
positive_days = ts_sum(binary_result, window=250)# 輸出前10個結果
print("過去250天內正收益天數:", positive_days[:10])
實際應用場景
-
市場強度指標:
- 正收益天數占比高(如超過 130 天),表明市場處于強勢。
-
勝率分析:
- 對策略回測而言,該指標反映策略在歷史上的勝率。
-
風險評估:
- 連續低正收益天數可能預示市場風險上升。
注意事項
-
邊界處理:
- 對于前 249 天的數據,無法形成完整窗口,需特殊處理(如返回 NaN 或 0)。
-
參數選擇:
window=250
?通常對應一年交易日,但需根據實際需求調整。
-
與其他指標結合:
- 單獨使用意義有限,建議結合收益率均值、波動率等指標綜合分析。
這個函數本質是一個動量類指標,通過統計正收益天數的頻率來衡量市場或資產的強勢程度。
編輯
分享
如何解釋ts_sum(if_else(greater(returns,0),1,0),250)的輸出結果?
ts_sum(if_else(greater(returns,0),1,0),250)和ts_sum(if_else(greater(returns,0),1,0),120)有什么區別?
ts_sum(if_else(greater(returns,0),1,0),250)的計算結果可以用于哪些分析場景?
解析?ts_sum(if_else(greater(returns,0),1,0),250)
這個表達式是金融時間序列分析中的一個復合函數,用于統計過去 250 天內收益率為正的天數。下面我來逐步拆解它的邏輯:
函數拆解與執行流程
1.?內層函數?greater(returns,0)
- 功能:判斷每日收益率是否大于 0。
- 輸入:
returns
?是每日收益率序列。 - 輸出:布爾值序列,收益率 > 0 時為?
True
,否則為?False
。
2.?中間函數?if_else(...)
- 功能:根據布爾值進行條件賦值。
- 邏輯:
plaintext
if returns[i] > 0:賦值為 1 else:賦值為 0
- 輸出:由?
1
?和?0
?組成的序列,其中?1
?代表當天收益率為正。
3.?外層函數?ts_sum(...,250)
- 功能:計算過去 250 天內的累積和。
- 滑動窗口:對每個時間點?
t
,統計?[t-249, t]
?區間內所有?1
?的總和。 - 輸出:每個時間點對應的過去 250 天內正收益天數。
數學表達
對于時間序列?returns = [r?, r?, ..., r?]
,該函數計算:
plaintext
result[t] = ∑_{i=t-249}^{t} I(r_i > 0)
? 其中?I(r_i > 0)
?是指示函數,當?r_i > 0
?時為 1,否則為 0。
Python 實現示例
python
運行
import numpy as npdef ts_sum(if_else_result, window=250):"""計算滑動窗口內的累積和"""return np.convolve(if_else_result, np.ones(window), mode='valid')def greater(returns, threshold):"""判斷序列中每個元素是否大于閾值"""return returns > thresholddef if_else(condition, value_if_true, value_if_false):"""根據條件進行賦值"""return np.where(condition, value_if_true, value_if_false)# 示例數據:生成1000天的隨機收益率
np.random.seed(42)
returns = np.random.normal(0.0005, 0.01, 1000) # 假設日均值0.05%,標準差1%# 計算正收益天數
condition = greater(returns, 0)
binary_result = if_else(condition, 1, 0)
positive_days = ts_sum(binary_result, window=250)# 輸出前10個結果
print("過去250天內正收益天數:", positive_days[:10])
實際應用場景
-
市場強度指標:
- 正收益天數占比高(如超過 130 天),表明市場處于強勢。
-
勝率分析:
- 對策略回測而言,該指標反映策略在歷史上的勝率。
-
風險評估:
- 連續低正收益天數可能預示市場風險上升。
注意事項
-
邊界處理:
- 對于前 249 天的數據,無法形成完整窗口,需特殊處理(如返回 NaN 或 0)。
-
參數選擇:
window=250
?通常對應一年交易日,但需根據實際需求調整。
-
與其他指標結合:
- 單獨使用意義有限,建議結合收益率均值、波動率等指標綜合分析。
這個函數本質是一個動量類指標,通過統計正收益天數的頻率來衡量市場或資產的強勢程度。
When creating Alpha, you can fine-tune Alpha positions using conditions, using operators like?if_else()?or?trade_when().
創建 Alpha 時,您可以使用條件微調 Alpha 位置,使用?if_else()?或?trade_when() 等運算符。
您可以使用常用邏輯運算符(>、<、>=、<=、==?等)創建條件。例如,如果您只想在交易量高于平時(20 天平均值)時更改位置,則可以將條件設置為交易量 > adv20。以下是一些需要考慮的條件,但您可以創建所需的任何條件。請注意,設置過于嚴格的條件可能會減少數據點的數量并增加過度擬合的風險。
在實現 Alpha 時,有時您需要根據特定條件返回不同的值。if_else 運算符是一個基本但功能強大的運算符,可讓您簡單地實現條件邏輯。
You can also use?if_else?in the form of?condition? if_true:if_false. For example,?ts_sum(returns>0? 1:0, 250)?uses this format.
你也可以用?condition if_else 的形式 if_true:if_false。?例如,ts_sum(returns>0? 1:0, 250)?使用此格式。
使用?trade_when?運算符,您可以決定何時更改 Alpha 中的值。trade_when?運算符僅在特定條件下更改 Alpha 值,否則保持較早的位置。
trade_when takes three inputs:?entry condition, Alpha expression, and exit condition.
trade_when 接受三個輸入:?進入條件、Alpha 表達式和退出條件?。
- When the?entry condition is true, it?updates the Alpha expression value daily.
當輸入條件為 true?時,它每天更新 Alpha 表達式值?。 - When the?exit condition is true,?it?liquidates the position?(NaN).
當退出條件為 true 時,?它會清算倉位?(NaN)。 - When?both are false, it?maintains the last Alpha value?from when the entry condition was true.
當兩者都為 false?時,它將保持進入條件為 true 時的最后一個 Alpha 值?。讓我們設置入場條件。基于動量在成交量激增時起作用的假設,我們可以使用以下方法檢測成交量增加:
- volume > adv20?#adv20: 20-day average volume
交易量 > adv20?#adv20:20 天平均交易量 - volume > ts_mean(volume, N_DAYS)
Volume > ts_mean(volume, N_DAYS) - volume * vwap > ts_mean(volume * vwap, N_DAYS)
交易量 * VWAP > ts_mean(交易量 * VWAP, N_DAYS) -
You can test different values for?N_DAYS.
您可以測試?N_DAYS?的不同值。Since momentum is a long-term effect, it's effective to set the?exit condition to –1?to keep positions longer once established. Or you can set your own exit condition which you think is effective.
由于動量是一種長期影響,因此將退出條件設置為 –1?以在建立后保持更長時間是有效的。或者您可以設置您認為有效的退出條件。
Also, since momentum effects tend to occur across industries rather than within them, it's better to?set Neutralization options to avoid overly detailed grouping?such as Market or Sector.
此外,由于動量效應往往發生在行業之間,而不是行業內部,因此最好設置 Neutralization 選項以避免過于詳細的分組?,例如 Market 或 Sector。
你提供的代碼是一個量化交易策略,結合了兩個關鍵信號:正收益天數頻率和成交量異常。下面我來詳細拆解這個策略的邏輯和應用場景。
- 功能:統計過去 250 個交易日中,收益率為正的天數總和。
- 信號含義:
positive_days
?越大,說明近期市場強勢(上漲天數多)。- 典型閾值:若?
positive_days > 130
,意味著超過半數時間市場上漲。