本文整理汇总了C#中Krs.Ats.IBNet类的典型用法代码示例。如果您正苦于以下问题:C# Krs.Ats.IBNet类的具体用法?C# Krs.Ats.IBNet怎么用?C# Krs.Ats.IBNet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Krs.Ats.IBNet类属于命名空间,在下文中一共展示了Krs.Ats.IBNet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InteractiveBrokersBrokerage
/// <summary>
/// Creates a new InteractiveBrokersBrokerage from the specified values
/// </summary>
/// <param name="account">The Interactive Brokers account name</param>
/// <param name="host">host name or IP address of the machine where TWS is running. Leave blank to connect to the local host.</param>
/// <param name="port">must match the port specified in TWS on the Configure>API>Socket Port field.</param>
/// <param name="agentDescription">Used for Rule 80A describes the type of trader.</param>
public InteractiveBrokersBrokerage(string account, string host, int port, IB.AgentDescription agentDescription = IB.AgentDescription.Individual)
: base("Interactive Brokers Brokerage")
{
_account = account;
_host = host;
_port = port;
_clientID = Interlocked.Increment(ref _nextClientID);
_agentDescription = agentDescription;
_client = new IB.IBClient();
}
示例2: MapSymbol
/// <summary>
/// Maps the IB Contract's symbol to a QC symbol
/// </summary>
private static Symbol MapSymbol(IB.Contract contract)
{
var securityType = ConvertSecurityType(contract.SecurityType);
if (securityType == SecurityType.Forex)
{
// reformat for QC
var symbol = contract.Symbol + contract.Currency;
return new Symbol(SecurityIdentifier.GenerateForex(symbol, Market.FXCM), symbol);
}
if (securityType == SecurityType.Equity)
{
return new Symbol(SecurityIdentifier.GenerateEquity(contract.Symbol, Market.USA), contract.Symbol);
}
throw new NotImplementedException("The specified security type has not been implemented: " + securityType);
}
示例3: if
/// <summary>
/// Converts a QC order to an IB order
/// </summary>
private IB.Order ConvertOrder(Order order, IB.Contract contract, int ibOrderID)
{
var ibOrder = new IB.Order
{
ClientId = _clientID,
OrderId = ibOrderID,
Account = _account,
Action = ConvertOrderDirection(order.Direction),
TotalQuantity = Math.Abs(order.Quantity),
OrderType = ConvertOrderType(order.Type),
AllOrNone = false,
Tif = IB.TimeInForce.GoodTillCancel,
Transmit = true,
Rule80A = _agentDescription
};
if (order.Type == OrderType.MarketOnOpen)
{
ibOrder.Tif = IB.TimeInForce.MarketOnOpen;
}
var limitOrder = order as LimitOrder;
var stopMarketOrder = order as StopMarketOrder;
var stopLimitOrder = order as StopLimitOrder;
if (limitOrder != null)
{
ibOrder.LimitPrice = RoundPrice(limitOrder.LimitPrice, GetMinTick(contract));
}
else if (stopMarketOrder != null)
{
ibOrder.AuxPrice = RoundPrice(stopMarketOrder.StopPrice, GetMinTick(contract));
}
else if (stopLimitOrder != null)
{
var minTick = GetMinTick(contract);
ibOrder.LimitPrice = RoundPrice(stopLimitOrder.LimitPrice, minTick);
ibOrder.AuxPrice = RoundPrice(stopLimitOrder.StopPrice, minTick);
}
// not yet supported
//ibOrder.ParentId =
//ibOrder.OcaGroup =
return ibOrder;
}
示例4: HandleOrderStatusUpdates
/// <summary>
/// Handle order events from IB
/// </summary>
private void HandleOrderStatusUpdates(object sender, IB.OrderStatusEventArgs update)
{
try
{
var order = _orderProvider.GetOrderByBrokerageId(update.OrderId);
if (order == null)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to locate order with BrokerageID " + update.OrderId);
return;
}
var status = ConvertOrderStatus(update.Status);
if (order.Status == OrderStatus.Filled && update.Filled == 0 && update.Remaining == 0)
{
// we're done with this order, remove from our state
int value;
_orderFills.TryRemove(order.Id, out value);
}
var orderFee = 0m;
int filledThisTime;
lock (_orderFillsLock)
{
// lock since we're getting and updating in multiple operations
var currentFilled = _orderFills.GetOrAdd(order.Id, 0);
if (currentFilled == 0)
{
// apply order fees on the first fill event TODO: What about partial filled orders that get cancelled?
var security = _securityProvider.GetSecurity(order.Symbol);
orderFee = security.FeeModel.GetOrderFee(security, order);
}
filledThisTime = update.Filled - currentFilled;
_orderFills.AddOrUpdate(order.Id, currentFilled, (sym, filled) => update.Filled);
}
if (status == OrderStatus.Invalid)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): ERROR -- " + update.OrderId);
}
// set status based on filled this time
if (filledThisTime != 0)
{
status = update.Remaining != 0 ? OrderStatus.PartiallyFilled : OrderStatus.Filled;
}
// don't send empty fill events
else if (status == OrderStatus.PartiallyFilled || status == OrderStatus.Filled)
{
Log.Trace("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Ignored zero fill event: OrderId: " + update.OrderId + " Remaining: " + update.Remaining);
return;
}
// mark sells as negative quantities
var fillQuantity = order.Direction == OrderDirection.Buy ? filledThisTime : -filledThisTime;
order.PriceCurrency = _securityProvider.GetSecurity(order.Symbol).SymbolProperties.QuoteCurrency;
var orderEvent = new OrderEvent(order, DateTime.UtcNow, orderFee, "Interactive Brokers Fill Event")
{
Status = status,
FillPrice = update.LastFillPrice,
FillQuantity = fillQuantity
};
if (update.Remaining != 0)
{
orderEvent.Message += " - " + update.Remaining + " remaining";
}
// if we're able to add to our fixed length, unique queue then send the event
// otherwise it is a duplicate, so skip it
if (_recentOrderEvents.Add(orderEvent.ToString() + update.Remaining))
{
OnOrderEvent(orderEvent);
}
}
catch(InvalidOperationException err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to resolve executions for BrokerageID: " + update.OrderId + " - " + err);
}
catch (Exception err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): " + err);
}
}
示例5: HandleError
/// <summary>
/// Handles error messages from IB
/// </summary>
private void HandleError(object sender, IB.ErrorEventArgs e)
{
// https://www.interactivebrokers.com/en/software/api/apiguide/tables/api_message_codes.htm
// rewrite these messages to be single lined
e.ErrorMsg = e.ErrorMsg.Replace("\r\n", ". ").Replace("\r", ". ").Replace("\n", ". ");
Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));
// figure out the message type based on our code collections below
var brokerageMessageType = BrokerageMessageType.Information;
if (ErrorCodes.Contains((int) e.ErrorCode))
{
brokerageMessageType = BrokerageMessageType.Error;
}
else if (WarningCodes.Contains((int) e.ErrorCode))
{
brokerageMessageType = BrokerageMessageType.Warning;
}
// code 1100 is a connection failure, we'll wait a minute before exploding gracefully
if ((int) e.ErrorCode == 1100 && !_disconnected1100Fired)
{
_disconnected1100Fired = true;
// begin the try wait logic
TryWaitForReconnect();
}
else if ((int) e.ErrorCode == 1102)
{
// we've reconnected
_disconnected1100Fired = false;
OnMessage(BrokerageMessageEvent.Reconnected(e.ErrorMsg));
}
if (InvalidatingCodes.Contains((int)e.ErrorCode))
{
Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError.InvalidateOrder(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));
// invalidate the order
var order = _orderProvider.GetOrderByBrokerageId(e.TickerId);
const int orderFee = 0;
var orderEvent = new OrderEvent(order, DateTime.UtcNow, orderFee) { Status = OrderStatus.Invalid };
OnOrderEvent(orderEvent);
}
OnMessage(new BrokerageMessageEvent(brokerageMessageType, (int) e.ErrorCode, e.ErrorMsg));
}
示例6: GetMinTick
private decimal GetMinTick(IB.Contract contract)
{
IB.ContractDetails details;
if (_contractDetails.TryGetValue(contract.Symbol, out details))
{
return (decimal) details.MinTick;
}
details = GetContractDetails(contract);
if (details == null)
{
// we were unable to find the contract details
return 0;
}
return (decimal) details.MinTick;
}
示例7: TimeoutException
/// <summary>
/// Gets the execution details matching the filter
/// </summary>
/// <returns>A list of executions matching the filter</returns>
public List<IB.ExecDetailsEventArgs> GetExecutions(string symbol, IB.SecurityType? type, string exchange, DateTime? timeSince, IB.ActionSide? side)
{
var filter = new IB.ExecutionFilter
{
AcctCode = _account,
ClientId = _clientID,
Exchange = exchange,
SecurityType = type ?? IB.SecurityType.Undefined,
Symbol = symbol,
Time = timeSince ?? DateTime.MinValue,
Side = side ?? IB.ActionSide.Undefined
};
var details = new List<IB.ExecDetailsEventArgs>();
using (var client = new IB.IBClient())
{
client.Connect(_host, _port, IncrementClientID());
var manualResetEvent = new ManualResetEvent(false);
int requestID = GetNextRequestID();
// define our event handlers
EventHandler<IB.ExecutionDataEndEventArgs> clientOnExecutionDataEnd = (sender, args) =>
{
if (args.RequestId == requestID) manualResetEvent.Set();
};
EventHandler<IB.ExecDetailsEventArgs> clientOnExecDetails = (sender, args) =>
{
if (args.RequestId == requestID) details.Add(args);
};
client.ExecDetails += clientOnExecDetails;
client.ExecutionDataEnd += clientOnExecutionDataEnd;
// no need to be fancy with request id since that's all this client does is 1 request
client.RequestExecutions(requestID, filter);
if (!manualResetEvent.WaitOne(5000))
{
throw new TimeoutException("InteractiveBrokersBrokerage.GetExecutions(): Operation took longer than 1 second.");
}
// remove our event handlers
client.ExecDetails -= clientOnExecDetails;
client.ExecutionDataEnd -= clientOnExecutionDataEnd;
}
return details;
}
示例8: InteractiveBrokersBrokerage
/// <summary>
/// Creates a new InteractiveBrokersBrokerage from the specified values
/// </summary>
/// <param name="orderProvider">An instance of IOrderProvider used to fetch Order objects by brokerage ID</param>
/// <param name="securityProvider">The security provider used to give access to algorithm securities</param>
/// <param name="account">The Interactive Brokers account name</param>
/// <param name="host">host name or IP address of the machine where TWS is running. Leave blank to connect to the local host.</param>
/// <param name="port">must match the port specified in TWS on the Configure>API>Socket Port field.</param>
/// <param name="agentDescription">Used for Rule 80A describes the type of trader.</param>
public InteractiveBrokersBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider, string account, string host, int port, IB.AgentDescription agentDescription = IB.AgentDescription.Individual)
: base("Interactive Brokers Brokerage")
{
_orderProvider = orderProvider;
_securityProvider = securityProvider;
_account = account;
_host = host;
_port = port;
_clientID = IncrementClientID();
_agentDescription = agentDescription;
_client = new IB.IBClient();
// set up event handlers
_client.UpdatePortfolio += HandlePortfolioUpdates;
_client.OrderStatus += HandleOrderStatusUpdates;
_client.UpdateAccountValue += HandleUpdateAccountValue;
_client.Error += HandleError;
_client.TickPrice += HandleTickPrice;
_client.TickSize += HandleTickSize;
_client.CurrentTime += HandleBrokerTime;
// we need to wait until we receive the next valid id from the server
_client.NextValidId += (sender, e) =>
{
// only grab this id when we initialize, and we'll manually increment it here to avoid threading issues
if (_nextValidID == 0)
{
_nextValidID = e.OrderId;
_waitForNextValidID.Set();
}
Log.Trace("InteractiveBrokersBrokerage.HandleNextValidID(): " + e.OrderId);
};
}
示例9: HandlePortfolioUpdates
/// <summary>
/// Handle portfolio changed events from IB
/// </summary>
private void HandlePortfolioUpdates(object sender, IB.UpdatePortfolioEventArgs e)
{
_accountHoldingsResetEvent.Reset();
var holding = CreateHolding(e);
_accountHoldings[holding.Symbol] = holding;
OnPortfolioChanged(new SecurityEvent(holding.Symbol, e.Position, e.AverageCost));
}
示例10: HandleOrderStatusUpdates
/// <summary>
/// Handle order events from IB
/// </summary>
private void HandleOrderStatusUpdates(object sender, IB.OrderStatusEventArgs update)
{
try
{
if (update.Status == IB.OrderStatus.PreSubmitted
|| update.Status == IB.OrderStatus.PendingSubmit)
{
return;
}
var status = ConvertOrderStatus(update.Status);
if (status != OrderStatus.PartiallyFilled &&
status != OrderStatus.Filled &&
status != OrderStatus.Canceled &&
status != OrderStatus.Submitted &&
status != OrderStatus.Invalid)
{
Log.Trace("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Status: " + status);
return;
}
if (status == OrderStatus.Invalid)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): ERROR -- " + update.OrderId);
}
var order = _orderProvider.GetOrderByBrokerageId(update.OrderId);
if (order == null)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to locate order with BrokerageID " + update.OrderId);
return;
}
// mark sells as negative quantities
var fillQuantity = order.Direction == OrderDirection.Buy ? update.Filled : -update.Filled;
var orderEvent = new OrderEvent(order, "Interactive Brokers Fill Event")
{
Status = status,
FillPrice = update.AverageFillPrice,
FillQuantity = fillQuantity
};
if (update.Remaining != 0)
{
orderEvent.Message += " - " + update.Remaining + " remaining";
}
// if we're able to add to our fixed length, unique queue then send the event
// otherwise it is a duplicate, so skip it
if (_recentOrderEvents.Add(orderEvent.ToString() + update.Remaining))
{
OnOrderEvent(orderEvent);
}
}
catch(InvalidOperationException err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to resolve executions for BrokerageID: " + update.OrderId + " - " + err.Message);
}
catch (Exception err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): " + err.Message);
}
}
示例11: HandleBrokerTime
void HandleBrokerTime(object sender, IB.CurrentTimeEventArgs e)
{
DateTime brokerTime = e.Time.ToLocalTime();
_brokerTimeDiff = brokerTime.Subtract(DateTime.Now);
}
示例12: MapSymbol
/// <summary>
/// Maps the IB Contract's symbol to a QC symbol
/// </summary>
private static Symbol MapSymbol(IB.Contract contract)
{
if (contract.SecurityType == IB.SecurityType.Cash)
{
// reformat for QC
return new Symbol(contract.Symbol + contract.Currency);
}
return new Symbol(contract.Symbol);
}
示例13: HandleTickPrice
void HandleTickPrice(object sender, IB.TickPriceEventArgs e)
{
var symbol = default(SymbolCacheKey);
if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return;
var tick = new Tick();
// in the event of a symbol change this will break since we'll be assigning the
// new symbol to the permtick which won't be known by the algorithm
tick.Symbol = new Symbol(symbol.Item2);
tick.Time = GetBrokerTime();
if (symbol.Item1 == SecurityType.Forex)
{
// forex exchange hours are specified in UTC-05
tick.Time = tick.Time.ConvertTo(TimeZones.NewYork, TimeZones.EasternStandard);
}
tick.Value = e.Price;
if (e.Price <= 0 &&
symbol.Item1 != SecurityType.Future &&
symbol.Item1 != SecurityType.Option)
return;
switch (e.TickType)
{
case IB.TickType.BidPrice:
tick.TickType = TickType.Quote;
tick.BidPrice = e.Price;
_lastBidSizes.TryGetValue(symbol, out tick.Quantity);
_lastBidPrices[symbol] = e.Price;
break;
case IB.TickType.AskPrice:
tick.TickType = TickType.Quote;
tick.AskPrice = e.Price;
_lastAskSizes.TryGetValue(symbol, out tick.Quantity);
_lastAskPrices[symbol] = e.Price;
break;
case IB.TickType.LastPrice:
tick.TickType = TickType.Trade;
tick.Value = e.Price;
_lastPrices[symbol] = e.Price;
break;
case IB.TickType.HighPrice:
case IB.TickType.LowPrice:
case IB.TickType.ClosePrice:
case IB.TickType.OpenPrice:
default:
return;
}
lock (_ticks)
if (tick.IsValid()) _ticks.Add(tick);
}
示例14: HandleTickSize
void HandleTickSize(object sender, IB.TickSizeEventArgs e)
{
var symbol = default(SymbolCacheKey);
if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return;
var tick = new Tick();
// in the event of a symbol change this will break since we'll be assigning the
// new symbol to the permtick which won't be known by the algorithm
tick.Symbol = new Symbol(symbol.Item2);
tick.Quantity = AdjustQuantity(symbol.Item1, e.Size);
tick.Time = GetBrokerTime();
if (tick.Quantity == 0) return;
switch (e.TickType)
{
case IB.TickType.BidSize:
tick.TickType = TickType.Quote;
_lastBidPrices.TryGetValue(symbol, out tick.BidPrice);
_lastBidSizes[symbol] = tick.Quantity;
tick.Value = tick.BidPrice;
break;
case IB.TickType.AskSize:
tick.TickType = TickType.Quote;
_lastAskPrices.TryGetValue(symbol, out tick.AskPrice);
_lastAskSizes[symbol] = tick.Quantity;
tick.Value = tick.AskPrice;
break;
case IB.TickType.LastSize:
tick.TickType = TickType.Trade;
decimal lastPrice;
_lastPrices.TryGetValue(symbol, out lastPrice);
_lastVolumes[symbol] = tick.Quantity;
tick.Value = lastPrice;
break;
default:
return;
}
lock (_ticks)
if (tick.IsValid()) _ticks.Add(tick);
}
示例15: HandleOrderStatusUpdates
/// <summary>
/// Handle order events from IB
/// </summary>
private void HandleOrderStatusUpdates(object sender, IB.OrderStatusEventArgs update)
{
try
{
if (update.Status == IB.OrderStatus.PreSubmitted
|| update.Status == IB.OrderStatus.PendingSubmit)
{
return;
}
var status = ConvertOrderStatus(update.Status);
if (status != OrderStatus.PartiallyFilled &&
status != OrderStatus.Filled &&
status != OrderStatus.Canceled &&
status != OrderStatus.Submitted &&
status != OrderStatus.Invalid)
{
Log.Trace("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Status: " + status);
return;
}
if (status == OrderStatus.Invalid)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): ERROR -- " + update.OrderId);
}
var order = _orderProvider.GetOrderByBrokerageId(update.OrderId);
if (order == null)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to locate order with BrokerageID " + update.OrderId);
return;
}
int filledThisTime;
lock (_orderFillsLock)
{
// lock since we're getting and updating in multiple operations
var currentFilled = _orderFills.GetOrAdd(order.Symbol, 0);
filledThisTime = update.Filled - currentFilled;
_orderFills.AddOrUpdate(order.Symbol, currentFilled, (sym, filled) => update.Filled);
}
// don't send empty fill events
if (filledThisTime == 0 && (status == OrderStatus.PartiallyFilled || status == OrderStatus.Filled))
{
Log.Trace("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Ignored zero fill event: OrderId: " + update.OrderId + " Remaining: " + update.Remaining);
return;
}
// mark sells as negative quantities
var fillQuantity = order.Direction == OrderDirection.Buy ? filledThisTime : -filledThisTime;
const int orderFee = 0;
var orderEvent = new OrderEvent(order, DateTime.UtcNow, orderFee, "Interactive Brokers Fill Event")
{
Status = status,
FillPrice = update.LastFillPrice,
FillQuantity = fillQuantity
};
if (update.Remaining != 0)
{
orderEvent.Message += " - " + update.Remaining + " remaining";
}
// if we're able to add to our fixed length, unique queue then send the event
// otherwise it is a duplicate, so skip it
if (_recentOrderEvents.Add(orderEvent.ToString() + update.Remaining))
{
OnOrderEvent(orderEvent);
}
}
catch(InvalidOperationException err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): Unable to resolve executions for BrokerageID: " + update.OrderId + " - " + err);
}
catch (Exception err)
{
Log.Error("InteractiveBrokersBrokerage.HandleOrderStatusUpdates(): " + err);
}
}