August 14, 2020
Simplest coding strategies for Stop Loss and Take Profit orders
Stop Loss and Take Profit are common strategies for investors to limit their loss or to lock in their profit in trading. This blog will show you how to write Stop Loss and Take Profit orders on the Tradingview platform.
Here we will use a simple moving average strategy as an example. Let’s first look at the strategy without Stop Loss and Take Profit design. Yaonology set a fast moving average at 10 days and a slow moving average at 20 days. When the 10-day MA is higher than the 20-day MA, there is a buying point. When the 10-day MA is lower than the 20-day MA, there is a selling point.
Below is the code for the strategy:
//@version=4
//Step One: Initialization
strategy("Yaonology Moving Average Tutoring", overlay=true, default_qty_type= strategy.percent_of_equity ,default_qty_value=100, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0)
//Step Two: Parameter Setting
fst = input(title = "MA_Fast", defval = 10)
slw = input(title = "MA_Slow", defval = 20)
ma_fast = sma(close, fst)
ma_slow = sma(close, slw)
//Step Three: Plot
plot(ma_fast, color = color.blue)
plot(ma_slow, color = color.green)
//Step Four: Strategy Application
if crossover(ma_fast, ma_slow)
strategy.entry(id = "ma_long", long = true)
if crossunder(ma_fast, ma_slow)
strategy.close(id = "ma_long")
This is the plotted version of the strategy:

From this chart, the blue line is the 10-day MA, and the green line is the 20-day MA.
And the backtesting result:

* Adding Stop Loss and Take Profit to the strategy
Then we add the Stop Loss and Take Profit to the strategy. The take profit is initially set at 10% above the long entry and the stop loss is set at 5% below the long entry. You can adjust the percentages at your will. If the security rises to the take-profit point, the T/P order is executed and the position is exited for a gain. If the security falls to the stop-loss point, the S/L order is executed and the position is exited for a loss. But notice that when the short signal appears, which is the 10-day MA cross under the 20-day MA, the position will be closed even if it has not reached a take-profit point or a stop-loss point.
Below is the code:
//@version=4
//Step One: Initialization
strategy("Yaonology Take Profit & Stop Loss Tutoring", overlay=true, default_qty_type= strategy.percent_of_equity ,default_qty_value=100, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0)
//Step Two: Parameter Setting
fst = input(title = "MA_Fast", defval = 10)
slw = input(title = "MA_Slow", defval = 20)
ma_fast = sma(close, fst)
ma_slow = sma(close, slw)
// Define Long condition
longCondition = crossover(ma_fast,ma_slow)
shortCondition = crossunder(ma_fast,ma_slow)
// Define Exit Condition
tp_perc = input(title="Take_Profit_Percentage", defval=10,minval=0, maxval=100, step=0.1)/100
sl_perc=input(title="Stop_Loss_Percentage", defval=5, minval=0, maxval=100, step=0.1)/100
long_stop_level = float(na)
long_profit_level = float(na)
long_even_level = float(na)
long_stop_level := longCondition ? close*(1 - sl_perc) : long_stop_level[1]
long_profit_level := longCondition ? close*(1 + tp_perc) : long_profit_level[1]
long_even_level := longCondition ? close + 0 : long_even_level[1]
//Step Three: Plot
plot(ma_fast, color = color.blue)
plot(ma_slow, color = color.green)
//Step Four: Strategy
if longCondition
strategy.entry(id ="Buy", strategy.long)
strategy.exit (id ="long_Exit", stop=long_stop_level, limit=long_profit_level)
if shortCondition
strategy.close(id ="Buy")
This is the plotted version of the strategy:


Leave a comment and tell us what you think!
#algorithmictrading #algotrading #trading #tradingstrategy #bitcoin #sp500 #stock #traderslife #signal #tradingplan #tradingview #setalert #alert #beginner #easyinvestment #strategy #study #sellsignal #buysignal #stoploss #takeprofit #orders