How to create a trading robot with your own hands? Robot-Scalper

Where to start algorithmic trading. Writing robots from scratch for trading. Training a trading robot in python from scratch. Algo trading:

My first thought about trading appeared in my 4th year at the Faculty of Economics, when I realized that it was necessary to have passive income. I started with bank deposits and mutual funds (UIFs), then fate threw me into IT. I became very interested in this, entered a technical university and set a goal to combine information technology and stock trading, as it is interesting, promising and highly paid.

I will tell you where to start mastering algorithmic trading without any experience in trading or programming. More precisely, it will be a series of articles divided into stages with a detailed description of the learning process and recommendations. The training program is suitable not only for beginners, but also for experienced programmers, because... it also involves mastering stock trading. Also, it may be of interest to traders who will gain the necessary knowledge in programming. Each stage is designed so that it does not take much time; it is possible to combine it with work or study.

The stock market is a high-tech industry that is actively developing, which makes working in this area very attractive for IT specialists. The global foreign exchange market, where daily trading volume is estimated at $5.1 trillion, is becoming less dependent on humans. Thus, 94 heads of the largest US and Canadian trading and foreign exchange companies announced their intention to switch to automation of the majority of their foreign currency transactions during 2018-2020. The development of automated trading today has already become an irreversible process in the global foreign exchange, stock and derivatives markets.

While it is difficult to say that for successful trading it is necessary to create a trading robot, you can trade manually, using automation to search for assets and entry points, or control open positions. But the share of robots in US trading volume in 2005 exceeded 50%, indicating their demand among traders.

Let's move on to the training plan.

Python will be used as the programming language to implement the strategies. This language is cross-platform (which is a mandatory attribute for supporters of Linux and macOS), easy to learn (from my experience of learning Java, I can say that Python is easier), widespread (used in Big Data, web development, test automation, science, systems administration, for prototyping quantum models in hedge funds and “quantum” trading units in banks, etc.)

Connecting the motors

The simplest way to connect motors to the Raspberry Pi is to use a ready-made controller, the choice of which depends only on the expected power of the motors. I chose with a reserve based on L298N. The search string on aliexpress “L298N motor driver board” will cost you about $3 with delivery.

Also, to power the raspberry, you will need at least one step-down voltage converter. I took it based on LM2596. The search string on aliexpress “DC-DC LM2596” will cost you about $1 with delivery.

With this connection, in order to force the machine to execute one of our commands, it is enough to set the corresponding GPIO level high.

Stage 1.1. Learning Python Basics for a Trading Robot

App training from SoloLearn.

The first step will be to install the SoloLearn application on your smartphone. It will be especially useful for those who have not programmed before. The application will teach you the very basics of the language. The undeniable advantage is that it can be used at any time and anywhere.

Taking the course on Stepik.org “Programming in Python”

Perfect for beginners, interactive, presentation flows smoothly from simple to complex, quite interesting tasks, you can watch comments from users, which is very useful if you don’t know the solution. The course is free.

Advice: Some conditions of the tasks are described very unclearly, in this regard, immediately look in the comments, there you will find an explanation of the task.

I recommend spending 1 hour a day on this resource.

Link to course: https://stepik.org/course/67/syllabus

Solving problems in Pythontutor.ru

To consolidate knowledge, the Pythontutor resource will be useful. This is an interactive Python tutorial, with lots of examples and good problems. An interesting feature is that after solving a problem, you can see possible solutions from developers and other participants.

I recommend solving at least one problem every day.

Link to tutorial: https://pythontutor.ru/

Books: Swaroop - “A Byte of Python”

"Python's Bite" is a free book on Python, characterized by a minimal amount of "water" and a large number of examples. It contains only 150 pages, but perfectly demonstrates the capabilities of the language.

New York Stock Exchange Investors and Traders Club

Tip: When reading books on programming, you should run sample code in your IDE to better understand the material.

It is enough to read 5-10 pages a day. I recommend the free PyCharm Community as an IDE.

Optional: Python video courses on YouTube

A series of video lessons with an average duration of 10 minutes, consisting of 21 episodes, will help fill gaps in previously acquired knowledge.

