0bps

Strategy vault / Trend following

Cross Limit Order Cross Period Trading Strategy

This strategy is a trend following trading strategy that uses simple moving average to determine market trend direction and place limit orders along the moving average line to follow the trend. 1. Calculate the…

Author
ChaoZhang
Complexity
Intermediate · 63 lines
Parameters
16
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Summary

This strategy is a trend following trading strategy that uses simple moving average to determine market trend direction and place limit orders along the moving average line to follow the trend.

Strategy Logic

  1. Calculate the Simple Moving Average (SMA) and the trend direction trend.

  2. If Anti-Saw filter is enabled, uptrend is identified when lows are above SMA, downtrend is identified when highs are below SMA. If Anti-Saw filter is disabled, uptrend is identified when close is above SMA, downtrend is identified when close is below SMA.

  3. Place limit orders at SMA price according to the trend direction trend and enabled trading directions needlong and needshort:

    • If long trade is needed (needlong is true) and in uptrend, place long limit order at SMA price

    • If short trade is needed (needshort is true) and in downtrend, place short limit order at SMA price

  4. Set stop loss logic to exit positions if position direction does not match trend direction.

  5. Only trade within specified date range based on date range parameters.

Advantage Analysis

  1. Using SMA to determine trend can effectively filter market noise and lock in longer term trend.

  2. Placing limit orders at SMA price can get good entry points when trend starts.

  3. Flexibility to only go long or short according to personal trading style.

  4. Stop loss in place to avoid enlarging losses.

  5. Trading time range sets to avoid volatility around major events.

Risk Analysis

  1. SMA as trend indicator has lagging effect, may miss trend turning points and cause losses.

  2. Limit orders lack flexibility, may not get into positions due to short term trend adjustments.

  3. SMA period parameter needs proper configuration, improper settings lead to wrong trend determination.

  4. Trading session parameters need to be reasonable to avoid missing opportunities or trading in risky periods.

Optimization Directions

  1. Consider adding other indicators for multi-indicator confirmation, avoiding SMA lagging issues.

  2. Switch to market order trailing when price breaks SMA, improving tracking flexibility.

  3. Dynamically optimize SMA period to adapt to different market cycles.

  4. Set stop loss to swing low/high instead of strictly at SMA price for more flexible stops.

  5. Increase algorithmic elements for smarter dynamic trading sessions avoiding major risk events.

Summary

Overall this is a relatively simple trend following strategy, with the core idea of determining trend direction with SMA and placing limit orders at SMA price to follow the trend. With certain optimizations it can improve flexibility, adaptability and intelligence. The strategy is easy to understand and implement, suitable for algorithmic trading beginners, but requires risk awareness, cautious backtest evaluation, strict monitoring and optimization for live trading.

Parameters

NameDefaultWhat it controls
v_input_1truelong
v_input_2trueshort
v_input_3100Lot, %
v_input_4_close0MA Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_55SMA length
v_input_6falseSMA offset
v_input_7trueAnti-saw filter
v_input_8falseReverse
v_input_9trueShow MA
v_input_10falseShow background
v_input_111900From Year
v_input_122100To Year
v_input_13trueFrom Month
v_input_1412To Month
v_input_15trueFrom day
v_input_1631To day

Source Pine Script

/*backtest
start: 2022-10-27 00:00:00
end: 2023-03-12 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//Noro
//2020

//@version=4
strategy(title = "Noro's CrossLimit", shorttitle = "CrossLimit", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100.0, pyramiding = 0, commission_value = 0.0)

needlong = input(true, "long")
needshort = input(true, "short")
lotsize = input(100, defval = 100, minval = 1, maxval = 10000, title = "Lot, %")
src = input(close, defval = close, title = "MA Source")
len = input(5, defval = 5, minval = 1, title = "SMA length")
off = input(0, defval = 0, minval = 0, title = "SMA offset")
anti = input(true, defval = true, title = "Anti-saw filter")
rev = input(false, defval = false, title = "Reverse")
showma = input(true, defval = true, title = "Show MA")
showbg = input(false, defval = false, title = "Show background")
fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")

//MA
ma = sma(src, len)[off]
macol = showma ? color.blue : na
plot(ma, color = macol, linewidth = 3, transp = 0)

//Background
trend = 0
trend := anti == false and close > ma ? 1 : anti == false and close < ma ? -1 : low > ma ? 1 : high < ma ? -1 : trend[1]
bgcol = showbg ? trend == 1 ? color.lime : trend == -1 ? color.red : na : na
bgcolor(bgcol, transp = 70)

//Signals
bar = close > open ? 1 : close < open ? -1 : 0
up = (trend == 1 and rev == false) or (trend == -1 and rev == true)
dn = (trend == -1 and rev == false) or (trend == 1 and rev == true)

//Trading
size = strategy.position_size
truetime = time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)
lot = 0.0
lot := size != size[1] ? strategy.equity / close * lotsize / 100 : lot[1]
if trend != 0
    strategy.entry("Long", strategy.long, lot, limit = ma, when = needlong and truetime and up)
    strategy.entry("Short", strategy.short, lot, limit = ma, when = needshort and truetime and dn)
if size > 0 and needshort == false and trend == -1
    strategy.exit("Stop Long", "Long", limit = ma)
if size < 0 and needlong == false and trend == 1
    strategy.exit("Stop Short", "Short", limit = ma)
if time > timestamp(toyear, tomonth, today, 23, 59)
    strategy.close_all()
    strategy.cancel("Long")
    strategy.cancel("Short")

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-03. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Cross-Limit-Order-Cross-Period-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open