Products

Charts

Resources

Products

Charts

Resources

Back to Blog

by Finage at June 30, 2021 7 MIN READ

Real-Time Data

Trading Bots and the Economic Point of View 

 


Python can exponentially increase your brain's power by analyzing large amounts of data, but getting started with coding can be just as chaotic as joining any other community in everyday life. There are various versions of the language, various modules for accomplishing the same goal, various habits for testing and debugging code, various software programs that can be used as a coding interface, and so on. There are rituals in an everyday culture that provide structure to the chaotic environment we live in. We greet our neighbors, attend school to earn a diploma, and brush our hair in the morning. These activities can be thought of as rituals that provide structure to our interactions with others.

 

The activities have a positive impact on those who participate in the ritual by fostering confidence and a sense of belonging. They can, however, be extremely painful. Traditional circumcision during manhood is associated with a high complication rate and, of course, pain. The symbolic meaning of belonging to a group and achieving in-group status dominates the fear and pain of these individuals during the painful act, no matter how absurd it may appear to an outsider. I'll propose a similar ritualistic process for your coding career in this post. Don't worry, the one I recommend won't harm your delicate, overcivilized skin, but it will involve a large friendly green snake that will harm your bank account if you mishandle it... 


To create a trading bot with Python, you'll need to write code that uses historical financial data to make useful predictions about future stock prices. If you want to create a successful algorithmic trading application, you'll need to have a variety of skills:

1) Devise a strategy.

2) Comprehend the required tools.

3) Securely store and retrieve data.

4) Fine-tune the subunits of a complex system.

5) Understand how to debug if something goes wrong.

 

All of these are essential skills that you will need in the future if you continue to code. 
Testing your trading strategy (also known as backtracking or paper trading) is not a big deal emotionally, but real trading is a different story. There are delays in your order with price changes in the interim, orders that are deleted because a time limit has been exceeded, and unexpected value changes as a result of news reports. Losing some foreskin will probably have a similar mental effect as coding a trading bot. You become fearful of losing money and concerned about the potential consequences on your real financial situation. 
Unfortunately, I cannot guarantee that this will not occur when you begin developing a trading bot. You must, however, be courageous. If you finish the project correctly and create a live trading bot, you've officially turned one of your coding projects into a very small, but real part of the economy... At the end of the journey, we can tell ourselves and others that we were able to create something useful with our code... 

 

THE WORK PROCESS 
Here's a modified workflow adapted from Ernest Chan's book Machine Trading to help you get started. A graphical user interface isn't required, but it does make it easier to turn switches on and off and adjust variables during the fine-tuning process. 

Workflow of a trading bot Of course, ideally, you'd have a feedback loop from the GUI to the trading bot. 

 


How long will it take to complete the project? 


The time it takes to code the trading bot is, of course, proportional to your skill level. Here's a rough estimate based on my one-year coding experience: 


Variables, loops, functions, and loops (basic level) — 4 weeks full time 
2–4 weekends for intermediate level: object-oriented programming, inheritance, and decorators. 
Professional coder — a couple of hours (?). Advanced level: 
I'm not sure which Broker Server we should use.


There are many Python trading servers available in the United States, but there are fewer options if you live outside the country (Robinhood and Alpaca, for example, do not allow non-US residents). Because I live in the Netherlands, finding a broker with a suitable API took a long time. Here's a list of websites where you can open a brokerage account if you're not based in the United States. None of the ones on that list allow you to communicate with a Python API, as far as I can tell. DeGiro, which has its own python API and clear documentation on the basic commands you'll use 99 percent of the time in the bot, is one I'm using that isn't on the list. I'm satisfied with it; their Python API is very user-friendly, so I recommend using it if you live outside of the United States. 


Which type of feedback should we use? 
You have two options for running your code 24 hours a day, seven days a week if you want to get feedback from your trading bot: 
Make use of a cloud platform such as Google Cloud Platform, Amazon Webservices, or Heroku. 
Allow an old laptop to trade 24 hours a day, seven days a week. 
You can send yourself a feedback signal by using the following methods: 
Sending yourself an e-mail or SMS notification (using a service like Twilio) for a certain number of transactions, a certain time interval, or something similar.


Visualize the data on a cloud server using Matlplotlib, Seaborn, Plotly, Dash, or another package so you can check the performance of your bot from anywhere using your phone.


The Heroku platform's cloud service is the most user-friendly. If you go with that option, I recommend carefully following Charming Data's tutorial to avoid making any mistakes during the setup. Remember to set the WEB-CONCURRENCY option to 1 as described in this post to avoid having your buy and sell orders executed twice.


