Setting MaxBarsBack  [SOLVED]

Questions about MultiCharts and user contributed studies.
bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Setting MaxBarsBack

Postby bowlesj3 » 03 Jul 2018

Hi, is there a way to to determine the number of actual bars on a chart then set the maxbarsback to 1/2 this value.

I am developing a script to draw lines on the RSI (or on its plot) such that I get the maximum number of bars of RSI showing while at the same time I get the .largest MaxBarsBack size so I can use the MouseClick commands to get anchor points for the lines without tripping a hidden recalculate due to clicking past the maxbarsback setting. The ideal is a MaxBarsBack exactly 1/2 the actual bars on the chart. It is not a problem if there are a lot of bars but if there are a limited number as sometimes happens in the weekly bars then it becomes more important.

Thanks,
John

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Setting MaxBarsBack

Postby TJ » 03 Jul 2018

>... set the maxbarsback to 1/2 this value.


This does not make sense.

maxbarsback = minimum number of bars needed to do a complete calculations of ALL the indicators in the chart.

eg.
if you have a 10 period MA, then the maxbarsback = 10
if you have a 20 period MA, then the maxbarsback = 20

if your chart has a 10 period MA and a 20 period MA, then the maxbarsback of the chart = 20

The first "Valid" bar in the chart would be #21, because that's the first bar that has all the indicators calculated.

If you change the maxbarsback to a number larger than 21, then you will have less "valid" bars to work with.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Setting MaxBarsBack

Postby bowlesj3 » 03 Jul 2018

No.

You get more RSI plot lines if you set the MaxBarsBack to the default which is 14 (or maybe it adjusts to 22). However that restricts the distance back you can use the MouseClickDateTime.

1/ The problem does not occur when you click the mouse and get the values (either date/time or BarNumber).
2/ The problem does not occur when you compute the backward offset using these values.
The problem occurs when you use the offset that was correctly computed go back and get the historic RSI value for the purpose of plotting the line.

To understand this remember this MouseClick is occurring when the study has computed through to the last bar on the chart and at that point the maxbars back is only 14 bars back from the last bar on the chart.

I have copied some code below to help make it clearer. My actual code is more complex but I won't get into why. Again I just want to make it clear what the problem is. I have two ways I can get the offset. I was using a binary search but I am now using the MouseClickBarNumber which I had to copy into a special field called MouseClickBarNumberCorrect because of the fact that this code executes when I click a text which is out front of the last bar on chart. In other words MouseClickBarNumberCorrect contains the value that was obtained when I click a historic bar exactly where I want the line to anchor dead on the RSI plot. So what happens is this. If that bar is farther back than the MaxBarsBack Setting of 14 then it causes the hidden recalculate at the time it executes this command.

Code: Select all

AngleRvalue2 = Round(Plot1[TextOffset],5); {We round to 5 decimales so that the GV will store the value correctly}
This forces the MaxBarsBack to a higher value reducing the number of RSI plot lines but it isn't that smart about it. It generally sets the MaxBarsBack too small. So another problem occurs too. If I set the MaxBarsBack to a value of lets say 300 which is fine for BCE as soon as I display BB which has less weekly bars MC goes into what I figure is an infinite recalculate loop (never stops calculating). I am not sure why it does that at this point. I just know how to solve the problem (if it can be done that is).

Code: Select all

if LastBarOnChart then {if:482}
begin
ActualBars = BarNumber + MaxBarsBack;
TextOffsetCalc = ActualBars - MouseClickBarNumberCorrect;
TextOffsetSearch = A_FindOffsetViaBinary(MouseClickDate,MouseClickTime,strProjName);
TextOffset = TextOffsetCalc;
//AngleRvalue2 = Round(MyRSI[TextOffset],5); {We round to 5 decimales so that the GV will store the value correctly}
AngleRvalue2 = Round(Plot1[TextOffset],5); {We round to 5 decimales so that the GV will store the value correctly}
End;
Last edited by bowlesj3 on 03 Jul 2018, edited 4 times in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Setting MaxBarsBack

Postby TJ » 03 Jul 2018

If your RSI is 14, the maxbarsback will be 14 plus whatever offset needed in the RSI calculation.

If your mouse click "look back" involves looking 100 bars back, then your maxbarsback will be automatically set to 100.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Setting MaxBarsBack

Postby TJ » 03 Jul 2018

Hi, is there a way to to determine the number of actual bars on a chart ...

...

Look up these keywords

Currentbar
Symbol_CurrentBar

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Setting MaxBarsBack

Postby bowlesj3 » 03 Jul 2018

Symbol_CurrentBar is the same as my ActualBar. However I think you might have triggered some thinking on my part as to how I can fix this under one assuption (the assumption being I can set the MaxBarsBack setting with my code - I seem to remember seeing it can be done but I don't remember if I ever did it nor how to do it). I am working on the code now under that assumption.

========
I set the maxBarsBack to the default. My MS-Access program can set this on the first call to each new symbol's chart in the scanner.
I grab the Symbol_CurrentBar once the LastBarOnChart occurs.
I divide this value by 2 and store it in a GV.
I force a recalculate command using a GV to control it so it will not loop endlessly.
When the script re-executes during the currentbar=1 code I set the MaxBarsBack to the value in the GV and the other GV stops the recalculate from occurring again.

In the end if I can get this to work it becomes a trade off (less RSI plots for more plots I can put lines on). I commented out the first line of code below hoping I could put lines on farther back that the maxbarsback setting but unfortunately not.

Code: Select all

//AngleRvalue2 = Round(MyRSI[TextOffset],5);
AngleRvalue2 = Round(Plot1[TextOffset],5);

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Setting MaxBarsBack

Postby bowlesj3 » 03 Jul 2018

Trying to set the MaxBarsBack in the code gives a compile error.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Setting MaxBarsBack

Postby TJ » 03 Jul 2018

The maxbarsback is set during initialization through a declaration. I know of no way to change it with a code after initialization.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Setting MaxBarsBack

Postby bowlesj3 » 03 Jul 2018

It might be that I need to store the mouse click date and time in GVs then force a controlled recalculate and apply the line to the plots as the script is recalculating. This would allow me to leave the recalculate setting to automatic while going back as far as I want with the mouse clicks. I would get my cake and get to eat it all too :-) I have sort of done this type of thing before. I figure it should work. For the lines on an angle I would draw the line when I hit the second anchor point during the recalculate process. For the horizontal line I could do it as soon as I hit the first anchor point. It would mean looping all the GVs for all of the 10 possible lines on each new bar as the script recalculates (a lot of extra processing). Yeah, that should work. Tomorrow I will see if I can get it to work and post the results.

Correction: I only loop the lines table once when I click the "A" or "H" interactive code. I am already doing that to find a free line table entry to load the info into. Once I find a free table entry I will store the date and time info leaving the RSI value blank and I also store in a GV the line number pointer into the table. During the recalculate when the date and time are finally reached I will have the code put the value in. So not too much extra processing.

The GVs are used to store the line info along with an MS-access database program. This is needed for when I move through the scanner switching symbols and also overnight when I switch the machine off. By storing the info at just the correct time (always controlling the scanner from the database program with keystroke commands) I greatly reduce the number of GVs I need thus increasing the number of stocks I can track.

I am doing something similar in the price chart soon. I might need to do the same thing.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Setting MaxBarsBack  [SOLVED]

Postby bowlesj3 » 04 Jul 2018

It was a challenge but I finally go it to work properly including with the correction I mentioned in my prior post. It is faster than putting lines on manually and being programmed you can do more stuff with them by passing the GV info to other charts.


Return to “MultiCharts”