You are on page 1of 3

An example trading strategy in PineScript that uses the ETH/USDT pair on a 30-minute

chart. This strategy is a simple trend-following strategy that uses the moving average
crossover as the signal to enter and exit trades.

```

//@version=4

strategy("Moving Average Crossover Strategy", overlay=true)

// Define the inputs

fast_ma_len = input.int(title="Fast Moving Average Length", defval=10, minval=1)

slow_ma_len = input.int(title="Slow Moving Average Length", defval=30, minval=1)

// Define the moving averages

fast_ma = sma(close, fast_ma_len)

slow_ma = sma(close, slow_ma_len)

// Define the long and short signals

long_signal = crossover(fast_ma, slow_ma)

short_signal = crossunder(fast_ma, slow_ma)

// Define the strategy entry and exit conditions

if (long_signal)

strategy.entry("Long", strategy.long)
if (short_signal)

strategy.entry("Short", strategy.short)

// Define the strategy exit conditions

if (short_signal)

strategy.exit("Long", "Long", profit=100, loss=50)

if (long_signal)

strategy.exit("Short", "Short", profit=100, loss=50)

// Plot the moving averages

plot(fast_ma, color=color.green, title="Fast MA")

plot(slow_ma, color=color.red, title="Slow MA")

```

In this strategy, we define two inputs, `fast_ma_len` and `slow_ma_len`, which are used
to define the lengths of the fast and slow moving averages, respectively. The moving
averages are then calculated using the `sma()` function.

We then define the long and short signals based on the crossover of the fast and slow
moving averages using the `crossover()` and `crossunder()` functions.

The strategy entry and exit conditions are defined using the `strategy.entry()` and
`strategy.exit()` functions. The `strategy.long` and `strategy.short` arguments are used
to define the direction of the trades.
Finally, we plot the fast and slow moving averages using the `plot()` function.

You might also like