tradingview / lightweight-charts

Performant financial charts built with HTML5 canvas
https://www.tradingview.com/lightweight-charts/
Apache License 2.0
9.16k stars 1.59k forks source link

Plot a trade #1449

Open oliverpolden opened 10 months ago

oliverpolden commented 10 months ago

Is your feature request related to a problem? Please describe.

No

Describe the solution you'd like

I would like to use lightweight charts to analyse trades performed by a backtest.

I see there is the ability to add Markers that can be used to indentify when trades are opened and closed but that does not provide enough information such as stop, target or exact entry price.

I have implemented this feature using Python's plotly as follows:

With this format I can easily see the following:

Additional context

Further features could be stop and target at entry, stop and target at exit. My stop is static but the target tracks the 100MA hence the target shown is that when the trade closes, not when it opens.

Analysis of the losing trade shows that if the stop was a bit further, then the trade could eventually have exited at a smaller loss. It's not shown on the screenshot but it does soon come back down to the 100MA.

Screenshot 2023-10-29 at 15 36 47

SlicedSilver commented 10 months ago

It appears to me that your main requirements are:

The library supports plotting multiple series on a single chart. This can be used to plot the candleStick series, and a line series for each of the moving average lines.

The annotations (drawings) on the chart for the rectangles and lines aren't possible via the normal API of the library but can be achieved through plugins. We actually already have plugins for drawing rectangles and trend lines. These could be combined to achieve the screenshot you've shared.

oliverpolden commented 10 months ago

Thank you for your response and suggestion of the plugins, being able to draw a rectangle and a simple line may allow me to achieve my goal but it's not an ideal solution.

I am specifically requesting a feature to plot historic trades.

Plotting a trade would be something I expect is important to many people, so being able to provide a list of trades in a similar way we can create an OHLC series and the API plotting them, would be an ideal solution.

const trades = [
  {
    open_time: ...,
    close_time: ...,
    open_price: ...,
    close_price: ...,
    stop_price: ...,
    target_price: ...,
  },
  { ... },
  { ... },
]

Perhaps a stop and target at open and close would be desirable as well. These could be plotted with broken or dashed lines: stop_price_initial: ..., target_price_initial: ..., stop_price_final: ..., target_price_final: ...,

SlicedSilver commented 10 months ago

I appreciate that this would be a useful feature, however there are hundreds of useful features which could also be implemented and one of the goals of this library is to remain 'lightweight'. So going forward we are being very selective about feature requests, and if something could be achieved via our Plugins system then in almost all cases we will suggest this approach instead. This allows us to keep the bundle size small, and the library focused on the core features.

This specific feature request is an ideal example of an enhancement which should be implemented as a Plugin. The plugin could accept the data in the form you've suggested and draw the required rectangles and lines on the chart.

If no else is interested in creating this plugin then I might do it myself when I get some free time.

oliverpolden commented 10 months ago

Thanks @SlicedSilver. I have other things I need to work on first otherwise I would have a stab at this now. Perhaps I can have a go in a couple of weeks or so. Are the names conventions appropriate?

SlicedSilver commented 10 months ago

Apologies, I forget to reply to this message.

The names I would suggest are:

interface Execution {
    price: number;
    time: Time;
}
interface Trade {
   open: Execution;
   close: Execution;
   stop: number;
   target: number;
}

This removes the need to use underscores. However, it is up to you if you develop the plugin to use whichever names you like the best.

difurious commented 10 months ago

I have a build that let's you create a box and other drawing tools. Check out here. The goal it to get it into a plugin. You can define where in time and price and modify the color and transparency of the box. Also add text and you can also manually modify it with your mouse after the fact. It can also make lines, just not indicator moving average. But I guess you can use the path tool to do something similar to a MA.