TradingView
Bjorgum
14 paź 2021 15:20

Bjorgum AutoTrail 

Bitcoin / TetherUS PERPETUAL CONTRACTBinance

Opis

One Time Trade Risk Management

Incorporating the new interactive feature, this script is meant as a one time trailing stop for the active trader to manage positional risk of an ongoing trade. As a crypto trader or Fx trader, many may find themselves in a position late into the evening, or perhaps daily life is calling while a trade progresses in their favor. Adding a trailing stop to a position thats trending can help to keep you in the trade and lock in gains if things turn around when you are unable to react.

To use the trail, the user would add the script to the chart. Once added, a set of crosshairs will appear allowing the user to choose a point to begin. Often choosing to start a trail from a swing high/ low can be an ideal option. This tends to provide some protection for a stop by placing it under support for a long trade or above resistance for a short trade.

Price based trail



The trail will automatically plot and the offset is a factor of the distance from price action selected by the crosshairs. If placed above price action the script will plot a short trail, if placed below it will trail for a long position.

Additionally, there are several other trail types other than price based. There is also percent based, which offsets the trail as a percent from close. A hard stop is placed at the cross hair value, then once the distance is exceeded by the percentage specified, the trail begins.

There are 2 more volatility based trails. There is a PSAR trail which can provide quicker and tighter stops that accelerate with the trend locking in gains faster, and an ATR trail that keeps a distance from price action as a function of volatility. Volatility levels can be adjusted from the menu.

Volatility based trail (ATR)



Volatility based trail (PSAR)



Lastly, within the code for more the more technical savvy, is some starting setups for string alerts to be sent to exchanges via 3rd party or custom API applications. Some string manipulation is required for specific providers to meet their requirements, but there is some building block alerts that will take the ticker symbol, recognize the asset your trading (Fx, Crypto, etc) and take input quantity or exchange names from the settings via inputs.

Complex strings can be built to perform almost any trade related task when to comes to alerts via web hook. A little setup this way with some technology to back your system can mean a semi-automated half man, half machine setup that actually manages your trail stop while you cannot. For those that don’t go this far, there is some basic alert functionality that well trigger when a trail is hit so you can react and make a decision.

Please note that for now, interactive mode is engaged only when the script is added to the chart. Additional stops, or for adjustments to be made it is best to add a new version. Also as real trades could be at play managing an actual position, alerts are designed to go off only once to ensure no duplicate orders are sent meaning alerts are not reoccurring. Once an alert is triggered, a new trail is to be set up.

A modified version of the TradingView built in SAR equation was used in this script. To provide the value of the SAR on the stop candle, it was necessary to alter the equation to extract this value as the regular SAR “flips” at this point. Thank you to TradingView for supplying access to the built in formula so that this SAR could behave the same as the built-in function outside of these alterations

Example of SAR value maintained in trigger candle



Cheers and happy trading.

Informacje o Wersji

Added variable source input to allow open and close to be a choice in addition to the original high/low default.

Informacje o Wersji

Added time check to make Runtime error more robust. Runtime error will occur if stop is placed between the high and the low of the initialization candle as this triggers a stopped condition within the code.

Informacje o Wersji

Added color differentiation between short and long bias - adjustable from settings menu

Informacje o Wersji

Implemented a new level check which changes the starting point for ATR and Percent type trailing stops. Previously, if the price point for these two trail types was ABOVE the calculated ATR or percent trail start point, the start point would drop down to the calculated point causing an increase in risk. Now the script will use the point selected with the mouse to start the stop loss, and the script will begin trailing at such a time that the start point is exceeded by the calculated trailing value. If the calculated value is greater than where the cursor is placed, the start point will be risk adjusted upwards to the calculated value.

Komentarze
PineCoders
cajole
@PineCoders, "Please note that for now, interactive mode is engaged only when the script is added to the chart. "

^ That is a big limitation when switching between tickers. Is there any planned solution to this? I wish I could access user-drawn shapes, for example, since those persist between tickers.
carnagecain
Great job, really appreciate you making this! This script spurred an idea. It would potentially be useful to use the time/price inputs to act as an entry and then scale a stop and 2 targets for example, to allow the user to backtest different strategies without having to code them all. Eventually even statistic trade results could be added as well to potentially speed up manual backtesting. Again awesome script and thanks!
Bjorgum
@carnagecain, thank you for generosity and suggestions! I agree this would be great additions, the only thing holding me back is the functionality of the interactive feature. With the inability to Re-engage interactive mode without adding the script new to the chart, it makes a complex script difficult as users wouldn’t be able to set the different variations. I believe this will be improved as the feature is new, and feedback amongst users seems to support the idea. The ability to go back in and set limits and different variations once the script is on-chart would make these ideas much easier to support. I’ll get working on some of this in the mean time! Cheers and all the best to you
carnagecain
@Bjorgum, I think In order to do it you would have to force the user to chose whether it’s long or short, so if they are positioning the time/price on the wrong side of price it wouldn’t make much sense without defining that. Also the scaled stop/target levels would have to be input defined so the only thing you would interact with would be the single price/time dot like you have now in the trailing stop. And that dot can be interacted with/moved after the initial loading of the script just by drag/drop, same as you have now. Hopefully the clarification makes sense. Thanks for the reply!
niftybank2060
Unable to apply this script to charts. Is anyone facing the same issue?
Bjorgum
@niftybank2060, Hello friend, I have updated the script. Please check when you have a chance as that should have sorted out the issue. Take care
uvjoshi
@Bjorgum, The message is popping up - "Set the 1 time and price"...!!! what to do sir?
uvjoshi
@Bjorgum, want you share you the error message. But Tradingview not allowing to post the pics, Please send me your email, thanks
Więcej