For those who know English, it will be very good to watch the Learn Python - Full Course for Beginners course. In total, the material consists of four and a half hours of training video .

Advice : If you don’t know English yet, then be sure to start learning it, because... the vast majority of necessary materials on algorithmic trading will be available there. However, I don’t think I discovered America here, most people already know that, I rather just wanted to remind you.

There are many methods and materials for learning English on the Internet, everyone can choose the right one for themselves, but I would not refuse to recommend a book on grammar by Raymond Murphy - Essential Grammar in Use. Written entirely in English, but in extremely simple language and with many illustrations.

Concept

Conceptually, the framework is a set of user-supported modules that implement various robotics functions. When running, Bubot builds a network of processes that can asynchronously receive and send messages among themselves. You can also build a network of robots that will communicate with each other.

The process network is built on the basis of the standard Python multiprocessing module. The messaging system and shared memory are implemented using Redis.

Each Bubot has a built-in web server, the Tornado web server, which allows you to monitor the state, control the robot, change parameters (calibrate) the robot on the fly, and also provides the ability to exchange data between robots.

Bubot is not a real-time system, although Bubot can be integrated with real-time code.

Stage 1.2. Basics of stock trading

It is also necessary to study the basics of stock trading and below I will give a couple of tips.

Exploring the functionality of Trader Workstation from Interactive Brokers:

To do this, you will need to open a demo account.

The TWS terminal has an excellent demo mode, where 1,000,000 virtual dollars are available and unlimited in time. It can be studied up and down. But first you need to learn how to simply navigate the interface, be able to search, select, buy, sell assets.

Here are some training videos:

  • Hotkeys,
  • Working with charts,
  • Indicators,
  • Glass setting,
  • How to place an order
  • How to make transactions
  • Working with bonds,
  • Watchlists & scanners.

Interactive brokers - review of the number 1 American broker (interactive broker)

It's also worth taking the time to study on your own. Do in person what you watched in the video.

Reading literature: Elder - “Trading with Dr. Elder”

I recommend starting with this book . In it, the author traditionally talks a lot about the psychology of trading. In addition, it describes the entire trading process. Elder is also good because he talks about trading in all the details, i.e. prepares for the difficulties that will need to be faced.

I recommend reading 15 pages a day.

Advice: If you have never traded and do not know how the exchange works, I strongly recommend reading the first 180 pages of the book by Tvardovsky and Parshikov - “Secrets of Exchange Trading”. To understand how the exchange works in general, types of orders, who brokers , market makers are, etc.

Automatic trading

By choosing a trading strategy, we can fully automate trading operations. To speed up the process, we use data with a discreteness of 5 seconds, instead of 1 minute, as was the case during testing. You can automate trading using one fairly compact class:

In [5]: class MomentumTrader(opy.Streamer): # 25 def __init__(self, momentum, *args, **kwargs): # 26 opy.Streamer.__init__(self, *args, **kwargs) # 27 self .ticks = 0 # 28 self.position = 0 # 29 self.df = pd.DataFrame() # 30 self.momentum = momentum # 31 self.units = 100000 # 32 def create_order(self, side, units): # 33 order = oanda.create_order(config['oanda']['account_id'], instrument='EUR_USD', units=units, side=side, ENGINE='market') # 34 print('\n', order) # 35 def on_success(self, data): # 36 self.ticks += 1 # 37 # print(self.ticks, end=', ') # appends the new tick data to the DataFrame object self.df = self.df. append(pd.DataFrame(data['tick'], index=[data['tick']['time']])) # 38 # transforms the time information to a DatetimeIndex object self.df.index = pd.DatetimeIndex (self.df['time']) # 39 # resamples the data set to a new, homogeneous interval dfr = self.df.resample('5s').last() # 40 # calculates the log returns dfr['returns '] = np.log(dfr['ask'] / dfr['ask'].shift(1)) # 41 # derives the positioning according to the momentum strategy dfr['position'] = np.sign(dfr[ 'returns'].rolling( self.momentum).mean()) # 42 if dfr['position'].ix[-1] == 1: # 43 # go long if self.position == 0: # 44 self.create_order('buy', self.units) # 45 elif self.position == -1: # 46 self.create_order('buy', self.units * 2) # 47 self.position = 1 # 48 elif dfr ['position'].ix[-1] == -1: # 49 # go short if self.position == 0: # 50 self.create_order('sell', self.units) # 51 elif self.position = = 1: # 52 self.create_order('sell', self.units * 2) # 53 self.position = -1 # 54 if self.ticks == 250: # 55 # close out the position if self.position == 1: # 56 self.create_order('sell', self.units) # 57 elif self.position == -1: # 58 self.create_order('buy', self.units) # 59 self.disconnect() # 60

