Back to Home

How to make a crutch for Tinkoff Investments with your own hands or notifications of action required for take profit / stop loss

Tinkoff Bank · Tinkoff · Tinkoff Investments · python · take profit · stop loss · action required · sms

How to make a crutch for Tinkoff Investments with your own hands or notifications of action required for take profit / stop loss

    There is such a broker - Tinkoff Bank. And there is a problem in the fact that at the moment this broker does not have take profit / stop loss orders. Therefore, if you want to trade more actively, then you need to crutch some kind of temporary solution, while in the depths of Tinkoff programmers develop a killer feature take profit / stop loss, and under the cut is one of them.
    update: 03/22/2019, The broker rolled out the major version 3.0.0 on Google Play, in which take profit / stop loss still appeared.

    Why did I decide to write this article here? It seemed to me that Tinkoff Bank and its products are quite popular among IT people, and perhaps someone has the same need, but there is no desire or time to put on a bike. Therefore, I share my.

    To begin with - about the alternative opportunities provided by the broker itself.
    First, Tinkoff has limit orders that appeared in February 2019 (they waited two years, no jokes!), But they work within one day and, worse, in a short money interval, which creates inconvenience in the volatile market. It is simply impossible to set values ​​less (more) than a certain threshold calculated from current quotes. Well, it’s probably impossible to set more than one limit order (when I try to save the first order, the mobile application always crashes, but the site does not have such functionality).
    Second, inside their mobile application, you can subscribe to a price change by setting an absolute threshold or threshold for a percentage change (increase or decrease), but you can set one and only one threshold per asset.

    The logic of my bike is simple:
    1) we have thresholds (hereinafter referred to as thresholds) for our security (asset), on which we must have a manual take profit / stop loss action. Thresholds are calculated independently, based on the purchase price of the asset;
    2) we need to parse the data of the current price of the asset from somewhere;
    3) send yourself a notice if one of the thresholds has been reached.
    Despite the straightforward description, there are nuances in the implementation :)

    1) While my portfolio consisted of one asset, I registered the thresholds directly in the body of the script, and the paper search was made not even a variable, but simply a magic line. Bad decision, but enough to test the idea. As the portfolio diversified, I made a file where the securities are loaded from, the exchanges on which they are traded, and threshold values.

    2) Since my asset was a foreign security that is traded on the St. Petersburg Exchange, I first decided to parse data from the St. Petersburg Exchange website from the following page: spbexchange.ru/ru/market-data/Default.aspx
    Sorting on the St. Petersburg Stock Exchange takes place according to the trading volume, and my security has always been on the first page. It worked great, but on March 8th everything broke. For some reason, TSLA was already on page 25, and their paginator loads the data dynamically through JS. This problem can be solved "head-on": parsing all the pages until we find our asset. But this approach is not very effective, if we consider the execution time of the script loop. Instead, I decided to add parsing from tradingview.com. There is no need to shovel long lists on more pages. There, each asset has something like this link:
    www.tradingview.com/symbols/NASDAQ-TSLA
    It seemed to me that everything should start up quickly and simply, but there was a problem - the data of interest to me is loaded via JS and the usual Requests could not handle it.
    This problem has three solutions I know of:
    PyQT, selenium (webdriver), and the Requests-HTML extension. Since I already had Requests in the project, it was decided to use its own extension.
    Unfortunately, this solution did not work very stable; I had to look for solutions.
        session = HTMLSession()
        r = session.get(url)
        my = r.html.render(timeout=30)
        selector = 'span.tv-symbol-header-quote__value.tv-symbol-header-quote__value--large.js-symbol-last'
        price = r.html.find(selector)[0].text
        r.close()
        session.close()

    Note the timeout, as well as the calls to the close () method. They can not be found in all examples, but it works better with them than without them.

    3) We register on a service that can send SMS (sms.ru), take their API, create a key. Up to 5 SMS per day - free of charge. It’s enough for me.
    The key looks like this:
    24A41EA5-EEEE-Priv-5555-094143C2EDDD
    and sending SMS in the first versions was implemented like this:
    def send_message(mymessage):
        sms_url = 'https://sms.ru/sms/send?api_id=key&to=number&msg=message&json=1'
        sms_url = sms_url.replace('key', mykey)
        sms_url = sms_url.replace('number', mynumber)
        sms_url = sms_url.replace('message', mymessage)
        sms_response = requests.get(sms_url)


    During development, the following question arose: what to do if we have already sent an SMS to the user about the threshold crossing? While there were no checks, it sent SMS again. Everytime. Quite quickly “ate” the free limit and began to think what to do with it. I had to add a counter of sent SMS (sms_counter), which we check before calling send_message.

        global sms_counter
        sms_counter = sms_counter + 1


    Another question will be trailed: excellent, during the trading session we process one threshold crossing with a certain asset, and that suits us. What to do for the next trading session? It was decided to reset the counter of sent SMS. There were three options: to store data in the database (but I currently have a stateless application), parse time / date or restart the script. So far I am making the third option, but in the future I will move on to the second or first option.

    Now the solution is already workable, and it can be downloaded from Github.
    For users who do not understand what Python is and how to configure it, I suggest trying to launch a packaged solution for Windows

    Plans for further development:
    1) parse the date / time to reset the SMS counter (instead of restarting the script);
    2) now it is a stateless application, but I intend to screw the database;
    3) after item 2, I want to add tracking of sharp jumps in price increase / decrease, relative to the closing price of the previous day;
    4) expand “communication” opportunities: more paths (Telegram, Viber, voice calls, other options) and providers (I intend to add smsc.ru, since sms.ru sometimes loses responsiveness, and although it sends SMS, but the script is not executed further until we get sms_response).

    Only registered users can participate in the survey. Please come in.

    Do you use Tinkoff Investments?

    • 43.6% Yes 45
    • 56.3% No 58

    If you use Tinkoff Investments, was this article helpful to you?

    • 45.8% Yes 22
    • 54.1% No 26

    Read Next