Skip to content

fcsapi/websocket-python

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.

About

Python WebSocket Library - Real-time Forex Trading Data, Cryptocurrency Prices, Stock Market Quotes | FCS API

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages