Strategy vault / Tools & templates
Classic Moving Average Strategy
"Talk is cheap. Show me the code" For teaching purposes, use with caution in real trading. Note: Strategy uses a trading template library I hope that novices can get started with this strategy, learn to write…
"Talk is cheap. Show me the code"
For teaching purposes, use with caution in real trading.
Note: Strategy uses a trading template library
I hope that novices can get started with this strategy, learn to write strategies step by step, and experience the impact of simulation and real environments on the trading system.
https://dn-filebox.qbox.me/fb4d0c7d773ca83e9d0230927705d419dc0bbeaa.png
Parameters
| Name | Default | What it controls |
|---|---|---|
| FastPeriod | 5 | Market Entry Express Cycle |
| SlowPeriod | 15 | Slow market entry cycle |
| EnterPeriod | 2 | Market Entry Observation Period |
| ExitFastPeriod | 7 | Exit express cycle |
| ExitSlowPeriod | 15 | Exit slow cycle |
| ExitPeriod | true | Market Exit Observation Period |
| PositionRatio | 0.8 | Position ratio |
| StopLossRatio | 0.05 | Stop loss ratio |
| Interval | 10 | Polling interval (seconds)) |
Source JavaScript
/*backtest
start: 2019-01-01 00:00:00
end: 2019-07-01 00:00:00
period: 1d
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
*/
function main() {
var STATE_IDLE = -1;
var state = STATE_IDLE;
var entryPrice = 0;
var initAccount = _C(exchange.GetAccount);
Log(initAccount);
while (true) {
if (state === STATE_IDLE) {
var n = $.Cross(FastPeriod, SlowPeriod);
if (Math.abs(n) >= EnterPeriod) {
var account = _C(exchange.GetAccount);
var ticker = _C(exchange.GetTicker);
var obj = n > 0 ? $.Buy(_N(account.Balance * PositionRatio / ticker.Sell, 3)) : $.Sell(_N(account.Stocks * PositionRatio, 3));
if (obj) {
opAmount = obj.amount;
entryPrice = obj.price;
state = n > 0 ? PD_LONG : PD_SHORT;
Log("Open position details", obj, "Cross period", n);
}
}
} else {
var n = $.Cross(ExitFastPeriod, ExitSlowPeriod);
var needCover = Math.abs(n) >= ExitPeriod && ((state === PD_LONG && n < 0) || (state === PD_SHORT && n > 0));
if (needCover) {
Log("Close the position after exiting the market");
} else {
var ticker = _C(exchange.GetTicker);
if (state === PD_LONG) {
if (ticker.Buy < entryPrice*(1-StopLossRatio)) {
needCover = true;
Log("Stop loss and close position");
}
} else {
if (ticker.Sell > entryPrice*(1+StopLossRatio)) {
needCover = true;
Log("Stop loss and close position");
}
}
}
if (needCover) {
var nowAccount = _C(exchange.GetAccount);
var obj = state === PD_LONG ? $.Sell(_N(nowAccount.Stocks - initAccount.Stocks, 3)) : $.Buy(_N(initAccount.Stocks - nowAccount.Stocks, 3));
state = STATE_IDLE;
nowAccount = _C(exchange.GetAccount);
LogProfit(nowAccount.Balance - initAccount.Balance, 'Money:', nowAccount.Balance, 'Coins:', nowAccount.Stocks, 'Close details:', obj, "Cross cycle", n);
}
}
Sleep(Interval*1000);
}
}Published by Zero on the FMZ strategy square · last modified 2019-10-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 tested, production-ready module from strategies/Classic-Moving-Average-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
- Binance Account Cross Transfer Small ToolJavaScript · Tools & templates
- Get cryptocurrency current and max supplyJavaScript · Tools & templates
- Interaction TemplateJavaScript · Tools & templates
- KuCoin Fund Transfer PluginJavaScript · Tools & templates
- Moving Average Bot 30 linesJavaScript · Tools & templates · 1d
- OKEx V5 Candlestick Data Paginated Query ExampleJavaScript · Tools & templates