featuredImage
Posted in

How I Created a Smart Trading Strategy Using 3 EMAs and ChatGPT

3 EMA Trend Strategy Thumbnail
3 EMA Trend Strategy Thumbnail

Introduction

Trading can be tricky. But with the help of ChatGPT and TradingView, I built a simple and smart trading strategy using 3 EMAs (Exponential Moving Averages). It also includes a special stop loss that gets tighter as the trade becomes more profitable.

Let me explain how I made this strategy step by step.


My First Prompt to ChatGPT

I started with a simple idea. I wanted a strategy that enters a trade when 3 EMAs line up in a bullish trend:

  • EMA 7 is greater than EMA 21
  • EMA 21 is greater than EMA 35

This trading strategy means the price is going up strongly, and it’s a good time to buy.

I also wanted a 10% trailing stop loss. This means if the price falls 10% from the highest point after entry, the trade will close automatically.

ChatGPT prompt for strategy script

Adding More Rules to Make It Smarter

After testing the first version, I wanted to make it better. I told ChatGPT to:

  • Tighten the trailing stop to 5% if the trade makes a 10% profit
  • Tighten it further to 2% if the trade makes a 20% profit
  • Lock the stop loss so it doesn’t loosen again

This strategy adjustment helps me protect more profits as the trade becomes stronger.


Using a Pullback to Enter the Trade

Then I asked ChatGPT to add one more smart rule. Don’t just enter anytime the EMAs line up. Wait for a pullback — when the price dips below EMA 7 and then goes back up. This gives a better entry price.

Now the trading strategy waits for:

  1. EMAs lined up (7 > 21 > 35)
  2. Price dips and then jumps up

This strategy adjustment gives stronger and safer entries.


How It Looks on TradingView

Here’s a quick summary of what the strategy does:

ConditionAction Taken
EMAs aligned + pullbackEnter LONG (Buy)
Trade reaches 10% profitTrailing Stop set to 5%
Trade reaches 20% profitTrailing Stop locked at 2%
Price drops to stop levelExit Trade
NVDA performance on 3 EMA strategy

Upgrade your TradingView account to unlock full charting features and real-time strategy backtesting.

Final Thoughts

Thanks to ChatGPT, I was able to create, tweak, and test a professional strategy for trading — even without coding from scratch. It was like working with a helpful teammate who never gets tired.

Want to learn more strategies like this? Stick around at eemanispace.com — more cool stuff is coming soon!

If you want to try this strategy, you can copy the Pine Script from this article and run it in TradingView. And don’t forget to backtest it with different stocks!

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © eemani123

//@version=5
strategy("3 EMA Trend Strategy (Locks Trailing Stop Tightening)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// === INPUTS ===
ema1Len = input.int(7, title="Fast EMA")
ema2Len = input.int(21, title="Medium EMA")
ema3Len = input.int(35, title="Slow EMA")
trailStopInitial = input.float(10.0, title="Initial Trailing Stop %", minval=0.1)
trailStopTight = input.float(5.0, title="Tightened Trailing Stop %", minval=0.1)
profitTrigger = input.float(20.0, title="Profit % Trigger to Tighten Stop", minval=1.0)

// === EMA CALCULATIONS ===
ema1 = ta.ema(close, ema1Len)
ema2 = ta.ema(close, ema2Len)
ema3 = ta.ema(close, ema3Len)

// === ENTRY CONDITION ===
longCondition = ema1 > ema2 and ema2 > ema3

// === TRAILING STOP STATE ===
var float highSinceEntry = na
var float trailPrice = na
var float entryPrice = na
var bool stopTightened = false

inTrade = strategy.position_size > 0
profitPercent = inTrade and not na(entryPrice) ? (close - entryPrice) / entryPrice * 100 : 0

// === ENTRY ACTION ===
if (longCondition and not inTrade)
    strategy.entry("Long", strategy.long)
    entryPrice := na
    stopTightened := false  // reset tight stop flag

// === TRAILING STOP MANAGEMENT ===
if (inTrade)
    entryPrice := na(entryPrice) ? strategy.position_avg_price : entryPrice
    highSinceEntry := na(highSinceEntry) ? high : math.max(highSinceEntry, high)

    // Lock the tightened stop if profit hits target
    if not stopTightened and profitPercent >= profitTrigger
        stopTightened := true

    // Use the correct trail % (and stay at 5% if it was triggered)
    currentTrailPerc = stopTightened ? trailStopTight : trailStopInitial
    trailPrice := highSinceEntry * (1 - currentTrailPerc / 100)

    strategy.exit("Trailing Stop", from_entry="Long", stop=trailPrice)
else
    highSinceEntry := na
    trailPrice := na
    entryPrice := na
    stopTightened := false

// === PLOTS ===
plot(ema1, title="EMA 7", color=color.teal)
plot(ema2, title="EMA 21", color=color.orange)
plot(ema3, title="EMA 35", color=color.fuchsia)

trailColor = stopTightened ? color.yellow : color.red
plot(trailPrice, title="Trailing Stop", color=trailColor, style=plot.style_linebr, linewidth=2)

// === MARKERS ===
plotshape(longCondition and not inTrade, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", textcolor=color.white)

// === ALERTS ===
alertcondition(longCondition and not inTrade, title="Buy Alert", message="BUY Signal: 3 EMAs aligned - Strategy triggered LONG")
alertcondition(inTrade and not na(trailPrice) and close < trailPrice, title="Exit Alert", message="EXIT Triggered: Price hit trailing stop")

Watch this short video to see how I created 3 EMA Trading Strategy with Smart Trailing Stop – Powered by ChatGPT

👉 checkout ChatGPT strategy Post

Disclosure: Some of the links in this post are affiliate links. If you make a purchase through them, I may earn a small commission at no extra cost to you. Thank you for supporting my blog!



Discover more from Welcome to EEMANi'Space

Subscribe to get the latest posts sent to your email.

2 thoughts on “How I Created a Smart Trading Strategy Using 3 EMAs and ChatGPT

Leave a Reply

Advertisement
PrivacyPolicy

Discover more from Welcome to EEMANi'Space

Subscribe now to keep reading and get access to the full archive.

Continue reading