This thread is about implementing the strategy In & Out for paper/live trading. Ideally we will work together on this and will end up with a mix between a tutorial and a discussion where the In & Out algo is an example for any algo one wants to trade live. My plan is to share what I've learned about porting a strategy from Quantopian to live paper trading.
And of course you are welcome to share your experience with this as well!

A while ago I met someone with whom I considered founding something like a small hedge fund. Part of this was to look for the best way to run algorithms that execute actual trades. As it is best practice, the first stage was to paper trade the algorithm for a while before we start investing in it. Long story short, our algorithm dind't make it beyond the paper stage and we abandoned the project, no one lost money but we gained valuable experience which I'm willing to share.

In order to automatially trade a strategy you generally need

• a computer where the algorithm can run and
• a broker where the algo can send the orders to.

I've tried some different roads which would probably lead to the same goal but naturally chose the one that was easiest for me.

• implementing the algo on QuantConnect to trade it from there --> would have cost money just to try it
• using IBridgePy --> wasn't possible on Linux
• using Zipline-live --> was a bit of a hassle to set up with data
• using Alpaca with Blueshift integration --> Blushift didn't have the symbols I needed
• and probably some I don't remember...

I ended up using Alpaca as a broker. They have no costs for orders and a very nice python library. I also belive it was the only no (or 'low') cost broker I could find that has an API and a paper account where you can use the API without having to put in funds first.
For instance, allthough Thinkorswimm has a paper account, you couldn't use the TD-Ameritrade API with it. Perhaps this has changed by now, but back then you only could use the API for live trading with real money.
There may be brokers better suited for your needs, so feel free to use them instead!

