Changing chart's interval using chart parameters' slider

Hello,

Recently my friend, who is a candlesticks' enthusiast (and likes to test and look for different patterns in several less popular intervals), asked me if it would be possible to change chart's interval using chart parameter's slider. I knew it is doable and wrote a simple code allowing to do that. Because I thought that it might be also useful for other users, here it is:

_SECTION_BEGIN( "Custom Chart Interval v 1.0" );
// By Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/changing-charts-interval-using-chart-parameters-slider/8795/1

MyIntervalT = Param( "Custom interval in Ticks", 0, 0, 500, 1 );
MyIntervalS = Param( "Custom interval in Seconds", 0, 0, 60, 1 );
MyIntervalM = Param( "Custom interval in Minutes", 0, 0, 60, 1 );
MyIntervalH = Param( "Custom interval in Hours", 0, 0, 24, 1 );
MyIntervalD = Param( "Custom interval in Days", 1, 0, 251, 1 );
MyIntervalR = Param( "Custom Range Bars", 0, 0, 100, 1 );
MyIntervalV = Param( "Custom Volume Bars", 0, 0, 50000, 50 );

if( MyIntervalT != 0 ) MyInterval  = MyIntervalT * -1 ;

else
    if( MyIntervalV != 0 ) MyInterval = -2000000 - MyIntervalV ;

    else
        if( MyIntervalR != 0 ) MyInterval  = -1000000 - MyIntervalR ;

        else MyInterval = MyIntervalD * inDaily + MyIntervalH * inHourly + MyIntervalM * in1Minute + MyIntervalS * in1Second;

if( Nz( StaticVarGet( "LastInterval" ) ) != MyInterval AND MyInterval != 0 )

{

    AB = CreateObject( "Broker.Application" );
    doc = AB.ActiveDocument;
    doc.Interval = MyInterval;
    StaticVarSet( "LastInterval", MyInterval );

}

Custom%20Interval%209

Above solution might be useful for the users sharing similar approach to my friend, wanting to quickly and easily test their indicators in several, custom intervals. Of course I'm not saying, that entering 3m or 3h or 300V in the interval Box and hitting Enter or clicking an appropriate shortcut is a big effort - of course it's not - it's very handy, but if someone wants to quickly browse through several (sometimes only slightly different intervals), using a slider for that might be an interesting alternative. It might also help to realize that a very strict or rigorous approach to patterns is not always the best idea, because if some setup or pattern looks very bullish in 5 minute timeframe, it might look a lot different when the TF is switched to e.g. 4 or 6 minutes.

In this version it allows to change chart's interval to n ticks, seconds, minutes, hours, days, range bars or volume bars. Weekly, monthly or yearly interval can also be easily added. It is worth noting, that if someone wants to change interval to n Ticks or to range bars (for the best results), database should use Tick as Base Time Interval.

Because it uses OLE instead of TimeFrameMode() and TimeFrameSet(), it should be used with caution in case of AFLs making use of those two functions.

One of the advantages of this approach - when comparing to using TimeFrameMode() and TimeFrameSet() is the fact, that it changes the interval of the whole chart window with all it's panes and different indicators in one go.

Another advantage is the fact, that using this code, someone can still change the chart's interval manually in any other way - it won't interfere.

To display range charts, first you need to specify the TickSize in the Symbol–>Information window:
http://www.amibroker.com/kb/2014/09/19/how-to-display-range-bars/


I used some ideas from Tomasz's code:

Additional reading:

https://www.amibroker.com/guide/afl/timeframemode.html
https://www.amibroker.com/guide/afl/timeframeset.html

14 Likes

Just a small correction. There's one line missing in the above code :

_SECTION_END();

So here's the whole code again:

_SECTION_BEGIN( "Custom Chart Interval v 1.0" );
// By Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/changing-charts-interval-using-chart-parameters-slider/8795/1

MyIntervalT = Param( "Custom interval in Ticks", 0, 0, 500, 1 );
MyIntervalS = Param( "Custom interval in Seconds", 0, 0, 60, 1 );
MyIntervalM = Param( "Custom interval in Minutes", 0, 0, 60, 1 );
MyIntervalH = Param( "Custom interval in Hours", 0, 0, 24, 1 );
MyIntervalD = Param( "Custom interval in Days", 1, 0, 251, 1 );
MyIntervalR = Param( "Custom Range Bars", 0, 0, 100, 1 );
MyIntervalV = Param( "Custom Volume Bars", 0, 0, 50000, 50 );

if( MyIntervalT != 0 ) MyInterval  = MyIntervalT * -1 ;

else
    if( MyIntervalV != 0 ) MyInterval = -2000000 - MyIntervalV ;

    else
        if( MyIntervalR != 0 ) MyInterval  = -1000000 - MyIntervalR ;

        else MyInterval = MyIntervalD * inDaily + MyIntervalH * inHourly + MyIntervalM * in1Minute + MyIntervalS * in1Second;

if( Nz( StaticVarGet( "LastInterval" ) ) != MyInterval AND MyInterval != 0 )

{

    AB = CreateObject( "Broker.Application" );
    doc = AB.ActiveDocument;
    doc.Interval = MyInterval;
    StaticVarSet( "LastInterval", MyInterval );

}

_SECTION_END();

Enjoy :slight_smile:

12 Likes

@Milosz
After pondering , should I start a new post or continue with your post, decided that it may be better if I continue with your post as my question is directly correlated with the wonderful code you have posted.

I have been using an ATR based volatility ribbon plotted below stochastic and then manually (param) change the values of "periods, Ksmooth, Dsmooth" for the stochastic indicator as the volatility changes.

After plotting your code I have tried to write a code to automatically change the values but still being relatively new have had no success.
First I will post the code with my comments

