TradingView
jason5480
18 Th04 2021 14:06

Trailing Take Profit Trailing Stop Loss 

Bitcoin / TetherUSBinance

Mô tả

This script demonstrated a dual trailing take profit and stop loss strategy. Where you take 50% (configurable) of the profit when a price level is reached and then you get the rest when a trailing stop loss is hit.

Phát hành các Ghi chú

  • Logic for short positions added
  • Logic to exit when "trend" (based on the two sma) changes. This prevents overlapping positions and results in more clear demonstration.
  • Add option to enable/disable stop loss trailing
  • Add min max for inputs
  • Add plots for short take profit and stop loss

Phát hành các Ghi chú

  • Fix bug that caused Stop Loss to not set correctly when Take Profit was triggered in the very first candle
  • Give more descriptive names to strategy orders

Phát hành các Ghi chú

  • Fix quantity taken on "Short Take Profit" order to match the given quantity percentage given by the user

Phát hành các Ghi chú

  • Add the option to start Stop Loss trailing from the beginning of the entry order as @Soincoin proposed
  • Trailing Stop Loss is enabled when TP order is filled not when TP target is reached
  • Simplified Take Profit and Stop Loss Price code to be more readable
  • Simplified strategy orders
  • Add offset to the plots to have a more intuitive representation
  • Fixed a bug where TP and SL orders were not filled if they triggered in the very first candle
  • Don't draw the TP price if the TP order is filled

Phát hành các Ghi chú

  • Fixed a rare bug that caused some transactions to not exit 100% of the position size when Stop Loss and take Profit was hit at the same candle.
  • Fixed a bug that made the percentage quantity handled by the Stop loss order incorrect.
  • Changed the name of "Take Profit" odrers to "Take Profit / Stop Loss" to better reflect the fact that the order will exit if stop loss price is reached first.
  • Simplified strategy order code and added closeLongDeal and closeShortDeal variable to make code more readable.

Phát hành các Ghi chú

  • Add check to calculate TP and SL prices when it is within backtest period to avoid unnecessary calculations and painting outside the area of interest
  • Add check to the Take Profit Executed condition that compare the high/low price with the take profit price

Phát hành các Ghi chú

  • Replace deprecated transp argument with color.new to prevent warnings during compilation of the script

Phát hành các Ghi chú

  • Fix a rare bug that were causing the stop loss to be calculated wrong when there was a back to back entry with the previous one
  • Use strategy.percent_of_equitity
  • Draw lines with width 1
  • Add highlighter for positive and negative price moves

Phát hành các Ghi chú

  • Update to pinescript V5
  • Take profit steps to 0..05 and deviation steps to 0.01

Phát hành các Ghi chú

  • Minor transparency issue

Phát hành các Ghi chú

  • Transparency deprecation

Phát hành các Ghi chú

  • New Break Even feature added!


If Break Even check box is checked, then when the take profit price is hit, the stop loss will be moved to the entry price or "better" depending on the stop loss % and the trailing settings. The more strict value will be applied.

Phát hành các Ghi chú

  • Let's face it a Green-Red color model is more intuitive!

Phát hành các Ghi chú

minor optimization

Phát hành các Ghi chú

  • Thanks to the user @HBGJA for pointing out and solving a bug that caused longs after a short close alerts not to be executed.

Phát hành các Ghi chú

  • Happy New Year!
  • Most of the questions I had last year was about setting strategy alerts and connecting them with webhooks to your bots.
    So I added the ALERT MESSAGES section to show you how you can send different execution messages to your bots and execute your orders!
    If you are using the 3Commas bots all you have to do is to put your bot IDs and email token in the strategy settings. Add an alert, check the webhook, set the URL and write the [[strategy.order.alert_message]] in the message text box.
    If you are using any other bot system that can be used with webhooks, you can always modify the messages according to your Bots API
  • Many code refactorings and optimizations to better use this script as a template and plug your own strategy at ease

Phát hành các Ghi chú

  • Add bot tags

Phát hành các Ghi chú

  • Fix quotes around string message fields of 3Commas messages (message_type, email_tocken, action)

Phát hành các Ghi chú

  • Let the user put the whole alert message as an argument. Strategy will forward it to the {{strategy.order.alert_message}} place holder as it is

Phát hành các Ghi chú

  • Bug fix: (rare/minor) Remove 'strategy.position_size == 0' condition in stop loss calculation that made the very first stop loss price target to be calculated based on previous 'strategy.position_avg_price' instead of 'close' if the previous position closed exactly one bar before!

Phát hành các Ghi chú

