Skip to content

Write your first strategy

Start with one instrument and a simple rule. You will finish with a complete strategy you can read, change, and backtest.

Start with a rule

Enter: RSI is above 50 and the 50-period EMA is above the 200-period EMA.

Exit: RSI falls below 45.

These are state conditions, not crossing events. When the entry conditions are true and the strategy can open a position, it requests a long entry. An exit condition applies when that side is held.

Copy the complete strategy

In the SharkSigma editor, choose TradeScript and paste this into a strategy draft:

my_first_strategy.strat
//@version=1

symbol: TCS
run: daily
size: 5% of portfolio per instrument

entry:
    long when: rsi(14) > 50 and ema(50) > ema(200)
exit:
    long when: rsi(14) < 45

Use an instrument available to your chosen market and data source. The example uses TCS; a symbol name alone does not establish account availability.

Read the five parts

Part What it means
//@version=1 Optional: version 1 is the default. If included, keep it as the first non-comment line.
symbol: TCS Trade one instrument.
run: daily Check the strategy on a daily schedule.
size: 5% of portfolio per instrument Target a 5% allocation from this strategy's portfolio value, subject to capacity.
entry: and exit: Rules for opening and closing a long position.

Allocation is not risk. A 5% position does not mean a 5% planned loss. This example has a signal exit and no price stop. Learn about stops and targets after the basics.

Change one thing

Change 50 to 55 in the entry condition. This raises the RSI threshold. Keep the rest of the script the same so you can understand that change.

Check your draft

Save your strategy and read the editor diagnostics. Fix errors before backtesting. Check a function's arguments and meaning in the indicator library, including RSI and EMA used here.

Walk through three observations

Assume suitable history is available and a position can be opened:

Observation RSI Trend filter Result
First 52 EMA(50) above EMA(200) The long entry condition matches.
Later 48 Still above Neither rule matches: the independent holding continues.
Later 44 Either relationship The long exit condition matches.

The middle observation matters: RSI below the entry threshold does not itself close the trade. Only the authored exit or other configured protection does that. RSI exactly 50 does not satisfy > 50, and exactly 45 does not satisfy < 45.

What to keep, and what to change

Keep section colons and when: syntax. You may omit the version line; the strategy still uses version 1. Blank lines separate ideas; indentation makes rule blocks easier to read. The arguments 14, 50 and 200 are indicator periods, while the numbers after comparisons are thresholds.

When changing the instrument, check its exact identifier. When changing run:, check the data interval too: the same EMA periods describe different horizons on different bars. When changing size:, read sizing defaults rather than treating allocation as a loss limit.

Your first backtest check

Use enough valid history for the 200-period EMA and the other indicators. Look for a trade whose RSI and EMA values satisfy both entry tests. Compare its exit to RSI below 45. A run with no trades can be valid; use the no-trades checklist to distinguish restrictive rules from missing data or capacity.

Leaving out the version line

You can start directly with your instrument scope. This complete strategy uses version 1 and behaves identically if you add //@version=1 at the top:

symbol: TCS
run: each bar
size: 10 units
entry:
  long when: close > open
exit:
  long when: close < open

Existing strategies with the header need no changes. An explicit version is still checked: //@version=2 is unsupported, and //@version=one is invalid. If you include a header, put it before declarations (blank lines and comments may precede it). Omitting the header does not remove the instrument, sizing or rule checks. Canonical formatting may add the explicit version-1 header back; it has the same meaning as leaving it out.

When selecting a language in an editor or API, keep TradeScript selected. Automatic detection recognizes a headerless strategy's section syntax, but a fragment such as symbol: TCS can also be a Python type annotation. If no language is declared and the whole source is valid Python, detection keeps it as Python; declare TradeScript for an ambiguous fragment.