How to develop a simple Buy&Sell strategy using Pine ScriptIn 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.
"Buy&Sell Strategy Template "
Select whether to show the output on the chart or within a dashboard. In this example will show the output on the chart.
overlay = true
Specify that a percentage of the equity will be used for each trade.
default_qty_type = strategy.percent_of_equity
Specify percentage quantity to be used for each trade. Will be 5%.
default_qty_value = 5
Choose the backtesting currency.
currency = currency.EUR
Choose the capital portfolio amount.
initial_capital = 10000
Let's define percentage commissions.
commission_type = strategy.commission.percent
Let's set the commission at 0.07%.
commission_value = 0.07
Let's define a slippage of 3.
slippage = 3
Calculate data only when the price is closed, for more accurate output.
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.
sma = ta.sma(close, 200)
3. DEFINITION OF TRADING CONDITIONS
Now define the trading conditions.
entry_condition = ta.crossunder(close, sma)
The close condition involves a bullish crossing of the closing price with the average.
exit_condition = ta.crossover(close, sma)
4. TRADING EXECUTION
At this step, our script will execute trades using the conditions described above.
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.
plot(sma, title = "SMA", color = color.red)
Complete code below.
//@version=6
strategy(
"Buy&Sell Strategy Template ",
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)
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.
Buyandsell
FCX Freeport-McMoRan Options Ahead of EarningsAnalyzing the options chain and the chart patterns of FCX Freeport-McMoRan prior to the earnings report this week,
I would consider purchasing the 40usd strike price Calls with
an expiration date of 2025-2-21,
for a premium of approximately $1.94.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
GME GameStop Among My Top 10 Picks for 2025 | Price TargetIf you haven`t bought GME before the previous breakout:
My price target for GME in 2025 is $43, driven by the following fundamental factors:
Transformation into a Digital Retailer:
GameStop is actively transitioning from a traditional brick-and-mortar retailer to a digital-first company. This strategic pivot includes enhancing its e-commerce platform and investing in digital gaming, which are essential for capturing the growing online gaming market. As consumers increasingly shift towards digital purchases, GameStop's ability to adapt and innovate positions it to benefit from this trend, potentially driving significant revenue growth in the coming years.
Financial Recovery and Profitability Focus:
After a challenging period, GameStop is on a path toward profitability. Analysts predict that the company will earn approximately $0.08 per share in the fiscal year ending January 2025, reflecting a positive trend in its financial performance. The company's focus on reducing excess costs and improving operational efficiencies will further enhance its bottom line. As profitability improves, investor confidence is likely to increase, supporting higher stock valuations.
Strong Market Sentiment and Stock Performance:
GameStop has demonstrated remarkable stock performance over the past year, with a return of over 110%, significantly outperforming major indices 1. This momentum has created positive market sentiment around GME, which could attract more investors looking for growth opportunities. The current trading price around $26.84 suggests that there is room for appreciation as the company continues to execute its strategic initiatives.
Strategic Partnerships and Collaborations:
GameStop's collaborations with various technology partners are opening new avenues for growth. These partnerships are aimed at enhancing customer experience and expanding product offerings, particularly in the pre-owned game category where GameStop has unique refurbishment capabilities. By leveraging these strengths, GameStop can cater to niche segments of the gaming market, further solidifying its competitive position.
S SentinelOne Options Ahead of EarningsIf you haven`t bought S before the previous breakout:
Now analyzing the options chain and the chart patterns of S SentinelOne prior to the earnings report this week,
I would consider purchasing the 30usd strike price Calls with
an expiration date of 2024-12-6,
for a premium of approximately $0.87.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
TSLA Tesla Options Ahead of EarningsIf you haven`t bought the dip on TSLA:
Now analyzing the options chain and the chart patterns of TSLA Tesla prior to the earnings report this week,
I would consider purchasing the 210usd strike price Puts with
an expiration date of 2025-9-19,
for a premium of approximately $32.75.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
Overall, I’m bullish on TSLA in the long run, so this might just be a short-term play.
MDT Medtronic plc Options Ahead of EarningsIf you haven`t bought the dip on MDT:
Now analyzing the options chain and the chart patterns of MDT Medtronic plc prior to the earnings report this week,
I would consider purchasing the 85usd strike price Calls with
an expiration date of 2024-8-23,
for a premium of approximately $1.32.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
CSCO Cisco Systems Options Ahead of EarningsIf you haven`t bought the dip on CSCO:
Now analyzing the options chain and the chart patterns of CSCO Cisco Systems prior to the earnings report this week,
I would consider purchasing the 45usd strike price Puts with
an expiration date of 2024-8-16,
for a premium of approximately $1.41.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
ZION Zions Bancorporation Options Ahead of EarningsAnalyzing the options chain and the chart patterns of ZION Zions Bancorporation prior to the earnings report this week,
I would consider purchasing the 45usd strike price Puts with
an expiration date of 2024-8-16,
for a premium of approximately $0.80.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
DKNG DraftKings Options Ahead of EarningsIf you haven`t bought DKNG before the previous earnings:
Now analyzing the options chain and the chart patterns of DKNG DraftKings prior to the earnings report this week,
I would consider purchasing the 36usd strike price Puts with
an expiration date of 2024-8-2,
for a premium of approximately $1.65.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
VIST Vista Energy Options Ahead of EarningsAnalyzing the options chain and the chart patterns of VIST Vista Energy prior to the earnings report this week,
I would consider purchasing the 60usd strike price Calls with
an expiration date of 2024-12-20,
for a premium of approximately $1.70.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
AEHR Test Systems Options Ahead of EarningsIf you haven`t bought AEHR before the rally:
nor sold the Double Top:
Then analyzing the options chain and the chart patterns of AEHR Test Systems prior to the earnings report this week,
I would consider purchasing the 15usd strike price Calls with
an expiration date of 2024-7-19,
for a premium of approximately $0.98.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
RCL Royal Caribbean Cruises Options Ahead of EarningsIf you haven`t bought RCL before the previous earnings:
Now analyzing the options chain and the chart patterns of RCL Royal Caribbean Cruises prior to the earnings report this week,
I would consider purchasing the 165usd strike price Calls with
an expiration date of 2024-10-18,
for a premium of approximately $13.95.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
NOK Nokia Options Ahead of EarningsAnalyzing the options chain and the chart patterns of NOK Nokia prior to the earnings report this week,
I would consider purchasing the 4usd strike price Calls with
an expiration date of 2024-7-19,
for a premium of approximately $0.09.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
HOOD Robinhood Markets Options Ahead of EarningsIf you haven`t bought HOOD before the previous earnings:
Then analyzing the options chain and the chart patterns of HOOD Robinhood Markets prior to the earnings report this week,
I would consider purchasing the 20usd strike price Calls with
an expiration date of 2024-9-20,
for a premium of approximately $2.22.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
OCGN Ocugen Options Ahead of EarningsIf you haven`t bought OCGN when they released their Covid vaccine:
Then analyzing the options chain and the chart patterns of OCGN Ocugen prior to the earnings report this week,
I would consider purchasing the 1.50usd strike price Calls with
an expiration date of 2024-5-17,
for a premium of approximately $0.22.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
CSCO Cisco Systems Options Ahead of EarningsIf you haven`t bought the dip on CSCO:
Then analyzing the options chain and the chart patterns of CSCO Cisco Systems prior to the earnings report this week,
I would consider purchasing the 50usd strike price Calls with
an expiration date of 2024-8-16,
for a premium of approximately $1.52.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
GME GameStop Options Ahead of EarningsIf you haven`t sold GME before the previous earnings:
Then analyzing the options chain and the chart patterns of GME GameStop prior to the earnings report this week,
I would consider purchasing the 13.50usd strike price Calls with
an expiration date of 2024-3-28,
for a premium of approximately $1.41.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
ETH Ethereum falling wedge pattern If you haven`t sold ETH at the top:
As the cryptocurrency market navigates through choppy waters, Ethereum, the second-largest cryptocurrency by market capitalization, finds itself ensnared within a bearish formation known as a falling wedge pattern. This technical setup, coupled with broader market uncertainties, casts a shadow over Ethereum’s short-term prospects, with a looming price target of $2400 becoming increasingly probable.
A falling wedge pattern typically signals a continuation of a prevailing downtrend, suggesting that Ethereum’s recent struggles may persist in the near term. Characterized by a series of lower highs and lower lows, this pattern reflects a gradual erosion of bullish momentum, often culminating in a breakdown below the wedge’s lower boundary.
DIS The Walt Disney Company Options Ahead of EarningsIf you haven`t bought the dip on DIS:
Then analyzing the options chain and the chart patterns of DIS The Walt Disney Company prior to the earnings report this week,
I would consider purchasing the 112usd strike price Puts with
an expiration date of 2024-5-10,
for a premium of approximately $2.82.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
FSLY Fastly Options Ahead of EarningsIf you haven`t sold FSLY on this head and shoulders chart pattern:
Then analyzing the options chain and the chart patterns of FSLY Fastly prior to the earnings report this week,
I would consider purchasing the 12.50usd strike price in the money Calls with
an expiration date of 2024-6-21,
for a premium of approximately $1.81.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
AMD Advanced Micro Devices Options Ahead of EarningsIf you haven`t bought AMD on the strong buy rating:
Then analyzing the options chain and the chart patterns of AMD Advanced Micro Devices prior to the earnings report this week,
I would consider purchasing the 170usd strike price Calls with
an expiration date of 2024-5-3,
for a premium of approximately $2.39.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
WDC Western Digital Corporation Options Ahead of EarningsIf you haven`t bought WDC before the previous earnings:
Then analyzing the options chain and the chart patterns of WDC Western Digital Corporation prior to the earnings report this week,
I would consider purchasing the 67.50usd strike price in the money Calls with
an expiration date of 2024-9-20,
for a premium of approximately $9.10.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.
SPOT Spotify Technology Options Ahead of EarningsIf you haven`t entered SPOT in the buy area:
nor before the previous earnings:
Then analyzing the options chain and the chart patterns of SPOT Spotify Technology prior to the earnings report this week,
I would consider purchasing the 270usd strike price Calls with
an expiration date of 2024-5-17,
for a premium of approximately $20.30.
If these options prove to be profitable prior to the earnings release, I would sell at least half of them.