For the computer there are many choices, from your own computer over cloud servers, to a dedicated VPS. I even could have used the host of my website - if they had python 3 instead of 2 (and no, they still haven't upgraded).
In the end I decided to use PythonAnywhere. Like the name hints it has already python on it with the most common libraries. With their free plan you get everything you need to run a trading script once a day (or continuously, but I wouldn't do that).

So, what I can show here is to use Alpaca + PA in order to run a live paper test. It's probably just one or two steps from there to trading with real money, but I haven't done that yet and also have no plans to do so just to show how it works ;)

Setting everything up will take some work/time and I can't post everything at once, so I will post it bit by bit. Also, I'll start with a simpler algo first, from there we can expand it further until we have the In & Out strategy 'up and running'.

If you have some experience with this, feel free to join!
It would probably be beneficial to see how different people tackle this task.
In the meantime I'll prepare a notebook with some simple code using the alpaca library.

32 responses

Great job,
If the algo had extensive trading/rebalancing intraday, a full automated solution on the cloud would be very necessary.

If there are only a few rebalancing daily/weekly, a local PC would be good enough.

Eighter on local or remote server, we
1. get data
2. generate signal
3. execute

So let's narrow down: We are discussing an algo based on EOD(End of Day) data.

We need a script that can:
1. get data: Yahoo has good EOD data
2. run algo to generate signal: once a day after market close
3. execute: send a limit order to the broker after market close and the broker would execute the orders the next day.

So, we do it once a day after the market close.

Just wondering - is the forum going to stay alive after Nov-14?

has someone used the portfoliovisualizer?

I'm a bit shocked about the Q shutdown. Now I can't post notebooks any more...

You can retrieve Yahoo data with pandas_datareader which can be installed using

pip install pandas_datareader


A history request might look like this:

import pandas_datareader as web

start_date='2014-01-01'
end_date='2015-01-01'
hist = web.DataReader('SPY', 'yahoo', start_date, end_date)


You can also pass a list of symbol to get prices for all of them at once. The end_date is optional, if you leave it out you get prices up to the most recent.

Another library I know of is yfinance, also installable with pip.
And brokers with an API usually also offer price data.

So where will we go now? What platforms do you know?
I'm already on QuantConnect but haven't been very active there.

@Aleksei
I think with community platform they mean the whole website, so the forum will be gone too.
What a shame, this forum is full of gemstones...

Does anyone have a good alternative for backtesting strategies? I loved the ease of use of Q.

@Charlie
I guess QuantConnect could be an alternative. But there are many differences, one of them being that you can only run one backtest at a time with a free account. And of course it takes a bit of research, time and work to get your Quantopian strategies working with QC code. But I think this will be the case with any alternative.

If any one knows other platforms please post them.

Hello everybody, I created a slack for discussion this strategy:

Another way to backtest trading Ideas would be Quantiacs.

Pros:

• You can install their toolbox locally (I think you don't even need to sign up for that).
• There's a competition going on right now where you can win an allocation

Cons:

• It is meant for trading futures, so the universe is limited to 70 something futures + around 500 stocks
• They used to have a forum some time ago where I could find answers to a lot of my questions but that's gone now

Can anyone please explain this piece of the code that @Vladimir kindly shared with us?

    if exit: BULL = 0; OUT_DAY = COUNT;
elif (COUNT >= OUT_DAY + WAIT_DAYS): BULL = 1
COUNT += 1



I am a bad coder, trying to re-run his strategy in Excel, but it's clearly not working as intended without this piece.

This is the entire code if anyone is interested:

# Price relative ratios (intersection) with wait days
import numpy as np
# -----------------------------------------------------------------------------------------------
STOCKS = symbols('QQQ'); BONDS = symbols('TLT','IEF'); LEV = 1.00; wt = {};
A = symbol('SLV'); B = symbol('GLD'); C = symbol('XLI'); D = symbol('XLU');
MKT = symbol('QQQ'); VOLA = 126; LB = 1.00; BULL = 1; COUNT = 0; OUT_DAY = 0; RET_INITIAL = 80;
# -----------------------------------------------------------------------------------------------
def initialize(context):
schedule_function(daily_check, date_rules.every_day(), time_rules.market_open(minutes = 140))
schedule_function(record_vars, date_rules.every_day(), time_rules.market_close())
def daily_check(context,data):
global BULL, COUNT, OUT_DAY
vola = data.history(MKT, 'price',  VOLA + 1, '1d').pct_change().std() * np.sqrt(252)
WAIT_DAYS = int(vola * RET_INITIAL)
RET = int((1.0 - vola) * RET_INITIAL)
P = data.history([A,B,C,D], 'price',  RET + 2, '1d').iloc[:-1].dropna()
ratio_ab = (P[A].iloc[-1] / P[A].iloc[0]) / (P[B].iloc[-1] / P[B].iloc[0])
ratio_cd = (P[C].iloc[-1] / P[C].iloc[0]) / (P[D].iloc[-1] / P[D].iloc[0])
exit = ratio_ab < LB and ratio_cd < LB
if exit: BULL = 0; OUT_DAY = COUNT;
elif (COUNT >= OUT_DAY + WAIT_DAYS): BULL = 1
COUNT += 1
wt_stk = LEV if BULL else 0;
wt_bnd = 0 if BULL else LEV;
for sec in STOCKS: wt[sec] = wt_stk / len(STOCKS);
for sec in BONDS: wt[sec] = wt_bnd / len(BONDS)

for sec, weight in wt.items():
order_target_percent(sec, weight)
record( wt_bnd = wt_bnd, wt_stk = wt_stk )

def record_vars(context, data):
record(leverage = context.account.leverage)


Just a thought, if QuantConnect has the slightest chance of rising to the top maybe one of the contributors or OP of In_Out can get something started on QC soon so you capture the main thread for it, even if its a wip share as porting issues are worked out. Since In_Out became quite a popular algo the move of it to somewhere else will give the Q community a good indication where to go next.

I'm in the middle of playing around with a port on QC now but personally Id rather see the owner or one of the other main contributors be OP for on it QC. I'll help with porting and DM the owner or any of the main contributors my progress if you want it (send me a DM on Q).

My fear is that with things like QuantConnect or Quanticas we become too much dependant on external infrastructure which can be shut down any day. I was hoping there are some tools that allow to leverage broker's infrastructure for algo trading. I know people are running backtests and trade algos using Interactive Brokers' not exactly sure how they get there though.

Please migrate to QuantConnect. It is very reliable and many are live trading on it for years!!

Quantconnect is the best option. Please do this amaizing strategy on it. QC also has a very vibrant community forum. Plus their staff is actively helping people.

What about IBridgePy.com - did anyone try?

I use the IbridgePy since months. The problem is tutorial is quite simple. And it seems not so easy to do the backtesting. If you need more you have to pay.

I see this in the post of closing community:

Erol Aspromatis 2 hours ago
Very sad to see this happen. Guess I'm lucky as I've already transitioned to my own server and data using Zipline and Pyfolio for backtesting. If anyone needs help, feel free to reach out to me. I've also recently created a few videos on my YouTube channel on how to set up and run Zipline and Pyfolio locally if it helps anyone:

This sounds interessting for backtesting.

Has someone seen this?

@ Thomas Chang, understood, thank you!

Any chance someone could help with the code above please? The strategy seems too good to be true, so I am recalculating it in Excel to manually check. So far I am getting results inferior to Quantopian test, but I don't understand the piece of code highlighted. I am willing to share the results, but want to finalize the test first.

Hello everybody, I created a slack for discussion this strategy:

Backtrader is also another platform I've used before. It purely offline so it doesn't require a subscription to use.

Since Quantopian is based on Zipline, I tried out Norgate Data for my backtesting futures and stocks. It works well (only on a daily timeframe though) and easy to port any price-based filters across. Has some nice features for historical index constituent universes/delisted stocks and futures rolls.

Hi,

just a quick idea: i started to implement this strategy with a python script and yahoo finance. as this strategy trades on a monthly basis, i dont think, that there is a need to automate the trading. i plan to implement a telegram bot, which sends notifications.

Alex

For what it’s worth I’ve traded $200K live through QC and IB. It’s well worth the$20/mo which includes a machine to run your algo on... it’s basically the price of a VPS.

Even getting something to run locally you’ll need a dedicated machine to rune your code on.

I don’t think there is anything that can be beat at its current price point. I was honestly still surprised people were still on Quantopian after they shut down live paper trading.

A lot of the guys from here moved over to QC. I actually suggested the “In and out” strategy over there in a thread.

I’ll make a new thread there and people can hop over if they’ll like.

Quantconnect support will even help in migrating the strategy fully over, what else can you ask for?

You have to be logged into Quantconnect to see the thread for some reason FYI.

I get 404 error.

I like Quantopian like platform such as zipline.

Slack group link for discussing the algo: https://join.slack.com/t/quantopian-workspace/shared_invite/zt-imkqihmt-IkFhFhc6tEAGrc4mQjlgUA

@Elsid:
I also get a 404 error even when logged in...

@all:
I've decided to start a thread with the original idea of this one on QuantConnect.
But I guess it will be next week since I'm otherwise engaged over the weekend, I'll send a link here once it started.
Hope to see you all there!

For anyone who are interested in running Quantopian code for live trading on your local machine, you may consider IBridgePy www.IBridgePy.com, a python platform to backtest and live trade with Interactive Broker, TD Ameritrade and Robinhood.
We have a tutorial about migrating from Quantopian to IBridgePy and you may run your Quantopian codes even without any changes.

"Algorithmic Trading Python From Idea to Back test to Live Markets Interactive Brokers TD Robinhood" by Dr. Hui Liu https://youtu.be/LBfHeixw6rA "Overview of IBridgePy: backtest and live trading" by Dr. Hui Liu https://youtu.be/xWMzTgGWv48 If you need any help on coding, please check out our well known Rent-a-Coder service.

Disclaimer: This is Dr. Hui Liu, who created IBridgePy.

I want to point out that
1. IBridgePy supports Ubuntu Linux, Windows and Mac.
2. IBridgePy can be setup on cloud, for example EC2 https://ibridgepy.com/set-virtual-server-ibridgepy-amazon-ec2/
3. Users can use any data source to backtest on IBridgePy's backtest system.
4. Most of features in IBridgePy are free but we need to make paid features for long term growth.

For what it is worth: I started live trading with Quantopian, I traded for years with zipline-live and later with zipline-trader but after a few years, I decided to split the research and the execution. So currently I use Alpaca on a bastardized Pylivetrader (with data redundancy functions) but I'm changing it to lambda functions running on Alpaca as an execution venue (and IB as a backup venue). This way I can reuse the controlling functions (stop-loss, deviation from risk parameters) I'll migrate my research to Quant Connect and my own installed zipline on my own servers for the AI/ML research.

Twice now Quantopian has ripped the carpet under us... I'll be more independent from now on: more tedious but being dependent on flaky companies is not cool