Skip to content

Write a strategy in Python

Use Python when you want to express your trading rules in functions. If you prefer declarative rules, start with the TradeScript guide.

Your first strategy

Choose Python in the SharkSigma editor and paste this into a draft:

first_strategy.py
symbol = "TCS"

def on_tick():
    if len(close) < 20:
        return
    average = mean(close, 20)
    if position.is_flat and close[0] > average:
        long(10, stop=0.02, target=0.04)
    elif position.is_long and close[0] < average:
        close()

This reads 20 bars of history, compares price with their average, requests a long target of 10 units, and closes when price is below the average. It includes a 2% stop and 4% target. Use an instrument supported by your chosen market and account.

Read the structure

symbol identifies the instrument. on_tick() contains rules checked on each dispatched bar. close[0] reads the current closing price; close() requests an exit. position.is_flat and position.is_long tell you which branch makes sense.

The platform supplies close, mean, long and position inside strategy functions, so you do not import them. Keep market-data calculations inside these functions. A 20-bar guard prevents reading before enough history exists.

Check and backtest

Save the draft, read diagnostics and open SharkSigma's backtest flow with supported historical data and capital settings. Inspect trades alongside the rules. Terminal setup and the engine's synthetic runner are separate developer tools.

Next task Guide
Read price history Price data and bars
Build an indicator rule Use indicators in Python
Look up an indicator's Python arguments Python indicator library
Import additional indicators or read multiple lines More indicators with TAP
Choose quantity and exits Open and close positions
Limit trading hours Trading hours
Add protective exits Stops and targets
Pause entries after losses Risk checks
Inspect signals and decisions Plot, alert and label
Read current holdings Read positions
Choose instruments by score Rank instruments
Size numeric forecasts jointly Forecast portfolios
Calculate Alpha Lab signals Alpha Lab signals
Choose a runtime and supported instrument Execution modes
Compare parameter sets and walk forward Research experiments

Understand what runs, and when

The module declares the instrument and functions. on_tick() runs when a bar is dispatched; keep price reads, position decisions and order requests inside it. on_start() can provide tunable parameters and initial state before those decisions begin.

In the sample, the first 19 available bars return without a signal. Once history is sufficient, a flat position above the average requests ten units with protection. An existing long below the average requests an exit. A remaining long above the average does not keep adding another ten units.

Make a first edit

Change the quantity from 10 to another suitable whole-unit target, or change the lookback consistently in the guard and mean call. Keep the other rule fixed while comparing. Python percentages use fractions in order arguments: stop=0.02 means 2%; stop=2 is not that spelling.

A missing name can be a typo, an unavailable provider, or a price read outside a strategy function. Read the traceback's line and check data availability. Python syntax alone does not establish data or execution support.