To add the DO EVERY PERIOD query block, select Tools>Add Query Block>Do Every Period from the menu bar.
Example Query:
The following query generates an analog chart. The OLD symbol is the historical one, the @ is the instrument you are interested in. In this example they are the same but it does not always have to be the same (for instance you could compare the current stock market against soybeans). The CorTimePeriod is the look-back window for the correlated history search. If CorTimePeriod is set to 100, for example, it looks back at the last 100 days of history in the @ ticker and searches the Old ticker for 100-day periods whose coefficient of correlation is strongest, meaning that the movements are closest to the current periods.
%graph.plot.colors: "black blue red green yellow cyan blue magenta orange pink"
%graph.back.color: "white"
%graph.scalewith: "@" "@"
%graph.scalewith: "Old" "Old"
LET
Old = SPX
@ = SPX
ATTR CorTimePeriod = 100
ATTR StartYear = 1950
INITIALIZE
daysAgo = 0
AND
beginDate = construct_date ( 1, 1, StartYear )
AND
BestValue = 0
AND
BestDate = 0
LET
ATTR iteration = 1, 2
DO EVERY 1 day
iteration is exactly 1
AND
Old CorTimePeriod days ago is DEFINED
AND
Date is after 1 day before date:beginDate
AND
Date is before CorTimePeriod days before Old last_data_day
AND
thatMany = periods_in_range from today to Old last_data_day
AND
thisCor = correlation ( Old, @ thatMany days later, CorTimePeriod days
)
AND
IF
thisCor is more than BestValue
THEN
BestDate = current date
AND
BestValue = thisCor
ENDIF
SHOW
{
Old: BAR of Old
@: BAR of @ thatMany days later
Correlation: IF
Old thatMany days later is DEFINED
THEN correlation ( Old, @ thatMany days later,
CorTimePeriod days )
ENDIF
}
repeated from CorTimePeriod units ago to CorTimePeriod units later
WHEN
iteration is exactly 2
AND
Date is after 1 day before date:beginDate
AND
Date is date:BestDate
AND
thatMany = periods_in_range from today to Old last_data_day In the resulting chart, the correlation value is plotted in the lower pane.
