Sirwin
Sirwin

Superalgos // Supertrend Strategy

By 9808us | Superalgos | 17 Feb 2021


 

Let’s build a custom strategy based on Supertrend indicator

This tutorial is quite long … so grab a big cup of coffee, listen to relaxing music and run Superalgos!

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

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

1*WxcOEvh9xa2Y2ksPYlno7g.png Supertrend in action!

Today I’ll show you how to implement a trading system from scratch in Superalgos. The aim of this article is not to build a profitable strategy but to show you how to implement your own strategy!

I will guide you from the start, so whitout any furter ado let’s start!
This tutorial is divided in 2 parts:
1) Set up an indicator, for this tutorial will be Supertrend
2) Write the strategy

For this tutorial I assume you already have Superalgos up and running
GitHub https://github.com/Superalgos/Superalgos and you are using the default workspace with the default Binance BTC/USD market installed.

PART ONE // SET UP INDICATOR

This is what you will see the first time you run Superalgos, you are welcomed with a tutorial that will guide you to explore this amazing piece of software, I’m sure once you get comfortable with this huge project you will love it!

1*qwSkIQM0HxsHZ4O_YNKKoA.png Superalgos first run welcome screen

Let’s Stop the Welcome Interective Tutorial and start exploring the huge workspace. I assume that you are somehow familiar at least with the node names and hierarchies and have a basic understandig of the program.

The first trouble we must solve is that Superalgos come with a very simple workspace with minimal indicators ready to use. Indicators are present in Superalgos when you download it, but we need to add them to the workspace to make them work. Navigate Superalgos folder and find them!

1*uLlzFjcMhHRikrJrsSc_jA.png

You can find useful infos here https://docs.superalgos.org/community-contributions.html -> REMOVED! -> Now docs are available only in SA when you have it open

1*VplWVK3uGWKqOXg0sXs_iQ.png Community plugins

When you first run Superalgos you will find that it has only 1 indicator ready to use that is Resistances and Supports. For this tutorial we don’t need it so let’s detach it from the Task Manager (You can attach it again if you need it).

1*d88cji7Lrk_Hxg9iypkeag.png

Now we need to add our Supertrend indicator and make it available for our trading system. Supertrend indicator is present under Zeus Data Mines with other indicators. So let’s add Zeus to Superalgos Workspace!

1*xU1Ep8UoyViH5GBasMfxRw.png Drag to add

Depending on where you released the Data Mine, you should see something like this:

1*E-EW-3DJDV7SCdSNqRA-VA.png

Take some time to expand its offsprings and notice that it has these indicators inside:

1*wYJG-kdeZtHGouLAuDOaXw.png

Ok perfect, but to make it works we need to do some extra actions.
Expand the Market Data Tasks and click on Add Missing Data Mine Tasks

1*Bu1o-MedpCS8wIX-yl69HA.png

Expand the new Zeus Data Mine and click on Add All Tasks

1*u4OENhhaiJC9DE7hTt6KSg.png

You should now see all the indicators that are present under Zeus Data Mine

1*VJt311x0ULMp6sk2bAu2fg.png

For this tutorial we just need Supertrend, so feel free to detach indicators you don’t need. Keep in mind that every indicator need space on you HD and consume resources, so if you are setting up a Raspberry for example, keep your workspace tiny and clean!

Now that Supertrend is added to the Data Tasks we need to add it to the Data Storage and to the chart so we can see its beautiful Green and Red colors :-)

1*uybkNfC58R-SOLjm4D39sg.png

Expand Data Storage and navigate the offsprings to add missing Data Mines

1*Ie4CWL9ItiiqlAYaWXkgtg.png

When you click on Add All Data Mine Products probably you will end having doubled the already present Data Mine. This is expected as Superalgos doesn’t check what Data Mines are already present and it just add all. Delete what you don’t need!

1*EmkmONvQOgNO3jQ_LOhjdQ.png

Expand Zeus Data Mine and add what’s missing!

1*2HcTKbGNMSc9xvN40PWOZA.png

And now we need to make a reference to the charting space to have the indicator up and running in the chart!

1*IkK81EnV3VYKk-Foj2i42g.png

Add a new Timeline Chart and make a reference from the new Layer Manager to Zeus Data Mine Product

1*W8HORbzPTHYwrM2g7VRk-A.png

Once the reference is in place you can add all Mine Layers (Feel free to delete what you don’t need). Now if you open the chart space you will be surprised to see the newly added indicators! Amazing :-)

1*tVtYege220aliY7pcsn1Aw.png There is no data in the chart because we didn’t run any task jet to grab candles

Perfect, now we are ready to run data mines and start populate our beautiful charts!

1*KIhEV6cr5uMxuY5k9ypjLw.png

Be patient and let the indicators build up, then you should see the indicators ready in chart

1*fkjfb75J2GuTtebLBq1sQw.png Isn’t it beautiful :-)

Wow! You just set up a new indicator in Superalgos, made it work to grab and munch data and see it in the chart! Now that indicator is ready we can finally use it in our strategy, we can backtest it or write a strategy for a live session.
If you don’t plan to do a Live Session then you can stop the mining tasks.

