You can use Nicolas’code from lines 17 t0 56 that you can find at https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.
Just append them to your code changing the two values in trailingstart and trailingstep.
Make sure you are not using the same names for some variables in your code.
If you search this forum for TRAILING STOP you will find many more topics and lots of code snippets that deal with trailing stops.
1 user thanked author for this post.