Let’s build the mother of all trading strategies The Moving Average Cross
I can’t wait any longer! Show me how to create a trading bot with Moving Average Cross between MA50 and MA200 on 4H Time Frame because I know this is all I need, make it work for me 24/7 and get rich!
Attention! This tutorial is based on Superalgos Beta 8! If you are running Beta 9 then you have to do some refactoring:
https://github.com/Superalgos/Superalgos/tree/develop#migrating-from-superalgos-beta-8
This article is deliberately short and quick to go right straight to the point!
Ok it’s not true, there is some to read. Do you have coffee?
This tutorial is divided in to two parts:
PART 1 ->For who has no time and just want a quick and ready to use bot without any explanations
PART 2 -> We dig a bit on how to implement a SMA Cross with some explanations and hints
PART 1
TL:TR // Disclaimer: While the devs have done a fantastic job in making SA easy and fun to use, keep in mind that it’s not a toy but a powerful tool. I know that reading and studying is boring but keep in mind that here we are talking about money, make your own considerations.
You can download the bare minimum workspace from this Gihub repository:
https://github.com/9808us/Superalgos-Strategies
!!!!!!!!!!!!!!!! ATTENTION WARNING DANGER 😂 !!!!!!!!!!!!!!!!
This tutorial you are reading is based on Beta8 BUT the workspace you download is based on Beta9 so you can safely use the latest SA version
Some users like to start from the end and asked me to make a quick handy scheme about a complete trading system with minimal fuss. So for our Hello World tutorial here you a screenshot with all the minimal formulas and conditions to use to start with your first bot. So take it, simple and quick with the bare minimal set up.
STEP 1 // Set up the Trigger Stage
STEP 2 // Set up the Open Stage
STEP 3 // Set up the Manage Stage
STEP 4 // Set up the Close Stage
CONDITIONS & FORMULAS USED
chart.at04hs.popularSMA.sma50 > chart.at04hs.popularSMA.sma200
chart.at04hs.popularSMA.previuos.sma50 < chart.at04hs.popularSMA.previous.sma200
(It’s a single line, no interruption!)
tradingEngine.current.episode.episodeQuotedAsset.balance.value
tradingEngine.current.position.entryTargetRate.value * 0.97
tradingEngine.current.position.entryTargetRate.value * 1.03
chart.at04hs.candle.close
tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value
N.B. Don’t forget to reference your Execution Algorithm
PART 2
If you want to better understand what happens under the hood then take some time with me as I’ll go step by step in creating a strategy and making some considerations about it.
Let’s say it, Superalgos is a fantastic project
Then you hate it
When you start Superalgos for the first time, you are fascinated and stare at the monitor looking at how candles are build in the chart for the first time. I really love this moment! You click Run on the Data Task node, move to the chart space and see the magic, candles start to appear in the screen!
Your mind runs imagining what you could create with all this power available! But then you stop because you don’t understand anything. In your mind there is only one thought “I want to create my own bot!” but although it is really a breeze [I’ll show you how to do it step by step] initially we remain a little thoughtful in front of all the information and complexity we see in.
For this tutorial I assume:
- You have the latest available Superalgos version
- You are in the Getting Started Workspace
- You did the Intro Tutorial to know at least the very basic concepts
Let’s do it quick and simple and prepare the environment for our Hello World Strategy. We need two steps:
1) Add Sparta Data Mine to the workspace
If you are in the Getting Started workspace, probably there is only the Master Data Mine installed, but PopularSMA indicators are under Sparta Data Mines. To add that to our workspace, locate the data mine in this subfolder:
Simply drag the Sparta Data Mine to Superalgos Workspace and drop it where you prefer. Later you can arrange your workspace as you prefer.
Note: The names of the Data Mines are irrelevant, think at them like folders you can name as you prefer. It’s the creator of the Data Mine that chose the name, but I admit they are amazing!
Ehy wait a minute! I can hear you asking:
“..but sir .. where or how can I find these indicators? Can I have a list please?” “Yes! Of Course!”
Here you: https://docs.superalgos.org/community-contributions.html
Here you can find useful infos about what indicators are available in Superalgos and in which Data Mines you can find them with a brief explanation and examples.
2) Add a new Trading System
Simply expand Superalgos Project node and under Other Hierarchies you will find the option to add a Trading System. Click on it and a new empty Trading System will appear on the workspace. Name it as you prefer to easily locate it.
And now to speed up things let’s do a little trick! Now that you have Sparta Data Mine added to the workspace and a new Trading System, we need to make them to work. Now, if you want a super detailed in-depht view of the various references that make up the magic under the hood of Superalgos, then you can read this tuorial Superalgos // Supertrend Strategy where I explain how to install an indicator manually.
But here we use the lazy way and simply do this:
- Unistall the Market
- Install the Market
Don’t worry, all your data is safu! You are not going to delete anything, so don’t worry and do it! Superalgos will take care of connecting all the dots for you and in few seconds you are ready to mine some data to feed the Sparta Data Mine so it can build the PopularSMA indicator.
Ok, before moving on, let’s make a quick recap:
- Superalgos is up to date
- We are using the standard Getting Started workspace
(Market Binance BTC/USDT) - We added a new Data Mine
(and now you know how and where to find them and put them at work) - We added a new Trading System
- Indicators are up to date -> You run the data mine!
- Make sure that the Trading System Reference under your Backtesting Session has a reference to the trading system you have just added (See screenshot below)
Tip: To make a reference, drag the node (click and hold left mouse button while dragging) near the Trading System and click right mouse button - Make sure that the Trading Engine Reference under your Backtesting Session has a reference to the Low Frequency Trading Engine
Let’s now move to the Trading System node, expand it and have look around. You should see something like this screenshot below:
To start and don’t overcomplicate things, let’s delete all the nodes we don’t need, so it’s easy to understand what we are doing. Don’t worry, we will add the deleted nodes later.
In Superalgos strategies are splitted in 4 distinct stages, you will agree that in this way we can keep things easy and mostly consistent, shaping and accustomizing our mind to work with trading systems.
Here https://docs.superalgos.org/suite-trading-systems.html you can read a lot about what is a trading system, take some time and read it as here we deal with money and even if I tend to keep things funny, loosing money because we don’t understand what we are doing is not funny at all!
What we should see now if you have deleted the unuseful nodes (for the moment!) is something like the screenshot below:
Let’s take few minutes to think here at what we are going to do.
Are you thinking?
Trigger Stage is the first stage that our trading system has to deal with.
Here we are telling the bot WHY it should open(Take) a position.
Yes it’s only in this 3 offsprings that all our trading strategy come alive!
Let’s recap our Hello World strategy:
- Strategy is based on Moving Averages Cross
-> We need the popularSMA indicator that is under Sparta Data Mine - We look at MA50 and MA200 cross
- Time Frame is 4H
- If MA50 > MA 200 let’s take position
What we are going to do now it’s a little trick, yes I love tricks!
I’m a lazy person and tend to find shortcuts everywhere
Look at this screenshot and follow what it suggests:
Why in the hell I’m doing this you probably are wondering!
Simple answer: To keep things easy so if there are any problems it will be pretty fast to locate the troubles and solve them.
A brief explanation: You should know that Conditions are Boolean that means that they can just have two possibles values: Yes or No that is True or False in developer language. So if we force the aswer what we do is skipping a step. We demand all the calcs to a single node that is the Trigger On Event and in case it evaluates True then the ball is given to the next node, the Open Stage.
We set (we write) “false” on the Trigger Off Event because at the moment we don’t have any reason to unlock our funds, we have only one strategy. And we want to keep things easy so if conditions are met then take position, isn’t it?
And that’s why we set (we write) “true” in the Take Position Event, so as soon as the Trigger Node Event validates “true” then we don’t think any more and take the position.
So now let’s translate the strategy into a language that is understandable by a bot … stupid bots 😂
We need to translate this:
“Look at the 4H TF chart and if the 50 periods simple moving average cross up the 200 periods simple moving averages then take a position”
In Javascript language (yes, if you didn’t notice Superlgos is made with love and JS) all we have to write is:
chart.at04hs.popularSMA.sma50 > chart.at04hs.popularSMA.sma200
That’s it! With a single line of code we are telling the bot that we want to take position every time MA50 is above MA200.
You can now go to the Testing Trading Tasks where you can find your backtesting session and from there run your strategy.
Make sure that your Trading Parameters make sense, for this tutorial I set:
- Time Range
{
“initialDatetime”: “2020–01–01”,
“finalDatetime”: “2020–12–31”
} - Time Frame
{
“label”: “04-hs”
}
You don’t need to set anything else for the moment, you will have time later to explore the options you can set here.
Now please run the backtest!
When backtest finishes you can stop it and move to the charting space. You should see something like this screenshot below, if not refresh F5 the browser.
You probably would not believe it but our strategy is complete!
“What?” I can hear you say “What are you saying that strategy is complete? How could it be?”.
The answer lies in the way Superalgos deals with strategy and the fact that a complete trading system is divided in to 4 distinct stages!
Now close everything, put on a pair of comfortable shoes and go for a walk and think!
Next tutorial will dig a bit more on this Trigger Stage node and the offsprings we arbitrarily set to “false” and “true” and with the next logical stage, the Open Stage! (*but has not yet been written 2021/02/01)
Disclaimer: trading is not a game. Losing money is quick and easy. I’m not giving any financial advice here. If you find any error or you think something is not right, just write me on telegram @us9808 or comment here.