Get Weekly K Line
Combine daily candlesticks into weekly candlesticks, with the default week starting on Sunday (adjustable).). Usage Method: $.GetRecordsWeek(exchange)
Combine daily candlesticks into weekly candlesticks, with the default week starting on Sunday (adjustable).).
Usage Method: $.GetRecordsWeek(exchange)
Parameters
| Name | Default | What it controls |
|---|---|---|
| weekStarts | 0 | The week starts on: Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
Source JavaScript
$.GetRecordsWeek = function(exchange) {
var rec1 = exchange.GetRecords(PERIOD_D1);
if (!rec1) return null;
if (rec1.length === 0) return [];
var recN = [];
var tmp = {
Time: rec1[0].Time,
Open: rec1[0].Open,
High: rec1[0].High,
Low: rec1[0].Low,
Close: rec1[0].Close,
Volume: rec1[0].Volume
};
for (var i = 1; i < rec1.length; i++) {
if (Math.floor((rec1[i].Time / 86400000 - 3 - weekStarts + 1/3) / 7 + 1e-6) > Math.floor((rec1[i-1].Time / 86400000 - 3 - weekStarts - 1/3) / 7 + 1e-6)) { // new week
recN.push({
Time: tmp.Time,
Open: tmp.Open,
High: tmp.High,
Low: tmp.Low,
Close: tmp.Close,
Volume: tmp.Volume
});
tmp.Time = rec1[i].Time;
tmp.Open = rec1[i].Open;
tmp.High = rec1[i].High;
tmp.Low = rec1[i].Low;
tmp.Close = rec1[i].Close;
tmp.Volume = rec1[i].Volume;
} else if (tmp.Time) { // same week
tmp.High = Math.max(tmp.High, rec1[i].High);
tmp.Low = Math.min(tmp.Low, rec1[i].Low);
tmp.Close = rec1[i].Close;
tmp.Volume += rec1[i].Volume;
}
}
recN.push({
Time: tmp.Time,
Open: tmp.Open,
High: tmp.High,
Low: tmp.Low,
Close: tmp.Close,
Volume: tmp.Volume
});
return recN;
};
function main() {
var rec = $.GetRecordsWeek(exchange);
Log(new Date(rec[rec.length-1].Time).toString());
}Published by 数·狂 on the FMZ strategy square · last modified 2016-08-17. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Get-Weekly-K-Line.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.