Login / Join

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

https://sahmeto.com/message/3308028

How to develop a simple Buy&Sell strategy using Pine Script

Neutral
Price at Publish Time:
$83,649.54
،Technical،thequantscience

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 description The 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 code Now 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 extrapolation 2) Researching condition and data filtering 3) Trading execution 1. GENERAL PARAMETERS OF THE STRATEGY First 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 = true Specify that a percentage of the equity will be used for each trade. Pine Script® default_qty_type = strategy.percent_of_equity Specify percentage quantity to be used for each trade. Will be 5%. Pine Script® default_qty_value = 5 Choose the backtesting currency. Pine Script® currency = currency.EUR Choose the capital portfolio amount. Pine Script® initial_capital = 10000 Let's define percentage commissions. Pine Script® commission_type = strategy.commission.percent Let's set the commission at 0.07%. Pine Script® commission_value = 0.07 Let's define a slippage of 3. Pine Script® slippage = 3 Calculate data only when the price is closed, for more accurate output. Pine Script® process_orders_on_close = true 2. DATA EXTRAPOLATION In 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 CONDITIONS Now 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 EXECUTION At 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. DESIGN In 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 lines The 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.

Source Message: TradingView
Signals
Top Traders
Feed
Alerts