Skip to content
Docs
Open Portal
Copy manually

Your browser did not allow direct clipboard access. Select and copy the text below.

Understand a Pine Script strategy

Add a simple strategy, find its simulated trades, and compare the signal candle with the fill candle.

You have used Pine to draw signals. A strategy also asks TradingView to simulate orders and records the results. In this lesson, you will find one simulated entry and exit and explain when each happened.

Use the BITSTAMP:BTCUSD, 1-minute, Candles chart from the earlier lessons. You need to recognise entry and exit conditions. This exercise stays inside TradingView’s simulator; it needs no connected broker account or webhook.

1

Open Pine Editor

Click Pine in the right toolbar. Open the script-name menu and choose Create new → Strategy. Keep My EMA practice saved for the indicator lessons.

What you should see

The editor is open beside your chart.

TradingView right-hand toolbar with the Pine Editor icon highlighted
Enlarge screenshot
Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

TradingView right-hand toolbar with the Pine Editor icon highlighted
Pine Editor menu points from Create new to Strategy.
Enlarge screenshot
Open the script-name menu, choose Create new (1), then Strategy (2). This keeps the indicator practice file separate.

Open the script-name menu, choose Create new (1), then Strategy (2). This keeps the indicator practice file separate.

Pine Editor menu points from Create new to Strategy.

Click Copy below. Back in TradingView, click inside the new editor, select all with Cmd+A / Ctrl+A, and paste over the starter code.

Complete long-only practice strategyPine v6 · Simulated orders
TradingView → Pine Editor Pine Script
//@version=6
strategy("Pine practice: strategy", overlay = true, initial_capital = 1000000,
default_qty_type = strategy.fixed, default_qty_value = 1, pyramiding = 0,
calc_on_every_tick = false, calc_on_order_fills = false, process_orders_on_close = false)
int length = input.int(20, "EMA length", minval = 1)
float ema = ta.ema(close, length)
bool crossedUp = ta.crossover(close, ema)
bool crossedDown = ta.crossunder(close, ema)
bool entrySignal = crossedUp and barstate.isconfirmed
bool exitSignal = crossedDown and barstate.isconfirmed
// These orders belong to TradingView's simulation, not a broker account.
if entrySignal and strategy.position_size == 0
strategy.entry("Practice long", strategy.long, disable_alert = true)
if exitSignal and strategy.position_size > 0
strategy.close("Practice long", disable_alert = true)
plot(ema, title = "EMA", color = color.blue, linewidth = 2)
plotshape(entrySignal, title = "Entry condition", style = shape.triangleup,
location = location.belowbar, color = color.blue, size = size.small, text = "Entry")
plotshape(exitSignal, title = "Exit condition", style = shape.triangledown,
location = location.abovebar, color = color.orange, size = size.small, text = "Exit")
2

Save and add it to the chart

Click the cloud-shaped Save control. Name this new file My strategy practice and click Save. Then click the triangle-shaped Add to chart control beside the script name.

What you should see

The chart lists Pine practice: strategy, with an EMA and Entry/Exit condition markers.

If the chart is crowded, hover over the earlier practice indicator’s name and use its eye control to hide it. Keep the new strategy visible.

Pine Editor save control highlighted beside Add to chart
Enlarge screenshot
Click the cloud-shaped Save control, or press Cmd+S / Ctrl+S with focus inside the editor.

Click the cloud-shaped Save control, or press Cmd+S / Ctrl+S with focus inside the editor.

Pine Editor save control highlighted beside Add to chart
Save script dialog names the new file My strategy practice.
Enlarge screenshot
Save the new file as My strategy practice (1), then click Save (2). Keep your indicator practice file separate.

Save the new file as My strategy practice (1), then click Save (2). Keep your indicator practice file separate.

Save script dialog names the new file My strategy practice.

The saved file is My strategy practice. The chart label Pine practice: strategy comes from the strategy(...) declaration. That declaration is also what distinguishes this complete script from the earlier indicator(...) examples.

3

Open the strategy's Trades view

Adding the strategy opens its report beneath the chart. Check that the report selects Pine practice: strategy. Click the table-shaped button near the report’s top left; its tooltip says Trades.

What you should see

The report heading reads List of trades, with Entry and Exit rows for closed simulated trades.

