Login Page - Create Account

Support Board

Date/Time: Tue, 23 Jan 2018 22:03:13 +0000

Post From: Offering To The Community: Brett's Multi-Function Trailing Stop

[2015-10-14 20:28:26]
bjohnson777 (Brett Johnson) - Posts: 204
I've been wanting a MA based trailing stop including trend line and time based exits for a very long time. But first...

A short public service announcement: With the Dow 1000 point crash at the end of August, I decided it was time to take profits on the long term investments. My stuff was easy, but with my elderly mother suffering from Parkinson's, it was very difficult. My health problems didn't make it any easier. Her CFP decided to invest more for himself than her (discovered this was typical of LPL). The hardest to get information were on the variable annuities (which I tend to strongly dislike). They were high cost (3.1% on one) and poor performing. They were also "poison pill" type investment strategies with absurd contracts and penalties (the few contracts I could find). In short, I've spent (wasted) the past 1.5 months sorting and digging through hundreds of pages of statements, getting power of attorney, and hiring a lawyer to clean up an investment mess that was supposed to be simple by mom's instructions. FINRA won't get involved because there weren't any real losses... better not be over 12 years... but the "investments" grossly underperformed the S&P 500 "SPY" ETF and a SMA200 crossover trading strategy for comparison. Moral of the story: now is a very good time to check long term investments for you and your family members.

Now back to the regularly scheduled post...

I've tested this program with my trading style and have it debugged. I normally do longer day trades to swing trades. It may or may not work well with other trading styles. If you run into a problem, I will need detailed steps to reproduce the issue on my end. Please take good notes.

Since I'm not done programming all of the tools I want yet, I do not have a live trading account to test it against. In theory, real time simulation trades should be almost equivalent to live trades, but there might be a quirk or two that needs ironing out. Please take good notes again. I plan to have a live trading account in a month or two... barring any more disasters.

From the help file:

Trading: Brett Johnson's Multi-Function Trailing Stop. Copyright 2016 under GNU GPL v1. If you use this code in your own program, share it like I have. This program provides a trailing stop based on a moving average. This helps prevent premature exits caused by 'bar whiskers' and some stop running. There is also:

A traditional trailing stop further away for safety.
An exit trend line useful for catching channel break outs.
A price target feature.
An exit based on the time of day (such as market closing).
An exit based on the end of the trading week (avoiding weekend gaps).

Note For Lawyers: The goal of this trading program is to keep losses small and let winners run. While this code has been written as solid as possible with many extra safeties in the code, every possible scenario cannot be accounted for. It will also not protect from bad trades 100% of the time. It will not make a bad trading plan into a good one. It is not a magic black box with unlimited profits. There will be money lost. Hopefully losses will be limited and the gains will be far more. There is no warranty expressed nor implied. This trading program is open sourced. If you have questions about programming competency or how it works, source code can be easily checked. There is also extensive documentation in the 'Display Study Documentation' button on how things work and the quirks of the program.

By using this program, you first agree to test it out in simulation mode. While the conceptual operation is quite simple, some of the actions will be unexpected. I wrote this program and some of the actions caught me off guard. When there is real money on the line, always test first.

Functionality and Observations

Sierra Chart must be up and running and connected to the trade service to manage trades. A connection loss to the trade server will mean that the watched orders will go unmanaged and may go horribly wrong. Always use the Safety Trailing Stop option as it will leave a protection Stop at the trade server that does not depend on Sierra Chart connectivity. Note that since the Safety Trailing Stop is on the trade server, it will execute much faster than Sierra Chart triggering an exit order. The Safety Trailing Stop is a traditional Trailing Stop managed by Sierra Chart (not this program) and will be triggered by any price action hitting it. Sierra Chart manages its Trailing Stop by periodically moving a fixed Stop on the trade server.

Make sure there is at least enough data for the 'Price MA Smoothing Period' before trading.

This program is for managing a single order after it has been opened. Multiple trades going in will be treated as an overall single trade. This may not have the desired effect you were expecting.

On the chart, up arrows are used for long positions and down arrows are used for short positions. A green arrow shows an order entry. A red arrow shows an order exit.

The Message Log is used to record entry, exit, and notable changes. If there is an error, the Message Log will pop up with a description of the problem.

The Settings window has a bunch of Flag values at the top. The Flags are set from Sierra Chart's current internal settings. Changing them in the Settings window will do nothing. The point of the Flags is to report Sierra Chart settings in one convenient location and show if trade management will be enabled or not. Set 'MA Trailing Stop Offset' to 0.0 (disabled) to check Flag values before trading. Some values may not change until the Settings window is closed and then reopened.

