
This strategy is a simple position holding strategy based on SMA lines. It goes long when the short term SMA line crosses over the long term SMA line, and closes position when the short term SMA line crosses below the long term SMA line.
The strategy uses two SMA lines, one short term 20-day line and one long term 50-day line. The short term line can catch price trend changes faster, while the long term line filters out short term noise. When the short term line rises quickly above the long term line, it indicates the trend may have started a long term upturn, so we go long here. When the short term line drops below the long term line, it suggests the uptrend may have ended, so we close position here.
In summary, this strategy utilizes the curve features of SMA lines to determine price movement trends on two time dimensions, and makes stable profits with relatively steady position holding.
The advantages of this strategy include:
The risks of this strategy include:
This strategy can be further optimized in the following aspects:
In summary, this SMA position holding strategy is stable, simple and easy to operate, suitable for beginner live trading. As algo trading keeps evolving, this strategy can incorporate more indicators and techniques for better performance.
/*backtest
start: 2022-12-11 00:00:00
end: 2023-12-17 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy('Zlema Strateg Long 5m', overlay=true )
// FUNCTIONS
Atr(p) =>
atr = 0.
Tr = math.max(high - low, math.max(math.abs(high - close[1]), math.abs(low - close[1])))
atr := nz(atr[1] + (Tr - atr[1]) / p, Tr)
atr
// ZLEMA
length = input(title='Length', defval=14)
highlightMovements = input(title='Highlight Movements ?', defval=true)
src = input(title='Source', defval=close)
lag = math.floor((length - 1) / 2)
zlema = ta.ema(src + src - src[lag], length)
zlemaColor = highlightMovements ? zlema > zlema[1] ? color.green : color.red : #6d1e7f
plot(zlema, title='ZLEMA', linewidth=2, color=zlemaColor, transp=0)
// TAKE PROFIT AND STOP LOSS
long_tp1_inp = input.float(1, title='Long Take Profit 1 %', step=0.1) / 100
long_tp1_qty = input.int(10, title='Long Take Profit 1 Qty', step=1)
long_tp2_inp = input.float(5, title='Long Take Profit 2%', step=0.1) / 100
long_tp2_qty = input.int(50, title='Long Take Profit 2 Qty', step=1)
long_take_level_1 = strategy.position_avg_price * (1 + long_tp1_inp)
long_take_level_2 = strategy.position_avg_price * (1 + long_tp2_inp)
// Stop Loss
multiplier = input.float(2.2, 'SL Mutiplier', minval=1, step=0.1)
ATR_period = input.int(17, 'ATR period', minval=1, step=1)
// Strategy
entry_long = zlema > zlema[1]
entry_price_long = ta.valuewhen(entry_long, close, 0)
SL_floating_long = entry_price_long - multiplier * Atr(ATR_period)
exit_long = zlema < zlema[1]
///// BACKTEST PERIOD ///////
testStartYear = input(2022, 'Backtest Start Year')
testStartMonth = input(1, 'Backtest Start Month')
testStartDay = input(1, 'Backtest Start Day')
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)
testStopYear = input(9999, 'Backtest Stop Year')
testStopMonth = input(12, 'Backtest Stop Month')
testStopDay = input(31, 'Backtest Stop Day')
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
if testPeriod()
strategy.entry('long', strategy.long, comment='Long', when=entry_long)
strategy.exit('TP1', 'long', qty_percent=long_tp1_qty, limit=long_take_level_1) //, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)
strategy.exit('TP2', qty_percent=long_tp2_qty, limit=long_take_level_2) //, trail_points=entry_price_long * long_trailing / syminfo.mintick, trail_offset=entry_price_long * long_trailing / syminfo.mintick)
strategy.close('long', when=exit_long, comment='exit long')
// LONG POSITION
plot(strategy.position_size > 0 ? long_take_level_1 : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='1st Long Take Profit')
plot(strategy.position_size > 0 ? long_take_level_2 : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='2nd Long Take Profit')
plot(strategy.position_size > 0 ? SL_floating_long : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Stop Loss')
if testPeriod()
strategy.entry('long', strategy.long, comment='Long', when=entry_long)
// LONG POSITIONplot(strategy.position_size > 0 ? SL_floating_long : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Stop Loss')