The following code fragment starts the MomentumTrader class to execute. The momentum strategy is calculated based on intervals of 12 observations. The class automatically stops trading after receiving 250 blocks of data. This value is chosen arbitrarily to quickly demonstrate how the MomentumTrader class works.

In [6]: mt = MomentumTrader(momentum=12, environment='practice', access_token=config['oanda']['access_token']) mt.rates(account_id=config['oanda']['account_id'] , instruments=['DE30_EUR'], ignore_heartbeat=True)

The output below shows the individual trading operations performed by the MomentumTrader class during the demo:

{'price': 1.04858, 'time': '2016-12-15T10:29:31.000000Z', 'tradeReduced': {}, 'tradesClosed': [], 'tradeOpened': {'takeProfit': 0, ' id': 10564874832, 'trailingStop': 0, 'side': 'buy', 'stopLoss': 0, 'units': 100000}, 'instrument': 'EUR_USD'} {'price': 1.04805, 'time' : '2016-12-15T10:29:46.000000Z', 'tradeReduced': {}, 'tradesClosed': [{'side': 'buy', 'id': 10564874832, 'units': 100000}], ' tradeOpened': {'takeProfit': 0, 'id': 10564875194, 'trailingStop': 0, 'side': 'sell', 'stopLoss': 0, 'units': 100000}, 'instrument': 'EUR_USD' } {'price': 1.04827, 'time': '2016-12-15T10:29:46.000000Z', 'tradeReduced': {}, 'tradesClosed': [{'side': 'sell', 'id': 10564875194, 'units': 100000}], 'tradeOpened': {'takeProfit': 0, 'id': 10564875229, 'trailingStop': 0, 'side': 'buy', 'stopLoss': 0, 'units' : 100000}, 'instrument': 'EUR_USD'} {'price': 1.04806, 'time': '2016-12-15T10:30:08.000000Z', 'tradeReduced': {}, 'tradesClosed': [{' side': 'buy', 'id': 10564875229, 'units': 100000}], 'tradeOpened': {'takeProfit': 0, 'id': 10564876308, 'trailingStop': 0, 'side': 'sell ', 'stopLoss': 0, 'units': 100000}, 'instrument': 'EUR_USD'} {'price': 1.04823, 'time': '2016-12-15T10:30:10.000000Z', 'tradeReduced' : {}, 'tradesClosed': [{'side': 'sell', 'id': 10564876308, 'units': 100000}], 'tradeOpened': {'takeProfit': 0, 'id': 10564876466, ' trailingStop': 0, 'side': 'buy', 'stopLoss': 0, 'units': 100000}, 'instrument': 'EUR_USD'} {'price': 1.04809, 'time': '2016-12- 15T10:32:27.000000Z', 'tradeReduced': {}, 'tradesClosed': [{'side': 'buy', 'id': 10564876466, 'units': 100000}], 'tradeOpened': {}, 'instrument': 'EUR_USD'}

The image below shows the Oanda fxTrade Practice application, where we see the MomentumTrader class in action.

All results presented in this article are obtained using a demo account, which does not use real money. This account is a simulator for trial implementation of algorithmic trading. To proceed to real transactions with real money, you need to set up a full-fledged Oanda account, deposit the necessary funds, and change the account parameters in the code. The code itself does not need to be changed.

We implement the web interface

Each robot can have an unlimited number of web interfaces. In our case, to control the robot, we only need one - implementing 4 buttons, which, when pressed, will give a command, and when pressed, cancel it.

