OptionEdge.API.AliceBlue
1.0.0.1-beta
See the version list below for details.
dotnet add package OptionEdge.API.AliceBlue --version 1.0.0.1-beta
NuGet\Install-Package OptionEdge.API.AliceBlue -Version 1.0.0.1-beta
<PackageReference Include="OptionEdge.API.AliceBlue" Version="1.0.0.1-beta" />
paket add OptionEdge.API.AliceBlue --version 1.0.0.1-beta
#r "nuget: OptionEdge.API.AliceBlue, 1.0.0.1-beta"
// Install OptionEdge.API.AliceBlue as a Cake Addin #addin nuget:?package=OptionEdge.API.AliceBlue&version=1.0.0.1-beta&prerelease // Install OptionEdge.API.AliceBlue as a Cake Tool #tool nuget:?package=OptionEdge.API.AliceBlue&version=1.0.0.1-beta&prerelease
.Net library for AliceBlue REST API
Client library to communicate with AliceBlue v2 REST API.
OptionEdge client library provides simpler interface to connect to AliceBlue REST Api and live streaming services.
READ THIS CAREFULLY
Current AliceBlue V2 Api documentation and Postman collection provided has many inconsistencies. Abbreviated & inconsistent field names & data types are used many places. API seems to be still under development. I have tried my best to abstract away all the lower level details and provided more readable & consistent interface to interact with the Api. However, current REST Api is still not stable and Api parameters, data types & rest endpoint might change.
Currently this library is in beta stage, please expect the methods, parameters to be changed based on the feedback.
test
Refer to my channel on API Usage Guide ProfTheta - Your Guide to Options (to be posted)
Please refer this Algo Trading Course on Youtube
Requirements
This library targets netstandard2.0 and can be used with .Net Core 2.0 and above & .Net Framework 4.6 and above.
Getting Started
You have to first generate the Api Key from ANT Web application. Please login to ANT web app and then click on the Generate API Key button.
Log into ANT Web » Apps » API Key » Generate/Regenerate API Key
Please note down the Api key & your Alice Blue User Id. You will be using the Api key & user id to initialize the client library.
It is a requirement from AliceBlue that you login to your account (ANT web) everyday before using the Api/Client library else REST calls will fail.
Troubleshooting
enable logging flag is set to true during the initialization, client library will log all the error/info messages to the console. This will help to troubleshoot any issues while integrating library with your project. You can disable this flag in production.
Install library
Install-Package OptionEdge.Api.AliceBlue
Sample project
Please refer the sample project FeaturesDemo.cs
which demonstrate the capabilities of this library.
Getting Started guide on Youtube
Please refer this Youtube (posting soon) video to get started using the library by creating a new project and calling the provided methods from the library for placing orders, getting order & trade history, historical data & live quotes.
Initialize
// Create new instance of AliceBlue client library
_aliceBlue = new AliceBlue(_settings.UserId, _settings.ApiKey, enableLogging: _settings.EnableLogging,
onAccessTokenGenerated: (accessToken) =>
{
// Store the generated access token to database or file store
// Token needs to be generated only once for the day
File.WriteAllText(_cachedTokenFile, accessToken);
}, cachedAccessTokenProvider: () =>
{
// Provide the saved token that will be used to making the REST calls.
// This method will be used when re-initializing the api client during the the day (eg after app restart etc)
// If token is invalid or not available, just return empty or null value
return File.Exists(_cachedTokenFile) ? File.ReadAllText(_cachedTokenFile) : null;
});
Live Feeds Data Streaming
// Create Ticker instance
// No need to provide the userId, apiKey, it will be automatically set
_ticker = _aliceBlue.CreateTicker();
// Setup event handlers
_ticker.OnTick += _ticker_OnTick;
_ticker.OnConnect += _ticker_OnConnect;
_ticker.OnClose += _ticker_OnClose;
_ticker.OnError += _ticker_OnError;
_ticker.OnNoReconnect += _ticker_OnNoReconnect;
_ticker.OnReconnect += _ticker_OnReconnect;
// Connect the ticker to start receiving the live feeds
// DO NOT FORGOT TO CONNECT else you will not receive any feed
_ticker.Connect();
Subscribe for live feeds
// Once connected, subscribe to tokens to start receiving the feeds
// Subscribe live feeds
// Set feed mode to Quote (no depth data will be received)
// Set feed mode to Full to get depth data as well
// Token for multiple exchnages in one call
_ticker.Subscribe(Constants.TICK_MODE_QUOTE,
new SubscriptionToken[]
{
new SubscriptionToken
{
Token = 26000,
Exchange = Constants.EXCHANGE_NSE
},
new SubscriptionToken
{
Token = 26009,
Exchange = Constants.EXCHANGE_NSE
},
new SubscriptionToken
{
Token = 35042,
Exchange = Constants.EXCHANGE_NFO
}
});
// or subscribe to tokens for specific exchange
_ticker.Subscribe(Constants.EXCHANGE_NSE, Constants.TICK_MODE_FULL, new int[] { 26000, 26009 });
Unsubscribe from live feeds
// To unscubscribe
// tokens for multiple exchanges in one call
_ticker.UnSubscribe(new SubscriptionToken[]
{
new SubscriptionToken
{
Token = 35042,
Exchange = Constants.EXCHANGE_NFO
},
new SubscriptionToken
{
Token = 26000,
Exchange = Constants.EXCHANGE_NSE
}
});
// or unsubscribe tokens from specific exchange
_ticker.UnSubscribe(Constants.EXCHANGE_NFO, new int[] { 26000, 26000 });
Auto Reconnect
// Ticker has built in re-connection mechanism
// Once disconnectd it will try to reconnect, once connected, it will automatically subscribe to
// previously subscribed tokens
// Reconnection settings
_ticker.EnableReconnect(interval: 5, retries: 20);
// interval: interval between re-connection retries.
// For every reconnect attempt OnReconnect event will be called
// retries: number of retries before it give up reconnecting.
// OnNoReconnect will be called if not able to reconnect
Download Master Contracts
_aliceBlue.SaveMasterContracts(Constants.EXCHANGE_NFO, $"c:\\data\\master_contracts_{Constants.EXCHANGE_NFO}.csv");
// or load Master Contracts into list
var masterContracts = _aliceBlue.GetMasterContracts(Constants.EXCHANGE_NFO);
Account Details
var accountDetails = _aliceBlue.GetAccountDetails();
Funds
var funds = _aliceBlue.GetFunds();
Historical Data
- Only Day and Minute data will be available. Other resolutions can be calculated on their own, based user preferences using these resolutions.
- Historical data API will be available from 5:30 PM (evening) to 8 AM (Next day morning) on weekdays (Monday to Friday). Historical data will not be available during market hours
- Historical data API will be available fully during weekends and holidays.
- For NSE segment, 2 years of historical data will be provided.
- For NFO, CDS and MCX segments, current expiry data will be provided.
- BSE, BCD and BFO Chart data will be added later.
var historicalCandles = _aliceBlue.GetHistoricalData(
Constants.EXCHANGE_NFO,
36257,
DateTime.Parse("6-Sep-2022 9:30 AM"),
DateTime.Parse("8-Sep-2022 3:30 PM"),
Constants.HISTORICAL_DATA_RESOLUTION_1_MINUTE);
Holdings
var holdings = _aliceBlue.GetHoldings();
Open Interest
var openInterest = _aliceBlue.GetOpenInterest(new OpenInterestParams
{
OpenInterestTokens = new[]
{
new OpenInterestToken
{
Exchange = Constants.EXCHANGE_NFO,
InstrumentToken = 36257
}
},
UserId = _settings.UserId
});
// or pass tokens for specific exchange
var openInterestNFO = _aliceBlue.GetOpenInterest(Constants.EXCHANGE_NFO, new[] { 12345, 43343, 5432 });
Scrip Quote
var scripQuote = _aliceBlue.GetScripQuote(Constants.EXCHANGE_NFO, 36257);
Order Book
var orderBook = _aliceBlue.GetOrderBook();
Trade Book
var tradeBook = _aliceBlue.GetTradeBook();
Order History
var orderHistory = _aliceBlue.GetOrderHistory(orderNumber: "123456789");
Day/net wise Position Book
var positionBookDayWise = _aliceBlue.GetPositionBookDayWise();
var positionBookNetWise = _aliceBlue.GetPositionBookNetWise();
Place Order - Regular
var placeRegularOrderResult = _aliceBlue.PlaceOrder(new PlaceRegularOrderParams
{
Exchange = Constants.EXCHANGE_NFO,
OrderTag = "Test",
PriceType = Constants.PRICE_TYPE_MARKET,
ProductCode = Constants.PRODUCT_CODE_MIS,
Quantity = 25,
TransactionType = Constants.TRANSACTION_TYPE_BUY,
InstrumentToken = 46335,
TradingSymbol = "BANKNIFTY2290838000PE"
});
Place Order - Cover
var placeCoverOrderResult = _aliceBlue.PlaceCoverOrder(new PlaceCoverOrderParams
{
Exchange = Constants.EXCHANGE_NSE,
OrderTag = "Test",
PriceType = Constants.PRICE_TYPE_LIMIT,
ProductCode = Constants.PRODUCT_CODE_MIS,
TransactionType = Constants.TRANSACTION_TYPE_BUY,
InstrumentToken = 2885,
TradingSymbol = "RELIANCE-EQ",
Quantity = 1,
Price = 2560m,
TriggerPrice = 2559m,
StopLoss = 2545m
});
Place Order - Bracket
var placeBracketOrderResult = _aliceBlue.PlaceBracketOrder(new PlaceBracketOrderParams
{
Exchange = Constants.EXCHANGE_NSE,
OrderTag = "Test",
PriceType = Constants.PRICE_TYPE_LIMIT,
ProductCode = Constants.PRODUCT_CODE_MIS,
TransactionType = Constants.TRANSACTION_TYPE_BUY,
InstrumentToken = 2885,
TradingSymbol = "RELIANCE-EQ",
Quantity = 1,
Price = 2560m,
TriggerPrice = 2559m,
StopLoss = 2545m,
Target = 2590m,
});
Modify Order
if (placeRegularOrderResult != null && placeRegularOrderResult.Status == Constants.STATUS_OK)
{
Console.WriteLine($"Order executed. Order Number: {placeRegularOrderResult.OrderNumber}");
// Get the status of the order
var orderStatus = _aliceBlue.ModifyOrder(new ModifyOrderParams
{
Exchange = Constants.EXCHANGE_NFO,
OrderNumber = placeRegularOrderResult.OrderNumber,
Price = .5m,
PriceType = Constants.PRICE_TYPE_LIMIT,
ProductCode = Constants.PRODUCT_CODE_MIS,
Qty = 25,
TradingSymbol = "BANKNIFTY2290838000PE",
TransactionType = Constants.TRANSACTION_TYPE_SELL,
});
}
Cancel Order
if (placeRegularOrderResult != null && placeRegularOrderResult.Status == Constants.STATUS_OK)
{
Console.WriteLine($"Order executed. Order Number: {placeRegularOrderResult.OrderNumber}");
var orderStatus = _aliceBlue.CancelOrder(placeRegularOrderResult.OrderNumber);
}
Web Socket event handlers
private static void _ticker_OnTick(Tick TickData)
{
Console.WriteLine(JsonConvert.SerializeObject(TickData));
}
private static void _ticker_OnReconnect()
{
Console.WriteLine("Ticker reconnecting.");
}
private static void _ticker_OnNoReconnect()
{
Console.WriteLine("Ticker not reconnected.");
}
private static void _ticker_OnError(string Message)
{
Console.WriteLine("Ticker error." + Message);
}
private static void _ticker_OnClose()
{
Console.WriteLine("Ticker closed.");
}
private static void _ticker_OnConnect()
{
Console.WriteLine("Ticker connected.");
_ticker.Subscribe(new SubscriptionToken[]
{
new SubscriptionToken
{
Exchange = Constants.EXCHANGE_NSE,
Token = 26000
},
new SubscriptionToken
{
Exchange = Constants.EXCHANGE_NSE,
Token = 26009
},
new SubscriptionToken
{
Exchange = Constants.EXCHANGE_NFO,
Token = 35042
},
}, Constants.TICK_MODE_FULL);
}
Links
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Cryptisk.Utf8Json (>= 1.4.0)
- LumenWorksCsvReader (>= 4.0.0)
- Microsoft.CSharp (>= 4.7.0)
- Newtonsoft.Json (>= 13.0.1)
- RestSharp (>= 108.0.1)
- RestSharp.Serializers.NewtonsoftJson (>= 108.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on OptionEdge.API.AliceBlue:
Package | Downloads |
---|---|
OptionEdge.API.AliceBlue.Smart
Smart Extensions for AliceBlue .Net Client Library |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 138 | 7/17/2024 |
1.0.7 | 163 | 5/5/2024 |
1.0.6 | 183 | 4/23/2024 |
1.0.5 | 104 | 4/23/2024 |
1.0.4 | 160 | 4/23/2024 |
1.0.3 | 837 | 3/27/2023 |
1.0.2 | 1,123 | 10/13/2022 |
1.0.1 | 1,506 | 9/23/2022 |
1.0.0.15-beta | 662 | 9/16/2022 |
1.0.0.14-beta | 667 | 9/16/2022 |
1.0.0.13-beta | 699 | 9/15/2022 |
1.0.0.12-beta | 649 | 9/15/2022 |
1.0.0.11-beta | 647 | 9/15/2022 |
1.0.0.10-beta | 678 | 9/14/2022 |
1.0.0.9-beta | 648 | 9/14/2022 |
1.0.0.8-beta | 655 | 9/14/2022 |
1.0.0.6-beta | 667 | 9/14/2022 |
1.0.0.5-beta | 622 | 9/11/2022 |
1.0.0.4-beta | 618 | 9/11/2022 |
1.0.0.3-beta | 593 | 9/10/2022 |
1.0.0.2-beta | 624 | 9/9/2022 |
1.0.0.1-beta | 593 | 9/9/2022 |