Strategy vault / Trend following
Moving Average Cross
Pine Script strategy by Zer3192.
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | true | Start Date |
| v_input_int_2 | true | Start Month |
| v_input_int_3 | 2012 | Start Year |
| v_input_int_4 | true | End Date |
| v_input_int_5 | true | End Month |
| v_input_int_6 | 2022 | End Year |
| v_input_int_7 | 69 | Slow MA Length |
| v_input_int_8 | 29 | Fast MA Length |
Source Pine Script
/*backtest
start: 2021-05-08 00:00:00
end: 2022-05-07 23:59:00
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Market-Edge-Trading
//@version=5
strategy("Moving Average Cross", overlay=true, commission_type= strategy.commission.percent, default_qty_value=100, default_qty_type=strategy.percent_of_equity, margin_long = 1, margin_short=1)
// Inputs for configuring sample date range
startDate= input.int(1, title="Start Date", minval=1, maxval=31)
startMonth= input.int(1, title="Start Month", minval=1, maxval=12)
startYear=input.int(2012, title="Start Year", minval=1700, maxval=2100)
endDate= input.int(1, title="End Date", minval=1, maxval=31)
endMonth= input.int(1, title="End Month", minval=1, maxval=12)
endYear=input.int(2022, title="End Year", minval=1700, maxval=2100)
inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
// Set up moving averages
Slow_MA_Length = input.int(69, title="Slow MA Length", minval = 1, maxval = 1000)
Slow_MA = ta.sma(close, Slow_MA_Length)
plot(Slow_MA, color=color.white, linewidth=2)
Fast_MA_Length = input.int(29, title="Fast MA Length", minval = 1, maxval = 1000)
Fast_MA = ta.sma(close, Fast_MA_Length)
plot(Fast_MA, color=color.yellow, linewidth=2)
// Set long and short conditions
longCondition = ta.crossover(Fast_MA, Slow_MA)
shortCondition = ta.crossunder(Fast_MA, Slow_MA)
// Make trades
if(longCondition and inDateRange)
strategy.entry("Long Entry", strategy.long)
if(shortCondition and inDateRange)
strategy.entry("Short Entry", strategy.short)
// Exit open positions when date range ends
if (not inDateRange)
strategy.close_all()
// table of optimized values
testTable = table.new(position = position.top_right, columns = 3, rows = 6, bgcolor = color.black, border_width = 1)
table.cell(table_id = testTable, column = 0, row = 0, text = " ", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 0, text = "Slow MA", text_color=color.white)
table.cell(table_id = testTable, column = 2, row = 0, text = "Fast MA", text_color=color.white)
table.cell(table_id = testTable, column = 0, row = 1, text = "EURUSD", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 1, text = "69", bgcolor=color.white)
table.cell(table_id = testTable, column = 2, row = 1, text = "29", bgcolor=color.yellow)
table.cell(table_id = testTable, column = 0, row = 2, text = "BTCUSD", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 2, text = "44", bgcolor=color.white)
table.cell(table_id = testTable, column = 2, row = 2, text = "13", bgcolor=color.yellow)
table.cell(table_id = testTable, column = 0, row = 3, text = "SP500", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 3, text = "197", bgcolor=color.white)
table.cell(table_id = testTable, column = 2, row = 3, text = "4", bgcolor=color.yellow)
table.cell(table_id = testTable, column = 0, row = 4, text = "GOLD", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 4, text = "198", bgcolor=color.white)
table.cell(table_id = testTable, column = 2, row = 4, text = "30", bgcolor=color.yellow)
table.cell(table_id = testTable, column = 0, row = 5, text = "OIL", text_color=color.white)
table.cell(table_id = testTable, column = 1, row = 5, text = "110", bgcolor=color.white)
table.cell(table_id = testTable, column = 2, row = 5, text = "34", bgcolor=color.yellow)
Published by Zer3192 on the FMZ strategy square · last modified 2022-08-28. 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/Moving-Average-Cross.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
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m