0bps

Strategy vault / Breakout

Turtles strategy

Demonstrate how to increase positions in pine language

Author
发明者量化
Complexity
Intermediate · 109 lines
Parameters
8
Backtest bar
1d · swing
Tested on
BTC/USD · spot

Demonstrate how to increase positions in pine language

Parameters

NameDefaultWhat it controls
v_input_1trueRiskRatio
v_input_220ATRLength
v_input_320BOLength
v_input_455FSLength
v_input_510TELength
v_input_6trueContractUnit
v_input_70.01MinStock
v_input_8trueLastProfitableTradeFilter

Source Pine Script

/*backtest
start: 2021-04-29 00:00:00
end: 2022-04-28 23:59:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
*/

strategy("Turtles strategy", overlay=true, pyramiding=4)
// inputs
RiskRatio = input(1)                   // % Risk Per N ( 0 - 100)
ATRLength = input(20)                  // Average fluctuation cycle ATR Length
BOLength = input(20)                   // short cycle BreakOut Length
FSLength = input(55)                   // long cycle FailSafe Length
TELength = input(10)                   // Market exit cycle Trailing Exit Length
ContractUnit = input(1)
MinStock = input(0.01)
LastProfitableTradeFilter = input(true, "LastProfitableTradeFilter")              // Use entry filtering conditions

// var
var float PreEntryPrice = na
var PreBreakoutFailure = false
var SendOrderThisBar = true
var CurrentBarIndex = -1
    
// body
if CurrentBarIndex != bar_index
    CurrentBarIndex := bar_index
    SendOrderThisBar := false
    if bar_index == 0
        PreEntryPrice := na
        PreBreakoutFailure := false

N = ta.atr(ATRLength)
plot(N, "N", join=false)
turtelUnits = math.round((strategy.equity*RiskRatio/100) /(N * ContractUnit), 3)
donchianHi = ta.highest(high[1], BOLength)
donchianLow = ta.lowest(low[1], BOLength)
fsDonchianHi = ta.highest(high[1], FSLength)
fsDonchianLo = ta.lowest(low[1], FSLength)
exitLowestPrice = ta.lowest(low[1], TELength)
exitHighestPrice = ta.highest(high[1], TELength)
ldh=plot(donchianHi, "donchianHi")
ldl=plot(donchianLow, "donchianLow")
fill(ldh, ldl)
plot(fsDonchianHi, "fsDonchianHi")
plot(fsDonchianLo, "fsDonchianLo")




if strategy.position_size == 0 and ((not LastProfitableTradeFilter) or (PreBreakoutFailure))
    if high > donchianHi and turtelUnits >= MinStock
        PreEntryPrice := high
        strategy.entry("breakout long", strategy.long, qty=turtelUnits)
        SendOrderThisBar := true
        PreBreakoutFailure := false

    if low < donchianLow and turtelUnits >= MinStock
        PreEntryPrice := low
        strategy.entry("breakout short", strategy.short, qty=turtelUnits)
        SendOrderThisBar := true
        PreBreakoutFailure := false

if strategy.position_size == 0
    if high > fsDonchianHi and turtelUnits >= MinStock
        PreEntryPrice := high
        strategy.entry("breakout long", strategy.long, qty=turtelUnits)
        SendOrderThisBar := true
        PreBreakoutFailure := false

    if low < fsDonchianLo and turtelUnits >= MinStock
        // Open position price takes the breakout of the lower band-The greater value between a price level and the lowest price, which brings the situation closer to reality and helps ensure a closer transaction is made
        PreEntryPrice := low
        strategy.entry("breakout short", strategy.short, qty=turtelUnits)
        SendOrderThisBar := true
        PreBreakoutFailure := false

if strategy.position_size > 0
    if low < ta.lowest(low[1], TELength)
        strategy.close_all()
    else
        if not na(PreEntryPrice) and turtelUnits >= MinStock
            if high >= PreEntryPrice + 0.5*N 
                PreEntryPrice := high
                strategy.entry("B1", strategy.long, qty=turtelUnits)
                SendOrderThisBar := true

        // StopLoss
        if low <= PreEntryPrice - (2 * N) and SendOrderThisBar == false
            alert("stop loss #ff0000")
            strategy.close_all()
            PreBreakoutFailure := true
else if strategy.position_size < 0
    if high > exitHighestPrice
        strategy.close_all()
    else
        if not na(PreEntryPrice) and turtelUnits >= MinStock
            if low <= PreEntryPrice - 0.5*N
                PreEntryPrice := low
                strategy.entry("S1", strategy.short, qty=turtelUnits)
                SendOrderThisBar := true

        // STOPLOSS
        if high >= PreEntryPrice + (2 * N) and SendOrderThisBar==false
            alert("stop loss #ff0000")
            strategy.close_all()
            PreBreakoutFailure := true

Published by 发明者量化 on the FMZ strategy square · last modified 2022-05-21. 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/Turtles-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