This program will wait in standby mode for an order to appear. After that, it will start managing it. Check the Flags in the Settings window to make sure detected trade values are correct. When the order closes for any reason, this program will go back into standby mode waiting for the next order.

The MA Trailing Stop is based on a distance from the Price MA line instead of price bars. If the bar price crosses the Price MA line, nothing will happen. This is to help keep noise from causing a premature exit. Note that the Price MA's smoothing period should not be set too high or it will cause significant lag during a high volume jump that will exit the trade late. At the same time, a less noisy price line will allow for tighter stops. If the Price MA line crosses the MA Trailing Stop line, then the MA Trailing Stop will be triggered. The same will happen if the Price MA line crosses the Exit Trend Line. All orders will be flattened and pending orders will be canceled. This leaves a clean chart for the next trade.

The MA Trailing Stop Leading Edge line is used to show where the Price MA line would have to cross to move the MA Trailing Stop line. If the Price MA line is bouncing between the two lines, then they won't move.

If there is an event that causes a graph reset, a redraw will be issued. The redrawn trailing stop lines will be close but not be exactly as they were before due to all the live data not being saved. The Safety Trailing Stop will jump a little at the end of the redraw because of rounding to the nearest tick and the variables realigning over the next few bars. It also is not possible to reset a Safety Trailing Stop with the same price distance information as before, so it might step backwards.

Warning: Changing the chart time period (like from 1m to 5m) will redraw the Price MA line using different bar data and could trigger an immediate stop out. If you need to see multiple time frames, duplicate your chart window before this program starts and leave this program running in it undisturbed.

Warning: if there are multiple open orders not generated by this program, behavior will be undefined. Early partial exit orders could screw up the Price Target feature. A partial Stop order could screw up the Safety Trailing Stop feature. In this case it would very likely create unwanted positions in an unwanted direction. Use the 'Cancel All Other Orders On Entry' feature to stop this from happening. Note again that this program was designed to manage a single order at a time. Adding additional pending orders after order management has started may cause undefined behavior.

The Safety Trailing Stop is constantly moving and much more complicated to keep track of. This program cannot reliably track it. If you move it on the chart, it will be moved back during a reset. This may not be a desired behavior. If you need to change the Safety Trailing Stop, do it in the Settings window.

The Exit Trend Line position will be saved with the chart. This is the same as if saving regular drawing lines with the chart. If you're not in the habit of saving the chart, turn on autosave: Global Settings >> General Settings >> General2 >> Autosave Chartbooks Time In Minutes. If the saved trade data (symbol, date stamp, volume, trade direction (long/short)) doesn't match the current trade on program re-entry, the Exit Trend Line will reset itself flat at your trade start point. If it does match, the Exit Trend Line will resume its previous function. If it does match and the Price MA line has already crossed the Exit Trend Line on program reload, that will trigger an immediate order exit. Make sure the Exit Trend Line drawing points are not in the empty Fill Space or it may jump to a new position on program re-entry. This could cause a premature or late exit.

This program has a safety order limiter in place. If there are 3 new position entries and exits within 15 seconds, this program will disable itself. This helps prevent massive losses in commissions from multiple automated bad position entries that might never stop until someone manually pulls the plug. Be warned that the entered positions will no longer be managed. The automated entry program should also have a safety similar to this. Some might call this an Order Exit Circuit Breaker to prevent costly race conditions.

Replay Notes. The time exit features will not work in any Back Test or Replay Mode. This program uses sc.CurrentSystemDateTime to set the time exit variables. The sc.CurrentSystemDateTime variable always returns current time and not Replay time. There is no clean way to get around this limitation. When you are finished with Replay testing, double check the Settings window to make sure this program is set up for your normal operations. It is recommended to run Replays and Back Tests in a separate chart window to avoid conflicts with your normal trade settings.

The rest of the program functionality is described in the Input Descriptions blocks.

Minor update 2017-12-13. Minor update to compile with the new SC version. No function change.
Regular compiles moved to "Brett Johnson's Standard Tool Kit" DLL.
Date Time Of Last Edit: 2017-12-13 06:30:39
Attachment Deleted.
Attachment Deleted.
attachmentTrading_BrettJohnsonsMultiFunctionTrailingStop.cpp - Attached On 2017-12-13 06:30:17 UTC - Size: 124.57 KB - 7 views
attachmentTrading_BrettJohnsonsMultiFunctionTrailingStop.dll - Attached On 2017-12-13 06:30:25 UTC - Size: 886.5 KB - 8 views