This library can be used to track (hypothetical) trades on the chart. Enter the Open, SL, and TP prices (or TP in R to have it calculated) and then call Trade.TrackTrade(barIndex). Keep track of your trades in an array and then simply call TradeTracker.UpdateAllTrades(close) to update all trades based on the current close price.
How to use: 1. Import the library, as always. I'm assuming the alias of "Tracker" below. 2. The Type Trade is exported, so generate a Trade object like newTrade = Tracker.Trade.new(). 3. Set the values for Open, SL, and TP. TP can be set either by price or by R, which will calculate the R based on the Open->SL range: newTrade.priceOpen = 1.0 newTrade.priceSl = 0.5 newTrade.priceTp = 2.0 -- or in place of the third line above -- newTrade.rTp = 2 4. On each interval you want to update (whether that's per tick/close or on each bar), call trades.UpdateAllTrades(close). This snippet assumes you have an array named trades(var trades = array.new<Tracker.Trade>()).
In future updates, more customization options will be created. This is the initial prototype.
method CalculateProfits(t, _close) Calculates profits/losses for the Trade, given _close price Namespace types: Trade Parameters: t (Trade) _close (float)
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.