LoginSignup
1
0

More than 5 years have passed since last update.

#Quantopian Algorithm で order_target_value

Last updated at Posted at 2018-03-02

order_target_value

order_target_valueは,とある銘柄をポートフォリオに指定した額だけ保有する注文方法です.

order_target_value(asset, amount, style=OrderType)

asset: Equity/Future オブジェクト
amount: 取引額.正であればLong,負であればShort.
style: 注文方法(指定方法はorder_target_valueを参照)

アルゴリズム例

(オーダー方法以外は,#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):
            # ポートフォリオに1000ドル分のアップル株を保有するように注文する.
            order_target_value(context.security, 1000)
            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)

メモ

order_target_valueは,order_valueと同様に,株数ではなく〇〇ドル分保有する,という注文ができます.1000ドル分とは,1000ドルを超えないオーダーという意味になります.

order_valueとの違いは,ポートフォリオに〇〇ドル分保有したら,その後,注文トリガーに当たっても取引はしない,というところです.
上記コードで言えば,current_price が 5日平均価格よりも1%高ければ,アップルを1000ドル注文する,というロジックですが,もし既にポートフォリオにアップルを1000ドル分持っていれば,注文はDoneされません.この挙動は,order_targetと同じのでご参照ください.

他のオーダー方法

#Quantopian Algorithm で order
#Quantopian Algorithm で order_value
#Quantopian Algorithm で order_percent
#Quantopian Algorithm で order_target

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