NewBee the way to write a trading bot
Positive sum of wins to losses:
All non-zero sum games can be divided into 2 groups: kitchens – where the founders and possibly a small layer of the most successful players feed at the expense of the bulk of participants. A simple example of forex – those who have a stable percentage of transactions are in stable profit: I once created a virtual account on forex – in the first 3 days I made 8%, which at that time would have taken a year on a bank deposit, and after a couple of weeks I had already zeroed out the account.
A reverse example of Bitcoin during the cryptocurrency growth – once a trading bot was connected to a hamster's playground – which decided: what, when to buy, sell. And the rodent showed stable profits for more than one month of trading…
Minimax Theorem:
To maximize your chances of winning when playing against much more capable and experienced players, any trading strategy should be based primarily on minimizing losses.
This is somewhat reminiscent of exchanging money for experience – the slower you squander your first start-up capital, the more experience you will gain from it… if you have experience, you will have money.
Hence the moral – forget it, you need to play at least on a slightly growing market, first of all do not consider short positions – if you bought something that has fallen in price – then at most you will lose the price of what you bought, if you sold something that is growing in price – then you can lose everything.
No pain, no gain:
It would seem that in order to lose nothing but time, it is best to play on a virtual account… But why do you think they create it for you in the kitchens? And how do those who created it live?
On the other hand, those who provide real accounts almost for free say that you can start with 1 thousand rubles on a real account… let's say that taking into account diversification, it is highly desirable to have at least 10 times more on it and you will have to take into account that shares at a cost of one piece comparable to Yandex will be difficult for you to access.
Belated introduction:
So you can be a great programmer, capable of writing a bot in a couple of hours, which in turn can lose a fortune on the wrong positions on the wrong exchange by entering it at the wrong time. And one of my friends, a failed nurse, studying at a medical college, who earned extra money by teaching cryptocurrency trading during her student years … opened her own private medical center, simply fixing all the profits before the crypto exchange collapsed …
But on the other hand, if you are a good programmer, then the amount of starting capital that you can lose by teaching a bot to trade will be inexhaustible… and remembering my good old days, back when online trading was not yet casualized… having found the right trading hub, having chosen stable (it would seem, what does the standard deviation have to do with it, which is capable of assessing some financial risks?) and in-demand positions, having saved up a little money – you can quite easily exist in the game by trading… but without special intelligence, luck and connections… really more interesting sources of income will be missed.
So, a good programmer will most likely find it much more profitable to work on an interesting project, or write trading bots for someone… but still, if you have a dream, then why not try to understand how to move financial flows with profits.
What is required for this – you can try to reset the virtual capital on Forex with a zero cycle, then you will need a starting capital that you will not mind losing … in modern realities, most likely from 10 thousand rubles, a theoretically growing exchange, a convenient client for experiments – in modern realities, I had to opt for Quick and the understanding that before you start training the bot, you need to learn yourself to a minimum extent first, how not to lose a fortune on the exchange, working manually … and then teach the bot how to give signals in order to understand how machine learning differs from working manually … Unless of course you are a Kaggle prize winner, who can immediately skip the first couple of these noob stages … who, if he still does not understand how machine learning works, but for some reason it already works stably for him.
Iteration one:
Someone more capable after zeroing out on Forex would have immediately gotten into programming, but I preferred to leaf through a couple of books on technical analysis, looked for something on fundamental analysis – and realized that I didn’t understand anything about it.
I plucked up the courage to open an account on the Moscow Exchange and gradually deposited up to 10 thousand rubles. As a broker, I recommend looking for a reasonable Broker fee among banks. Since its payment turned out to be quite comparable with the profits of the first iteration and several times more than the taxes paid to the state and the exchange commissions.
The trading strategy was to buy and hold in cases where a misunderstanding of technical analysis turned me from a speculator into an investor.
Take profit, stop losses.
I think that as soon as you understand that technical analysis is a little more understandable than fundamental analysis, and in addition a small depression comes, then the main skill that should be learned from the first iteration of the game on the stock exchange is setting pending orders that will fix profits without expiration of the statute of limitations – since you will probably quickly get tired of watching how another day drives you into ever greater losses, each day updating the closing of positions at the price you need with the start of trading … and few of the beginners will have time every morning, with the start of trading, to study the change in the situation on the stock exchange.
And if the depression recedes, then it will be possible to open an order for a week or two, try to organize new losses for ourselves, naturally at the price we need … which the exchange may or may not reach. And most importantly, when you finally get tired of trading, you can leave open-ended orders to close all positions with a small profit
Completion of the first iteration:
Before taxes, I earned about 68 rubles of profit in 2 months from stock trading, when all unprofitable positions from the annual interest position were closed with a profit. If you calculate the annual interest after taxes, it turned out, if I'm not mistaken, to be about 3.6% per annum – which at that time was slightly more than 2 times less profitable than the interest given on a bank deposit.
All half-hearted attempts to write software in bare Python to assess the situation on the stock exchange have failed.
The most useful thing I learned from the first iteration is that if you master take profit, you can trade on the exchange at any time convenient for you, the time required for trading is reduced by an order of magnitude, as is the nervousness in making decisions based on emotions – when you place one-day orders manually, constantly checking the stock prices.
Iteration two: bond trading.
Of course, the lower percentage of profits than on a bank deposit has nothing to do with the experience and education of bank specialists, unlike me, as does the transformation from a speculator into an investor with a long-term depression. All that I thought at the next enlightenment: at that time, the stock market had a very bad situation with dividends … and as a result, I got into a very unfavorable situation playing against professional investors, without the amortization of my decisions with massive dividend payments.
And then I came up with a second business plan… it would seem, what is the difference between shares and bonds? The first thing that came to my mind was that in a difficult economic situation, dividend payments are not at all obligatory, but failure to pay coupons will threaten bankruptcy. And I decided to try investing in bonds, it would seem that no particular growth in their price should be expected, but I will receive a slightly higher interest rate over a longer period than on a bank deposit.
I will say right away – you should not get involved in massive bond trading without a clear understanding of what duration is, especially with a rising key rate or rising interest rates on deposits.
No pain, no gain v2:
So my first impressions about duration were: if a high interest rate on a deposit is paid by banks for 3 months at most, then with a high duration, a slightly lower interest rate on bonds will pay off in 3-6 months after the rate on deposits in banks is reduced. Therefore, having decided that I had done quite well with shares for a beginner, I decided to gradually increase the size of the portfolio that I would not mind losing from 10 to 20 thousand rubles and invest in bonds
It would seem that it would be worthwhile to just estimate when the interest rates on deposits would go down. And they would go down with the rise of the economy. And I am a big disliker of travel, I looked around my area: real estate is being built, there is a shortage of commercial real estate on the ground floors, the shelves are full and the turnover of goods is good … and I invested in bonds with a duration of 2038.
But here a small nuance appeared: the growth of the key rate, and then the growth of interest on deposits, as a result of which most investors began to get rid of bonds with a longer duration more intensively. And it turned out that coupons will cover such a drop in value in about a year … if, of course, this drop stops.
Having realized that I know nothing about investments, I stopped the growth of the investment portfolio at 12 thousand rubles. As a result, the investment account with 68 rubles of profit from the last iteration showed 450-500 rubles of losses (bond prices do fluctuate).
Iteration three: the precursor to the signal bot.
If earlier I tried to write all sorts of crap in Python, which did not give me any understanding of financial markets. Then after the psychosis with a hypomanic state, for some reason in 5 days I figured out the pandas library in a minimal volume, which before seemed completely unbearable to me. And after the third enlightenment I got some numbers that gave me a slightly more complete understanding of duration than it is explained on trading training sites:
The left column contains bonds with a short duration, the right column contains those in which I invested the most, trying to win back my losses. The last column shows the fall in value, indicated as a percentage per annum, that occurred over the number of days indicated in the penultimate column, based on the assumption that the rate of decline will continue throughout the year.
If we evaluate this column from the position of losses, then with a shorter duration we have approximately 4.5 periods out of 15 when the fall in the value of the bonds was comparable to the coupon being paid, and in 1 period out of 15 the fall in value was 2 times greater than the coupon. So in terms of the bonds that I bought, those with a high duration 10 periods out of 15 the fall in value was 2-3 times greater than the coupon being paid, and another 2 periods out of 15 were comparable to the coupon.
This is, so to speak, a small reference to game theory, to that part of it when most players end up in the red when playing with the wrong bonds they chose.
Tips and Conclusions:
If you are not a Kaggle winner, then before training the bot, it is advisable to figure out how to play on the stock exchange yourself.
At low coding skills, using python without pandas will not bring you any useful information.
Before you start learning how to play on financial markets, you should find the growing part of the market and, in accordance with the minimax theorem, build a strategy based on minimizing losses.
Without understanding duration, you shouldn't get into the bond market with amounts you wouldn't be happy to lose.
And in general, it is better to start learning to trade with a minimum amount that you do not mind losing, and which will allow you to diversify your investments.
When experimenting with bots, it is best to start with signal bots, which only suggest you profitable investment positions.
The best place to start experimenting is with Kaggle Learning, my gut tells me that DecissionTreeRegression from the first tutorial on machine learning should already give interesting signals.