I just started using Quantopian, can someone help me replace the cash (when there are no positions) with a bond ETF like IEF.
--Thanks
I just started using Quantopian, can someone help me replace the cash (when there are no positions) with a bond ETF like IEF.
--Thanks
Michael!
I hear you in fact when I wrote my first algo i had the same problem with standing cash and guess what.... I too ecide to fill it with bonds!
Here is the link to it if you are interested. Basically there are many ways to solve this problem, I think the approach of using the
order_target_percent()
method to be really nice as it also allows you to control your leverage should you ever decide that's needed. With that method you can explicitly target your portfolio's holding to whatever security you want.
order_target_percent(stock, 0.75)
order_target_percent(bond, 0.25)
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.
Welcome Michael.
As James pointed out you can use the order_target_percent function.
See attached backtest.
If this were live trading, would it be better to sell the bond prior to buying the security in the MA1 < MA2 case? I'm curious if the buy would fail on an account without margin based on which orders are submitted.
Thanks for the help,
I noticed it makes trades every day to maintain the allocation, is there a way to limit the trades to just when the MAs cross?
Yeah you could do it so only when there is a cross trades are being made. Consider setting somesort of boolean flag whose state you change upon a cross over.
def initialize(context):
context.MA1_above_MA2 = False
def handle_data(context, data):
MA1 = data[context.stock].mavg(5)
MA2 = data[context.stock].mavg(9)
current_mavg_indicator = MA1 > MA2
if not context.MA1_above_MA2 and current_mavg_indicator:
order_target_percent(context.stock, .40)
order_target_percent(context.bond, .25)
order_target_percent(context.ibond, .30)
order_target_percent(context.gold, .15)
elif context.MA1_above_MA2 and not current_mavg_indicator:
order_target_percent(context.stock, 1)
order_target_percent(context.bond, 0)
order_target_percent(context.ibond, 0)
order_target_percent(context.gold, 0)
context.MA1_above_MA2 = current_mavg_indicator
Try that out let me know if I made any mistakes and I'll edit the reply.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.