Skip to content

C# WebSocket client for real-time Forex, Crypto, and Stock market data from FCS API - Stream live prices, OHLCV data with auto-reconnect for .NET applications.

License

Notifications You must be signed in to change notification settings

fcsapi/websocket-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

FCS API WebSocket C#

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

License: MIT .NET

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 events

Installation

Clone the repository:

git clone https://github.com/fcsapi/websocket-csharp.git
cd websocket-csharp

Examples

Example Description Run Command
CryptoExample Real-time crypto prices (BTC, ETH, BNB, SOL) cd examples/CryptoExample && dotnet run
ForexExample Real-time forex prices (EUR/USD, GBP/USD) cd examples/ForexExample && dotnet run
StockExample Real-time stock prices (AAPL, GOOGL, MSFT) cd examples/StockExample && dotnet run
SimpleExample Minimal quick start example cd examples/SimpleExample && dotnet run

CryptoExample.cs

using System;
using System.Collections.Generic;
using System.Text.Json;
using FcsApi;

class Program
{
    const string ApiKey = "fcs_socket_demo";
    static readonly string[] Symbols = { "BINANCE:BTCUSDT", "BINANCE:ETHUSDT", "BINANCE:BNBUSDT", "BINANCE:SOLUSDT" };
    const string Timeframe = "1D";
    static readonly Dictionary<string, PriceData> Prices = new();

    static void Main(string[] args)
    {
        Console.WriteLine("\nConnecting to FCS WebSocket...");
        Console.WriteLine("Press Ctrl+C to stop\n");

        using var client = new FcsClient(ApiKey);

        client.OnConnected += () =>
        {
            Console.WriteLine("Connected! Subscribing to crypto symbols...");
            foreach (var symbol in Symbols)
                client.Join(symbol, Timeframe);
        };

        client.OnMessage += (data) =>
        {
            var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
            if (type != "price") return;
            if (!data.TryGetProperty("prices", out var prices)) return;

            var symbol = data.TryGetProperty("symbol", out var s) ? s.GetString() : "";
            var mode = prices.TryGetProperty("mode", out var m) ? m.GetString() : "";

            if (mode == "initial" || mode == "candle")
            {
                var name = symbol.Contains(":") ? symbol.Split(':')[1] : symbol;
                var close = prices.TryGetProperty("c", out var c) ? c.ToString() : "";
                Console.WriteLine($"  [{name}] Price: ${close}");
            }
        };

        client.OnClose += (code, msg) => Console.WriteLine($"\n[!] Connection closed: {code} - {msg}");
        client.OnError += (error) => Console.WriteLine($"\n[!] Error: {error.Message}");

        client.Connect();
        client.RunForever();
    }

    class PriceData
    {
        public string Open { get; set; }
        public string High { get; set; }
        public string Low { get; set; }
        public string Close { get; set; }
        public string Volume { get; set; }
        public string Ask { get; set; }
        public string Bid { get; set; }
    }
}

Demo API Key

Use demo API key for testing: fcs_socket_demo


Quick Start

using FcsApi;

var client = new FcsClient("YOUR_API_KEY");

client.OnConnected += () => {
    Console.WriteLine("Connected!");
    client.Join("BINANCE:BTCUSDT", "1D");
};

client.OnMessage += (data) => {
    Console.WriteLine(data);
};

client.Connect();
client.RunForever();

Usage Examples

Example 1: Simple Crypto Price

using FcsApi;

var client = new FcsClient("fcs_socket_demo");

client.OnConnected += () => {
    client.Join("BINANCE:BTCUSDT", "1D");
};

client.OnMessage += (data) => {
    var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
    if (type == "price")
    {
        var symbol = data.GetProperty("symbol").GetString();
        var price = data.GetProperty("prices").GetProperty("c").ToString();
        Console.WriteLine($"{symbol}: ${price}");
    }
};

client.Connect();
client.RunForever();

Example 2: Multiple Forex Pairs

using FcsApi;

var client = new FcsClient("fcs_socket_demo");

client.OnConnected += () => {
    Console.WriteLine("Connected! Subscribing to forex pairs...");
    client.Join("FX:EURUSD", "1D");
    client.Join("FX:GBPUSD", "1D");
    client.Join("FX:USDJPY", "1D");
};

client.OnMessage += (data) => {
    var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
    if (type == "price")
    {
        var symbol = data.GetProperty("symbol").GetString();
        var prices = data.GetProperty("prices");
        var ask = prices.GetProperty("a").ToString();
        var bid = prices.GetProperty("b").ToString();
        Console.WriteLine($"{symbol}: Ask={ask} Bid={bid}");
    }
};

client.Connect();
client.RunForever();

Example 3: Background Thread (Non-blocking)

using FcsApi;

var client = new FcsClient("fcs_socket_demo");

client.OnConnected += () => {
    client.Join("BINANCE:ETHUSDT", "1D");
};

client.OnMessage += (data) => {
    var type = data.TryGetProperty("type", out var t) ? t.GetString() : null;
    if (type == "price")
        Console.WriteLine($"Price update: {data.GetProperty("symbol")}");
};

// Connect and run in background thread
client.Connect();
client.RunForever(blocking: false);

// Your other code continues here...
Console.WriteLine("Main thread continues...");
Thread.Sleep(60000);
client.Disconnect();

API Reference

Create Client

using FcsApi;

var client = new FcsClient(apiKey, url: null);
client.ShowLogs = true;  // Enable console logs (default: false)

Connection

client.Connect();                    // Connect to server
client.RunForever(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.RemoveAll();                      // Unsubscribe from all

Event Handlers

client.OnConnected += () => {
    Console.WriteLine("Connected!");
};

client.OnMessage += (data) => {
    Console.WriteLine(data);
};

client.OnClose += (code, msg) => {
    Console.WriteLine($"Closed: {code}");
};

client.OnError += (error) => {
    Console.WriteLine($"Error: {error.Message}");
};

client.OnReconnected += () => {
    Console.WriteLine("Reconnected!");
};

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

{
    "type": "price",
    "symbol": "BINANCE:BTCUSDT",
    "timeframe": "1D",
    "prices": {
        "mode": "candle",
        "t": 1766361600,
        "o": 88658.87,
        "h": 90588.23,
        "l": 87900,
        "c": 89962.61,
        "v": 8192.70,
        "a": 89962.62,
        "b": 89962.61
    }
}

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

C# WebSocket client for real-time Forex, Crypto, and Stock market data from FCS API - Stream live prices, OHLCV data with auto-reconnect for .NET applications.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages