1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#Quantopian Algorithm で order_target

Last updated at Posted at 2018-03-01

order_target

order_targetは,orderと同様に,銘柄と株数を指定する注文関数ですが,このときの株数とはポートフォリオに保有したい株数という意味になります.もし既に指定した分だけの株数を持っている場合は,新たに買い増したり売り増したりすることはありません.

アルゴリズム例

(オーダー方法以外は,#Quantopian Algorithm で order - Qiitaにコメントで説明していますのでご参照下さい)

def initialize(context):
    context.security = sid(24)
    schedule_function(rebalance, 
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close(minutes = 1))

def rebalance(context, data):
    price_history = data.history(
        context.security,
        fields='price',
        bar_count=5,
        frequency='1d'
    )
    average_price = price_history.mean()
    current_price = data.current(context.security, 'price') 
    if data.can_trade(context.security):
        if current_price > (1.01 * average_price):
            # 成り行きでAAPLを100株買う.しかし,既に持っていたら買わない.
            order_target(context.security, 100)
            log.info("Buying %s" % (context.security.symbol))
        elif current_price < average_price:
            order_target(context.security, 0)
            log.info("Selling %s" % (context.security.symbol))
    
    record(current_price=current_price, average_price=average_price)

メモ

上記を full backtest してログを確認すると,例えば2017/05/09〜13までは条件(current_price > (1.01 * average_price))に当てはまるので,AAPLをロング注文していますが,Transaction Detailを見ると分かるように,2017/05/09しか取引していません.

Log の画像

Screenshot from 2018-03-01 21-00-06.png

Transaction Detailの画像
Screenshot from 2018-03-01 21-02-10 (1).png

注文する前にポジション確認しなくてもほしい株数だけ保有してくれるというのは,とてもベンリですね.
次回は,order_target_valueです.

他のオーダー方法

#Quantopian Algorithm で order
#Quantopian Algorithm で order_value
#Quantopian Algorithm で order_percent
#Quantopian Algorithm で order_target_value

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?