I debugged my strategy and can explicitly see that the method order_target(sid, 0) gets called to close out a long position. But the position is never closed. Has anyone experienced similar issue?
I debugged my strategy and can explicitly see that the method order_target(sid, 0) gets called to close out a long position. But the position is never closed. Has anyone experienced similar issue?
Here are my log statements. I log the opening and closing of position and also log open orders. Order remains open and I have a long position that is never closed.
2012-09-13myfunc:268INFOopen position
2012-09-14myfunc:223INFOSecurity(41281 [FPA])
2012-09-14myfunc:273INFOclose position
2012-09-17myfunc:223INFOSecurity(41281 [FPA])
2012-09-18myfunc:223INFOSecurity(41281 [FPA])
2012-09-19myfunc:223INFOSecurity(41281 [FPA])
2012-09-20myfunc:223INFOSecurity(41281 [FPA])
2012-09-21myfunc:223INFOSecurity(41281 [FPA])
2012-09-24myfunc:223INFOSecurity(41281 [FPA])
2012-09-25myfunc:223INFOSecurity(41281 [FPA])
I have a feeling that the order to buy FPA on 13th was actually filled on 25th and hence the issue. Why so long?
Hi Pavy,
The following code snippet correctly opens and closes positions with order_target
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
context.bar = 0
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
# data[sid(X)] holds the trade event data for that security.
# context.portfolio holds the current portfolio state.
# Place orders with the order(SID, amount) method.
# TODO: implement your own logic here.
if context.bar % 3 == 0:
order_target(sid(24), 50)
if context.bar % 4 == 0:
order_target(sid(24), 0)
log.info(context.portfolio.positions[sid(24)])
context.bar += 1
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.