Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

要如何設定不以借貸方式買進股票? #3

Closed
tej87681088 opened this issue Sep 6, 2023 · 0 comments
Closed

要如何設定不以借貸方式買進股票? #3

tej87681088 opened this issue Sep 6, 2023 · 0 comments
Labels
question Further information is requested

Comments

@tej87681088
Copy link
Contributor

tej87681088 commented Sep 6, 2023

不開槓桿解法

Case1 : 案例中的ending_cash曾經<0(花額外的現金),且shorts_count曾經>0(有放空),有沒有辦法控制?

  • 超買:
    當Zipline做多股票且該股票成交量足夠的情形下,就算現金不足,仍會融資進行購買。
    若要避免超買,則需在交易前(t=0)進行試算,若股款(t=0)>帳上現金(t=0,可利用context.portfolio.cash取得),則不下單或減少下單數量(本例減少下單數量)。而股款須利用實際交易價格(t=1)及下單股數計算。
    根據Zipline交易機制:當演算法在某一天(t=0)下單時,該訂單會在下一個交易日(t=1)成交。而利用data.current(context.asset, 'close')取得的收盤價為t=0期的收盤價,而非實際交易價格(t=1)。
    若要取得真正交易價格可以透過fetch_tej_api(),並利用pre_func()將收盤價(Close)往前平移一期。
  • 超賣:
    若賣單的絕對數量>帳上該股票數量時,Zipline會進行放空。
    若要避免超賣,則需先判斷交易前(t=0)帳上股數(利用context.portfolio.positions[asset].amount取得該asset目前帳上股數)是否足夠賣出,若不夠賣則不下單或減少下單數量(本例減少下單數量)。

Case2 :

  • 問題
    根據Zipline交易機制:當演算法在某一天(t=0)下單時,該訂單會在下一個交易日(t=1)成交。因此,利用order_target_percent()下單時,會利用t=0期的收盤價計算下一個交易日(t=1)要購買多少股數。所以當t=1期的收盤價>t=0期的收盤價時就有可能超買,反之則會超賣。

  • 超買
    下多單利用order_target_percent():Place an order to adjust a position to a target percent of the current portfolio value. If the position doesn’t already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target percent and the current percent.
    為了不超買,這邊將權數 * 0.9 (adj_ratio),形成緩衝避免買超過。

  • 超賣
    下賣單利用order_target():Place an order to adjust a position to a target number of shares. If the position doesn’t already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target number of shares and the current number of shares.
    若用order_target_percent(asset,0),會有超賣可能性;所以這邊利用order_target(asset, 0)強制將持有股數設定為0。

不開槓桿設定:https://github.com/tejtw/TQuant-Lab/blob/a5afecf9db5a607a36cfd088474a296b8c7bb7e9/Problem/QA_%E4%B8%8D%E9%96%8B%E6%A7%93%E6%A1%BF%E8%A8%AD%E5%AE%9A.ipynb

@tej87681088 tej87681088 added the question Further information is requested label Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant