Skip to content

Auto-scale Y-axis for indicators graph when zooming #356

@zlpatel

Description

@zlpatel
Contributor

Original Discussion Post: #355

There's something that I noticed which makes it difficult to view indicator graph on the chart if you're backtesting for longer period. Below is an example of an indicator view on the chart without zooming in. Notice the y axis scales.
Screenshot 2021-05-21 at 4 03 16 PM

Now, when I zoom in to focus on a specific period when ttm squeeze value stayed between -5 to +5, it still shows the full scale from -15 to+20. which makes it really hard to visualize the indicator chart. See below.
Screenshot 2021-05-21 at 4 09 25 PM

I know that re-scaling is allowed for the price chart/Volume. Is that something that can be implemented on the indicator charts as well?

Activity

zlpatel

zlpatel commented on May 22, 2021

@zlpatel
ContributorAuthor

I fiddled around with the code and made it work :)

Here's what I did:

I have added code

indicator_max=value.df.max(axis='columns')
indicator_min=value.df.min(axis='columns')
source.add(indicator_max, f'indicator_{i}_range_max')
source.add(indicator_min,f'indicator_{i}_range_min')

right above

for j, arr in enumerate(value, 1):

then,

indicator_ranges = {}
for idx,indicator in enumerate(indicator_figs):
    indicator_range_key = f'indicator_{idx}_range'
    indicator_ranges.update({indicator_range_key:indicator.y_range})
custom_js_args.update({'indicator_ranges':indicator_ranges})

right below these lines

if plot_volume:
custom_js_args.update(volume_range=fig_volume.y_range)

then,

if(indicator_ranges){
    let keys = Object.keys(indicator_ranges);
    for(var count=0;count<keys.length;count++){
        if(keys[count]){
            max = Math.max.apply(null, source.data[keys[count]+'_max'].slice(i, j));
            min = Math.min.apply(null, source.data[keys[count]+'_min'].slice(i, j));
            if(min && max){
                _bt_scale_range(indicator_ranges[keys[count]], min, max, true);
            }    
        }
    }
}

right below these lines

if (volume_range) {
max = Math.max.apply(null, source.data['Volume'].slice(i, j));
_bt_scale_range(volume_range, 0, max * 1.03, false);
}

Here's how it looks on the graph:

Without Zooming in:
Screenshot 2021-05-22 at 12 13 14 PM

When Zoomed in:
Screenshot 2021-05-22 at 12 13 36 PM

changed the title [-]Allow Auto-scaling of Y axis values for Indicator(when overlay=False) when you zoom in or out the chart.[/-] [+]Auto-scale Y-axis for indicators when zooming[/+] on May 23, 2021
changed the title [-]Auto-scale Y-axis for indicators when zooming[/-] [+]Auto-scale Y-axis for indicators and Profit/Loss graph when zooming[/+] on Jun 13, 2021

2 remaining items

changed the title [-]Auto-scale Y-axis for indicators and profit/loss graph when zooming[/-] [+]Auto-scale Y-axis for indicators graph when zooming[/+] on Jun 13, 2021
zlpatel

zlpatel commented on Jun 13, 2021

@zlpatel
ContributorAuthor

@kernc can you review my fix for this?

lkfrota

lkfrota commented on Dec 2, 2021

@lkfrota

Thank you for the great work. That is something that I was looking for and made the job.
However, did you notice that it works only if the volume is plotted along (i.e. plot_volume=True)?
...or did I miss something (I'm not an experienced user).

zlpatel

zlpatel commented on Dec 13, 2021

@zlpatel
ContributorAuthor

Thank you for the great work. That is something that I was looking for and made the job. However, did you notice that it works only if the volume is plotted along (i.e. plot_volume=True)? ...or did I miss something (I'm not an experienced user).

Thanks for pointing it out. I will need to figure out the dependency on volume chart and separate it from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @kernc@zlpatel@lkfrota

      Issue actions

        Auto-scale Y-axis for indicators graph when zooming · Issue #356 · kernc/backtesting.py