User interfaces are stored in the ui directory. Each user interface page is described in a separate subdirectory, and consists of at least 2 files:

  • [Page name].html - page markup.
  • [Page name].json - interface description. Each page (session) for the framework is essentially a separate service; this file contains a description of the events to which this page is subscribed, as well as the messages that it generates.
  • [Page name].py - (optional) may contain server logic for processing commands of this user interface, in our case it will not be useful.

Let's simplify it a little more to improve perception. In the example below, consider the one button forward algorithm. The rest can be done by analogy.

So, we create a scout_easy subdirectory in the ui directory and in it there are two files scout_easy.html and scout_easy.json with the following content (text comments).

\ui\scout_easy\scout_easy.html

Pay attention to div id=console, if it is present, then the framework will output all console messages to it, incl. server-side code errors.

bubot_socket.js - must be present on every ui page, since it is responsible for establishing a connection to the server and exchanging messages.

The connection to the server occurs via a web socket; the bubot_send_message([message name], [message parameters]) method is responsible for sending messages to the server. On the server, when a message arrives, the method of the same name is called, to which the message parameters are passed. In our case, the method sending the set_move_motor_power request is called, the framework takes the name of the message recipient service from the user interface description file, the names of these services are determined in the final section when describing the robot.

\ui\scout_easy\scout_easy.json

