Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algo going -22000%

I thought i had prevented borrowing when

set_long_only()

also got

trying to eliminate going short.

if current_position > 0:
order(sec, -current_position)

Why is the algo still going more than -100%.?

Please HELP!!

2 responses

A few observations to maybe help out.

First 'set_long_only()' , in a backtest, will only log an error if the algorithm encounters a short order. It doesn't do any logic to prevent it. Look at the logs and you will see a number of messages like 'Order for 29184 shares of Equity(33734 [OPTT]) at 2016-11-02 19:40:00+00:00 violates trading constraint
LongOnly({}).'

The culprit is the 'profit_time' method. Even though the logic only tries to sell the amount in 'context.portfolio.positions[stock].amount', the logic doesn't look for any outstanding orders. Moreover, you are calling this method from the 'handle_data' method in addition to scheduling. This results in placing multiple orders to sell the same stock over and over again. Remove the call to this method from 'handle_data' and I believe this should be fixed (not sure what the purpose of calling this every minute in handle_data).

There are several ways to control leverage. One is to check if 'context.account.available_funds' has enough cash to place a buy order. Which appears is the approach taken here at least in the ' buy_time' method. The other is to use the 'order_target_percent' method when ordering and make sure the target percents never exceed 1.

Another observation. The 'screen_time' method should be called in the 'before_trading_start' method and NOT as a scheduled function. The way you have it being called at the end of each day actually grabs the previous days data so the data you are working with first thing in the morning is actually 2 days old and, worse, isn't split adjusted in the case of a stock-split effective today.

Good luck.

Thank you so much for your prompt answer.

I eliminated 'handle_data' and I get a %40 return now. The transactions seem to be more in order with what I'm doing. I misunderstood 'handle_data' and that's the reason i had the sell function in there.

Now i have to research about stock-split and also preventing 'PDT'.

Thanks for your help.
When I have the Algo in live trading, the screen_time function will work with the information of the day and not with the info of 2 days before. Am I correct in this assumption? That's the reason I set it that way.