Are You Tired of Buy and Hold? Try This EMA Trading Bot Instead

ema bot chatgpt

In this article, I will show you the simple EMA trading strategy that I had ChatGPT create, based on the conditions I wanted to see on the chart. It took me about 30 minutes to create this; it wasn’t just a copy-paste. I had to repeat the process with ChatGPT several times to achieve this result.

If we simply use a basic EMA crossover here, there are too many false signals, and it’s not profitable. I experimented with different conditions to reach this result. The win rate isn’t very high, unlike other bots I’ve created that have higher returns and win rates. But that’s okay. I just wanted to show you how it’s done and give you an idea of the limitations of the EMA crossover or simple moving average.

Where is it best to use, and where is it not?

By the way, for those waiting for Part 2 of our crypto trading bot tutorial on how to connect it to an exchange, I haven’t finished it yet. But the plan is to teach you how to use Python. There are many ways to set up alerts on TradingView and then use third-party providers or interactive brokers to automate the buying and selling of the trading bot on the exchange. However, there are subscriptions involved, so if we don’t want to subscribe to third-party providers, we can simply use Python and the exchange’s API like Binance.

Just like what I’m doing here, I’m using Visual Studio and running a Python bot. I currently have a position on Binance. I’m still in the testing period because I’ve already written about 300 lines of code or had ChatGPT create them for me. And I’m still polishing some bugs and errors.

As you can see, it’s currently running and waiting for the take profit or stop loss to be hit. Once that’s reached, it will continue running to look for new signals to enter either a long or short position. But for now, I’ll just teach you how to automate your buy and sell orders on the exchange for simplicity. The difficult part here is setting up Python and the required libraries.

But the rest is relatively easy. Anyway, it’s still a work in progress.

So this is a simple Exponential Moving Average crossover strategy. It’s profitable for the 1-hour and daily time frames but not profitable for lower time frames like 15 minutes or below, as this strategy produces negative results. It’s best to use in a trending market but not in a sideways or trading range market because that’s how it’s designed for swing trading.

What did I ask ChatGPT to do?

I told ChatGPT to create an EMA crossover trading strategy with adjustable settings so that I can control it to buy when there’s a crossover and sell when there’s a cross-under. Very simple.

But using only those two indicators doesn’t give us good results. It shows mostly red, not even able to make a $1 profit in 200 trades. So what I did next was to add the ATR to control the stop loss based on volatility, but the results didn’t change. And because this strategy doesn’t perform well in a trading range, where there are many false signals due to numerous crossovers and cross-unders, I told ChatGPT that the bot shouldn’t enter trades when the EMA is flat or in a trading range.

And ChatGPT provided me with adjustable settings for this, allowing me to control the threshold for a flat EMA. By increasing it, the number of trades within the trading range decreases. However, we need to exercise control because if we go too high, we might miss out on profitable trades that occur within the trading range, which is not our intention.

Even after implementing these adjustments, the

results were still not satisfactory for me. So I added another condition: the bot should only enter a trade if there is a higher low and a lower high.

For example, if there is a crossover between the EMAs, the price needs to rise, and the bot won’t enter until a higher low is formed. If you’re not familiar with this concept, it’s a simple action trading, which I have covered before on this channel. ChatGPT provided me with the settings for this as well, making it adjustable. I set it at 3, which represents the distance of the candle or bar. If I set it too high, profitability decreases.

So we need to fine-tune these settings, and that’s the purpose of backtesting. It’s based on past data, giving us something to base our decisions on. Some might ask if I have used this in live trading. The answer is no, but if I had used it three months ago and followed this strategy religiously, I would have achieved a net profit of 33%. Of course, we can’t future test or predict what will happen in the future. No one knows what the future holds, so we can’t seek certainty in trading and investing. We are dealing with uncertainty, no matter how great our strategy is.

Such questions often come from newbies who are searching for the holy grail in trading, which doesn’t exist.

Anyway, the settings we see here play a significant role in our trading strategy. They also depend on the trading pair. For example, BTCUSD on Binance might yield different results than BTCUSD on another exchange. So when you’re backtesting, make sure to use data from the same exchange where you’ll be trading. This strategy has been tested only on BTCUSD, and it may not be profitable for other trading pairs. Maybe we need to adjust the settings for those.

Now let me show you what the bot does based on the conditions we implemented in the script.

