Skip to main content

Real-time WebSocket client for Python library for Forex, Cryptocurrency, and Stock market data from FCS API

Project description

FCS WebSocket Python

Real-time WebSocket client library for Forex, Cryptocurrency, and Stock market data from FCS API.

License: MIT Python PyPI

Features

  • Real-time WebSocket - Live price updates via WebSocket connection
  • Multi-Market Support - Forex, Crypto, and Stock data
  • Auto-Reconnect - Handles connection drops automatically
  • Simple API - Easy to use with decorators

Installation

pip install fcsapi-websocket

Examples

To download example files, clone the repository:

git clone https://github.com/fcsapi/websocket-python
cd websocket-python/examples
python simple_example.py

Demo API Key

Use demo API key for testing: fcs_socket_demo


Quick Start

from fcs_client_lib import FCSClient

client = FCSClient('YOUR_API_KEY')

@client.on_message
def handle_message(data):
    if data.get('type') == 'price':
        print(data)

client.connect()
client.join('BINANCE:BTCUSDT', '1D')
client.run_forever()

Usage Examples

Example 1: Simple Crypto Price

from fcs_client_lib import FCSClient

client = FCSClient('fcs_socket_demo')

@client.on_message
def on_message(data):
    if data.get('type') == 'price':
        symbol = data.get('symbol')
        price = data['prices'].get('c')  # Close price
        print(f'{symbol}: ${price}')

client.connect()
client.join('BINANCE:BTCUSDT', '1D')
client.run_forever()

Example 2: Multiple Forex Pairs with Spread

from fcs_client_lib import FCSClient

client = FCSClient('fcs_socket_demo')

@client.on_connected
def on_connected():
    print('Connected! Subscribing to forex pairs...')
    client.join('FX:EURUSD', '1D')
    client.join('FX:GBPUSD', '1D')
    client.join('FX:USDJPY', '1D')

@client.on_message
def on_message(data):
    if data.get('type') == 'price':
        p = data['prices']
        symbol = data.get('symbol')
        ask = p.get('a')
        bid = p.get('b')
        spread = round((float(ask) - float(bid)) * 10000, 1) if ask and bid else '--'
        print(f'{symbol}: Ask={ask} Bid={bid} Spread={spread} pips')

client.connect()
client.run_forever()

Example 3: Background Thread (Non-blocking)

from fcs_client_lib import FCSClient
import time

client = FCSClient('fcs_socket_demo')

@client.on_message
def on_message(data):
    if data.get('type') == 'price':
        print(f"Price update: {data.get('symbol')}")

# Connect and run in background thread
client.connect()
client.run_forever(blocking=False)

# Subscribe after connection
time.sleep(2)  # Wait for connection
client.join('BINANCE:ETHUSDT', '1D')

# Your other code continues here...
print('Main thread continues...')
time.sleep(60)  # Keep running for 60 seconds
client.disconnect()

API Reference

Create Client

from fcs_client_lib import FCSClient

client = FCSClient(api_key, url=None)
client.show_logs = True  # Enable console logs (default: False)

Connection

client.connect()                    # Connect to server
client.run_forever(blocking=True)   # Start receiving (blocking=False for background)
client.disconnect()                 # Disconnect from server

Subscription

client.join('BINANCE:BTCUSDT', '1D')   # Subscribe to symbol
client.leave('BINANCE:BTCUSDT', '1D')  # Unsubscribe from symbol
client.remove_all()                     # Unsubscribe from all

Event Callbacks (Decorators)

@client.on_connected
def on_connected():
    print('Connected!')

@client.on_message
def on_message(data):
    print(data)

@client.on_close
def on_close(code, msg):
    print(f'Closed: {code}')

@client.on_error
def on_error(error):
    print(f'Error: {error}')

@client.on_reconnect
def on_reconnect():
    print('Reconnected!')

Properties

client.is_connected          # Connection status (bool)
client.active_subscriptions  # Current subscriptions (dict)
client.reconnect_delay       # Reconnect delay in seconds (default: 3)
client.reconnect_limit       # Max reconnect attempts (default: 5)
client.show_logs             # Enable/disable console logs (default: False)

Symbol Format

Market Format Examples
Forex FX:PAIR FX:EURUSD, FX:GBPUSD
Crypto EXCHANGE:PAIR BINANCE:BTCUSDT, BINANCE:ETHUSDT
Stock EXCHANGE:SYMBOL NASDAQ:AAPL, NYSE:TSLA

Timeframes

Timeframe Description
1 1 minute
5 5 minutes
15 15 minutes
1H 1 hour
1D 1 day
1W 1 week

Message Data Format

# Price update
{
    "type": "price",
    "symbol": "BINANCE:BTCUSDT",
    "timeframe": "1D",
    "prices": {
        "mode": "candle",  # or "initial", "askbid"
        "t": 1766361600,   # Timestamp
        "o": 88658.87,     # Open
        "h": 90588.23,     # High
        "l": 87900,        # Low
        "c": 89962.61,     # Close
        "v": 8192.70,      # Volume
        "a": 89962.62,     # Ask
        "b": 89962.61      # Bid
    }
}

Get API Key

  1. Visit FCS API
  2. Sign up for free
  3. Get your API key

Documentation

Support

License

MIT License - see LICENSE file.

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

fcsapi_websocket-4.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

fcsapi_websocket-4.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file fcsapi_websocket-4.0.0.tar.gz.

File metadata

  • Download URL: fcsapi_websocket-4.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fcsapi_websocket-4.0.0.tar.gz
Algorithm Hash digest
SHA256 20f4bce5e73ebd9f7c781455aa156a63975e3aa51e2883f64d84e0fb6c31422f
MD5 29bd6c641ac3cd2767bb21e4a2b7242d
BLAKE2b-256 e9ccb05b2465de1e980379e80d280b40c588f9efedaf72d1777222037ca1be80

See more details on using hashes here.

Provenance

The following attestation bundles were made for fcsapi_websocket-4.0.0.tar.gz:

Publisher: publish.yml on fcsapi/websocket-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fcsapi_websocket-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fcsapi_websocket-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbfdb3b9c2d5d611a4af28cbfc75e77387395acb008dc648d0a600514827fe2b
MD5 f2a06fa42ef62c652ff5729f05b3231a
BLAKE2b-256 d8b5c346c0d5d5ba438ac9e65bdab9abce88b9238a5a30c8dde7c5fdad713640

See more details on using hashes here.

Provenance

The following attestation bundles were made for fcsapi_websocket-4.0.0-py3-none-any.whl:

Publisher: publish.yml on fcsapi/websocket-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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