How to solve this loopback problem.  [SOLVED]

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

How to solve this loopback problem.

Postby bowlesj3 » 04 Oct 2017

Update: I solved it. See the 2nd post.
Hi, I am stumped on this one. I have some complex code I discovered this problem with. I put together the very simple script below to confirm the source of the problem and to also demo the problem. I describe the problem in the next paragraph.

If I comment out the "loop back code" in the script below the file append writes out the csv file in proper sequence as shown by the date and time fields. However if I let the "loop back code" run, the dates and times are out of sequence at exactly row 15 every time. So the cause appears to be looping back the date and time with the [indexY]. The "loop back code" runs every 15 bars (I tested that) and it is even more confusing that it only causes the date and time to go out of sequence in that one location.

It is pretty easy to recreate this. You need to adjust the logpath variable to your liking and you can use MS-excel to read the csv file with everything lined up in a nice easy to read format. You need to insert it in a chart with 5 minute resolution and adjust the symbol detection code. I first discovered the problem in the scanner where I have many symbols and many resolutions (explaining why I have the symbol and resolution test). However the problem occurs in a chart as well. You have to toggle the "loop back code" in and out a few times to be sure that is the cause. I used MS-Access code to determine that it is only effecting the sequence at line 15 of the csv file.

I tried a few things but they failed. Does anyone know how to solve this?

Code: Select all

variables:
LoopBackCnt(0),
IndexY(0),
Wdate(0),
Wtime(0),
LogFile(""),
LogPath(""),
RealSym(""),
BarSize(""),
BarType(""),
BarSizeType("");

if CurrentBar = 1 then
Begin
LogFile = "A_ScannerDateTimeTest.csv";
LogPath = "C:\Users\John Bowles\Downloads\" + LogFile;
//Filedelete(LogPath);
RealSym = getsymbolname;
switch bartype_ex
begin
case 1: {tick bars}
BarType = "T";
case 2: {minute bars}
BarType = "M";
case 4: {daily bars}
BarType = "D";
case 5: {weekly bars}
BarType = "W";
case 9: {second bars}
BarType = "S";
Default:
raiseruntimeerror("A_MY_RSI_HighestForTheDay has invalid bartype_ex. Aborting.");
end; {switch MySide}
BarSize = numtostr(barinterval,0);
BarSizeType = BarSize + BarType; //Possible values are 1W 1D 30M 15M and 5M
end; //if CurrentBar = 1 then

if Symbol = "FNV" and BarSizeType = "5M" then
begin

//START: loop back code. If the loop back code is commented out the append date/times are in sequence
LoopBackCnt = LoopBackCnt + 1;
if LoopBackCnt >= 15 then
begin
LoopBackCnt = 0; //This forces the loopback to occur every 15 bars.
IndexY = 0;
While IndexY < 5
begin
Wdate = Date[IndexY];
WTime = Time[IndexY];
IndexY = IndexY + 1;
end;
Wdate = Date[0]; //I was hoping this would fix the problem but it does not.
WTime = Time[0]; //I was hoping this would fix the problem but it does not.
end;
//END: loop back code. If the loop back code is commented out the append date/times are in sequence

FileAppend(LogPath,
Symbol +
"," +
BarSizeType +
"," +
numtostr(date,0) +
"," +
numtostr(time,0) +
NewLine);
end;
Last edited by bowlesj3 on 04 Oct 2017, edited 2 times in total.

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

Re: How to solve this loopback problem.  [SOLVED]

Postby bowlesj3 » 04 Oct 2017

I found the solution myself. It took a while since I have been away from MC for a while. Having MC auto update of the maxbarsback for this script was the problem. It does not know I am going to loop back so when my code loops back to before the first bar on the chart it has to restart the script with the larger maxbarsback setting (what I call the hidden recalculate). This causes additional records to be written out to the fileappend without the original records being removed. In this code I can figure out what size to make the maxbarsback. However in my actual complex code I have to set it fairly large and take a bit of a guess as to just how large.

Years back I created a post with a script you can use to determine if the hidden recalculate is occurring.
viewtopic.php?f=5&t=7362
How quickly we forget :-). Well it has been 2 or 3 years so not that fast :-)


Return to “MultiCharts”