Technical analysis by thequantscience about Symbol BTC on 3/4/2025

In this article, will explain how to develop a simple backtesting for a Buy&Sell trading strategy using Pine Script language and simple moving average (SMA).Strategy descriptionThe strategy illustrated works on price movements around the 200-period simple moving average (SMA). Open long positions when the price crossing-down and moves below the average. Close position when the price crossing-up and moves above the average. A single trade is opened at a time, using 5% of the total capital.Behind the codeNow let's try to break down the logic behind the strategy to provide a method for properly organizing the source code. In this specific example, we can identify three main actions:1) Data extrapolation2) Researching condition and data filtering3) Trading execution1. GENERAL PARAMETERS OF THE STRATEGYFirst define the general parameters of the script. Let's define the name.Pine Script®"Buy&Sell Strategy Template [The Quant Science]"Select whether to show the output on the chart or within a dashboard. In this example will show the output on the chart.Pine Script®overlay = trueSpecify that a percentage of the equity will be used for each trade.Pine Script®default_qty_type = strategy.percent_of_equitySpecify percentage quantity to be used for each trade. Will be 5%.Pine Script®default_qty_value = 5Choose the backtesting currency. Pine Script®currency = currency.EURChoose the capital portfolio amount.Pine Script®initial_capital = 10000Let's define percentage commissions.Pine Script®commission_type = strategy.commission.percentLet's set the commission at 0.07%.Pine Script®commission_value = 0.07Let's define a slippage of 3.Pine Script®slippage = 3Calculate data only when the price is closed, for more accurate output.Pine Script®process_orders_on_close = true2. DATA EXTRAPOLATIONIn this second step we extrapolate data from the historical series. Call the calculation of the simple moving average using close price and 200 period bars. Pine Script®sma = ta.sma(close, 200)3. DEFINITION OF TRADING CONDITIONSNow define the trading conditions.Pine Script®entry_condition = ta.crossunder(close, sma)The close condition involves a bullish crossing of the closing price with the average. Pine Script®exit_condition = ta.crossover(close, sma)4. TRADING EXECUTIONAt this step, our script will execute trades using the conditions described above. Pine Script®if (entry_condition==true and strategy.opentrades==0) strategy.entry(id = "Buy", direction = strategy.long, limit = close) if (exit_condition==true) strategy.exit(id = "Sell", from_entry = "Buy", limit = close)5. DESIGNIn this last step will draw the SMA indicator, representing it with a red line. Pine Script®plot(sma, title = "SMA", color = color.red)Complete code below.Pine Script®//@version=6 strategy( "Buy&Sell Strategy Template [The Quant Science]", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 5, currency = currency.EUR, initial_capital = 10000, commission_type = strategy.commission.percent, commission_value = 0.07, slippage = 3, process_orders_on_close = true ) sma = ta.sma(close, 200) entry_condition = ta.crossunder(close, sma) exit_condition = ta.crossover(close, sma) if (entry_condition==true and strategy.opentrades==0) strategy.entry(id = "Buy", direction = strategy.long, limit = close) if (exit_condition==true) strategy.exit(id = "Sell", from_entry = "Buy", limit = close) plot(sma, title = "SMA", color = color.red)Expand 19 linesThe completed script will display the moving average with open and close trading signals.IMPORTANT! Remember, this strategy was created for educational purposes only. Not use it in real trading.