t
// using ATR as a measure of volatility - hourly chart has 7 bars in a day,  14 bars = 2days and 35 bars = 1 week
X = (ATR(14)-LLV(ATR(14),35))*100/(HHV(ATR(14),35)-LLV(ATR(14),35)); //% position of current ATR(14) over last 35 bars
zonecolor = IIf(x > 70,colorRed, IIf( x > 40 AND x < 70, coloryellow, colorLime)); // below 40, color lime  = low volatility
Plot(3,"  Zone",Zonecolor,styleOwnScale|styleArea|styleNoLabel|styleNoTitle,0, 100 );

///// Is there a way to automatically change the values of "periods, Ksmooth, Dsmooth" as the volatility / time interval changes
//normally the volatility changes with the chart  interval  - some AFL code will be required
_SECTION_BEGIN("Stochastic %D");
periods = Param( "Periods", 15, 1, 200, 1 ); // colorred = 5, coloryellow = 14, colorlime = 21
Ksmooth = Param( "%K avg", 3, 1, 200, 1 ); // colorred = 3, coloryellow = 5, colorlime = 8
Dsmooth = Param( "%D avg", 3, 1, 200, 1 ); //colorred = 3, coloryellow = 5, colorlime = 8
Plot( StochD( periods , Ksmooth, DSmooth ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("Stochastic %K");
periods = Param( "Periods", 15, 1, 200, 1 ); //  colorred = 3, coloryellow = 5, colorlime = 8
Ksmooth = Param( "%K avg", 3, 1, 200, 1 ); //  colorred = 3, coloryellow = 5, colorlime = 8
Plot( StochK( periods , Ksmooth), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

The idea being that as one changes the time interval / normally volatility will change and the
values for " periods, Ksmooth, Dsmooth" should automatically change with volatility.

The basic concept is from the book "High Probability Trading Strategies " by Robert C. Miner
of DYNAMIC TRADER "https://www.dynamictraders.com/".

Thanks.

If you want period to be changed with interval, you can't use params that way. You can either hard code values

switch( Interval() )
{
  case inDaily: period = 30; break;
  case inWeekly: period = 20; break;
  default: period = 40; break;
}

or you can have separate parameters for each interval

dperiods = Param( "Daily Periods", 15, 1, 200, 1 ); 
wperiods = Param( "Weekly Periods", 30, 1, 200, 1 ); 

periods = IIF( Interval() == inDaily, dperiods, wperiods );
4 Likes

Milosz

Thank you very much for posting this code. I had been looking for something like this a while ago and your utility is a very useful step in that direction.
I really like the perspective it provides when cycling through a set of intervals.

Dave

1 Like

The solution was on the forum since July: Change chart interval every 20 seconds This thread is pretty much the same code plus few sliders. BTW: It is interesting as a demonstration of things that are possible, but I am not big fan of replicating functionality provided by already existing GUI: toolbar, key shortcuts, menus, etc.

@Dave - thanks for the positive feedback :+1: It's good to see that some users find this code useful. I'm always happy when I can post something useful or interesting to others.

I must say I personally also liked this idea from the beginning because this simple piece of code lets me analyse selected, interesting parts of the chart from several angles quickly and with ease. Besides when it comes to investing and coding in AFL I really enjoy experimenting with different, sometimes unusual solutions :slight_smile:


BTW. I recommend reading about all possible ways of interacting with Parameters window. I bet not all users are fully aware of all possibilities it offers.

https://www.amibroker.com/guide/w_param.html


@JEETU there are a few possible ways of solving your problem, but you should probably just follow one of Tomasz's advices.

Because Interval() function returns bar interval in seconds, in some cases it might be also possible to find a relation between those values and the values you want to get - which would make it the most flexible solution, but frankly it might be too problematic.

I wouldn't like to go into the details, because this thread is about alternative ways of changing chart's interval (in this case via Parameters) - about possible pros and cons of this approach not about Robert C. Miner trading strategies or parameters in general. Pasting a completly different code here derails the conversation.

If you expected help with the code, you could have created a separate thread and pasted link to this or any other thread...

1 Like

Yes, of course, I have pasted the link to your solution in my first post in this thread.

Tomasz I understand the reasons why you are not a big fan of similar solutions. Among some other things, it uses OLE and yes, it replicates some existing GUI functionalities, but on the other hand it offers just another, different approach to changing intervals. In some cases it can replace several custom interval buttons and offers greater speed and flexibility when changing custom intervals. Judging by the number of likes that my first two posts received, some other users also find this alternative approach interesting.

Of course there are several different ways of changing chart's interval and every user can decide which option suits him/her best. I only took advantage of the great flexibility that your software offers :slight_smile:

Searching for alternative solutions is in my nature, but I'm not recommending using my codes to anyone who doesn't need it or doesn't like it.

Regards

1 Like

For what it is worth: OLE is not bad 'per se'. It is only bad when it is used incorrectly, for example when it causes implicit infinite refresh loops. But in this particular example it is used correctly.

3 Likes

hello
I have long wondered how to use parameters for interval.
Only now I found.
Milosz, thank you very much for your post. It will be very helpful.
However, when I try to change any parameter I get a message
1
In the last line of the description:
Cannot write read-only properties

AB ver 6.10, EOD base
Does anyone know how to solve this problem?

Marek

You are not showing entire code, you need to send entire formula.

Generally speaking ActiveDocument does not need to be chart.
Chances are that you have other document active such as Analysis, Account, WebResearch, etc.
Only Chart document has .Interval property you are referring to.
The other thing is that you are not showing entire formula and no-one knows what MyInterval variable type is. Chances are the variable has incorrect type and can't be written to Interval property.

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question