Trading Strategy with Blocks on Blueshift¶
Programming with blocks on blueshift¶
Creating a trading strategy requires a solid understanding of financial
markets as well very good knowledge of a programming language to express
your logic - like Python on Blueshift. But that comes with the burden of
mastering the syntax, rules and libraries of that language if you are not
already familiar! For traders without any programming background, this
can be a serious challenge!
Many applications try to solve this by providing a graphical interface. But real-life trading algorithms are complex, and requires much more flexibility than a step by step selections of choices and parameters. That is why when we tried to solve this problem for blueshift, we came up with Visual programming .
We adapted Blockly from Google to implement an intuitive way of creating trading strategies, without compromising flexibility. Here you arrange blocks in a logical way - just like LEGO toys - to create a trading strategy of any complexity.
All without a single line of coding!
For a quick intro, check out this tutorial:
How does it work?¶
Blockly, by Google, is one of the leading frameworks for developing visual programming platforms today. To get some quick introductions of how Blockly works, check out these fun games here.
We have adopted the framework and created a complete programming language for strategy building. This provides you the full freedom of coding, without the shackles of syntax and grammers of a regular programming language.
Blocks are unit of algorithm building that can represent either an action or a value. To develop a trading strategy, you need to arrange blocks to define all the actions your algo may take, controlled by the values of different variables during the run-time. Not all blocks behave the same way.
Blocks that represent an action look like this -
.
This types of blocks have a notch on top as well as bottom so that
actions can be stacked up one after the other.
A blocks that returns a specific value looks like this -
.
This types of blocks have a protruding notch on the left, that can fit
to another block accpeting such values.
Action blocks can also have space inside them for grouping other
actions together. A blocks can also accept other blocks that returns
values. Also blocks can be nested one inside the other, if allowed.
See an example of a trading condition below expressed by an if blocks containing
another action block to place orders; and a logic block that specify the
condition to trigger the order:
.
Once you are done, we automatically analyze and translate each block's
action to a piece of Python code. We also automatically detect and
optimize any call to fetch data. Finally we stich all these together
to create the final output that Blueshift can run. In case you want to
see the underlying code generated, click View Code button in the help
panel. To customize, simply copy the code and create a new Python strategy
file.
Do you need blocks or Python?¶
That is totally up to you! On Blueshift you can either start with Python
strategies, or block-based strategies. If you are not very familiar with
Python you would probably want to start with blocks. If you want to
learn Python it is still a good idea to start with blocks. We are sure
even some Pythonistas will find it useful to start a boilerplate strategy
with blocks and then get in to the code file to customize it further.
How to create a strategy?¶
Your work area for developing strategies with blocks have a canvas
in the middle (where you arrange the blocks), a set of menu options on
the left and a help panel on the right. Use the expand icon on
right top corner of the canvas to maximize or minimize it.
.
On the canvas, you are provided with a basic set of blocks to start. In the top
structure, you select the data-set to run your backtest, as well as other
risk parameters - max order per day to define max number of trading orders
allowed per day (reset each day), max quantity per order to define maximum
quantity per order and select (or not) long only - which will stop your
strategy going short on a security. You can also optionally set the
commissions and slippage models here.
Once these basic setup is done, you go through a 4-step process to completely define your strategies:
Step 1: Define your universe - select the list of symbols based on thedata-setyou have chose already.Step 2: Select thealphafor your strategy. Analphais usually a signal variable (like a technical indicator or a statistical measure) that your strategy requires.Step 3: Define trading rules, using the blocks on the menu, you build your trading rules. This is the heart of the strategy.Step 4: In case you want to customize your order placing, you can add set the order parameters here, likemarketorlimitorders, the target in case oflimit, as well as define anystop-lossortake-profithandling here.
If you need help while you are working on a block, click on it. You will see the help associated with the selected block available on the right hand side pane.
Running backtest or going live¶
It is no different than what you would do if you are developing either with Python or blocks.
Once you have defined your strategy with blocks, just hit the Run button
or New Backtest button to run a quick or full backtest respectively. Or
you can click Go Live to launch a new exeution of your algorithm.
Things to look out for¶
If you are using blocks and do not have previous exposure to coding, you need
keep a few things in mind. Essentially, blocks will generate what you ask
explicitly. It is like any other language (in fact it is an almost
Turing Complete
programming language on its own). The advantage of using blocks is that
you do not have to remember the syntax or libraries of Python. But your
strategy still has to be clear and unambigous. We list some points to watch
for that we think may help the first time users:
-
Use the
Alphablocks to define all variables and signals you will need. This blocks is a very good place to define and declare your variables. Use more variables to make the strategy easy to understand. -
Use the
Scheduleblocks to do things at certain interval. -
Try to add your trading rules condition in the
Rulesblocks (e.g.enter long), this makes them easier to understand later, and also keeps the number of blocks low and within visiable area! -
Be careful with your conditions. Also notice some conditions are applicable for all the assets you have chosen to trade (e.g. a trading condition). Others are applicable to a specific variable (e.g. a variable you declared in the
Alphablock, likecan_tradein the point below). Do not mix them up. Do not use a combination of these two types of conditions. They may result in ambiguous code output. -
Always check the default choices in the block - is that really what you want? Example, make sure you did not forget to change the operator from the default
+to a*while using thearithmaticblock under theMathmenu. -
Use variables to control when you can trade. Define a
can_tradevaraible in yourAlphablock and set it toTruewhenever you want your algo to be able to trade, andFalsewhen you do not want it to trade. -
Take care how you handle your trade rules. If you have a signal variable and define a rule as trade whenever the signal is greater than a value, this means the algo will constantly trigger orders as long as this condition remains true (not just once, the first time it is true). Use a
can_tradevariable to control this, or alternatively use atargettype of orders. -
Finally, be patient. Check out the template strategies and see if you follow them. It takes a while to get the hang of it, but once you do, a world of possibilities opens up! Not only you can create your own strategy as you want it, you can also view the generated
Pythoncode and learn to customize.