Advertisement
cjc5013

Trailing Stop Example - Longs and Shorts

Oct 4th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import talib
  2. from math import sqrt
  3. import numpy as np
  4.  
  5. def initialize(context):      
  6.    
  7.     schedule_function(trade_long, date_rules.every_day(), time_rules.market_close(minutes=5))
  8.     #schedule_function(trade_short, date_rules.every_day(), time_rules.market_close(minutes=5))
  9.    
  10.     context.spy = sid(8554)
  11.     context.long_stop_price = 0
  12.     context.short_stop_price = np.inf
  13.        
  14. def trade_long(context, data):    
  15.    
  16.     #hist and transforms and rolling vol dataframe
  17.     prices = data.history(context.spy, 'price', 20, '1d')
  18.     current_price = data.current(context.spy, 'price')
  19.     basis = context.portfolio.positions[context.spy].cost_basis
  20.  
  21.     #This is just an example breakout logic to get us in a position
  22.     #This entry logic can be anything (breakout, MA cross, RSI pullback, etc)
  23.     if current_price > prices[:-1].max() and context.portfolio.positions[context.spy].amount == 0:
  24.         order_target_percent(context.spy, 0.50)
  25.         print('buy', current_price, prices[:-1].max())
  26.    
  27.    
  28.            
  29.     #If long, exit when price hits trailing stop
  30.     if context.portfolio.positions[context.spy].amount > 0:
  31.         context.long_stop_price = max(context.long_stop_price, (current_price * 0.95))
  32.         print('long_stop_price',context.long_stop_price)
  33.         if current_price < context.long_stop_price:
  34.             order_target_percent(context.spy , 0.0)
  35.             print('Trailing Stop Hit' , current_price, context.long_stop_price,)
  36.             context.long_stop_price = 0
  37.    
  38.    
  39. def trade_short(context, data):
  40.    
  41.     prices = data.history(context.spy, 'price', 20, '1d')
  42.     current_price = data.current(context.spy, 'price')
  43.     basis = context.portfolio.positions[context.spy].cost_basis
  44.    
  45.     if current_price < prices[:-1].min() and context.portfolio.positions[context.spy].amount == 0:
  46.         order_target_percent(context.spy, -0.5)
  47.         print('sell', current_price, prices[:-1].min())
  48.        
  49.        
  50.     #If Short, exit when price hits trailing stop
  51.     if context.portfolio.positions[context.spy].amount < 0:
  52.         context.short_stop_price = min(context.short_stop_price, (current_price * 1.05))
  53.         print('short_stop_price',context.short_stop_price)
  54.         if current_price > context.short_stop_price:
  55.             order_target_percent(context.spy , 0.0)
  56.             print('Trailing Stop Hit' , current_price,  context.short_stop_price)
  57.             context.short_stop_price = np.inf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement