Examples of recalculate command

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 2112
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Examples of recalculate command

Postby bowlesj3 » 12 May 2012

Hi. I am gathering any examples of the recalculate command in this one thread.

The recalculate command restarts your study. In other words all variables are reinitialize, currentbar=1 is reprocessed and all bars continue to be processed up until LastBarOnChart. At that point the individual ticks are processed (assuming you have that option turned on). I use this command a lot. I located the command in the LastBarOnChart section and trigger it by switching on a Global Variable (GV) with setting "R". The if test detects this and issues the command. In the CurrentBar=1 section of the chart you must set this GV back to setting "N". Obviously if you forget to reset the GV your study will recalculate over and over in an infinite loop. Here is an example. This is actually a test script designed to test the RecalcPersist key word. RecalcPersist was not available until MC 8.0.


You need to set up a way to alter your "RecalcPersistTest" GV to control this test.

Note: I have coded the GV definitions without the wrapper function. The calls are the same except they appear with GV_ instead of just GV at the front. You may need to do this if you ever have to redirect the DLLs to a different directory.

Code: Select all

variables:
MyVarNormal("Init"),
IntraBarPersist MyVarIntraBarPersist("Init"),
RecalcPersist MyVarRecalcPersist("Init"),
CurrentBar1ExecCnt(0);

if CurrentBar = 1 then
Begin
DefineDLLFunc:
"C:\Program Files\TS Support\MultiCharts\GlobalVariable.dll", int, "GV_SetNamedString", lpstr, lpstr ;
DefineDLLFunc:
"C:\Program Files\TS Support\MultiCharts\GlobalVariable.dll", lpstr, "GV_GetNamedString", lpstr, lpstr;
DefineDLLFunc:
"C:\Program Files\TS Support\MultiCharts\GlobalVariable.dll", int, "GV_SetNamedInt", lpstr, int ;
DefineDLLFunc:
"C:\Program Files\TS Support\MultiCharts\GlobalVariable.dll", int, "GV_GetNamedInt", lpstr, float ;

CurrentBar1ExecCnt = GV_GetNamedInt("RecalcPersistTest",0);
CurrentBar1ExecCnt = CurrentBar1ExecCnt + 1;
value1 = GV_SetNamedInt("RecalcPersistTest",CurrentBar1ExecCnt);

FileAppend("C:\RecalcPersistTest.txt",
"CurrentBar1ExecCnt=" +
numtostr(CurrentBar1ExecCnt,0) + " " +
"MyVarNormal=" +
MyVarNormal + " " +
"MyVarRecalcPersist=" +
MyVarRecalcPersist + " " +
"MyVarIntraBarPersist=" +
MyVarIntraBarPersist + " " +
NewLine);

value1 = GV_SetNamedString("RecalcPersistTest","N"); {Prevent the infinite loop}
end;


if LastBarOnChart then
begin
if GV_GetNamedString("RecalcPersistTest","N") = "R" then
begin
MyVarNormal = "BeforeRecalculate";
MyVarIntraBarPersist = "BeforeRecalculate";
MyVarRecalcPersist = "BeforeRecalculate";
recalculate();
end;
RecalcLastBarAfter(1);
end;
Why use recalculate:
** to recalculate plots with different settings such as the bollinger bands.
** to recalculate swing settings possibly.

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: Examples of recalculate command

Postby arnie » 07 Jul 2014

Hi John.

Question, what about if we want to recalculate a study 20 seconds after the market open. Every example I saw is always referring to the last bar on chart.

So ES opens at 830 and 20 seconds after the open I want to recalculate all the variables of my study. How can I achieve this?
This recalculation is only done once.

bowlesj3
Posts: 2112
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Re: Examples of recalculate command

Postby bowlesj3 » 07 Jul 2014

Hi Arnie,

I don't see why there would be a problem. If you want to recalculate all bars that are on the chart already (I assume there are lots of bars showing before the open either from the day before or from the overnight) you need to test for that bar using the time then issue the recalculate. You need to set a switch so that you do not get it done over and over. The switch could be a Global Variable. If you EL code is running overnight day after day you should store the date in a global variable and test to see if the bar you want to recalculate on is greater than this date. In other words you would need to store the date of the bar which triggered the GV so you do not trigger it again. Maybe I should make up some rough code.

Code: Select all

Variables: GV_RecalcDate(0);

if lastBarOnChart and Time_s = 083020 then
begin
GV_RecalcDate = GV_GetNamedInt("RecalcDate", 0); {Get the GV date which should be from the day before}
if Date > GV_RecalcDate then {If we have not done this days recalculate yet then trigger the code below}
begin
{Store the date in the GV so we do not set up an infinite loop}
GV_RecalcDate = date;
value1 = GV_SetNamedInt("RecalcDate", GV_RecalcDate);
Recalculate(); {Now that the date is stored we can force the recalculate}
end;
end;
The thing to remember is your chart bars are put out there before you study executes. This is the reason you can test during the first bar of your chart to see what the lastbaronchart is.

So the above code will do the recalculate once a day only at exactly that time. If the market is thin (and I have seen it like that) you may not get this 10 second bar. In this case you should probably test for (greater than or equal to). It will still only calculate once a day because the (if Date > GV_RecalcDate then) test is what stops it from occurring over and over. It will cause your (GV_RecalcDate = GV_GetNamedInt("RecalcDate", 0);) command to execute over and over but you probably have a fast computer and it should not even notice it.

wilkinsw
Posts: 559
Joined: 21 Apr 2013
Has thanked: 139 times
Been thanked: 91 times

Re: Examples of recalculate command

Postby wilkinsw » 05 Sep 2019

Does the recalculate command in a signal; cause the autotrading to be turned off?

Thanks.

bowlesj3
Posts: 2112
Joined: 21 Jul 2007
Has thanked: 202 times
Been thanked: 420 times

Re: Examples of recalculate command

Postby bowlesj3 » 05 Sep 2019

Does the recalculate command in a signal; cause the autotrading to be turned off?

Thanks.
I don't do auto trading but I can guess. If auto trading comes on automatically when you start MC then the recalculate will likely not turn it off. If you have to turn auto trading on every day after you start MC then maybe. I believe it won't turn if off because all recalculate does is cause the study to restart as if you had just applied it to the chart. In other words it goes back and reprocesses all the bars from currentbar=1. MC should be auto-trading on the last bar on the chart (obviously it should not be executing auto trade commands on historic bars). So once the recalculate of all the historic bars is complete after the recalculate has been initiated and the signal is processing new bars being created during (last bar on chart) then if your condition is met to send the signal out it will do so. I guess you could test this by putting a limit order in that puts you way outside the market (in your favour) so it does not actually get executed. Make it an order that will make you rich if it gets executed :-)


Return to “User Contributed Studies and Indicator Library”