# Code for trailing stop

**URL:** <https://forum.alpaca.markets/t/code-for-trailing-stop/487>\
**Category:** Alpaca Account Troubleshooting\
**Created:** [November 25, 2019, 4:27am UTC](https://forum.alpaca.markets/t/code-for-trailing-stop/487 "2019-11-25T04:27:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimboski93](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jimboski93](https://forum.alpaca.markets/u/jimboski93)\
**Post date:** [November 25, 2019, 4:27am UTC](https://forum.alpaca.markets/t/code-for-trailing-stop/487/1 "2019-11-25T04:27:10Z")

</div>

I saw in the documentation that trailing stop orders are to come.

Any help with code (python) that could monitor 1 minute price moves and create a market sell order when the ‘trailing stop’ has been reached?

For example:  
Buy SPY @ $300  
Currently, I could open a limit sell at $270 (down 10%).  
If SPY runs to $350, I’d like to have code that is checking every minute if SPY falls below $315 (down 10% from $350). Once the current price hits $315, I’d automatically enter a market sell order.

I’m learning python, so my rough logic would be:

def high\_price()  
executed\_order\_price = [get from orders]  
current\_price = [get from market]  
if(current\_price \> executed\_order\_price):  
return current\_price

def price\_trigger  
if current\_price \< (high\_price \* 0.9)  
api.submit\_order(‘SPY’,qty=100,side=‘sell’,type=‘market’)

Thanks,  
–Jim

---

<div class="post-metadata">

**Author:** ![jimboski93](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jimboski93](https://forum.alpaca.markets/u/jimboski93)\
**Post date:** [November 27, 2019, 4:16am UTC](https://forum.alpaca.markets/t/code-for-trailing-stop/487/2 "2019-11-27T04:16:50Z")

</div>

Ok, here’s how I’m implementing. I think this essentially gets me there:

current\_position\_price = api.polygon.last\_trade(ticker).price

barset = api.get\_barset(ticker,‘minute’,)  
ticker\_bars = barset[ticker]  
price\_1m\_ago = ticker\_bars[-2].c

if current\_position\_price \< price\_1m\_ago:  
Sell

My algo runs ~ every minute, so it will update with the last trade and compare to the last completed minute. If a stock is rising, the current\_position\_price will be greater than the previous minute. At the next refresh, the price\_1m\_ago will then be higher, and ‘trail’ the current price on its way up.

---

<div class="post-metadata">

**Author:** ![yoshi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/yoshi/32/12_2.png) [@yoshi](https://forum.alpaca.markets/u/yoshi)\
**Post date:** [August 19, 2020, 5:33am UTC](https://forum.alpaca.markets/t/code-for-trailing-stop/487/3 "2020-08-19T05:33:34Z")

</div>

**Trailing Stop - Beta Program**

Finally, we are ready for a Beta program for the Trailing Stop. Please see the draft document [here](https://bit.ly/3j1YRoL).

As of today, we plan to spend a couple of weeks doing a beta test for the Trailing Stop until the full launch. We are looking for 20-30 beta users for Trailing Stop.

If you are interested in this, please fill out your information in [this google form](https://forms.gle/7k4n8xLUHqu7zpnHA) so that we can invite you to the private Slack channel.

[https://forms.gle/7k4n8xLUHqu7zpnHA](https://forms.gle/7k4n8xLUHqu7zpnHA)
