Paper Trading a Strategy on Quantopian - Python for Finance 12

Algorithmic trading with Python Tutorial




The idea behind paper trading is to act as a method of "forward testing" a strategy. Paper trading is where we take fake (paper) money, and "pretent" to have invested in a company, only this time we're doing it in real time. This is our way to tracking, without hindsight bias how our strategy actually might do. This still obviously excludes you from actually entering the market, so this very fact makes this not a perfect method to test a strategy, but it is a lot better than an ordinary back-test.

To show how we can do a paper-trading example, we'll use the long/short strategy that we built back form: Achieving Targets - Python for Finance with Zipline and Quantopian 8. That code was:

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.features = {}
    context.stocks = symbols('XLY',  # XLY Consumer Discrectionary SPDR Fund   
                           'XLF',  # XLF Financial SPDR Fund  
                           'XLK',  # XLK Technology SPDR Fund  
                           'XLE',  # XLE Energy SPDR Fund  
                           'XLV',  # XLV Health Care SPRD Fund  
                           'XLI',  # XLI Industrial SPDR Fund  
                           'XLP',  # XLP Consumer Staples SPDR Fund   
                           'XLB',  # XLB Materials SPDR Fund  
                           'XLU')  # XLU Utilities SPRD Fund
    
    schedule_function(rebalance,
                     date_rule = date_rules.every_day(),
                     time_rule = time_rules.market_open())
    
def rebalance(context, data):
    pass
  
    

def handle_data(context, data):
    for stock in context.stocks:
        ma1 = data[stock].mavg(100) 
        ma2 = data[stock].mavg(300)
        
        price = data[stock].price
        
        if ma1 > ma2:
            order_target_percent(stock, 0.11)
            
        elif ma1 < ma2:
            order_target_percent(stock, -0.11)  

From here, what we need to do is run this code on a "full backtest" against minute data.

Once you've done that, you should have the option to choose the "live trade algorithm" button at the top right. Clicking that, you have the choice to either paper trade or connect your brokerage account. We'll just go the paper trading route. Once that's done, you're all set.

I've been running the long/short strategy at the time of my writing this for about a month and a half, here's the current situation:

At least so far, this strategy is clearly less volatile, and even currently outperforming.


There exists 2 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Programming for Finance with Python, Zipline and Quantopian
  • Programming for Finance Part 2 - Creating an automated trading strategy
  • Programming for Finance Part 3 - Back Testing Strategy
  • Accessing Fundamental company Data - Programming for Finance with Python - Part 4
  • Back-testing our strategy - Programming for Finance with Python - part 5
  • Strategy Sell Logic with Schedule Function with Quantopian - Python for Finance 6
  • Stop-Loss in our trading strategy - Python for Finance with Quantopian and Zipline 7
  • Achieving Targets - Python for Finance with Zipline and Quantopian 8
  • Quantopian Fetcher - Python for Finance with Zipline and Quantopian 9
  • Trading Logic with Sentiment Analysis Signals - Python for Finance 10
  • Shorting based on Sentiment Analysis signals - Python for Finance 11
  • Paper Trading a Strategy on Quantopian - Python for Finance 12
  • Understanding Hedgefund and other financial Objectives - Python for Finance 13
  • Building Machine Learning Framework - Python for Finance 14
  • Creating Machine Learning Classifier Feature Sets - Python for Finance 15
  • Creating our Machine Learning Classifiers - Python for Finance 16
  • Testing our Machine Learning Strategy - Python for Finance 17
  • Understanding Leverage - Python for Finance 18
  • Quantopian Pipeline Tutorial Introduction
  • Simple Quantopian Pipeline Strategy