LoginSignup
1
0

More than 5 years have passed since last update.

#Quantopian Algorithm で order_value

Last updated at Posted at 2018-02-27

order_value

order_valueは,銘柄と取引額を指定して注文する関数です.

order_value(asset, amount, style=OrderType)

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

アルゴリズム例

#Quantopian Algorithm で orderのコードを一箇所だけ変えました.

def initialize(context):
    # AAPL
    context.security = sid(24)

    # rebalance 関数を毎日クローズ一分前(15:59)に実行するように指定
    schedule_function(rebalance, 
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close(minutes = 1))

def rebalance(context, data):
    # 過去5日間の平均を取得して,今日の価格よりも1%大きければLong
    # 今日価格が移動平均より小さければ,ポジションクローズする

    # 過去5日間のヒストリカルデータを取得.
    # 【注意】https://www.quantopian.com/help#ide-history に説明があるとおり,
    # 日中にヒストリカルデータを複数日分取得すると,
    # 「過去4日分と今現在の価格」が取得できます.現在価格はhistorical data 同様,アジャストされた価格です.
    price_history = data.history(
        context.security,
        fields='price',
        bar_count=5,
        frequency='1d'
    )
    # price_historyを出力
    # log.info(price_history)

    # 平均値
    average_price = price_history.mean()

    # 現在の価格
    current_price = data.current(context.security, 'price') 
    # log.info(current_price)

    # 注文しようとしている銘柄が,現在上場されているか確認
    if data.can_trade(context.security):
        # 平均値より,1%大きければ,Long
        if current_price > (1.01 * average_price):
            # 成り行きで10株買う
            order_value(context.security, 1000) # ←変更箇所
            log.info("Buying %s" % (context.security.symbol))
        # 平均値より小さければ,ポジションをクローズ    
        elif current_price < average_price:
            # 0と注文することでポジションを精算することになる
            order_target(context.security, 0)
            log.info("Selling %s" % (context.security.symbol))

    # 平均値と現在価格を描画
    record(current_price=current_price, average_price=average_price)

メモ

このアルゴリズムは,条件にあえば1000ドル分買い足して行くというアルゴリズムです.1000ドル分とは,1000ドルを超えないオーダーという意味になります.例えば,AAPLが105ドルであれば,9株注文してくれます.株数を確認したい場合は,run full backtest を実行した後,Transaction Detailsを見ると確認できます.

(株価に合わせて,よしなに5株買ったり6株買ったりしている↓)

Screenshot from 2018-02-27 14-22-04.png

気が向いた時に,アルゴリズムの注文方法を少しずつ書いていこうと思います.
次はorder_percentです.

他のオーダー方法

#Quantopian Algorithm で order
#Quantopian Algorithm で order_percent
#Quantopian Algorithm で order_target
#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