Turtles strategy
Demonstrate how to increase positions in pine language
Demonstrate how to increase positions in pine language
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | true | RiskRatio |
| v_input_2 | 20 | ATRLength |
| v_input_3 | 20 | BOLength |
| v_input_4 | 55 | FSLength |
| v_input_5 | 10 | TELength |
| v_input_6 | true | ContractUnit |
| v_input_7 | 0.01 | MinStock |
| v_input_8 | true | LastProfitableTradeFilter |
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.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Turtles-strategy.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
- Breakout Strategy Based on Turtle TradingPine Script · Breakout · 1d
- Donchian Channel Breakout StrategyPine Script · Breakout · 4h
- Donchian Channel Trend Following StrategyPine Script · Breakout · 1h
- Donchian Channel and Larry Williams Large Trade Index StrategyPine Script · Breakout · 1m
- Momentum Breakout Trading Strategy 435896Pine Script · Breakout · 1d
- Multi Indicator Trend Breakout Strategy with Dynamic Stop LossPine Script · Breakout · 1d