Research

This example is one of my more advanced examples.  For some, it will serve as an example for doing research.  This script averages 19 years of April Live Cattle data to discover a bias for price movement between December 1st and April 1st. The results show that the April contract trends upward from a low around the 2nd week of December through the 2nd week of March.  The 19-year average price in early December is 68.000 and the average price in mid March is 70.500. To execute this script, you will need 19 years worth of April LC data.

Disclaimer:  The risk of loss in futures trading can be substantial. Past results are not necessarily indicative of future performance. Ensign Software is not in the business of rendering any investment advice or making any claim for trading results.  The sole purpose in providing this script is to illustrate coding techniques useful for performing research on historical data.

var
  i,j,startdate,enddate,bardate,iCount: integer;
  s: string;
  yr,mo: word;
  p,n,d: TArray;

procedure FillArray(m: integer; c: integer);
begin
  for i:=1 to c do begin
    inc(iCount);
    d.values[iCount]:=m*100+i;
  end;
end;

procedure PrepareDateArray;
begin
  iCount:=0;
  FillArray(12,31); {dec}
  FillArray(1,31); {jan}
  FillArray(2,29); {feb}
  FillArray(3,31); {mar}
  FillArray(4,30); {apr}
end;

procedure AveragePrices;
var b: boolean; l: integer;
begin
  for yr:=80 to 98 do begin
    startdate:=pred(yr)*10000+1201;
    enddate:=yr*10000+0401;
    for mo:=2 to 2 do begin
      s:='lc'+inttostr(yr)+Copy('gjmqvz',mo,1);
      chart(s); j:=1;
      for i:=60 to BarEnd-15 do begin
        bardate:=bar(eDate,i);
        l:=LongToDate(bardate);
        b:=DayOfWeek(l)=6; {true for Friday}
        if (barDate>=startdate) and (bardate<=enddate) then begin
          barDate:=Bar(eMonth,i)*100+Bar(eDay,i); {month&day}
          while barDate<>d.values[j] do inc(j); {finddate}
          p.values[j]:=p.values[j]+bar(eLast,i); {sum}
          n.values[j]:=n.values[j]+1; {count}
          if b then begin {if Friday, duplicate for weekend dates}
            inc(j); {saturday}
            p.values[j]:=p.values[j]+bar(eLast,i); {sum}
            n.values[j]:=n.values[j]+1; {count}
            inc(j); {sunday}
            p.values[j]:=p.values[j]+bar(eLast,i); {sum}
            n.values[j]:=n.values[j]+1; {count}
          end;
        end;
      end;
      mnuCloseWindow.click;
    end;
  end;
end;

procedure PlotResults;
var hi,lo: real;
begin
  for i:=1 to iCount do {average price}
    if n.values[i]>10 then p.values[i]:=p.values[i]/n.values[i]
    else p.values[i]:=p.values[pred(i)];
  p.Plot('AprLC',iCount);
end;

begin
  if Who=1 then begin
    p:=TArray.Create; {price}
    n:=TArray.Create; {number}
    d:=TArray.Create; {date}
      PrepareDateArray;
      AveragePrices;
      PlotResults;
    d.free;
    n.free;
    p.free;
  end;
end;