Now that you have done all this hard homework I’ll tell you a secret and you will hate me :-)
If you create the Trading System first, instead of creating the data structures in each section of the Network hierarchy and then the Charting Space, you can simply uninstall and re-install the market. All data structures and references are created automatically for all data mines and trading systems present in the workspace! But in creating all the references you would understand better what’s going on under the wood and how to check if something is not working

PART TWO // STRATEGY

Now that everything is set up and running it’s time to write our new strategy!

Locate the Superalgos Project node, expand it and under Other Hierarchies click on Add Trading System

1*fIl64-6bz8XYfNCkF-31Gw.png

If after clicking on Add Trading System your workspace looks messy don’t worry, just collapse the new Trading System and drag it on a empty space

1*29evyeNQY9TDUZj5N5ZqkQ.png
Workspace is messy …. just collapse the offsprings and drag it elsewhere on the Workspace

1*GlP9RgEbo7vTI7OMtOxTUw.png Then keep this view to start working

In Superalgos a trading system is splitted in 4 stages:
1) Trigger Stage
2) Open Stage
3) Manage Stage
4) Close Stage
I will go through them all to set up a Supertrend Strategy.
(N.B. There is also a Dynamic Indicators offspring but we don’t need it for our system to work)

Please take some time and read the documentation about Trading Systems to understand better what we are doing.

Ok, now let’s make some mental definition and write down our strategy based on Supertrend. At this point we must make some premises to better understand what’s going on under the hood.
Take some time and explore the Zeus Data Mine, locate Supertrend indicator and expand it until you find Data Building Procedure and notice that Supertrend is build up with this parameters:
ATR Periods = 10
Multiplier = 3
(I’m not explaining what this parameters are … I assume you know how Supertrend indicator works!)
N.B. Parameters can be changed here, just remember to delete old data if you already run it and let the indicator build up again

1*fCV5crQZWpRGDcLSyvZDHg.png

The strategy we are going to use will be based on Supertrend and make use of these values:
1) Trend
2) ATR
Have a look at in-app documentation to understand these parameters.

And we will enter and exit the market with these assumption:
1) Time Frame 2H
2) Strategy is Long
3) Use market orders to entry/exit
4) Enter the market when Trend > 0 and previous Trend < 0
5) Stop Loss based on ATR value
6) Take Profit based on Trend change

We will write some very simple code to instruct the bot how to operate and then we will make a backtest. At the end of this article I will show you some simple implementation that can be done to improve the system.

So let’s start writing the code we need starting from the Trigger Stage and going through all the 4 steps that define a strategy.

1. TRIGGER STAGE

1*1prV6KaDYLNk9_grsyL5yA.png

Basically, what the Trigger Stage do is locking your funds and made them ready for the strategy. Sound a bit confusing? It’s not. Now we are writing a very simple strategy with very few options. But strategies can be big and complex, sometimes even compete with each other so maybe you want that part of your funds are kept for a strategy, some for another ecc..

Let’s now recap our strategy and how it starts.

  • Our strategy is based on 2H Time Frame
  • Strategy is Long
  • Enter the market when Trend > 0 and previous Trend < 0

Writing this conditions is very simple, here is the code:

chart.at02hs.superTrend.trend > 0 && chart.at02hs.superTrend.previous.trend < 0

Please note here that the code is all in ONE line!
We could use two condition splitting the code under the same Situation. When both condition validate True, bot will enter the market.
For simplicity I use only one Condition concatenating two tests with the && operator.

Best practice is to write the simplest conditions possible and split the logic in multiple conditions instead of joining with &&. This helps debugging and maintaining strategies

1*dyI9Lx3038eSTJx7gRB0Fw.png

Strategy is simple, so we just write the same code in Javascript Code under Trigger On Event and Take Position Event.
Under Trigger Off Event in Javascript Code we write ‘false’ because there are no condition that can turn off the strategy.
And that’s it, Trigger Stage is complete!

2. OPEN STAGE

1*TQqLEkeU5qp6UBjdANAGxA.png

When you start a new strategy you will find it’s populated with preconfigured parameters. We need to delete some and add our strategy conditions.
Under the Open Stage (see screenshot above) you will find:
- Initial Targets
- Open Execution
- Close Stage Event

Without any hesitation let’s delete the Close Stage Event as our strategy doesn’t need it and also the Target Size in Base Asset under Initial Targets because our strategy is Long and we are going to use our Quoted Asset balance.

This is the code to write in Formula under Initial Targets:

  • Target Rate Formula ->
    tradingEngine.current.episode.candle.close.value
  • Target Size in Quoted Asset Formula -> tradingEngine.current.episode.episodeQuotedAsset.balance.value

Now we need to tell the bot how to open an order. When I defined our strategy I said it will entry and exit the market with Market Orders. To do that we need to add an Execution Algorithm and then a Market Buy Order because strategy is Long so the bot will first buy some asset and then sell it at a better price (.. we hope!).

1*zteaVs48PurDAncrjpm1fA.png

Once you add a Market Order you will find a new offspring Simulated Exchange Events that can be used to better define some aspects of the trade. We don’t need it so just delete the offspring, see screenshot below