Here, remember our condition: Enter a trade when there’s a crossover, a higher low for a long position, and a lower high for a short position.

The bot entered a bit early here, but it depends on the settings adjustment. And this is the most profitable configuration. The bot entered based on the condition, and its stop loss is set below based on the ATR (Average True Range). I didn’t plot the line for that with ChatGPT, but it’s okay. It exited here because there was a cross under and a lower high, which aligns with our settings. And this is what I mentioned earlier about the issue with this type of strategy—crossovers and cross-unders within a trading range.

Like here, the bot bought because of a crossover but exited immediately because of a cross under. The same happened here. These are the limitations of the trading strategy. That’s why the win rate is only 31%. But what’s important here is profitability, which others don’t understand. We can have an 80% win rate trading strategy, but it might result in a negative profit. This occurs when we have more wins with low returns and a few but large losses.

For example, with one of my other bots, the win rate is 62% for a lower time frame, which is high, but the profits are low.

When creating a trading strategy, we need to make a choice. We can either go for a high win rate with low returns or a lower win rate with high returns.

You can use this strategy even without a trading bot. You can mirror the trades manually, especially if you’re trading on Binance. You’ll have a script that guides your actions.

If you want to play around with the script, here’s the code:

//@version=4
strategy("EMA Cross Strategy with ATR SL", overlay = true)

// User inputs for EMA periods, TP and SL levels
emaShortPeriod = input(19, title = "Short Term EMA Period")
emaLongPeriod = input(32, title = "Long Term EMA Period")
multiplier = input(2, title = "ATR Multiplier")
atrLength = input(14, title = "ATR Period")
threshold = input(3, title = "Threshold for flat EMA")
bars_back = input(3, title = "Bars Back for HL and LH Pattern")

// Calculating EMAs
emaShort = ema(close, emaShortPeriod)
emaLong = ema(close, emaLongPeriod)

// Calculate ATR
atr = atr(atrLength)

// Checking for higherLow and lowerHigh conditions
higherLow = low > low[bars_back]
lowerHigh = high < high[bars_back]

// Checking for EMA cross-over with higher low formation for entry
bullCondition = crossover(emaShort, emaLong) and higherLow and emaLong - emaLong[1] > threshold

// Checking for EMA cross-under with lower high formation for entry
bearCondition = crossunder(emaShort, emaLong) and lowerHigh and emaLong[1] - emaLong > threshold

// If bullCondition is met, a long order is placed
if (bullCondition)
    strategy.entry("Buy", strategy.long)
    
// If bearCondition is met, a short order is placed
if (bearCondition)
    strategy.entry("Sell", strategy.short)

// Exit conditions
exitLong = crossunder(emaShort, emaLong) and lowerHigh
exitShort = crossover(emaShort, emaLong) and higherLow

// If exit conditions are met, close the respective positions
if (strategy.position_size > 0 and exitLong) // Long position
    strategy.close("Buy")
if (strategy.position_size < 0 and exitShort) // Short position
    strategy.close("Sell")

// Plotting EMAs on the chart
plot(emaShort, title = "Short Term EMA", color = color.red)
plot(emaLong, title = "Long Term EMA", color = color.blue)

However, please note that this is not financial advice, and there is a risk of losing money if you don’t know what you’re doing. I mentioned earlier that the win rate is only 31%, which means there are more losses than wins among the 200 trades.

For those interested in using this code on their TradingView account, you can simply paste it into your TradingView PineScript editor. If you haven’t watched my previous video on creating a trading bot, I recommend you watch it first to learn how to develop your own strategy with the help of ChatGPT.

I have tried several community script codes, but I wasn’t satisfied with the output. It’s still better to use a strategy that we are familiar with and know how to use in actual trading.

Because that’s the thing, we can’t code a bot if we don’t have a good understanding of trading strategy or simple technical analysis. We need to explain what we want to see on the chart to ChatGPT. Algorithmic trading is not for beginners. If you’re new to trading and don’t understand price action, volume, how moving averages, RSI, MACD, and candlestick patterns work, it will be difficult for you to create your own trading bot.

I suggest starting from the basics. We have a basic trading course on this channel, as well as various trading strategies that have been discussed. I have been writing about trading strategies on altcoinpinoy.com since 2019 when the Altcoin Pinoy YouTube channel didn’t exist yet. I have written many strategies, some of which I might have forgotten.

Anyway, if you have learned something from this article, please share it with your friends. Happy trading!

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart