Updating DayHigh and DayLow Time in Real Time  [SOLVED]

Questions about MultiCharts and user contributed studies.
avme
Posts: 21
Joined: 30 Oct 2013
Has thanked: 1 time
Been thanked: 2 times

Updating DayHigh and DayLow Time in Real Time  [SOLVED]

Postby avme » 12 Jan 2015

Hi All,

I write a piece of code for Market Scanner to record the DayHigh and DayLow Time. Resolution is in 1 tick.
The code does two things:
1) Even the code runs for example 2 hours after the market opens, the code will find the DayOpen, DayHigh and DayLow of today.
2) Calculate the latest daily change (Last-PreviousClose)/Previous.

I use the "lastbaronchart" to initiate the code. The code works as expected so far.

Code: Select all

Variable: FirstRun(True),
BarCount(0), //Find Yesterday bar
BarCountII(0), //Loop for Today's OHL
PreviousClose(0),
PreviousDate(0),
ThisBarDate(0),
ThisBarTime(0),
DayOpen(0),
DayHigh(0),
DayHighTime(0),
DayLow(0),
DayLowTime(0),
DailyChg(0);

If LastBaronChart AND FirstRun = True then begin
For BarCount = 1 to 100000 begin
If Date <> Date[BarCount] then begin
//Record Last Close
PreviousDate = Date[BarCount];
PreviousClose = Close[BarCount]; //Locate Previous Day Close by BarCount
//Record Today's first tick as Open, High, Low
ThisBarDate = Date[BarCount-1];
ThisBarTime = Time[BarCount-1];
DayOpen = Close[BarCount-1];
DayHigh = Close[BarCount-1];
DayLow = Close[BarCount-1];
Break;
End;
End;
For BarCountII = BarCount-2 downto 0 begin
//Find the DayHigh and DayLow from last bar to the first bar of today
If Close[BarCountII] >= DayHigh then begin
DayHigh = Close[BarCountII];
DayHighTime = Time[BarCountII];
End;
If Close[BarCountII] <= DayLow then begin
DayLow = Close[BarCountII];
DayLowTime = Time[BarCountII];
End;
End;
End;
Plot1(BarCount,"BarCount");
Plot2(PreviousDate,"PreviousDate");
Plot3(PreviousClose,"PreviousClose");
Plot4(DayHigh, "High");
Plot5(FormatTime("HH:mm",ELTimetoDateTime(DayHighTime)), "H-Time");
Plot6(DayLow, "Low");
Plot7(FormatTime("HH:mm",ELTimeToDateTime(DayLowTime)), "L-Time");
Plot8(DayOpen, "Open");
Plot9(ThisBarDate, "Today");
Plot10(FormatTime("HH:mm",ELTimeToDateTime(ThisBarTime)), "TodayT");
But then I found that many variables were not necessary to calculate on every tick. (e.g. Previous Close). And if I run hundreds of symbols, the code runs slow. So I used a true/false switch to separate variables before this tick. But it is not working and I cannot figure what goes wrong. All I know so far is that the key problem comes from the switch (Variable=FirstRun). Can anyone give me some hints?

Code: Select all

Variable: FirstRun(True),
BarCount(0), //Find Yesterday bar
BarCountII(0), //Loop for Today's OHL
PreviousClose(0),
PreviousDate(0),
ThisBarDate(0),
ThisBarTime(0),
DayOpen(0),
DayHigh(0),
DayHighTime(0),
DayLow(0),
DayLowTime(0),
DailyChg(0);
//Subsequent Run
If FirstRun = False then begin
If Close >= DayHigh then begin
DayHigh = Close;
DayHighTime = Time;
End;
If Close <= DayLow then begin
DayLow = Close;
DayLowTime = Time;
End;
End;

//FirstRun
If FirstRun = True AND LastBaronChart then begin
For BarCount = 1 to 100000 begin
If Date <> Date[BarCount] then begin
//Record Last Close
PreviousDate = Date[BarCount];
PreviousClose = Close[BarCount]; //Locate Previous Day Close by BarCount
//Record Today's first tick as Open, High, Low
ThisBarDate = Date[BarCount-1];
ThisBarTime = Time[BarCount-1];
DayOpen = Close[BarCount-1];
DayHigh = Close[BarCount-1];
DayLow = Close[BarCount-1];
Break;
End;
End;
For BarCountII = BarCount-2 to 0 begin
//Find the DayHigh and DayLow from last bar to the first bar of today
If Close[BarCountII] >= DayHigh then begin
DayHigh = Close[BarCountII];
DayHighTime = Time[BarCountII];
End;
If Close[BarCountII] <= DayLow then begin
DayLow = Close[BarCountII];
DayLowTime = Time[BarCountII];
End;
End;
FirstRun = False;
End;

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Updating DayHigh and DayLow Time in Real Time

