Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Overreaction

The basic idea of the algo is to short the stock when difference of previous high and low is more than standard deviation of past 200 days(price) . But it seems to be giving quite erratic results .

9 responses

Hi Yatharth,
I havent looked at your logic, but you may be leveraging and buying more than the amount you intent to buy.
Two things might fix it.
Use order_target_percent instead of order and on the next handle_data use get_open_orders to make sure that you dont order another tranche before the first one get fulfilled
Ajay

Any time the equity goes below -100%, it means over leveraging, yes?

I am not really sure if I am leveraging . Please point it out if so. This is my first algo

Hey Yatharth, I think the over-leveraging emerged from your ordering logic. You used

order_percent(sid(700), -1)  

This will actually take the current value of your portfolio and order that much again * -1. I think what you wanted was
order_target_percent(sid(700), -1) This will adjust your portfolio to be 100% shorting sid(700). I modified the ordering logic slightly in this backtest I'm attaching. It now doesn't have the same crazy behavior. Hopefully you can carry your idea forward from here.

Disclaimer

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.

BTW: what Delaney Granizo-Mackenzie suggested works perfectly

Thanks a lot everybody ! It works now

I have changed the backtest to long positions rather than short positions. But in the transaction details I am not able to see if it covered the position on the same day or not.

if context.portfolio.positions_value != 0.0 and intradingwindow_check(context):
for stock in context.stocks:
amount = context.portfolio.positions[stock].amount
if amount != 0:
order_target_percent(stock,0)

def intradingwindow_check(context):
# Converts all time-zones into US EST to avoid confusion
loc_dt = get_datetime().astimezone(timezone('US/Eastern'))
# if loc_dt.hour > 12 and loc_dt.hour < 15:
# if loc_dt.hour == 10 and loc_dt.minute == 0:
if loc_dt.hour > 15 & loc_dt.minute > 15:
return True
else:
return False

According to this code the same position should be squared off at 15 minutes prior to the close of the market. But transaction details do not show the square off transaction. Is square off even happening ?

Also in the transaction details I can see that after the very first buy of amount close to 1000000(set as initial capital) the next transactions are very small. I am covering the position everyday and my buy percent is 100% so shouldn't it buy as many shares as possible within 1000000 amount or the current portfolio amount?

Looks like you're running it in daily frequency mode, in which case handle data will never be called in the time range you specify. I would try switching to minute mode, and also using schedule_function, which can call a function at a specified time/frequency. Documentation for this can be found in the help docs.