1*_8pB7fJKY1yfpQ1jSvSxZw.png

And in Javascript Code under Market Buy Order just write “true” to tell the bot to take position immediately.

1*E--HNnEZOX8M2gojIIP4rQ.png

Now to keep track of what the bot is doing we must reference the Market Order to a Market Order under the Low-Frequency Trading Engine.
Navigate to the Low-Frequency Tradiung Engine, expand it and add a new Market Order and reference it from the Market Order you created in the strategy

1*Vw3Xp8EgArrXrY4qrKSmUg.png
Add Market Order under Low-Frequency

1*Hh_pNqwJCkQcTJjp9Lr5gw.png
Reference the Market Buy order from the Trading System

And the Open Stage is completed, nothing else to do here!

N.B. An important note about the Low-Frequency Trading Engine.
If you make any changes to this node you will loose everything you have done once you reload Superalgos. This is because Low-Frequency is a plugin and it just load the default settings when you run Superalgos. To not loose changes you need to clone the Low-Frequency node, remove it from the Plugins node and add the cloned Low-frequncy node to the workspace.

3. MANAGE STAGE

With the fiirst two stages we instructed the bot to lock and use some funds for our strategy and we defined the way the bot should take position.
Now it’s time to tell the bot what to do once it has entered the market.
Under the Manage Stage you can define a Take Profit and a Stop Loss logic:

Managed Stop Loss Formula ->
chart.at02hs.candle.close - chart.at02hs.superTrend.atrNPeriod*2.5

Managed Take Profit Formula ->
if (chart.at02hs.superTrend.trend > 0) {} else {tradingEngine.current.episode.candle.close.value}

A bit of explanation here! With the first formula we are telling the system that our SL is based on ATR value and with the second formula we set a TP based on Trend change. In fact if the Trend is >0 (that is the condition we used to enter the market) don’t do anything, else sell at candle close value.

And the Manage Stage node is complete, nothing else to do here!

4. CLOSE STAGE

1*PO_gXYobUcZ6TFS45S6PAQ.png

Close Stage is the equivalent of the Open Stage but in opposite direction.
So let’s delete the Close Stage Event node and as opposite to Open Stage we keep the Targed Size in Quoted Asset. Then we need to add a Market Sell Order under Close Execution — Execution Algorithm. 
N.B. After creating a Market Sell Order you need to reference it to a Market Sell Order under Low.Frequncy node, the same you did before but for Sell side. So add a Market Sell Order under Low-Frequency and reference the Market Order you just created to it. Don’t forget to write true in Javascript Code!

1*IOk5sG4j6wuaOkizdPriIQ.pngWrite true here!

And write this code in Formula under Initial Targets:

  • Target Rate ->
    tradingEngine.current.episode.candle.close.value
  • Target Size in Based Asset ->
    tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value

Our trading system is complete, give it a name of your choice if you haven’t done yet and backtest it! You can create a new session or just use the one that come with Superalgos. Here I just take the Lightining Strike Trading System Reference and reference it to the new strategy, see screenshot below

1*dal_YOQfuVRFQw5a9jgoLA.png

Before attaching the Trading System Reference to nthe new Trading System, you need to unlock it by clicking the correspondenting icon, see below

1*zuQyxZ70fg1C47PkozVPYA.png

Now change some parameters under Trading Parameters because we use different time frame for our strategy.

In particular we can change the Initial and Final Date

1*4Y0c_WCZuRWVXhw0DAAhTg.png

and the Time Frame in use

We can rename the Backtesting Session to avoid possible conflicts

1*2HI1WqDLUSOOBfeHRxla8Q.png

And that’s it! Really we have done it!

Probably strategy will show some errors in the console but you can just ignore them for the moment, this tutorial is to show you how to implement a trading system, and we will solve errors in another tutorial, stay tuned!

1*E4pSkY3tulOKIEJrxwWY7w.png

Final thoughts

The strategy we have written is very simple yet quite powerful thanks to the Supertrend indicator that makes the maths for you :-)

There are several ways we can refine the strategy.
We can add condition to the entry for example so to avoid choppy market conditions. 
We can add a condition that checks if trend on higher TF is up like this

1*SthEqha26hHZV0fzxclDvQ.png

Or we can split orders and change the way SL and TP are written.
We can add others indicators, let’s say an RSI to check oversold and overbought conditions.

I showed you the way, now it’s you that can build your future :-)

Disclaimer! Remember that trading is not a simple game and you can lose very easily a lot of money if you don’t know what you are doing.
I’m not responsible for your actions :-)

Publish0x Sign-up: https://www.publish0x.com?a=xkazDE7dJ0

 

Superalgos is the most advanced crypto trading bot
you can find and it is totally free on Github

How do you rate this article?

1


9808us
9808us

I enjoy helping people and sharing stuff I know. I'm a seeker but my dreams aren't like yours


Superalgos
Superalgos

FREE AND OPEN-SOURCE CRYPTO TRADING BOTS PLATFORM Sparse thoughts & tutorials Full credits to Github repository -> https://github.com/Superalgos/Superalgos

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.