Simple 3 step scalping strategy in pine script
This simple scalping strategy only uses 3 steps for trading.
- Find the trend: Using 20-day SMA, the strategy finds the price trend. The strategy requires the price to be above the 20-day SMA line.
- Right candle pattern: This strategy requires the first candle to be red, followed by two green candles.
- RSI: For entering a trade, this strategy requires the RSI score to be above 50.
This video explains the strategy in detail, with the steps of scripting the strategy with pine script. After that, The video also shows the performance of the strategy with different input parameters. Check out the video here
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © facelessfinance
//@version=5
strategy("3 step scalp", overlay=true, initial_capital = 50000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
input_trend_period = input.int(defval = 20, title = 'Moving Average', step = 10, tooltip = 'SMA')
input_rsi_length = input.int(defval = 14, title = 'RSI period', step = 1, tooltip = 'RSI Length')
input_rsi_buy_threshold = input.int(defval = 50, title = 'RSI buy singla threshold', step = 1, tooltip = 'RSI Length Threshold')
input_take_profit_percentage = input.float(defval = 0.01, title = 'Take Profit', step = 0.01, tooltip = 'Take Profit')
input_stop_percentage = input.float(defval = 0.01, title = 'Stop Loss', step = 0.01, tooltip = 'Stop Loss')
input_startTime = input.time(title = 'Start time', defval = timestamp("01 Jan 1995 13:30 +0000"), group = 'Time Filter', tooltip = 'Start date & time')
input_endTime = input.time(title = 'End time', defval = timestamp("1 Jan 2099 19:30 +0000"), group = 'Time Filter', tooltip = 'End date & time')
// date filter
filter_date = time >= input_startTime and time <= input_endTime
// Step 1: MA line
ma = ta.sma(close, input_trend_period)
ma_signal = false
if close > ma
ma_signal := true
// plot
plot(ma, title = 'SMA', color = color.yellow)
// Step 2: Candle Pattern
current_high = high , current_low = low, current_open = open, current_close = close
one_high = high[1] , one_low = low[1] , one_open = open[1] , one_close = close[1]
two_high = high[2], two_low = low[2], two_open = open[2], two_close = close[2]
three_high = high[3], three_low = low[3], three_open = open[3], three_close = close[3]
// find if the first candle is red
red_candle = false
if two_high < three_high and two_open < three_open and two_close < three_close
red_candle := true
first_green = false
if one_high > two_high and one_low > two_low and one_close > two_close
first_green := true
second_green = false
if current_high > one_high and current_low > one_low and current_close > one_close
second_green := true
candle_signal = false
if red_candle and first_green and second_green
candle_signal := true
// plot candle signal
//plotshape(candle_signal, title = 'Candle pattern', color = color.red)
// Step 3 : RSI signal
rsi_value = ta.rsi(close, input_rsi_length)
rsi_buy_signal = rsi_value >= input_rsi_buy_threshold
// buy signal
buy_signal = false
if ma_signal and candle_signal and rsi_buy_signal
buy_signal := true
// buy and sell condition
var float buy_price = 0
buy_condition = buy_signal and strategy.position_size == 0 and filter_date
take_profit_condition = strategy.position_size > 0 and close > buy_price + (buy_price * input_take_profit_percentage)
stop_loss_condition = strategy.position_size > 0 and close < buy_price - (buy_price * input_stop_percentage)
// Enter long position
if buy_condition
strategy.entry(id = 'Long', direction = strategy.long)
if buy_condition[1]
buy_price := open
// Exit position
if take_profit_condition or stop_loss_condition
strategy.close(id = 'Long', comment = (stop_loss_condition ? 'Stop Loss' : 'Take Profit'))
buy_price := na
Comments
Post a Comment