LoginSignup
1
0

More than 5 years have passed since last update.

#Quantopian Algorithm で order_percent

Last updated at Posted at 2018-02-28

order_percent

order_percentは,現在のポートフォリオ価値の〜%分注文する関数です.現在のポートフォリオ価値とは,ポジションの評価額と現金の合計です.

order_percent(asset, amount, style=OrderType)

asset: Equity/Future オブジェクト
amount: 割合.0.5であれば現在のポートフォリオ価値の50%分をLong.-0.5であれば50%分をShort.
style: 注文方法(指定方法はorder_percentの説明を参照)

アルゴリズム例

(オーダー方法以外は,#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):
            # 成り行きで現在のポートフォリオ価格に対して100%分のアップル株を買う
            order_percent(context.security, 1.0)
            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)

現在価格が,過去5日の平均より1%大きければ,今のポートフォリオの価値分AAPLを買うという(かなりアグレッシブな)アルゴリズムです.

1000ドルからスタートすると,このようになります.一日目に6株買い,次の日も6株買っていますね.

Screenshot from 2018-02-28 13-13-36.png

条件にあえば,ポートフォリオ価値分買い足すので,このような取引履歴になります.

メモ

注意していただきたいのは,ここでいうポートフォリオ価値というのは,自分が持っている株の評価額+現金です.上のTransaction Detailsを見るとわかるように,たとえば二日目の6株買い増しは,最初の自己資金1000ドルだけでは買えません.つまり借金してでも買い増すというアルゴリズムです.ポートフォリオ価値というのは,評価額+現金であって,評価額+現金-借金ではないというところに注意してください.

それでは困るよ,というのが普通(?)かとおもいます.
そういう時に使える,order_target / order_target_value / order_target_percent という取引関数がありますので,次回はその説明をします.

他のオーダー方法

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