Recent

Author Topic: OHLC - Draw weekly and Monthly  (Read 792 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1308
OHLC - Draw weekly and Monthly
« on: December 18, 2025, 06:38:12 pm »
Lazarus 4.4
There is an array with daily date - OHLC data.
I want to draw them weekly:
The first quote of Monday shall be "open", the last quote of Friday shall be "close".
and so on.

Must I calculate this by myself or is there a way in TAChart that I just can set my series to "weekly".
There are a lot of series in that chart. All other are weekly.
to add: They shall use the same bottom axis and a different left axis.
The axes thing shall have a solution and not part of the question.


What is my question here:

How to display this daily data in a weekly chart most efficiently?
Thanks.

wp

  • Hero Member
  • *****
  • Posts: 13398
Re: OHLC - Draw weekly and Monthly
« Reply #1 on: December 18, 2025, 08:19:52 pm »
TAChart only does the drawing of numbers passed on to it. If your primary data sources are daily rather than weekly you must do the calculation to get weekly data yourself.

Nicole

  • Hero Member
  • *****
  • Posts: 1308
Re: OHLC - Draw weekly and Monthly
« Reply #2 on: December 19, 2025, 05:34:09 pm »
Thank you for your answer.

As it was tricky to solve it, I share a method, which converts weekly to daily. Tricky, because we need a solution that will work as well, if Monday is a holiday or a data error says zero one day.

Code: Pascal  [Select][+][-]
  1. // verwandelt Tageskurse in Wochenkurse
  2. function TFrame_cot_dis.DailyInWeekly(Kurse: TKurseF): TKurseF;
  3. var i: Integer;
  4.     LastWeek, WeekIndex: Integer;
  5. begin
  6.   if Length(Kurse) = 0 then begin
  7.      ShowMessage('TFrame_cot_dis.DailyInWeekly: leeres Array wurde übergeben');
  8.      result:=Kurse;
  9.      Exit;
  10.    end;
  11.  
  12.   SetLength(Result, Length(Kurse));
  13.  
  14.   LastWeek := 0;
  15.   WeekIndex := 0;
  16.  
  17.   for i := 0 to High(Kurse) do begin   // sucht nach der Wochennummer
  18.     if WeekOf(Kurse[i].Datum) <> LastWeek then begin
  19.         Result[WeekIndex].Datum := Kurse[i].Datum;
  20.         Result[WeekIndex].open := Kurse[i].open;   // das stimmt auch, wenn Feiertage vorhanden sind
  21.         Result[WeekIndex].high := Kurse[i].high;
  22.         Result[WeekIndex].low := Kurse[i].low;
  23.         if (Result[WeekIndex].low = 0) and (i > 0)
  24.            then Result[WeekIndex].low:=Result[WeekIndex-1].low;
  25.         Result[WeekIndex].close := Kurse[i].close;
  26.         LastWeek := WeekOf(Kurse[i].Datum);
  27.         Inc(WeekIndex);
  28.       end else begin
  29.         if Kurse[i].high > Result[WeekIndex-1].high then
  30.           Result[WeekIndex-1].high := Kurse[i].high;
  31.  
  32.         if (Kurse[i].low < Result[WeekIndex-1].low) and (Kurse[i].low > 0) then
  33.           Result[WeekIndex-1].low := Kurse[i].low;
  34.  
  35.         Result[WeekIndex-1].close := Kurse[i].close;
  36.         Result[WeekIndex-1].Datum := Kurse[i].Datum;
  37.       end;
  38.     end;
  39.  
  40.  
  41.  
  42.  
  43.     SetLength(Result, WeekIndex);  // das array wird auf die Zahlen beschnitten
  44. end;

where
Code: Pascal  [Select][+][-]
  1. Type TEineKontraktZeile = packed Record   //eine Kursdatenzeile für Futures
  2.      id_kurszeile: integer;  // id der DB
  3.      Datum: TDateTime;
  4.      open, high, low, close, Volume, OpenInterest: double;
  5.      wert: double;
  6.         end;
  7. Type TKurseF  = array of TEineKontraktzeile;       //alle Kurse eines Kontrakte  

wp

  • Hero Member
  • *****
  • Posts: 13398
Re: OHLC - Draw weekly and Monthly
« Reply #3 on: December 19, 2025, 07:30:02 pm »
Code: Pascal  [Select][+][-]
  1. // verwandelt Tageskurse in Wochenkurse
  2.         if (Result[WeekIndex].low = 0) and (i > 0)
  3.            then Result[WeekIndex].low:=Result[WeekIndex-1].low;
  4.  
Does this mean that the weekly low-value carries over from the previous week to the current week? I cannot believe that this is correct. Because then a week could never have a higher low value than all weeks before.

Nicole

  • Hero Member
  • *****
  • Posts: 1308
Re: OHLC - Draw weekly and Monthly
« Reply #4 on: December 19, 2025, 08:02:23 pm »
I hope, I do not misunderstand you, because I am really tired for today.
What was my problem: sometimes the low values jumps to zero, because the data are not complete. Then it is better to have a false low value than a zero value, because it destroys the scaling.

I must figure out, if this is true for other values as well, e.g. the close.

wp

  • Hero Member
  • *****
  • Posts: 13398
Re: OHLC - Draw weekly and Monthly
« Reply #5 on: December 19, 2025, 10:32:28 pm »
I hope, I do not misunderstand you, because I am really tired for today.
 Then it is better to have a false low value than a zero value
But, suppose the new week has very high prices, and the real "low" is much higher than the false low copied from last week. In this case the low value for this week will be wrong...

In case of a missing low value for the first day of a new week why don't you calculate an estimated low from the other values that you have for this day: estimated_low := Min(open, close). Whenthe  next days of this same week have higher prices the estimated_low will be closer to reality than the low of last week.

Nicole

  • Hero Member
  • *****
  • Posts: 1308
Re: OHLC - Draw weekly and Monthly
« Reply #6 on: December 20, 2025, 09:53:19 pm »
Thank you so much for your thoughts! They are on my desktop for next week.

 

TinyPortal © 2005-2018