PyQuant News 🐍 Profile picture
Apr 13 14 tweets 4 min read Twitter logo Read on Twitter
The BEST algo trading simulator:

Zipline.

Quantopian had $100,000,000 in algorithmic strategies using Zipline.

300,000 people used Zipline on their platform.

So I spent 100 hours studying the code so I could use it too.

The step-by-step guide to getting started in 5 minutes:
Zipline was maintained by Quantopian before it was acquired in 2020.

Some of the features:

• Easy to use so you can focus on algo development
• Includes life-like slippage and commission models
• Dozens of common performance metrics built-in

Ready to dive in?
Start with the imports.

You’ll use pandas_datareader for index data, matplotlib for charting, and PyFolio for performance analysis. Image
Since you’re building in Jupyter Notebook, load the Zipline “magics.”

Zipline creates data “bundles” for backtesting.

You can build custom bundles to ingest any data you want.

Today, you’ll use the pre-built Quandl bundle to ingest price data between 2000 and 2018 for free. Image
Every Zipline strategy must have an initialize function. This is run at the beginning of the strategy.

Here, you set a counter to track the days, the symbol to trade, and set the commission and slippage models. Image
Every Zipline strategy must also have a handle_data function.

It's run at every “bar.”

handle_data is where your strategy logic lives.

In today’s example, you will build a simple dual-moving average cross-over strategy. Image
To start the backtest, define the start and end dates.

Then, grab some data to act as an index.

This helps you compare your algorithm to a benchmark.

Finally, run the backtest. Image
Now that the backtest is finished, use PyFolio to get a breakdown of the results.

This creates a full tear sheet based on your backtest results. There’s a ton of information here, but here are the highlights: Image
Performance analysis gives you statistical measures of your performance. Image
The FREE 7-day masterclass that will get you up and running with Python for quant finance.

Here's what you get:

• Working code to trade with Python
• Frameworks to get you started TODAY
• Trading strategy formation framework

7 days. Big results.

pythonforquantfinancemasterclass.com
Of course, there’s a cumulative return chart. Image
Detailed risk analysis with rolling volatility, Sharpe ratio, and drawdowns. Image
Now you’re comfortable setting up the Zipline backtesting framework.

By doing so, you can use the most powerful toolset for algorithmic trading.

Zipline has it all.
Get ready to supercharge your algorithmic trading with Zipline.

What to keep this thread handy?

Hop back to the top and retweet the top tweet so you can find it later - and so others can find it too!

Here's the link:

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with PyQuant News 🐍

PyQuant News 🐍 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @pyquantnews

Apr 12
Some market data costs $1,400 per month.

The oil that makes the world's financial markets operate.

Unaffordable for 99% of us.

A profit center for countless Wall Street firms.

Fight back.

Here are the 12 Python libraries that give it to you free:
yfinance

Data for stocks (historic, intraday, fundamental), FX, crypto, and options. Uses Yahoo Finance so any data available through Yahoo is available through yfinance.

github.com/ranaroussi/yfi…
pandas-datareader

pandas-datareader used to be part of the pandas project. Now an independent project. Includes data for stocks, FX, economic indicators, Fama-French factors, and many others.

pandas-datareader.readthedocs.io/en/latest/
Read 16 tweets
Apr 10
The Treynor ratio measures the reward-to-volatility ratio.

It helps traders understand how much reward they get for each unit of risk.

You don’t need to be a high-flying trader to use it.

You just need a few lines of Python.

Here’s how it works:
All you need is data.

Import the OpenBB SDK to get it.

This code pulls 8 years of stock price history, extracts the price data, and computes the daily returns. Image
Next, compute beta.

It’s the covariance between the returns of the benchmark and the asset divided by the variance of the benchmark.

Using pandas makes it easy to compute it. Image
Read 7 tweets
Apr 6
I spent a good portion of my $90,000 master's degree learning 1 thing:

Simulating stock prices.

The good news?

You don't need a master's degree to build your own stock price simulator in Python.

I'm going to show you how step-by-step:
By reading this thread, you'll be able to build your own stock price simulator in Python.

Here's what you'll learn:

1. About Geometric Brownian Motion
2. Import libraries and set up
3. Build the functions
4. Visualize results

Ready?
A primer:

GBM is a continuous-time stochastic process where the log of the random variable follows the Wiener process with drift.

What?

It’s a data series that trends up or down through time with a defined level of volatility.

And it’s perfect for simulating stock prices.
Read 15 tweets
Apr 5
ChatGPT is overhyped.

That's what I told myself after 2 weeks of failing to use it well.

Turns out, I was just a poor prompt writer.

But after spending 100 hours with it, I've cracked it.

And now, it's my personal research assistant and Python developer.

Here's how: Image
From the beginning, I expected ChatGPT to "know" how to code (and trade).

That was the critical mistake.

I gave it 1-sentence prompts and expected it to write code for me.

But garbage in = garbage out.

So I reframed it:
ChatGPT is my all-knowing research assistant.

And if I train it correctly, it writes code that I can use immediately.

Unfortunately, I spent hours and hours using it all wrong (and you might be too).

Let's dive into how to use AI as your research assistant:
Read 20 tweets
Apr 3
Statistical arbitrage is scary sh*t.

That's what I used to think until I spent 25 hours studying pairs trading.

Now I realize it's a great way to get started with algorithmic trading.

Here’s how to build a pairs trading strategy in Python.

Step by step:
By replicating this framework, you’ll be able to:

1. Get stock price data
2. Find cointegrated pairs
3. Model the spread
4. Trade the strategy

What is pairs trading anyway?
Pairs trading is a way of trading an economic relationship between two stocks.

Two companieswith the same supply chain will be impacted by the same economic forces.

Pairs trading tries to model that relationship and make money when the relationship temporarily breaks down.
Read 16 tweets
Apr 2
4 hacks to get started with Python

• Work on things you can use in real life
• Find a support group to give you help
• Build in Public to stay accountable
• Write a little bit of code everyday

The only real hacks aren't hacks at all.
I just saved you 100+ hours of wasted effort learning from tutorials.
The FREE 7-day masterclass that will get you up and running with Python for quant finance.

Here's what you get:

• Working code to trade with Python
• Frameworks to get you started TODAY
• Trading strategy formation framework

7 days. Big results.

pythonforquantfinancemasterclass.com
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(