Postby Henry MultiСharts » 12 Jan 2015

Hello avme,

The scripts have the following difference:
1st code:

Code: Select all

For BarCountII = BarCount-2 downto 0 begin
2nd code:

Code: Select all

For BarCountII = BarCount-2 to 0 begin

avme
Posts: 21
Joined: 30 Oct 2013
Has thanked: 1 time
Been thanked: 2 times

Re: Updating DayHigh and DayLow Time in Real Time

Postby avme » 12 Jan 2015

Thanks Henry for pointing it out. But the problem persists after correction. Let me be more specific.

When the code is running correctly, the variable "barcount" should get the total number of ticks of the last date. See below.

Image

The code is not efficient as it keeps updating the already recorded variables (e.g. previous close). It should only cross-check the close with the recorded variables, no need to go back every tick to update the unchanged variables.

So I break the code into "First Run" and "Subsequent Run" by using variable "FirstRun". The default is true. When it turns to false, the "FirstRun" is dormant. And it is the culprit that gives me the wrong barcount.

Image

I just can't figure out why the barcount would be different as I do not do anything about it. Any help would be appreciated.

Repost the code:

Code: Select all

Variable: FirstRun(True),
BarCount(0), //Find Yesterday bar
BarCountII(0), //Loop for Today's OHL
PreviousClose(0),
PreviousDate(0),
ThisBarDate(0),
ThisBarTime(0),
DayOpen(0),
DayHigh(0),
DayHighTime(0),
DayLow(0),
DayLowTime(0),
DailyChg(0);

//Subsequent Run
If FirstRun = False then begin
If Close >= DayHigh then begin
DayHigh = Close;
DayHighTime = Time;
End;
If Close <= DayLow then begin
DayLow = Close;
DayLowTime = Time;
End;
End;

//FirstRun
If FirstRun = True AND LastBaronChart then begin
For BarCount = 1 to 100000 begin //Assume total ticks are less than 100000
If Date <> Date[BarCount] then begin
//Record Last Close
PreviousDate = Date[BarCount];
PreviousClose = Close[BarCount]; //Locate Previous Day Close by BarCount
//Record Today's first tick as Open, High, Low
ThisBarDate = Date[BarCount-1];
ThisBarTime = Time[BarCount-1];
DayOpen = Close[BarCount-1];
DayHigh = Close[BarCount-1];
DayLow = Close[BarCount-1];
Break;
End;
End;
For BarCountII = BarCount-2 downto 0 begin
//Find the DayHigh and DayLow from last bar to the first bar of today
If Close[BarCountII] >= DayHigh then begin
DayHigh = Close[BarCountII];
DayHighTime = Time[BarCountII];
End;
If Close[BarCountII] <= DayLow then begin
DayLow = Close[BarCountII];
DayLowTime = Time[BarCountII];
End;
End;
FirstRun = False;
End;


Plot1(BarCount,"BarCount",White);
Plot2(FormatDate("dd/MM",ELDateToDateTime(PreviousDate)),"PreviousDate",Yellow);
Plot3(PreviousClose,"PreviousClose",Yellow);
Plot4(FormatDate("dd/MM",ELDateToDateTime(ThisBarDate)), "Today",magenta);
Plot5(DayHigh, "High",Green);
Plot6(FormatTime("HH:mm:ss",ELTimetoDateTime(DayHighTime)), "H-Time",Green);
Plot7(DayLow, "Low",Red);
Plot8(FormatTime("HH:mm:ss",ELTimeToDateTime(DayLowTime)), "L-Time",Red);
Plot9(DayOpen, "Open",lightgray);
Plot10(FormatTime("HH:mm:ss",ELTimetoDateTime(ThisBarTime)), "O-Time",LightGray);
Attachments
wrong barcount.jpg
(37.97 KiB) Downloaded 567 times
barcount number.JPG
(196.53 KiB) Downloaded 560 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Updating DayHigh and DayLow Time in Real Time

Postby Henry MultiСharts » 13 Jan 2015

You need to add BarCount = BarCount + 1; to your code as the following:

Code: Select all

//Subsequent Run
If FirstRun = False then begin
If Close >= DayHigh then begin
DayHigh = Close;
DayHighTime = Time;
End;
If Close <= DayLow then begin
DayLow = Close;
DayLowTime = Time;
End;
BarCount = BarCount + 1;
End;

avme
Posts: 21
Joined: 30 Oct 2013
Has thanked: 1 time
Been thanked: 2 times

Re: Updating DayHigh and DayLow Time in Real Time

Postby avme » 13 Jan 2015

Thanks Henry. It works perfectly.


Return to “MultiCharts”