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 barsPrevents moving anchors
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.

What I check before generating bot code

When I turn a SPY Fibonacci idea into code, I first write the pivot confirmation in plain language. If the sentence is ambiguous, the backtest will be ambiguous too.

I keep the risk rule separate from the signal rule. A better entry does not justify silently increasing account risk.

I test rejected setups as well as filled trades. Cancellation logic often changes the result more than adding another Fibonacci ratio.

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 is useful for chart-based testing because the generated strategy remains visible. MQL5 is suitable when the final workflow needs an Expert Advisor, but order execution and broker conditions require separate testing.

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.

Record the confirmation delay in the test. A five-right-bar pivot on a five-minute chart becomes available 25 minutes after the candidate extreme.

  • 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.

Position size = account risk in dollars / absolute entry minus stop

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

The example ignores gaps and fees. A production rule needs a buffer for both.

Generate an editable Fibonacci strategy

Pineify turns the specification into Pine Script that can be inspected, changed, and backtested before any automation decision.

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.
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