NEW FEATURE! After many requests for an ATR based trailing approach I added:
  • A new take profit method that is ATR based. Now you can set the length and the multiplier for long/short in the settings menu.
  • A new deviation method that is also ATR based. Deviation is used for take profit trailing!
  • A new stop loss method that is ATR based. Now you can set the length and the multiplier for long/short in the settings menu.

Phát hành các Ghi chú

NEW FEATURE RELEASE (TOO RISKY :P)
  • Risk/Reward Ratio Take Profit type added! Now you can set price target based on RR and your stop loss
  • Risk management for entry quantity calculation is added! You can set what percentage you are willing to risk for every trade and the amount for the entry is calculated accordingly
  • Leverage variable is added that multiplies the initial amount of every trade
  • Set minimum trade price that your exchange support
  • Set max drawdown percentage to stop strategy

Phát hành các Ghi chú

  • Re-write the risk quantity formula to clamp the amount between minimum trade and the equity (multiplied by the leverage)
  • Add the option for different leverage factors for long and short trades

Phát hành các Ghi chú

This script development has gone far beyond my expectations and from a combination of trailing take profit and stop loss, had became a template for new strategies to develop. New interesting feature requests was implemented. However, I decided to create a separate script that will work as a template for new strategies and that can be found [here](tradingview.com/script/n8bgynWl-Template-Trailing-Strategy/) with even more features! The features that are irrelevant from the initial purpose of this script were removed to minimize the clutter. At this point I want to thank all those people in the community that helped this to become a robust implementation by reporting several bugs to me after an extensive testing and thoroughly checking edge cases in different market conditions!

Phát hành các Ghi chú

  • Use time_filters library

Phát hành các Ghi chú

  • Replace deprecated "when=" with if statements in strategy calls

Phát hành các Ghi chú

  • Change fastMA default color to yellow

Phát hành các Ghi chú

  • Increase max labels to 150
  • Increase max lines to 300
  • Minor refactoring

Phát hành các Ghi chú

  • Update time_filters library

Phát hành các Ghi chú

  • Some minor label/color changes
  • Minor refactoring of the code

Phát hành các Ghi chú

  • small refactoring

Phát hành các Ghi chú

  • Update time_filters library version

Phát hành các Ghi chú

  • Update time_filters library version

Phát hành các Ghi chú

  • Use the 'chrono_utils' library for the date-time window filter
Bình luận
biswad
Little contribution from my side for your hard work bro. Stay safe, keep it up.
jason5480
@biswad, Thank you very much I really appreciate that! The fact that this was helpful to the community gives me the courage to open-source the rest of my strategies. However, because those strategies use some third party indicators I have to wait for an approval of their authors to do it so. I believe this will not take long. Stay tuned!
biswad
@jason5480, Thank you very much. Looking forward :) Keep up the good work. You are doing an amazing job!!
freyadog
Amazing developments, thank you very much!
Joesp123
This is awesome, has solved a problem I have been stuck on for a week thanks Jason!
jason5480
Your welcome @Joesp123!
Axs321
yeah, good work. but repaint for trailing...
PRADEEPNAGINA
trailing take profit in pine script have a major bug at the time of filling
order. when my condition met for trailing take profit, alarm notified me but at the same time if price movement again goes in profitable direction filling order
position also changes. That's a big problem for my strategy. I can't understand how to solve this.
jason5480
@PRADEEPNAGINA, This behavior has to do with the way that the backtester handles the bar data. This form of repainting happens when the real-time data are discarded and the bar becomes a history-bar. For more information there is this article here tradingview.com/pine-script-docs/en/v5/concepts/Strategies.html The best you can do is to enable the "Bar Magnifier" feature before you forward test it further. Otherwise, disable the take profit trailing and measure the performance of your strategy without that.
FITS34429
@jason5480, @jason5480, thanks for your quickly reply but my problem is not for back testing historical result through TTP. The problem begins when you are trading in real time and TTP gone wrong.
For example
In BTCUSDT.P 4h my strategy give me long entry signal at the price of 41272 and my take profit is 1% and TTP is 0.5%. when price hits take profit % TTP start to capture the price movement and my TTP alert me exit signal at exact price but yet TV not filled my exit order. After 4h candle close and my strategy give me another long entry signal, the problem begins here because my long entry signal is removed because TV filled my TTP exit order at next candle at high - 0.5%😁.

so the problem is long entry is opened through alert and according to TV no entry is opened. I am starting my strategy as bot how can we start. Sir I have no paid account kindly give me ur mail for conversation. Thanks a lot

Thanks for giving me your valuable time
Thêm nữa