{ "incoming_request": { "console": { "time": {}, "message": {} } }, "outgoing_request": { "set_move_motor_power": { "name": "set_power", "buject": " move_motor", "description": "command to set the power of the main motor", "param": { "value": { "description": "motor power in percent, forward > 0, backward 0, left

The best bot for cryptocurrency trading

Absent, since there are no universal utilities that would satisfy the needs of all traders. It was noted above that each software is written not only specifically for a specific crypto exchange, but also taking into account the requirements put forward by the speculator.

In addition to customizable, scripted, arbitrage, and trading bots, there are market makers, signal tools, and other utilities. Therefore, when deciding on the choice of an automated advisor, you need to soberly understand what trading strategies are being pursued - what the final result is expected to get from using the bot.

Bots for Binance crypto exchange

Robot advisors are conventionally divided according to the organization of functionality into customizable and scripted software, as well as according to the features of application - trading or arbitrage. Moreover, each utility is written taking into account the trading preferences of traders and directly for a specific trading platform.

The Binance crypto exchange was not chosen by chance as an example - it is the largest centralized platform, a leader in liquidity, listing, has a Russian-language interface and relatively small commissions, and is popular among domestic speculators. Let's take a closer look at the utilities that are most often used on Binance.

RevenueBot

Official website: https://app.revenuebot.io/

Defining Features:

  • smart (smart) grid of orders;
  • identification of exceptionally favorable trade entries;
  • back tests;
  • automatic switching of trade links.

The program supports several cryptocurrency pairs, allowing you to speculate not only on Binance, but also on other cryptocurrency exchanges. Provides statistics on bot operation cycles, orders, profits, referrals. There are no monthly fees or hidden tariff plans. The development company charges 20% of the trader’s real profit - funds are automatically transferred from the speculator’s account.

3Commas

Official website: https://3commas.io/

One of the most feature-rich robotic advisors for Binance and over 20 other trading platforms. Characteristics:

  • Manual trading is available through smart-trade;
  • automated trading can be simple or complex - for one or several pairs;
  • Short/Long algorithms are used;
  • purchase/sale of digital assets is optimized - all in one window;
  • supports graphics and indicators of the popular TradingView terminal;
  • provides the opportunity to copy transactions of other traders.

There is no commission on profits. The development company charges a monthly fee, which varies relative to the selected tariff from $14.5 to $49.5 per month. The VIP package can be tested for free for 72 hours.

Cryptorg

Official website: https://cryptorg.net/

This is a whole service for the development and configuration of bots. For comfortable trading, there is a chat, Telegram messages, and other options. Advantageous trading entry points are determined by intellectual strategies based on the results of technical analysis.

You can run a huge number of utilities at the same time - for all trading pairs that Binance supports. A two-week free trial is available. Then a monthly fee is charged - $15/month. It may increase if a trader wants to expand the limit of existing software, use futures bots, or gain access to other trading platforms.

HAASbot

Official website: https://www.haasonline.com/

This is a set of options for robotic trading. Possibilities:

  • local, cloud solution, script for developing your own strategies and protocols;
  • back tests and trading simulators minimize risks and optimize strategies;
  • support for synthetic orders, extensions, market scanners, business case management, and other options.

Depending on the functionality, the cost of an annual subscription varies from 0.031 BTC to 0.088 BTC. The maximum price tariff has no limit on the number of bots, security measures, insurance methods, or trading options.

Do you want to program a complex advisor? Writing technical specifications

So, you already have some trading experience, you have tested your idea in trading or have an accurate idea of ​​how it will work in the real market. This means it’s time to move on to writing the formal rules of your strategy. It is often at this step that problems begin for most followers of the humanities - people are faced with an insurmountable wall of misunderstanding, when a scheme worked out absolutely perfectly in their heads stops working when translated into a technical language.

This problem is so widespread that it has created an entire industry for translating abstract ideas into the formal language of clearly structured rules, of course, not for free. In fact, with minimal preparation you will not encounter such complexity simply because you understand the mechanism of how the gears of your Forex robot work down to the smallest detail, but development will still require some work.

In fact, a correctly written task is already half the work. However, this is not the job of a programmer. If you ask a programmer to write a task for you, be prepared to encounter misunderstanding, at best, and indignation in the vast majority of situations. It is the developer of the idea who is responsible for its precise formalization, and not the implementer - if you like, a translator into the language of low-level commands.

We start developing a Forex robot with an idea.

Developing a robot for trading in the markets is a continuous search for ideas and, in particular, a search for the Holy Grail. Contrary to the claims of unenlightened citizens, the Grail is found quite simply - let's say “thank you” to technological progress. Try typing “The Holy Grail of Forex” into Google – most likely, the advisor of your dreams is on the first page of search results, and it’s absolutely free.

Actually, the story could end here. If the ideal advisor already exists, why continue the search? But then the second question arises: why, if the ideal adviser has already been found, is there still a shortage of drinking water in Africa? If the answer is obvious to you, congratulations, you have passed the first stage of becoming a reasonable trader, having overcome the level of a beginner or an algorithmic fanatic who does not see the prospects for his development and is focused only on results.

I am sure that your advisor's idea is absolutely unique and has no analogues. But, in any case, it would be stupid not to try to find similar developments by other authors - the scale of the research done is difficult to assess until you come face to face with it. Humanity is on the path to technological singularity, and new developments are being introduced so quickly that it is often not possible to comprehend all the material laid out. If there are still no similar developments, and it is impossible to test the idea without conducting your own research, it’s time to move on to the next stage.

Advisor designer - don't let us pass by!

Newcomers to the foreign exchange market often have many questions, the answers to which are not entirely obvious, or are only a matter of inexperience. As a novice trader, it is enough to read one book by Bill Williams to replace the concept of trading chaos with trading order for the rest of your life. Then such people wonder why they can’t buy here and why they can’t sell here, and they order an advisor from programmers for the intersection of two moving averages. But at the initial stages of training, this can easily be left to ready-made development tools. One of these is MQL5 Wizard – a wizard for creating expert advisors for MetaTrader 5.

The Master is ideal for implementing simple things. You don't need to have any magical powers - no programming skills are required here. A few simple steps and the advisor is ready. A ready-made advisor can be tested in the same MetaTrader terminal. Keep a harvester for the production of simple grails.

Write me an indicator advisor

An example of a bad task for creating a trading advisor is its absence. The very intention of the customer may be clear: there is an indicator, you need to write a trading robot, but then what?

How to close a deal? How to accompany her? What volumes to choose? What to do with slippages and requotes? You need to understand that the programmer does not have psychic abilities. If you find someone like this, be sure to let me know, such people should not disappear.

Seeing the terms of reference for the creation of a work without the necessary details, the freelancer will add his own interpretation and, in some cases, can actually guess the real intentions of the customer. But, as a rule, such appeals end with two offended parties and a recourse to arbitration with further termination of the contract.

Moral of the story : The more detail you provide in an assignment, the easier it will be to understand, comprehend, and ultimately implement.

Rating
( 2 ratings, average 4.5 out of 5 )
Did you like the article? Share with friends:
For any suggestions regarding the site: [email protected]
Для любых предложений по сайту: [email protected]