Dash is the most straightforward way to create a graphical user interface. It's becoming increasingly popular, and the documentation is excellent. This is a medium-length tutorial on how to get started with Dash, but there are many more on YouTube. The advantage of Dash is that using the live update callback, you can quickly create a graph that gives you updates every x minutes/hours/days. This tutorial and this StackOverflow post will show you how to incorporate live updates into your app. If you use this tutorial, don't forget to adjust the callback function as described in my GitHub repository! If you're interested in learning more about running a scheduler in the background to coordinate trading frequency, check out this post.


What is the appropriate amount of money and stock to experiment with? 
You can use whatever parameters you want in this equation depending on your level of daredevilry, so this is a difficult one to advise you on. At the very least, consider the following parameters: 
The number of times your bot tries to trade. 
The maximum number of stocks you'll buy in a given time period (for example, 10 stocks in 24 hours). 
The number of stocks you want to have in your possession to the maximum extent possible. 
The highest price at which a stock can be purchased. Remember that the lower the maximum price you're willing to pay for a stock, the longer you'll have to hold it to break even if the stock price rises. Alternatively, if you choose a low maximum price, you will need to purchase a larger number of stocks. 


The lowest stock price at which you can make a profit after accounting for your online trading account provider's transaction costs (between 1–3 euros). 
After you've placed an order, the maximum number of days you want to keep trying to buy a stock. 
What is the best way to store your data in the workflow? 
To store your data, you can use JSON files, pickle files, CSV files, SQL databases, or other methods. They all have advantages and disadvantages. On StackOverflow, there is an interesting article. 


Is it necessary for me to use machine learning? 
There are some excellent machine learning tutorials available on the internet: 
LSTM in computer science (long-short term memory) Stock prediction using machine learning 
This video, on the other hand, provides compelling evidence that the value of contributory value in price prediction is limited. It answers the question of whether machine learning can really help traders. In summary: 
Machine learning can't predict future prices because it can't find patterns in the past. 
Overfitting will occur if you train your machine learning algorithm on more data or from more different data sources (seeing patterns that are not there).


If you do decide to give it a shot, the main goal should be to have fun while learning new things. 
While it may appear that analyzing patterns with machine learning is not beneficial, at least for the individual trader, there have been numerous publications that have highlighted the predictive power of sentiment analysis. Bollen et al. published one in 2011 that I found to be the most convincing. This open-source article provided the graph below. The calm sentiment analysis score during President Obama's election and Thanksgiving is shown in red. The average positivity in opinion is shown in blue.

 

When the calm mood is linked to stock prices, it's a win-win situation. The graph below is what you get three days later. At the bottom, the calm score is shown in red. The American Dow Jones stock price is shown in blue in the middle. The areas of congruency between stock price and sentiment are highlighted in grey in the graph at the top.

 

Sentiment analysis may have been a viable way of predicting the stock market at the time, but I can't say if it still is, or if it applies to the stock and sentiment in your country and language.

 


You can start building your own Trading Bots with Finage free Market Data API key.

Build with us today!

Get a Free API key

Back to Blog

Request a consultation

Blog

Sector Focus: Which CFDs Are Investors Watching Closely This Year?

In the fast-paced world of finance, Contract for Differences (CFDs) remains a popular instrument among investors seeking to capitalize on the price movements of various assets without actually owning them. As we navigate this year, certain sectors are attracting significant attention due to their

What's New at Finage: Latest Features and Services for 2024

Anyone on the stock market knows how important data is. Getting quality information on current trends will make a difference between making profits or losses. Since its inception, various platforms offering real-time data solutions have been committed to enhancing data quality. Most of them are me

Read more

Please note that all data provided under Finage and on this website, including the prices displayed on the ticker and charts pages, are not necessarily real-time or accurate. They are strictly intended for informational purposes and should not be relied upon for investing or trading decisions. Redistribution of the information displayed on or provided by Finage is strictly prohibited. Please be aware that the data types offered are not sourced directly or indirectly from any exchanges, but rather from over-the-counter, peer-to-peer, and market makers. Therefore, the prices may not be accurate and could differ from the actual market prices. We want to emphasize that we are not liable for any trading or investing losses that you may incur. By using the data, charts, or any related information, you accept all responsibility for any risks involved. Finage will not accept any liability for losses or damages arising from the use of our data or related services. By accessing our website or using our services, all users/visitors are deemed to have accepted these conditions.

Finage LTD 2024

Copyright