0bps

Strategy vault / Trend following

Random Walk Strategy

The random walk strategy is an automated trading strategy based on random number generation. It uses a linear congruential generator to randomly generate numbers based on a set seed. When the number is greater than a…

Pine ScriptTrend following
Author
ChaoZhang
Complexity
Intermediate · 52 lines
Parameters
12
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

Overview

The random walk strategy is an automated trading strategy based on random number generation. It uses a linear congruential generator to randomly generate numbers based on a set seed. When the number is greater than a threshold it goes long, when less it goes short, implementing random entry long/short.

Strategy Logic

The main parts implementing the random trading are:

  1. Set the parameters a, c and modulus m for random number generation, as well as the initial seed.

  2. Define the random number generation function GetRandom using the linear congruential algorithm to generate random numbers between 0-m.

  3. On each candlestick, if there is no position, compare the generated random number size, go long when greater than m/2, otherwise go short.

  4. Set stop loss and take profit conditions in percentage.

  5. Set backtest period by timeframe.

Through the above steps, the strategy realizes fully random long/short operations. When random number is greater than m/2 it opens long position, otherwise it opens short, then sets stop loss and take profit to exit positions. The backtest period is customizable.

Advantage Analysis

  • Simple and clear strategy logic, easy to understand and implement.

  • Random trading effectively avoids emotional impacts and reduces subjective misoperations.

  • Customizable random number generation parameters to adjust randomness.

  • Flexible stop loss and take profit settings to control single profit/loss.

  • Supports parameters optimization through backtesting different parameters' impact on total profit.

Risk Analysis

  • Random trading may result in undefined long term trend and uncertain profitability.

  • Unable to adjust positions based on market conditions, could miss trend opportunities.

  • Limited single profit, high drawdown risk.

  • Needs reasonable stop loss/take profit ratio to avoid significant losses.

  • Frequent open/close positions due to randomness increases trading costs.

  • Sufficient backtesting required to verify reasonable parameter settings, do not blindly use.

Risks could be reduced by adding trend judgment, optimizing stop loss mechanism, strictly controlling single profit/loss etc.

Improvement Directions

  • Add trend judgment to avoid trading against trend.

  • Add position sizing based on capital change.

  • Optimize random number generation algorithm for better randomness.

  • Dynamic stop loss/take profit percentage.

  • Limit order frequency.

  • Multi-parameter backtest optimization.

Summary

The random walk strategy realizes mechanical trading through random number controlled long/short. It has strong randomness and avoids emotional impacts and subjective misoperations. But random entries may also miss trend opportunities, limited single profit, needs optimized risk control mechanisms. Overall the strategy is suitable for verifying trading ideas, evaluating parameter impacts, but careful evaluation is needed for practical use.

Parameters

NameDefaultWhat it controls
v_input_1200seed
v_input_230Stop loss percentage(0.1%)
v_input_330Take profit percentage(0.1%)
v_input_4falseCustom Backtesting Dates
v_input_52018Backtest Start Year
v_input_63Backtest Start Month
v_input_76Backtest Start Day
v_input_88Backtest Start Hour
v_input_92018Backtest Stop Year
v_input_1012Backtest Stop Month
v_input_1114Backtest Stop Day
v_input_1214Backtest Stop Hour

Source Pine Script

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

//@version=3
//@author=Tr0sT
strategy(title = "Random strategy", shorttitle = "Random", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

a = 16
c = 10
m = 1000
GetRandom(prev) =>
    GetRandom = (a * prev + c) % m

seed = input(200, minval = 2, maxval = m)
stopLoss = input(30, title = "Stop loss percentage(0.1%)")
takeProfit = input(30, title = "Take profit percentage(0.1%)")


curRandom = na
curRandom := nz(curRandom[1]) == 0 ? seed : GetRandom(curRandom[1])
if (strategy.position_size == 0)
    if (curRandom >= m / 2)
        strategy.entry("Enter", strategy.long)
    else
        strategy.entry("Enter", strategy.short)
        
    strategy.exit("Exit", "Enter", loss = close * stopLoss / 1000 / syminfo.mintick, profit = close * takeProfit / 1000 / syminfo.mintick)            

// === Backtesting Dates ===
testPeriodSwitch = input(false, "Custom Backtesting Dates")
testStartYear = input(2018, "Backtest Start Year")
testStartMonth = input(3, "Backtest Start Month")
testStartDay = input(6, "Backtest Start Day")
testStartHour = input(08, "Backtest Start Hour")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,testStartHour,0)
testStopYear = input(2018, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(14, "Backtest Stop Day")
testStopHour = input(14, "Backtest Stop Hour")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,testStopHour,0)
testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false
isPeriod = testPeriodSwitch == true ? testPeriod() : true
// === /END
if not isPeriod
    strategy.cancel_all()
    strategy.close_all()

Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-09. 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/Random-Walk-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