Strategy vault / Trend following
Code Implementation and Feasibility Analysis of Following the Major Trend and Going Against the Minor Trend Strategy
JavaScript strategy by 深蓝.
JavaScriptTrend following
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| maLen | 100 | maLen |
| kd1 | 50 | kd1 |
| kd2 | 15 | kd2 |
Source JavaScript
//Syntax fixed format, callmainMain Function
function main() {
//Call from the commodities futures trading libraryCTAFramework
$.CTA("RM000", function(st) {
//ObtainKLine array
var j = st.records;
//Maximum reference for indicator calculationKNumber of lines
if (j.length < 100) {
return;
}
//Get the upper rootKThe closing price of the line
var c = j[j.length - 2].Close;
//ObtainKDJIndicator array
var kds = TA.KDJ(j, kd1, kd2, kd2);
//ObtainKDJIndicatorKArray of
var ks = kds[0];
//ObtainKDJIndicatorDArray of
var ds = kds[1];
//Get the upper rootKLine'sKvalue
var k = ks[ks.length - 2].toFixed(2);
//Get the upper rootKLine'sDvalue
var d = ds[ds.length - 2].toFixed(2);
//Get moving average array
var mas = TA.MA(j, 100);
//Get the upper rootKLine'sMAvalue
var ma = mas[mas.length - 2];
//Get the current position quantity, positive number indicates long position, Negative Value Indicates Short Position, 0Then do not hold position
var mp = st.position.amount;
//If currently holding a long position, and the previousKLine'sKValue less than the previous barKLine'sDvalue, close long position
if (mp > 0 && k < d) {
return -1; //If there are currently multiple orders, the specified return value is-N,It's flatNLong position.
}
//If currently holding a short position, and the previousKLine'sKValue greater than the previous barKLine'sDvalue, close short position
if (mp < 0 && k > d) {
return 1; //If there is currently an open order, the specified return value isN,It's flatNShort-handed order.
}
//If currently no position, and the previous candleKLine's closing price greater than the upper barKLine'sMAvalue, and rootKLine'sKValue greater than the previous barKLine'sDvalue, open long position
if (mp === 0 && c > ma && k > d) {
return 1; //If there is currently no position, the specified return value isN,Just OpenNLong position.
}
//If currently no position, and the previous candleKLine's closing price less than the upper barKLine'sMAvalue, and rootKLine'sKValue less than the previous barKLine'sDvalue, open short position
if (mp === 0 && c < ma && k < d) {
return -1; //If there is currently no position, the specified return value is-N,Just OpenNShort-handed order.
}
});
}Published by 深蓝 on the FMZ strategy square · last modified 2018-05-08. 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/Code-Implementation-and-Feasibility-Analysis-of-Following-the-Major-Trend-and-Going-Against-the-Minor-Trend-Strategy.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.
Related specs
- Large Medium and Small Three Cycle Leap Strategy V20 Spot TestJavaScript · Trend following · 1d
- Step by Step Position Adding Trading Strategy Based on EMA Trend Filtering TutorialJavaScript · Trend following · 1h
- KingKeltner Trend Strategy Low FrequencyJavaScript · Trend following
- Statistics Candlestick Backtesting to See If There Is a TrendJavaScript · Trend following
- SuperTrend Strategy LessonJavaScript · Trend following · 15m
- Trend Strategy V10 PublicJavaScript · Trend following