Which method do I need to implement for switching instruments

Custom indicators, trading strategies, data export and recording and more...
rickbarlow
Posts: 43
Joined: Mon Feb 08, 2021 5:37 pm
Been thanked: 1 time

Which method do I need to implement for switching instruments

Post by rickbarlow » Mon Mar 15, 2021 8:48 pm

I am plotting icons on the graph area, and I've found the methods to implement to support re-drawing the icons when I move left/right, or expand/contract the price ranges on the graph, and all works fine.
However, if I switch from one instrument tab to a 2nd one, and then go back to the first one, all of the icons previously drawn will disappear. New ones will appear as time passes, so the indicator is still working, but it seems I need to implement an additional method to trigger updates when I switch from one instrument tab to another and back again. Can you help?

I currently have implementations for the following:
onHeatmapTimeLeft
onHeatmapTimeWidth
onHeatmapFullPixelsWidth
onHeatmapFullPixelsHeight
onMoveEnd
onRightOfTimelineWidth
 

Svyatoslav
Site Admin
Posts: 278
Joined: Mon Jun 11, 2018 11:44 am
Has thanked: 2 times
Been thanked: 31 times

Re: Which method do I need to implement for switching instruments

Post by Svyatoslav » Tue Mar 16, 2021 4:00 pm

If I'm not mistaken, when tab is switched old ScreenSpacePainter gets disposed (you get a callback and must destroy relevant canvases) and then you will get a request to create a new one when user switches back. Do you handle disposing the old one/creating new one?

rickbarlow
Posts: 43
Joined: Mon Feb 08, 2021 5:37 pm
Been thanked: 1 time

Re: Which method do I need to implement for switching instruments

Post by rickbarlow » Tue Mar 16, 2021 4:49 pm

     Yes, I have the dispose() supported.

   @Override
        public void dispose() {
            heatmapCanvas.dispose();
            timelineCanvas.dispose();
        }

But I'm not sure what is required to creating new one and repopulating it, other than requesting the indicator to reload again. I seem to recall other indicators are able to repopulate without reloading the indicator. Am I supposed to add logic to cache all icons, lines, etc. and add them back, or is there another method to preserve and repopulate?
thanks
 

Svyatoslav
Site Admin
Posts: 278
Joined: Mon Jun 11, 2018 11:44 am
Has thanked: 2 times
Been thanked: 31 times

Re: Which method do I need to implement for switching instruments

Post by Svyatoslav » Tue Mar 16, 2021 7:14 pm

> I seem to recall other indicators are able to repopulate without reloading the indicator.
I don't think this relates to core API. Simplified API - yes, but it sacrifices efficiency for simplicity.

When tab is opened (or reopened) you get a call to ScreenSpacePainterFactory#createScreenSpacePainter. This is a moment when you should create a fresh ScreenSpacePainter instance. At any moment bookmap is allowed to ask you to dispose the ScreenSpacePainter or to create another one, but normally you'll only be asked to create it when tab is opened and dispose it when tab is closed.

Bookmap does not retain the state between ScreenSpacePainters - it assumes that those are independent objects. So you are right, any icons that you have created in old SSP should be created in new one again, as bookmap does not make any assumptions about SSPs being logically connected and expects your code to repopulate SSP on demand.

That's partially done to discourage developers from maintaining icons for invisible SSPs and/or invisible areas - that involves unnecessary extra CPU and memory usage, as in most cases users are only watching a relatively small portion of data (whatever fits in the window) for a few instruments out of what they are subscribed to (yeah, I know some watch every instrument they subscribe to at once, but many watch, say, 3 out of 12 that they are subscribed to).

rickbarlow
Posts: 43
Joined: Mon Feb 08, 2021 5:37 pm
Been thanked: 1 time

Re: Which method do I need to implement for switching instruments

Post by rickbarlow » Tue Mar 16, 2021 7:22 pm

ok, thank you. Very detailed response, very helpful!

Post Reply