If several strategies are loaded, use the strategy-name dropdown at the top of this report to select the practice strategy first.

The strategy report shows its table-shaped Trades control and List of trades heading.
Enlarge screenshot
In the strategy report below the chart, select the table-shaped Trades control (1). The heading changes to List of trades.

In the strategy report below the chart, select the table-shaped Trades control (1). The heading changes to List of trades.

The strategy report shows its table-shaped Trades control and List of trades heading.

Choose a closed trade in List of trades. Read its Entry and Exit rows together, including their times and Size. Your dates and prices will differ from the captured example.

A closed simulated trade has separate entry and exit times, prices and a simulated size of 1.
Enlarge screenshot
Read the Entry and Exit rows together (1). Size 1 belongs to TradingView’s simulation (2); it does not set your MetaTrader lot size.

Read the Entry and Exit rows together (1). Size 1 belongs to TradingView’s simulation (2); it does not set your MetaTrader lot size.

A closed simulated trade has separate entry and exit times, prices and a simulated size of 1.
My older layout says Strategy Tester

Use that panel’s List of trades view. The task is the same: select the practice strategy and inspect one completed simulated entry and exit.

Find these parts in the code, then compare them with the selected trade:

Code What this example asks TradingView to simulate
strategy.entry("Practice long", strategy.long, ...) Open a long trade when the confirmed upward cross occurs and the simulated position is flat.
strategy.close("Practice long", ...) Close that entry when a confirmed downward cross occurs.
strategy.position_size Read the strategy’s simulated position when deciding whether to enter or close.

The matching Practice long text connects the entry and close inside this simulation. It is not a PineConnector License ID or an identifier for a MetaTrader trade.

No trades shown? Confirm the selected script is the strategy, that it compiled, and that the loaded price history contains a qualifying cross. Inspect its Settings → Properties if the simulated capital or order size was changed. A compile error is handled in Find and fix a script error.

Compare the condition candle with the fill

Section titled “Compare the condition candle with the fill”

Find an Entry triangle drawn by the script. Then locate TradingView’s simulated entry marker or the corresponding entry time in the report.

The Entry condition triangle is on one candle and TradingView simulated entry arrow is on the next candle.
Enlarge screenshot
The lower Entry triangle marks the condition. TradingView’s separate arrow on the following candle marks the simulated fill. This is a simulator result, not a broker trade.

The lower Entry triangle marks the condition. TradingView’s separate arrow on the following candle marks the simulated fill. This is a simulator result, not a broker trade.

The Entry condition triangle is on one candle and TradingView simulated entry arrow is on the next candle.

With this script’s settings, the condition is checked at the candle close. The resulting market order is normally filled at the next bar’s open. The same distinction applies to the exit. A condition marker and a simulated fill therefore need not share the same candle or price. TradingView’s order-timing explanation

Hover over Pine practice: strategy on the chart, open its Settings gear, and select Properties. In the captured current interface, Script execution is On bar close and Order execution delay is One tick. Keep those settings for this exercise.

Strategy Properties show order size 1 Quantity, Script execution On bar close and Order execution delay One tick.
Enlarge screenshot
These are simulator settings: 1 Quantity (1), On bar close (2), and One tick (3). They do not set MetaTrader volume or guarantee its fill timing.

These are simulator settings: 1 Quantity (1), On bar close (2), and One tick (3). They do not set MetaTrader volume or guarantee its fill timing.

Strategy Properties show order size 1 Quantity, Script execution On bar close and Order execution delay One tick.

The source explicitly keeps calc_on_every_tick, calc_on_order_fills and process_orders_on_close false. The current interface groups execution choices in a menu; leave additional order-fill, history-tick and realtime-tick execution unselected. Changing these choices changes the timing you are comparing.

Choose one closed trade. Identify the entry-condition candle, simulated entry, exit-condition candle and simulated exit. Then predict: if you replaced the word Entry in plotshape(...) with Hello, would the strategy start buying at a different time?

Show the answer

No. That text labels the drawing. The order conditions and strategy.entry(…) call determine the simulated action. Changing a label is different from changing a rule.

The source specifies a simulated order quantity of 1 and initial simulated capital of 1,000,000. Those are properties of this teaching example, not a suggested account balance or broker trade size. TradingView’s quantity unit depends on its symbol; it does not automatically mean one MetaTrader lot. Strategy properties