Back to Community
Use a trailing stop loss

So, I modified the sample algorithm that we all are able to start with... made the numbers a bit more friendly to a small investor and plugged an S&P Index spyder for the security to invest with...

Now I'm trying to (without any success) to place a trailing stop along with each order. I'd like to limit losses on any trade to no more than 5% and have that margin move with the price.

A bit more advanced a possibility--I'd like to figure out how to widen the margin to 10% or 15% once such a margin would result in no loss of original capital.

Help for a newb?

Clone Algorithm
71
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
  # For this example, we're going to write a simple momentum script.  When the 
  # stock goes up quickly, we're going to buy; when it goes down quickly, we're
  # going to sell.  Hopefully we'll ride the waves.

  # To run an algorithm in Quantopian, you need two functions: initialize and 
  # handle_data.

def initialize(context):
  # This initialize function sets any data or variables that you'll use in
  # your algorithm.  For instance, you'll want to define the security (or 
  # securities) you want to backtest.  You'll also want to define any 
  # parameters or values you're going to use.

  # In our example, we're looking at Apple.  If you re-type this line 
  # yourself, you'll see the auto-complete that is available for the 
  # security ID.
  context.spy = sid(8554)
  
  # In these two lines, we set the maximum and minimum we want our algorithm 
  # to go long or short our security.  You don't have to set limits like this
  # when you write an algorithm, but it's good practice.
  context.max_notional = 1000.1
  context.min_notional = -10.0

def handle_data(context, data):
  # This handle_data function is where the real work is done.  Our data is
  # minute-level tick data, and each minute is called a frame.  This function
  # runs on each frame of the data.
  
  # We've built a handful of useful data transforms for you to use.  In this 
  # line, we're computing the volume-weighted-average-price of the security 
  # defined above, in the context.aapl variable.  For this example, we're 
  # specifying a three-day average.
  vwap = data[context.spy].vwap(3)
  # We need a variable for the current price of the security to compare to
  # the average.
  price = data[context.spy].price
     
  # Another powerful built-in feature of the Quantopian backtester is the
  # portfolio object.  The portfolio object tracks your positions, cash,
  # cost basis of specific holdings, and more.  In this line, we calculate
  # how long or short our position is at this minute.   
  notional = context.portfolio.positions[context.spy].amount * price
  
  # This is the meat of the algorithm, placed in this if statement.  If the
  # price of the security is .5% less than the 3-day volume weighted average
  # price AND we haven't reached our maximum short, then we call the or.9der
  # command and sell 100 shares.  Similarly, if the stock is .5% higher than
  # the 3-day average AND we haven't reached our maximum long, then we call
  # the order command and buy 100 shares.     
  if price < vwap * 0.995 and notional > context.min_notional:
    order(context.spy,-100)
  elif price > vwap * 1.005 and notional < context.max_notional:
    order(context.spy,+10000)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
4 responses

Hi Clestian,

I think you can do that with max(). You might need to use min() for short positions.

# initial set trailing stop for long position  
context.longstop = data[stock].price * (1 - .05) # 5% stop-loss price

# update: increase (never decrease) stop-loss  
context.longstop = max( context.longstop, data[stock].price * (1 - .05) ) # 5% target

# test stop-loss  
if data[stock].price < context.longstop:  
   # sell everything  

That didn't work for me... putting both context statements in my initial data (not sure where to put the update statement) seemed to cause an error with the def handle_data section of the script. However, I think I'm getting the desired result with the backtest below... slightly different buy/sell criteria... and the short stop is basically useless at this point;

next thing to do is implement shorting...

Clone Algorithm
129
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.max_notional = 1000000.1
    context.min_notional = -100
    set_commission(commission.PerTrade(cost=5.00))
    context.spy = sid(8554)
    context.long_stop_pct = .99
    context.short_stop_pct = 1.05
    

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.
    price = data[context.spy].price
    mavg100 = data[context.spy].mavg(100)
    mavg20 = data[context.spy].mavg(20)
    mavg5 = data[context.spy].mavg(5)
    
    notional = context.portfolio.positions[context.spy].amount * price
    qty = context.portfolio.positions[context.spy].amount
    cash = context.portfolio.cash
    
    # set (moving) long and short trailing stop prices.
    long_stop_price = data[sid(8554)].open_price * context.long_stop_pct
    short_stop_price = data[sid(8554)].high * context.short_stop_pct

    # take long position
    if mavg100 < mavg20 and mavg20 < mavg5 and notional < context.max_notional:
        order(context.spy,+(cash/price))
    
    # stop loss short position
    elif qty < 0 and price > short_stop_price:
        order(context.spy,+qty)
    
    # take short position (actually, just sell any long position...)
    elif mavg100 > mavg20 and mavg20 > mavg5 and notional < context.min_notional:
        order(context.spy,-qty)
    
    # stop loss long position
    elif qty > 0 and price < long_stop_price:
        order(context.spy,-qty)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

