Digital Currency Futures Double Moving Average Turning Point Strategy Teaching
Related articles:https://www.fmz.com/bbs-topic/8479
Related articles:https://www.fmz.com/bbs-topic/8479
Parameters
| Name | Default | What it controls |
|---|---|---|
| ema1Period | The period of the first EMA moving average indicator | EMA1 moving average period |
| ema2Period | The period of the second EMA moving average indicator | EMA2 moving average period |
| amount | Binance can be a decimal; most futures exchanges place contract orders based on the number of contracts (integers) | contract order volume |
| profitTarget | Take-profit price difference, enter 20 to take profit and close the position with a 20-point difference | Take profit |
| ct | Perpetual contracts: swap, Quarterly contracts: quarter; details can be checked in the API documentation | contract code. |
| okexSimulate | Switch to OKEX V5 demo account | Switch to OKEX V5 demo account |
Source JavaScript
/*backtest
start: 2021-09-01 00:00:00
end: 2021-12-02 00:00:00
period: 1h
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
var LONG = 1
var SHORT = -1
var IDLE = 0
function getPosition(positions, direction) {
var ret = {Price : 0, Amount : 0, Type : ""}
_.each(positions, function(pos) {
if (pos.Type == direction) {
ret = pos
}
})
return ret
}
function cancellAll() {
while (true) {
var orders = _C(exchange.GetOrders)
if (orders.length == 0) {
break
} else {
for (var i = 0 ; i < orders.length ; i++) {
exchange.CancelOrder(orders[i].Id, orders[i])
Sleep(500)
}
}
Sleep(500)
}
}
function cover(tradeFunc, direction) {
var mapDirection = {"closebuy": PD_LONG, "closesell": PD_SHORT}
var positions = _C(exchange.GetPosition)
var pos = getPosition(positions, mapDirection[direction])
if (pos.Amount > 0) {
cancellAll()
exchange.SetDirection(direction)
if (tradeFunc(-1, pos.Amount)) {
return true
} else {
return false
}
}
return true
}
function main() {
if (okexSimulate) {
exchange.IO("simulate", true) // Switch toOKEX V5Demo Account Test
Log("Switch toOKEX V5Simulation Account")
}
exchange.SetContractType(ct)
var state = IDLE
var holdPrice = 0
var preTime = 0
while (true) {
var r = _C(exchange.GetRecords)
var l = r.length
if (l < Math.max(ema1Period, ema2Period)) {
Sleep(1000)
continue
}
var ema1 = TA.EMA(r, ema1Period)
var ema2 = TA.EMA(r, ema2Period)
// Drawing
$.PlotRecords(r, 'KLine')
if(preTime !== r[l - 1].Time){
$.PlotLine('ema1', ema1[l - 2], r[l - 2].Time)
$.PlotLine('ema2', ema2[l - 2], r[l - 2].Time)
$.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
$.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
preTime = r[l - 1].Time
} else {
$.PlotLine('ema1', ema1[l - 1], r[l - 1].Time)
$.PlotLine('ema2', ema2[l - 1], r[l - 1].Time)
}
var up = (ema1[l - 2] > ema1[l - 3] && ema1[l - 4] > ema1[l - 3]) && (ema2[l - 2] > ema2[l - 3] && ema2[l - 4] > ema2[l - 3])
var down = (ema1[l - 2] < ema1[l - 3] && ema1[l - 4] < ema1[l - 3]) && (ema2[l - 2] < ema2[l - 3] && ema2[l - 4] < ema2[l - 3])
if (up && (state == SHORT || state == IDLE)) {
if (state == SHORT && cover(exchange.Buy, "closesell")) {
state = IDLE
holdPrice = 0
$.PlotFlag(r[l - 1].Time, 'coverShort', 'CS')
}
exchange.SetDirection("buy")
if (exchange.Buy(-1, amount)) {
state = LONG
holdPrice = r[l - 1].Close
$.PlotFlag(r[l - 1].Time, 'openLong', 'L')
}
} else if (down && (state == LONG || state == IDLE)) {
if (state == LONG && cover(exchange.Sell, "closebuy")) {
state = IDLE
holdPrice = 0
$.PlotFlag(r[l - 1].Time, 'coverLong', 'CL')
}
exchange.SetDirection("sell")
if (exchange.Sell(-1, amount)) {
state = SHORT
holdPrice = r[l - 1].Close
$.PlotFlag(r[l - 1].Time, 'openShort', 'S')
}
}
// take profit
if (state == LONG && r[l - 1].Close - holdPrice > profitTarget && cover(exchange.Sell, "closebuy")) {
state = IDLE
holdPrice = 0
$.PlotFlag(r[l - 1].Time, 'coverLong', 'CL')
} else if (state == SHORT && holdPrice - r[l - 1].Close > profitTarget && cover(exchange.Buy, "closesell")) {
state = IDLE
holdPrice = 0
$.PlotFlag(r[l - 1].Time, 'coverShort', 'CS')
}
LogStatus(_D())
Sleep(500)
}
}Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2025-02-18. 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/Digital-Currency-Futures-Double-Moving-Average-Turning-Point-Strategy-Teaching.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
- Test About the Long Term Effect of MartinJavaScript · Other · 1d
- Three Moving Average System ExodusJavaScript · Other · 1h
- Digital Currency Spot Multi Variety Double Moving Average Strategy TeachingJavaScript · Other
- 8 Hours EMAJavaScript · Other · 4h
- Teaching Strategy TCI Strategy Mai Language TransplantationJavaScript · Other · 1d
- Test Using KDJ and VOL Volume to Filter for Coins with Potential to RiseJavaScript · Other · 1d