This content originally appeared on Level Up Coding - Medium and was authored by Besarion Turmanauli
Rapid prototyping and validation of additional strategies for smarter AI
This is the fifth article in a series of tutorials: Build AI for generating quant trading strategies automatically, you can read the previous article here:
Build AI for Generating Quant Trading Strategies in C# (Part 4)
I did my homework before writing this article and created a reinforcement learning algorithm to replicate all the processes I went through for creating and validating the algorithmic strategy for Metatrader 5.
It makes a little bit better strategy than I do, in a fraction of a time. Although in order to improve the final strategy quality, either I should provide tons of data from the chart and indicators and then run the AI for weeks, or actually build more, different and better strategies manually, then teach all those methods to AI.
Unless you have a supercomputer at your disposal… actually we all do, we can rent not only a supercomputer but a quantum computer and run python code on it after reading the documentation on how to use its python libraries, exciting times but that’s overkill. In this article, we’re going to prototype, refine and validate more strategies for different financial instruments fast to make AI faster, smarter, and more efficient.
In case you’re still wondering, here are some pictures of a working program, it comes up with a strategy we’ve built in previous articles in 4–5 minutes. As I mentioned above, AI-generated strategy is actually slightly better but what if it can become far better, isn’t it the goal?
As you can see in the console, it generated buy logic, sell logic, number of trades, total profit in pips, and profit factor, you can see imported (raw) price data below:
And generated price data calculating a moving average, RSI, ATR and other values exactly like Metatrader 5:
In this way, generated mq5 source code will give exactly the same results in Metratrader 5 strategy tester, which’s crucial because the robot we generate using AI shall run in Metatrader 5 in the end.
As I said in previous articles, I don’t know any trading platform more powerful than Metatrader 5 for building and testing algorithmic trading strategies, and I built trading robots and indicators for Metatrader 4, NinjaTrader, JForex, IB TWS (using API) before, not to mention an OTC trading platform itself. But for very rapid prototyping, there’s another platform — Tradingview that comes with an in-built strategy tester and Pinescript programming language. If we come up with a profitable strategy, we still have to validate it in Metatrader 5 but if the strategy is not profitable in Teamviewer, there’s little to no chance it will be profitable in MT5.
Let’s start with dogecoin, it’s a young unsaturated market, so lots of very simple strategies will work on it before everyone jumps in, you can see a super simple RSI crossover strategy in Pinescript:
And quick test results on DOGEUSD 15 min chart:
Let’s not forget that trading comes with fees. Binance crypto exchange, for example, has the lowest trading fees in the world, each trade will carry a standard trading fee of 0.1%. Other exchanges will usually charge you even more. You can get an additional 10% fee discount on every trade, by the way, if you register from this link.
Let’s add a 0.1% trading fee in the Tradingview strategy tester and see what happens:
Still profitable, but less:
We need some magic… not a problem:
RSI (Relative Strength Index) indicator is an RS (Relative Strength) indicator bound by 0–100 lower and upper limits. Respectively, I added a rsi_limit input that kind of limits it further to 20–80, here’s the result:
Let’s set the rsi_limit input value to 40:
rsi_limit input value = 45:
Let’s try 55:
Here’s a thing: how does one distinguish curve fitting from improved trading logic? First of all, curve fitting is a method of adjusting your function to better fit existing data points, and if you have a small number of data points (trades in this case), the chances of curve fitting occurring increase. As you can see, the more we improved the profitability (profit factor (gross profit divided by gross loss)) the number of trades decreased significantly.
Now Imagine you have just 2 trades based on specific market entry criteria, one is a winning trade and another one is a losing trade, you can filter out the losing trade by adding one more criterion created specifically for it, you now have only 1 winning trade but that doesn’t mean your strategy will produce 100% winning trades in the future.
So do you think 50 trades are enough to get an accurate picture? Well, without going into more details I can tell you directly that even 500 trades aren’t enough, and I’m not being conservative.
Although what we just did could be in major part a product of curve fitting, it’s not more so than the results we got with an initial version of the strategy, and here’s why — I specifically increased rsi_limit input step-by-step (I didn’t show every single step but that’s what I did). I didn’t just choose one random value that produced the best results, almost all subsequent increases in rsi_limit value produced better and better results, that’s a very important thing to keep in mind.
Because there are a number of other factors playing a role, we still need to confirm that our trading logic keeps producing similar results with 1000 or 1500 entries at least. Tradingview can only give us access to 1 month of data on a 15-minute chart and this is one of the reasons we need to validate the strategy in Metatrader 5 with more data.
The validation process itself has its own rules, it’s highly recommended to create and optimize trading logic for the 2011–2015 period only, for example, and then “forward-test” it on 2016–2020, this excludes negative effects of not only curve fitting but other factors like an evolution of market behavior over time. If your 2016–2020 results are almost identical to or better than your 2011–2015 results, then as long as lots of people don’t adopt the same strategy and general market behavior doesn’t change significantly, you shall expect steady returns in the future on a real account.
If you’re using a scalping / high-frequency trading (HFT) strategy, it comes with additional risks but more on that later.
Before we move to the next strategy idea, it’s worth noting that if, for example, 10 relatively simple strategies work on an unsaturated financial instrument like DOGE/USD, in this case, as you move to a more saturated instrument where more people trade with different manual or algorithmic trading strategies, liquidity will generally increase, volatility will decrease and our 10 strategies either will produce less profit and/or start producing losses altogether, you’ll need a more complex, subtle and innovative approach to beat the competition.
Let’s try a couple of moving averages now (MA Crossover strategy):
- Chart: DOGEUSD, 1 hour
- Trading fee: 0.1%
Double MA Strategy:
- Chart: DOGEUSD, 1 hour
- Trading fee: 0.1%
Same strategy, 2-hour chart:
- Chart: DOGEUSD, 2 hours
- Trading fee: 0.1%
Same strategy, 3-hour chart:
- Chart: DOGEUSD, 3 hours
- Trading fee: 0.1%
Thanks for reading!
Would you like to see some medium magic? Click and hold that clap button and check what happens :)
Would you also like lots of free stuff? Check the links down below:
These contain affiliate links, and I may receive compensation if you use them
Deploy your next app in seconds: Get $100 in cloud credits from DigitalOcean using this link: https://m.do.co/c/8c5a2698b1a2
$140 from FBS: regulated by IFSC, this broker is one of the oldest and most established institutions, operating since 2009.
Requirements:
- Register a new account with $140 on it
- Use the leverage of 1:500 to maximize your profits
- You can withdraw all profits
Available Markets: Cryptocurrencies, Stocks, CFDs, Metals, Commodities, Foreign Exchange
$30 from Tickmill: regulated by FSA, this broker operates since 2015.
Requirements:
- Register a new account with $30 on it
- Use the leverage of up to 1:500 to maximize your profits
- Withdraw the profits after 5 lots are traded
- The maximum withdrawal amount is $300
Available Markets: Stock Indices, Oil, Precious Metals, Bonds, Foreign Exchange.
$30 from Roboforex: regulated by CySEC and IFSC, Roboforex is operating since 2009 and is one of the most popular and trusted brokers among traders today.
Requirements:
- Open an account and deposit $10 to verify your payment method (can be withdrawn at any time) and get $30 as a gift
- Profits are withdrawable without limitations
- If you trade the necessary number of lots, you can withdraw the $30 too
Available Markets: Stocks (all NYSE, NASDAQ, and AMEX shares + German and Chinese listed companies), Stock CFDs (on all stocks, $1.5 per trade fee on US-listed shares), Indices, ETFs, Commodities, Metals, Energy Commodities, Cryptocurrencies, Cryptoindices, Foreign Exchange.
10% Lifetime Discount on all trades from Binance: Binance is a cryptocurrency exchange that has the lowest fees in the world supporting by far the largest variety of ways you can trade or invest in crypto:
- Spot Trading;
- Peer to Peer (P2P) Trading;
- Margin (up to 10x leverage) Trading;
- Crypto Futures Trading;
- Crypto Conversion and more…
When you invest in any cryptocurrency, you can make an additional risk-free passive income by allowing lending for margin (leveraged) trading, it comes with a powerful Online (web) platform, Windows, Mac, and Linux software, as well as Android and iOS apps + Binance API for software developers.
Binance doesn’t only have the lowest fees, but is one of the few platforms that support trading Dogecoin, and is the largest crypto exchange in terms of traded volume and cryptocurrencies allowed for trading in the world.
Deposit options include:
- Direct crypto deposit of any listed cryptocurrency in your Binance wallet;
- Buying cryptocurrency using your Credit/Debit card;
- Deposit 35 different fiat currencies via Bank Transfer (SWIFT or SEN).
And you can now get an additional 10% discount on every trade you make when you register from this link.
Have an amazing day!
Build AI for Generating Quant Trading Strategies in C# (Part 5) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Besarion Turmanauli
Besarion Turmanauli | Sciencx (2021-05-25T12:51:00+00:00) Build AI for Generating Quant Trading Strategies in C# (Part 5). Retrieved from https://www.scien.cx/2021/05/25/build-ai-for-generating-quant-trading-strategies-in-c-part-5/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.