# Getting Started

Welcome to **go-arrow**, the official Go SDK for the Arrow Trading Platform. The package covers REST APIs for orders, portfolio, margins, and market data, plus WebSocket helpers for live quotes and order updates.

Current release: **v1.8.0**. Import the `arrow` package from [`github.com/arrow-trade/go-arrow`](https://github.com/arrow-trade/go-arrow).

## Key Features

<div class="grid cards" markdown>

-   :material-chart-line:{ .lg .middle } **Market Data & Analytics**

    ---

    Real-time quotes, OHLC, LTP, market depth, historical candles, option chains, Greeks, and more.

-   :material-cart:{ .lg .middle } **Order Management**

    ---

    Place, modify, and cancel orders across NSE, BSE, NFO, BFO, and MCXFO with limit, stop, and `mpp` market-style orders.

-   :material-broadcast:{ .lg .middle } **Real-time Streaming**

    ---

    WebSocket feeds for order updates, token market data (`ltp` / `ltpc` / `quote` / `full` + CAS), and HFT (zstd) ticks.

-   :material-shield-lock:{ .lg .middle } **Secure Authentication**

    ---

    Request-token OAuth (`Authenticate`) and fully automated login with TOTP (`AutoLogin`).

</div>

## Installation

```bash
go get github.com/arrow-trade/go-arrow@v1.8.0
```

Pin a release tag in production. `go get github.com/arrow-trade/go-arrow@latest` tracks the newest tagged version.

The **module path** is `github.com/arrow-trade/go-arrow`. Import the public package as:

```go
import "github.com/arrow-trade/go-arrow/arrow"
```

### Requirements

| Package | Version |
|---------|---------|
| Go | 1.24+ |
| github.com/valyala/fasthttp | Latest |
| github.com/gorilla/websocket | Latest |
| github.com/pquerna/otp | Latest |
| github.com/klauspost/compress | Latest |
| github.com/rs/zerolog | Latest |

Dependencies are pulled automatically by `go get`.

## Quick Start

### 1. Initialize the Client

`NewClient` takes both the application ID and the application secret. REST calls use a **10s** timeout by default (same as the Python SDK).

```go
package main

import (
    "time"

    "github.com/arrow-trade/go-arrow/arrow"
)

func main() {
    client := arrow.NewClient("YOUR_APP_ID", "YOUR_APP_SECRET")

    // Optional: verbose request lifecycle logs
    client.SetDebug(true)

    // Optional: override the 10s REST timeout
    // client := arrow.NewClientWithTimeout("YOUR_APP_ID", "YOUR_APP_SECRET", 30*time.Second)
    // client.SetHTTPTimeout(30 * time.Second)
}
```

!!! info "App secret at construction"
    Unlike the Python client (`ArrowClient(app_id=...)` then secret at login), Go stores `AppSecret` on `Client.Config` and uses it for `Authenticate` checksums. Do not hard-code it — load from the environment.

### 2. Authenticate

=== "Automated Login"

    ```go
    if err := client.AutoLogin("YOUR_USER_ID", "YOUR_PASSWORD", "YOUR_TOTP_SECRET"); err != nil {
        log.Fatal(err)
    }
    ```

=== "Web-based Login"

    ```go
    // Interactive: prints https://app.arrow.trade/app/login?appId=... and reads the request token from stdin
    client.Login()

    // Or exchange a token you already captured from the callback URL:
    token, err := client.Authenticate("request_token_from_callback")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Access token:", token)
    ```

### 3. Place Your First Order

```go
order, err := client.PlaceOrder("regular", arrow.OrderRequest{
    Exchange:        string(arrow.ExchangeNSE),
    Symbol:          "RELIANCE-EQ",
    Quantity:        "1",
    DisclosedQty:    "0",
    Product:         string(arrow.ProductCNC),
    OrderType:       string(arrow.OrderTypeLimit),
    TransactionType: string(arrow.TransactionTypeBuy),
    Price:           "1450.0",
    Validity:        string(arrow.ValidityDAY),
})
if err != nil {
    log.Fatal(err)
}
fmt.Println("Order placed:", order.Data.OrderNo)
```

Numeric fields on `OrderRequest` are **strings** (the REST body uses string quantities and prices).

### 4. Get Market Data

```go
quote, err := client.GetQuote(arrow.ExchangeNSE, "RELIANCE-EQ", arrow.InfoQuoteLTP)
if err != nil {
    log.Fatal(err)
}
// REST prices are integers in paise (×100)
ltp := quote["ltp"].(float64) / 100
fmt.Printf("Last traded price: ₹%.2f\n", ltp)
```

!!! note "Argument order"
    `GetQuote(exchange, symbol, mode)` — exchange first. The Python SDK is `get_quote(mode, symbol, exchange)`.

### 5. Stream Live Data

```go
import (
    "context"
    "fmt"
    "log"

    "github.com/arrow-trade/go-arrow/arrow"
)

streams, err := client.NewStreams() // order updates + token data stream
if err != nil {
    log.Fatal(err)
}
defer streams.Close()

if err := streams.DataStream.Subscribe(arrow.StreamModeQuote, []int32{3045, 1594}); err != nil {
    log.Fatal(err)
}

ctx := context.Background()
go streams.DataStream.ReadTicks(ctx, func(tick arrow.MarketTick) {
    fmt.Printf("Token: %d | LTP: %d | IsCAS: %v\n", tick.Token, tick.LTP, tick.IsCAS)
}, func(err error) {
    log.Println("tick error:", err)
})

select {} // keep the process alive
```

## What's Next?

| Topic | Description |
|-------|-------------|
| [Authentication](authentication.md) | Request-token and AutoLogin flows, session helpers |
| [Orders](orders.md) | Place, modify, cancel, margin, order book |
| [Portfolio](portfolio.md) | Positions, holdings, limits, user profile |
| [Market Data](market-data.md) | Quotes, candles, instruments, option chain, Greeks |
| [WebSocket Streaming](websocket-streaming.md) | Order stream, token stream, HFT, CAS |
| [API Reference](api-reference.md) | Complete method and constant catalog |

## Support

| Resource | Link |
|----------|------|
| Documentation | [https://docs.arrow.trade](https://docs.arrow.trade) |
| Source | [github.com/arrow-trade/go-arrow](https://github.com/arrow-trade/go-arrow) |
| Package docs | [pkg.go.dev/github.com/arrow-trade/go-arrow](https://pkg.go.dev/github.com/arrow-trade/go-arrow) |
| Support Email | [support@arrow.trade](mailto:support@arrow.trade) |

!!! tip "Pro Tip"
    Start with the [Authentication](authentication.md) guide before wiring live orders. Tokens expire after 24 hours.
