Skip to main content

Public API for Tradernet

Project description

tradernet-sdk

Public Python API for Tradernet

Installation

Installing tradernet with pip is straightforward:
python -m pip install tradernet-sdk
Instead of python you can use here and further pypy3 depending on your preferences.

Usage

Import the client library into your script:
from tradernet import TraderNetAPI
Initialize it with your credentials:
api = TraderNetAPI("public_key", "private_key")
or create a config file tradernet.ini with the following content:

[auth]
public   = public_key
private  = private_key

and initialize the client with api = TraderNetAPI.from_config("tradernet.ini")
Call any of its public methods, for example:
api.user_info()

How to trade

Usage of the trading interface is similar to common API. Import and instantiate Trading class:

from tradernet import Trading


order = Trading.from_config("tradernet.ini")

Now let's buy 1 share of FRHC.US at the market price:

order.buy("FRHC.US")

Websockets

Websocket API can be accessed via another class TraderNetWSAPI. It implements the asynchronous interface for Tradernet API, and its usage is a bit more complicated. First of all, password authentication is required to use it. It can be achieved by passing corresponding arguments to the constructor of the TraderNetCore class:
api = TraderNetCore(login="login", password="password")
or by updating the tradernet.ini file with [sid] section:

[sid]
login    = your_login
password = your_password

and then initializing the TraderNetCore instance with api = TraderNetCore.from_config("tradernet.ini"). Secondly, the TraderNetWSAPI class should be used as a context manager within a coroutine as in the example below:

from asyncio import run
from tradernet import TraderNetCore, TraderNetWSAPI


async def main() -> None:  # coroutine
    api = TraderNetCore.from_config("tradernet.ini")
    async with TraderNetWSAPI(api) as wsapi:  # type: TraderNetWSAPI
        async for quote in wsapi.market_depth("FRHC.US"):
            print(quote)


if __name__ == "__main__":
    run(main())

Password authentication

There are several methods that are still requiring login and password authentication. They are located in the module TraderNetSID:

from tradernet import TraderNetSID


sid = TraderNetSID(login="login", password="password")
tariffs = sid.get_tariffs_list()
print(tariffs)

Advanced techniques

One can import the core class to write their own methods:

from tradernet import TraderNetCore


class MyTNAPI(TraderNetCore):
    pass

This allows using sophisticated request methods for TN API like TraderNetCore.authorized_request.

One can have several instances of the API serving different purposes:

config = TraderNetCore.from_config("tradernet.ini")
order = Trading.from_instance(config)

The instance config stores the credentials, and order can be used to trade and may be destroyed after trades completed while config is still can be used to instantiate other classes.

Legacy API

The library also has the legacy PublicApiClient.py which provides almost the same functionality as most of Tradernet users used to:

from tradernet import NtApi


pub_ = "[public Api key]"
sec_ = "[secret Api key]"
cmd_ = "getPositionJson"
res = NtApi(pub_, sec_, NtApi.V2)
print(res.sendRequest(cmd_))

The only difference is that one does not have to decode the content of the response as before.

Options

The notation of options in Tradernet now can easily be deciphered:

from tradernet import TraderNetOption


option = TraderNetOption("+FRHC.16SEP2022.C55")
print(option)  # FRHC.US @ 55 Call 2022-09-16

or the scary old notation:

from tradernet import DasOption


option = DasOption("+FRHC^C7F45.US")
print(option)  # FRHC.US @ 45 Call 2022-07-15

Wrapping market data

Another feature is to get handy pandas.DataFrame objects with market data:

from pandas import DataFrame
from tradernet import TraderNetSymbol, TraderNetAPI


api = TraderNetAPI("public_key", "private_key", "login", "passwd")
symbol = TraderNetSymbol("AAPL.US", api).get_data()
market_data = DataFrame(
    symbol.candles,
    index=symbol.timestamps,
    columns=["high", "low", "open", "close"]
)
print(market_data.head().to_markdown())
# | date                |     high |      low |     open |    close |
# |:--------------------|---------:|---------:|---------:|---------:|
# | 1980-12-12 00:00:00 | 0.128876 | 0.12834  | 0.12834  | 0.12834  |
# | 1980-12-15 00:00:00 | 0.122224 | 0.121644 | 0.122224 | 0.121644 |
# | 1980-12-16 00:00:00 | 0.113252 | 0.112716 | 0.113252 | 0.112716 |
# | 1980-12-17 00:00:00 | 0.116064 | 0.115484 | 0.115484 | 0.115484 |
# | 1980-12-18 00:00:00 | 0.119412 | 0.118876 | 0.118876 | 0.118876 |

License

The package is licensed under permissive MIT License. See the LICENSE file in the top directory for the full license text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tradernet_sdk-1.0.2.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tradernet_sdk-1.0.2-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file tradernet_sdk-1.0.2.tar.gz.

File metadata

  • Download URL: tradernet_sdk-1.0.2.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Linux/5.15.0-118-generic

File hashes

Hashes for tradernet_sdk-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d47ba55389c96fcb76448ca190d4bbaa3ee0e941707774bd23f5713c9238f99e
MD5 1e9afd4ea6de10e5c1bd6e8c77018c4a
BLAKE2b-256 14ef9598b30a4dd1097b24f5b3d47c3662f6f5c0237e8326b16128d7027e5ae1

See more details on using hashes here.

File details

Details for the file tradernet_sdk-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: tradernet_sdk-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Linux/5.15.0-118-generic

File hashes

Hashes for tradernet_sdk-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9656389bb0da4ffe44cf4e726e2e9972806fd0e3f768b2909f17c76b0b990739
MD5 51a6346fca8d2d9d1526678cd070d182
BLAKE2b-256 3ec3dc34eba195c44e3f8fa827ccd7ac6f4d5c932b25c9de7535af36fc226516

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page