Rule-based automation

Fibonacci trading bot: define the rules before the code

A Fibonacci trading bot automates a written strategy that uses retracement or extension levels. The ratios do not create a strategy by themselves. The bot still needs objective swing detection, entry confirmation, exits, position sizing, and failure handling.

Direct answer

Start with a rule you can state without looking at the finished chart. One example is: confirm an uptrend, wait for a pullback into the 50% to 61.8% zone, require a close back above 50%, place the stop beyond the swing low, and size the trade to a fixed account risk.

Rules the bot needs before it can trade

Swing logic

Confirmed pivots or a fixed session range

Entry

Exact level plus a bar-close condition

Exit

Stop, target, timeout, and cancellation

Risk

Position size from stop distance

A testable Fibonacci bot specification

This is a baseline for research, not a recommended live strategy. Every value should be exposed as an input and tested with realistic costs.

DecisionExample ruleWhy it must be explicit
TrendClose above EMA(200)Limits long setups to one regime
SwingFive left and five right pivot barsDefines confirmation delay; freeze selected anchors separately
EntryClose back above the 50% levelDefines the trigger bar
StopBelow the confirmed swing lowDefines invalidation
TargetPrior high, then 1.272 extensionDefines the exit sequence
Risk0.5% of equity per tradeConnects stop distance to size

From chart idea to bot specification

  1. 1

    Write the market and timeframe

    A SPY daily strategy and an ETH five-minute strategy need different sessions, costs, and risk assumptions. Build one system at a time.

  2. 2

    Make the swing rule deterministic

    Use confirmed pivots, a fixed opening range, or another rule the code can calculate without hindsight.

  3. 3

    Define entry and cancellation

    State the exact level, confirmation event, and the condition that cancels an unfilled setup.

  4. 4

    Define every exit

    Include the protective stop, profit target, time exit, and what happens if the chart gaps beyond an order.

  5. 5

    Backtest before automation

    Use actual commission, spread, and slippage assumptions. Keep holdout data separate from the period used to choose settings.

Checks before generating bot code

Write the pivot-selection and confirmation rule before generating code. Include tie handling and the moment the anchors become available.

Keep the risk budget separate from the signal rule and cap quantity by buying power as well as stop distance.

Retain rejected setups, cancellations and filled trades. Evaluate each rule change on a fixed sample rather than assuming cancellations improve results.

A bot should expose its assumptions

A black-box signal can hide how swings were chosen or when a level moved. An inspectable strategy shows the anchor rule, active ratio, confirmation event, stop, target, and order timing.

Pine Script strategies simulate chart-based orders and can trigger alerts; they do not by themselves submit live broker orders. A separate execution bridge needs permissions, duplicate-order handling and risk controls. An MQL5 Expert Advisor can use trading APIs under terminal and broker permissions, but an indicator is not itself an EA.

Avoid lookahead in swing detection

A confirmed pivot needs future bars to establish that a high or low held. Code must wait for those bars before using the pivot. Plotting a pivot back on the original bar is fine for display, but the strategy cannot act as if it knew the pivot earlier.

A five-right-bar pivot is confirmed only after those five subsequent bars complete. On uninterrupted five-minute bars, this is 25 minutes after the candidate bar closes, or 30 minutes after its opening timestamp. The exact extreme occurred somewhere within that candidate bar; session breaks can make wall-clock delay longer.

  • Place orders only after the selected confirmation is available.
  • Use bar magnifier or lower-timeframe data only when the test documents it.
  • Reject fills that depend on prices unavailable under the chosen order model.

Automation does not remove trading risk

A bot can apply rules consistently and still lose money. Market regime changes, gaps, spread, latency, and bad parameter choices remain part of the system.

Start with paper trading or alerts. Compare live paper fills with the backtest before considering broker-connected execution.

Position sizing from stop distance

This illustrative example uses a $20,000 account, a 0.5% risk limit, a $510 entry, and a $505 stop.

Risk-based share ceiling = planned dollar risk / abs(entry price - stop price)

Account equity = $20,000
Risk limit = 0.5%
Risk budget = $100
Entry = $510
Stop = $505
Risk per share = $5
Maximum size before costs = 20 shares

Twenty shares cost $10,200 at the example $510 entry. Also apply buying-power, lot-size and fee constraints. Gaps can produce a realized loss larger than the planned $100 budget.

Generate an editable Fibonacci strategy

Pineify can draft a strategy for source review and TradingView simulation. Complete the unspecified order details before treating the draft as an automated trading system.

Create a Pine Script v6 strategy for long setups on SPY. Use EMA(200) as a trend filter and confirmed pivots with five left and five right bars. Draw the 50%, 61.8%, and 78.6% retracement levels from the latest confirmed upswing. Enter only after a bar closes back above 50% following a touch of the 50% to 61.8% zone. Stop below the swing low, take partial profit at the prior high, and exit the remainder at the 1.272 extension. Size each trade to 0.5% of equity and include commission and slippage inputs. Explicitly choose strict or tie-inclusive pivots and define which confirmed low/high pair is frozen for each setup. Do not backdate signals to anchor bars. Expose a tick buffer for stops, percentage quantity for the partial exit, setup expiry and timeout, and cancel unfilled orders when invalidated. Keep signals, simulated fills and external live orders separate. Cap quantity by buying power and skip cases with missing inputs or invalid stop distance.
Open the MQL5 workflow

This page is an information tool, not investment advice. Automated strategies can lose money. Backtests, paper trades, and generated code do not guarantee live execution or returns.

Common questions