0bps

Strategy vault / Trend following

Long Term Hedging Strategy

This strategy determines asset allocation and hedging based on long-term trends. The logic is: 1. Select a base asset, moving average period and resolution 2. Compute simple moving average of the asset 3. Price…

Author
ChaoZhang
Complexity
Starter · 39 lines
Parameters
4
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

Strategy Logic

This strategy determines asset allocation and hedging based on long-term trends.

The logic is:

  1. Select a base asset, moving average period and resolution

  2. Compute simple moving average of the asset

  3. Price crossing above MA signals long-term bullishness, go long the asset

  4. Price crossing below MA signals long-term bearishness, go short the asset

  5. Can also go long-only or short-only

  6. Judge long-term trend using asset price versus its MA

  7. Take opposing position for hedging short-term fluctuations

The strategy hedges near-term risks and focuses on the asset's secular trend, allowing steady gains.

Advantages

  • Simple MA system to determine long-term trend

  • Long/short pairing effectively hedges systemic risks

  • Clear long and short signals

Risks

  • MA lags price movements

  • Holding costs of long-term positions

  • Needs risk management across multiple legs

Summary

This strategy hedges using long-term and short-term asset combinations, emphasizing risk management. But MA lag and holding costs require consideration.

Parameters

NameDefaultWhat it controls
v_input_1BMFBOVESPA:IBOVbase
v_input_25SMA Period
v_input_3MSMA Resolution
v_input_40Strategy: Long/Short | Long Only | Short Only

Source Pine Script

/*backtest
start: 2023-08-14 00:00:00
end: 2023-09-13 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_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/
// © danilogalisteu

//@version=4
strategy("Long Term L/S", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

base = input("BMFBOVESPA:IBOV")
period = input(5, 'SMA Period', input.integer)
resolution = input(title="SMA Resolution", type=input.resolution, defval='M')
strat = input(title="Strategy", defval="Long/Short", options=["Long Only", "Long/Short", "Short Only"])
strat_val = strat == "Long Only" ? 1 : strat == "Long/Short" ? 0 : -1

base_cl = security((base), resolution, close)
base_ma = sma(base_cl, period)

longCondition = crossover(base_cl, base_ma)
if (longCondition)
    if strat_val > -1
        strategy.entry("LONG", strategy.long)
    if strat_val < 1
        strategy.close("SHORT")

shortCondition = crossunder(base_cl, base_ma)
if (shortCondition)
    if strat_val > -1
        strategy.close("LONG")
    if strat_val < 1
        strategy.entry("SHORT", strategy.short)

//plot(longCondition?1:0, 'L', color.blue)
//plot(shortCondition?-1:0, 'S', color.red)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-14. 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/Long-Term-Hedging-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