Skip to main content

Create Your First Strategy

This example uses a fast EMA crossing above a slow EMA as a long-entry condition. It explains the configuration and order checks required for a backtest; it does not establish that the rule is profitable.

Create the strategy and inputs​

Open Visual Pine Editor, follow its application link, sign in, and choose a strategy workflow available to your account. Screenshots below illustrate an earlier layout; consult the current manual if controls differ.

Create inputs for fast EMA length, slow EMA length, take-profit multiplier, and stop-loss multiplier. For this example, use EMA lengths of 20 and 50. These are 20 and 50 bars on the calculation timeframe; they are days only on a daily timeframe.

Earlier strategy input configuration

Add the EMAs and crossing condition​

Add fast and slow EMA calculations, bind their lengths to the inputs, and use the same source and timeframe. Define a buy condition when fast EMA crosses above slow EMA. Plot both lines so the condition can be checked visually.

Configure the entry​

Choose a long market entry triggered by that crossing. Give the entry an identifiable name, set an explicit position size, and decide whether additional same-direction entries are permitted.

A market order created at a bar close normally fills on the next available tick under TradingView's default calculation settings. The signal bar's close is not necessarily the entry fill price. Gaps and configured slippage can change the fill.

Choose the exit price basis​

The earlier screenshot-based example uses a target of 1.30 times a reference close and a stop of 0.95 times that close. If the reference close is 100, those absolute levels are 130 and 95. This arithmetic does not mean the actual position has a 30% target and a 5% stop relative to its fill.

Before setting the expressions, decide which behavior you want:

  • Signal-price levels: Capture the signal bar's close once, then derive fixed levels from it. Verify that later bars do not recalculate the reference.
  • Entry-fill levels: Derive levels from the actual position's average entry price after the script can observe the fill. Account for when the protective order is first created.
  • Tick-distance exits: Use the supported tick-distance controls when a fixed distance from entry is intended. Convert ticks to price using the instrument's minimum tick size.

Select the matching controls available in your editor and inspect the exported code. Do not substitute an expression using the current close if your intention is a fixed entry-based level. For the underlying parameters, see the strategy.exit guide.

Associate and inspect the exit​

Associate the exit with the intended entry and specify the quantity it controls. A full exit uses the full available position; multiple partial exits can reserve different portions. Plotting a level helps inspection, but does not prove that an order existed or filled there.

Earlier strategy exit configuration

Copy the final code into TradingView, compile it, and add the strategy to the chart. Review its strategy properties, including capital, sizing, commission, slippage, recalculation, and fill settings.

Check individual trades before comparing totals​

  1. Record the chart symbol, timeframe, date range, inputs, and code version.
  2. Locate a crossing and the resulting order creation time.
  3. Read the actual entry fill in the trade list and compute the intended exit levels from your chosen basis.
  4. Check when each exit order became active and whether any gap or intrabar assumption affected its fill.
  5. Evaluate out-of-sample periods and realistic costs before treating the aggregate result as evidence.

TradingView's strategy documentation explains the broker emulator. Stops and targets constrain order instructions; they do not guarantee a particular live fill or protect against every loss.