Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem in my top-triangle and bottom-triangle strategy

Basically I think my strategy is OK.
To rebalance the buy operation every day before trading start, buy stocks which meet the fundamental criteria and are in the bottom_triangle.

Sell stocks in the method of handle_data when the price is at top_triangle.

The overall performance is quite good in these two months. However, when I look into the transaction data, I noted that sometimes my algorithm keeps selling the same stock for several times and buy them back later. It's quite weird. Because I expect the program to sell it only if they have been bought before.

Could anyone have a look at my program and tell me the reason..I'm quite confused.

Many thanks :)

2 responses

right off the bat I can see that you have a leverage problem by adding this to your handle_data():

record(leverage=context.account.leverage)  

One reason for this is you don't have a check to see if you already have tried to sell/buy a stock before ordering it again, try using this check every time you place an order:

if not get_open_orders(stock):  
    place order  

you can also help leverage issue by adjusting how much stock you order based on how many stocks pass your filter:

order_target_percent(stock, 1.0/len(stocks_to_buy))  

Thank you so much for your advice.
After making the corresponding changes, the error has been solved.

However, even though i have set the slippage in my code, the log on info of program stills display some kind of warning:
WARN Your order for 15430 shares of IKNX failed to fill by the end of day and was canceled.

Do you understand what caused this kind of warning? I'm so confused.

Really appreciate for your help! Thank you !