trading212

package module
v0.0.0-...-df9915d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 12 Imported by: 0

README

trading212

Provides access to the undocumented Trading 212 website API.

Work in progress.

Issues

You can only have one session with Trading 212 at a time, e.g if you open the mobile app while on the website it will kick you off the website. Same with anything using this library, logging in on another platform will end your session.

This isn't ideal as I was going to implement a trailing stop loss but would be unable to run my script and use the website at the same time.

Sharing this package anyway to since most projects seem to be using Selenium and may give someone a head start.

Documentation

Index

Constants

View Source
const (
	OrderTypeMarket    OrderType = "MARKET"
	OrderTypeStop      OrderType = "STOP"
	OrderTypeLimit     OrderType = "LIMIT"
	OrderTypeStopLimit OrderType = "STOP_LIMIT"

	OrderTimeValidityDay           OrderTimeValidity = "DAY"
	OrderTimeValidityTillCancelled OrderTimeValidity = "GOOD_TILL_CANCEL"
)

Variables

This section is empty.

Functions

func NewError

func NewError(resp *http.Response) error

NewError creates a new Error from an API response.

Types

type AccountResponse

type AccountResponse struct {
	Account struct {
		Dealer    string `json:"dealer"`
		Positions []struct {
			PositionID            string    `json:"positionId"`
			HumanID               string    `json:"humanId"`
			Created               time.Time `json:"created"`
			AveragePrice          float64   `json:"averagePrice"`
			AveragePriceConverted float64   `json:"averagePriceConverted"`
			CurrentPrice          float64   `json:"currentPrice"`
			Value                 float64   `json:"value"`
			Investment            float64   `json:"investment"`
			Code                  string    `json:"code"`
			Margin                float64   `json:"margin"`
			Ppl                   float64   `json:"ppl"`
			Quantity              float64   `json:"quantity"`
			MaxBuy                float64   `json:"maxBuy"`
			MaxSell               float64   `json:"maxSell"`
			MaxOpenBuy            float64   `json:"maxOpenBuy"`
			MaxOpenSell           float64   `json:"maxOpenSell"`
			Frontend              string    `json:"frontend"`
			AutoInvestQuantity    float64   `json:"autoInvestQuantity"`
			FxPpl                 float64   `json:"fxPpl"`
		} `json:"positions"`
		Cash struct {
			Free                 float64 `json:"free"`
			Total                float64 `json:"total"`
			Interest             float64 `json:"interest"`
			Indicator            float64 `json:"indicator"`
			Commission           float64 `json:"commission"`
			Cash                 float64 `json:"cash"`
			Ppl                  float64 `json:"ppl"`
			Result               float64 `json:"result"`
			SpreadBack           float64 `json:"spreadBack"`
			NonRefundable        float64 `json:"nonRefundable"`
			Dividend             float64 `json:"dividend"`
			StockInvestment      float64 `json:"stockInvestment"`
			FreeForStocks        float64 `json:"freeForStocks"`
			TotalCashForWithdraw float64 `json:"totalCashForWithdraw"`
			BlockedForStocks     float64 `json:"blockedForStocks"`
			PieCash              float64 `json:"pieCash"`
		} `json:"cash"`
		LimitStop    []interface{} `json:"limitStop"`
		Oco          []interface{} `json:"oco"`
		IfThen       []interface{} `json:"ifThen"`
		EquityOrders []struct {
			OrderID        string    `json:"orderId"`
			Type           string    `json:"type"`
			Code           string    `json:"code"`
			Quantity       float64   `json:"quantity"`
			FilledQuantity float64   `json:"filledQuantity"`
			Status         string    `json:"status"`
			StopPrice      float64   `json:"stopPrice"`
			Created        time.Time `json:"created"`
			Frontend       string    `json:"frontend"`
		} `json:"equityOrders"`
		EquityValueOrders []interface{} `json:"equityValueOrders"`
		ID                int           `json:"id"`
		Timestamp         int64         `json:"timestamp"`
	} `json:"account"`
}

AccountResponse returned from various v2 endpoints.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client for interacting with the undocumented Trading 212 website API.

func NewClient

func NewClient(opts ...Option) *Client

NewClient returns a new client initialised with opts.

func (*Client) CancelOrder

func (c *Client) CancelOrder(
	ctx context.Context, id string,
) (AccountResponse, error)

CancelOrder with id.

func (*Client) CreateOrder

func (c *Client) CreateOrder(
	ctx context.Context, createReq CreateOrderRequest,
) (AccountResponse, error)

CreateOrder creates a new order.

func (*Client) Login

func (c *Client) Login(username, password string) error

Login as a user obtaining the session cookies required to make other requests.

func (*Client) UpdateOrder

func (c *Client) UpdateOrder(
	ctx context.Context, id string, updateReq UpdateOrderRequest,
) (AccountResponse, error)

UpdateOrder with id.

type CreateOrderRequest

type CreateOrderRequest struct {
	InstrumentCode string            `json:"instrumentCode"`
	OrderType      OrderType         `json:"orderType"`
	StopPrice      float64           `json:"stopPrice,omitempty"`
	LimitPrice     float64           `json:"limitPrice,omitempty"`
	Quantity       float64           `json:"quantity"`
	TimeValidity   OrderTimeValidity `json:"timeValidity"`
}

CreateOrderRequest contains the request parameters to create an order.

type Error

type Error struct {
	StatusCode int    `json:"-"`
	Body       []byte `json:"-"`

	Code    string `json:"code"`
	Message string `json:"message"`
}

Error returned from the Trading 212 API.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type Option

type Option func(*Client)

Option sets an optional setting on the Client.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient returns an Option to set the http.Client to be used.

func WithPracticeMode

func WithPracticeMode() Option

WithPracticeMode returns an Option to set the Client to make requests to the demo mode environment for practice.

type OrderTimeValidity

type OrderTimeValidity string

OrderTimeValidity is how long the order is valid for e.g OrderTimeValidityDay/OrderTimeValidityTilCancelled.

type OrderType

type OrderType string

OrderType is the type of order e.g OrderTypeStop/OrderTypeMarket.

type UpdateOrderRequest

type UpdateOrderRequest struct {
	StopPrice    float64           `json:"stopPrice,omitempty"`
	LimitPrice   float64           `json:"limitPrice,omitempty"`
	Quantity     int               `json:"quantity,omitempty"`
	TimeValidity OrderTimeValidity `json:"timeValidity,omitempty"`
}

UpdateOrderRequest contains the request parameters to update an order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL