Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot a trade #1449

Open
oliverpolden opened this issue Oct 29, 2023 · 6 comments
Open

Plot a trade #1449

oliverpolden opened this issue Oct 29, 2023 · 6 comments
Labels
help wanted Asking for outside help and/or contributions to this particular issue or PR. plugin This feature request should/could be implemented as a Plugin.

Comments

@oliverpolden
Copy link

oliverpolden commented Oct 29, 2023

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:

  • I have a green or red coloured transparent box whose bounds are (from top, clockwise to left): Target, trade exit time, stop, trade entry time. (Target and stop are swapped if it's a short)
  • The box colour represents whether the trade was a profit or a loss. Green for profit, red for loss.
  • The stop is red, the target is green.
  • The actual trade is plotted with a line that is coloured green or red depending on whether it was a buy or a sell. It is plotted exactly at the entry price and exit price.

With this format I can easily see the following:

  • Whether stops and targets are appropriate and calculated correctly.
  • Whether the entry price is correct.

Additional context

  • In this screenshot you can see two winning trades then one losing trade. (Two green boxes and one red one)
  • The trades are long, short, short: Green line, red line, red line.

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
Copy link
Contributor

It appears to me that your main requirements are:

  • Ability to plot moving average lines,
  • Draw coloured rectangles with the positions defined by price and time coordinates, and
  • Draw coloured lines with the positions defined by price and time.

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.

  • Rectangle drawing tool plugin example. Demo and code
  • Trend line plugin example. Demo and code

@SlicedSilver SlicedSilver added the question General question. label Oct 29, 2023
@oliverpolden
Copy link
Author

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
Copy link
Contributor

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.

@SlicedSilver SlicedSilver added help wanted Asking for outside help and/or contributions to this particular issue or PR. plugin This feature request should/could be implemented as a Plugin. and removed question General question. labels Oct 31, 2023
@oliverpolden
Copy link
Author

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
Copy link
Contributor

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Asking for outside help and/or contributions to this particular issue or PR. plugin This feature request should/could be implemented as a Plugin.
Projects
None yet
Development

No branches or pull requests

3 participants