How to do it...

  1. To get started, we will work with Tesla Motors (TSLA) stock and presume a purchase on the first trading day of 2017:
>>> import pandas_datareader as pdr
>>> tsla = pdr.DataReader('tsla', data_source='google',
start='2017-1-1')
>>> tsla.head(8)
  1. For simplicity, we will work with the closing price of each trading day:
>>> tsla_close = tsla['Close']
  1. Use the cummax method to track the highest closing price until the current date:
>>> tsla_cummax = tsla_close.cummax()
>>> tsla_cummax.head(8)
Date 2017-01-03 216.99 2017-01-04 226.99 2017-01-05 226.99 2017-01-06 229.01 2017-01-09 231.28 2017-01-10 231.28 2017-01-11 231.28 2017-01-12 231.28 Name: Close, dtype: float64
  1. To limit the downside to 10%, we multiply tsla_cummax by 0.9. This creates the trailing stop order:

>>> tsla_trailing_stop = tsla_cummax * .9
>>> tsla_trailing_stop.head(8)
Date 2017-01-03 195.291 2017-01-04 204.291 2017-01-05 204.291 2017-01-06 206.109 2017-01-09 208.152 2017-01-10 208.152 2017-01-11 208.152 2017-01-12 208.152 Name: Close, dtype: float64
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.196.244