So, the initial goal was to follow the price/share and have a trailing stop loss follow the price... What I'm doing here is using the opening price multiplied by my trailing stop loss value (.99 long, 1.01 short) to establish a stop loss. If the price goes below (long) or above (short) the stop loss, the position sells. Trading SPY vs. the benchmark... not too bad a margin (70.5% vs 46.5%) for trading the same index against each other...

any suggestions for improving my buy/sell criteria?

I'm going to attempt allowing the screen to run on multiple companies and see what happens... I'll probably be posting it to another discussion.

Clone Algorithm
129
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.max_notional = 1000000.1
    context.min_notional = -100
    set_commission(commission.PerTrade(cost=5.00))
    context.spy = sid(8554)
    context.long_stop_pct = .99
    context.short_stop_pct = 1.01
    

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.
    price = data[context.spy].price
    mavg50 = data[context.spy].mavg(50)
    mavg20 = data[context.spy].mavg(20)
    mavg5 = data[context.spy].mavg(5)
    
    notional = context.portfolio.positions[context.spy].amount * price
    qty = context.portfolio.positions[context.spy].amount
    cash = context.portfolio.cash
    
    # set (moving) long and short trailing stop prices.
    long_stop_price = data[sid(8554)].open_price * context.long_stop_pct
    short_stop_price = data[sid(8554)].open_price * context.short_stop_pct

    # take long position
    if mavg50 < mavg20 and mavg20 < mavg5 and notional < context.max_notional:
        order(context.spy,+((cash)/price))
    
    # stop loss short position
    elif qty < 0 and price > short_stop_price:
        order(context.spy,+qty)
    
    # take short position
    elif mavg50 > mavg20 and mavg20 > mavg5 and notional < context.min_notional:
        order(context.spy,-((cash)/price))
    
    # stop loss long position
    elif qty > 0 and price < long_stop_price:
        order(context.spy,-qty)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

You could try a breakout signal. For example, if it's a 30-day high, use that as a buy/long signal, and if it's a 30-day low, use that as a sell/short signal. You can do this by storing the last 30 highs and lows and checking if the current price exceeds the high or falls below the low.

So this example doesn't work great, it's not representative of the strategy, just something you can play around with. I just slightly modified your example, you can see a few new lines added and some adjusted if statements.

Also, you could try using stop or limit orders instead of checking every day to see if your price goes out of certain bounds. Although, you will probably have to update the stop or limit orders, but just something to think about. See here: https://www.quantopian.com/help#ide-ordering

Clone Algorithm
153
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from collections import deque

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.max_notional = 1000000.1
    context.min_notional = -100
    set_commission(commission.PerTrade(cost=5.00))
    context.spy = sid(8554)
    context.long_stop_pct = .99
    context.short_stop_pct = 1.01
    
    # create deques containing past prices
    context.past_highs = deque([], maxlen=30)
    context.past_lows = deque([], maxlen=30)
    

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    
    # wait for warmup
    if len(context.past_highs) < 30:
        context.past_highs.append(data[context.spy].high)
        context.past_lows.append(data[context.spy].low)
        return
    
    # Implement your algorithm logic here.
    price = data[context.spy].price
    mavg50 = data[context.spy].mavg(50)
    mavg20 = data[context.spy].mavg(20)
    mavg5 = data[context.spy].mavg(5)
    
    # 30 day high?
    h_30 = True if data[context.spy].price > max(context.past_highs) else False
        
    # 30 day low?
    l_30 = True if data[context.spy].price < min(context.past_lows) else False
    
    notional = context.portfolio.positions[context.spy].amount * price
    qty = context.portfolio.positions[context.spy].amount
    cash = context.portfolio.cash
    
    # set (moving) long and short trailing stop prices.
    long_stop_price = data[sid(8554)].open_price * context.long_stop_pct
    short_stop_price = data[sid(8554)].open_price * context.short_stop_pct

    # take long position
    if h_30 and mavg50 < mavg5 and notional < context.max_notional:
        order(context.spy,+((cash)/price))
    
    # stop loss short position
    elif qty < 0 and price > short_stop_price:
        order(context.spy,+qty)
    
    # take short position
    elif l_30 and mavg50 > mavg5 and notional < context.min_notional:
        order(context.spy,-((cash)/price))
    
    # stop loss long position
    elif qty > 0 and price < long_stop_price:
        order(context.spy,-qty)
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.