Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.0 kB
4
Indexable
Never
// This strategy is based on a moving average cross

// The length of the moving average
maLength = 20

// The lookback period for calculating the RSI
rsiLength = 14

// The overbought and oversold levels for the RSI
rsiOverbought = 70
rsiOversold = 30

// The entry and exit thresholds for the strategy
entryThreshold = 0.001
exitThreshold = 0.002

// Calculate the moving averages
maFast = sma(close, maLength)
maSlow = sma(close, maLength * 2)

// Calculate the RSI
rsi = rsi(close, rsiLength)

// Check for a moving average cross
if (maFast > maSlow)
    longPosition = true
else
    longPosition = false

// Check for an overbought or oversold RSI
if (rsi >= rsiOverbought)
    longPosition = false
else if (rsi <= rsiOversold)
    longPosition = true

// Check for entry and exit conditions
if (longPosition and (entryThreshold > 0) and (close > (entryThreshold * maFast)))
    strategy.entry("Long", strategy.long)

if (not longPosition and (exitThreshold > 0) and (close < (exitThreshold * maFast)))
    strategy.close("Long")