Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is it possible to go short in Quantopian and how to close short positions ( I am new in Quantopian)

Hi

I am new in Quantopian this Phyton code is to open (buy) a position (go long) but I would like to learn to
go short and how to close the short positions in Quantopian

price = data[context.stock].price

notional = context.portfolio.positions[context.stock].amount * price  
totalstocks = notional/price

    if exmavg>eexmavg and price > xxmavg:  
           order(context.stock,+100)  
    elif xmavg < xxmavg:  
         order(context.stock,-totalstocks)  

but I would like to know how to go short and how to close a short position in Phyton Quantopian

Thanks in advance
Eloy

6 responses

Hello Eloy,

Here's a quick example. It goes short 200 shares SPY and then closes 100 of the 200 shares the next day.

Note that the cash from the short sale ends up in your virtual account, along with any initial capital. There is no separate accounting. So, your algorithm will have to include code to sort out how much cash you can apply toward additional long/short positions.

Grant

Hi Grant, to make sure I understand this correctly, if you short a stock that you don't own, you basically need to keep track of the fact that some of the cash in your portfolio is borrowed money that you need pay back later. Correct? If so, isn't this a little weird? I'd expect the framework to help keep track of short positions so they can be closed later.

Yes, I believe that's correct. I'm no expert, but I think that part of the rationale is that in live trading, one has to consider account types, margin rules, securities, clearing periods, etc., so there is no clear-cut formula that can be applied. I think there are some examples of code out there (on this forum) for handling borrowing/shorting that run on the Quantopian backtester that address the issue, so it's not a show-stopper. It's just that there's nothing built-in.

Thanks Grant!

I will try it

Do you know if in Quantopian Live Trading, is it possible to run High Frequency Trade ?

Thanks in advance!
Eloy

You can trade at most every minute, but my understanding is that Quantopian works to minimize latency. Every minute, the minute bar data are computed (from the prior minute's live trade data) and are then immediately available within handle_data. Order submissions are asynchronous, meaning that they are sent to the broker as soon as the line of code is executed. However, I've never seen any statistics on the whole system, so, if you need tight timing, I'd dig a bit deeper with the Quantopian folks. My hunch is that there's enough latency and jitter to fall way outside the realm of what would be considered high-speed trading (competitive with institutional speeds). --Grant

Thanks Grant!