Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
My first test program, XIV

I'm completely new to Quantopian. Here's my first little program using XIV as example

In the below example, the main idea is to look at each day, if the price of XIV is 2% below previous days close, the buy 100% of XIV, then place an order to sell the full position at a limit price of previous day's close (i.e. target 2% gain). The back tested result seems to show the below algo is not what's intended. Any comments please?

Also, is there a place on the site that one can find generic programs so that newbies can learn from.

def initialize(context):
context.xiv = sid(40516)

def handle_data(context, data):
hist = data.history(context.xiv, 'price', 2, '1d')
if hist[1] / hist[0] < 0.98:
order_target_percent(context.xiv, 1.00)
order_target_percent(context.xiv, 0.00, style=LimitOrder(hist[0]))

Wonder if this is the correct way to program the idea?

7 responses

The order to sell is getting cancelled. First thing I found was that they cancel your order to sell by the end of the day, so you will have to initiate it again the next day. I'm not sure if they have a concept like an expiration date for an order.

In general, the best way I've found to troubleshoot is to run a full backtest and look at the actual trade history. Usually that reveals the problem. Or, you can insert log statements that demonstrate behaviors that you expect. If it does something other than what you expect, the code is wrong.

Many thanks Jeffery!

I have run full backtest. What's really puzzling me is that it seems the algo will buy or sell more than the portfolio size, i.e. it keeps on buying or keep on selling every minute. But i thought the codes

order_target_percent(context.xiv, 1.00)
order_target_percent(context.xiv, 0.00, style=LimitOrder(hist[0]))

only will execute up to the percentage indicated. Any idea what I had done wrong?

By putting your order to sell direcrly below the order to buy, you are essentially buying then immediately selling. If this wasn't your intended goal you should move to condition to sell outside the If statement and enclose it in a different If statement that provides your sell conditions.

I have never used the limit order function and can't say how. I prefer to just create a sell condition. If you want a profit target at 2% calculate it from the current price and make a new if statement

If hist [0] > objective_price:
order_target_percent(context.xiv, 0.00)

You also probably should use a bool variable to let your sell condition know you're in a trade

Another problem with your code is that it will call it every minute, so imagine your buy condition was met for the prior day, it will buy the instrument every minute the whole day.

You need a flag to tell it not to buy again until the position is sold. I just create a context variable called context.in _trade or something

Many thanks Jeffrey for the comments and kind help. Indeed this code is rather raw, and I've learned quite a few things over the last two days playing with the code and testing other variations. In case there are people in the same situation as I'm, I'd like to share some of my findings:

1) Since the algo runs every minute, the buy/sell order submitted at certain minute may not get filled immediately. When the next minute comes (and when condition is still valid), a new order will be submitted. This created the behavior that algo keeps on buying/selling many times. Solution would be to check before order whether there's any outstanding orders using:

get_open_orders(context.xiv)

2) Just learned that at end of each day, unfilled orders will be canceled by system. So need to resubmit order at next day's open.

def initialize(context):  
    context.xiv = sid(40516)  
    context.ziv = sid(40513)  
    context.vxx = sid(38054)

def handle_data(context, data):

    hist =  data.history(context.xiv, 'price', 2, '1d')  
    if hist[1] / hist[0] < 0.98:  
        order_target_percent(context.xiv, 1.00)  
        current_price = data.current(context.xiv, 'price')  
        if current_price >= hist[0] and data.can_trade(context.xiv):  
            order_target_percent(context.xiv, 0)