Skip to main content

truefix_twsapi_client/
message.rs

1/// Offset added to an outgoing message id when the payload is protobuf encoded.
2pub const PROTOBUF_MSG_ID: i32 = 200;
3
4/// Incoming TWS API message identifiers.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[repr(i32)]
7pub enum Incoming {
8    /// Tick price callback.
9    TickPrice = 1,
10    /// Tick size callback.
11    TickSize = 2,
12    /// Order status callback.
13    OrderStatus = 3,
14    /// Error callback.
15    ErrorMessage = 4,
16    /// Open order callback.
17    OpenOrder = 5,
18    /// Account value callback.
19    AccountValue = 6,
20    /// Portfolio value callback.
21    PortfolioValue = 7,
22    /// Account update time callback.
23    AccountUpdateTime = 8,
24    /// Next valid id callback.
25    NextValidId = 9,
26    /// Contract data callback.
27    ContractData = 10,
28    /// Execution data callback.
29    ExecutionData = 11,
30    /// Market depth callback.
31    MarketDepth = 12,
32    /// Market depth L2 callback.
33    MarketDepthL2 = 13,
34    /// News bulletin callback.
35    NewsBulletins = 14,
36    /// Managed accounts callback.
37    ManagedAccounts = 15,
38    /// Receive FA callback.
39    ReceiveFa = 16,
40    /// Historical data callback.
41    HistoricalData = 17,
42    /// Bond contract data callback.
43    BondContractData = 18,
44    /// Scanner parameters callback.
45    ScannerParameters = 19,
46    /// Scanner data callback.
47    ScannerData = 20,
48    /// Option computation callback.
49    TickOptionComputation = 21,
50    /// Generic tick callback.
51    TickGeneric = 45,
52    /// String tick callback.
53    TickString = 46,
54    /// EFP tick callback.
55    TickEfp = 47,
56    /// Current time callback.
57    CurrentTime = 49,
58    /// Real-time bar callback.
59    RealTimeBars = 50,
60    /// Contract data end callback.
61    ContractDataEnd = 52,
62    /// Open order end callback.
63    OpenOrderEnd = 53,
64    /// Account download end callback.
65    AccountDownloadEnd = 54,
66    /// Execution data end callback.
67    ExecutionDataEnd = 55,
68    /// Delta-neutral validation callback.
69    DeltaNeutralValidation = 56,
70    /// Tick snapshot end callback.
71    TickSnapshotEnd = 57,
72    /// Market data type callback.
73    MarketDataType = 58,
74    /// Commission and fees report callback.
75    CommissionAndFeesReport = 59,
76    /// Position data callback.
77    PositionData = 61,
78    /// Position end callback.
79    PositionEnd = 62,
80    /// Account summary callback.
81    AccountSummary = 63,
82    /// Account summary end callback.
83    AccountSummaryEnd = 64,
84    /// Verify message API callback.
85    VerifyMessageApi = 65,
86    /// Verify completed callback.
87    VerifyCompleted = 66,
88    /// Display group list callback.
89    DisplayGroupList = 67,
90    /// Display group updated callback.
91    DisplayGroupUpdated = 68,
92    /// Verify-and-auth message callback.
93    VerifyAndAuthMessageApi = 69,
94    /// Verify-and-auth completed callback.
95    VerifyAndAuthCompleted = 70,
96    /// Position multi callback.
97    PositionMulti = 71,
98    /// Position multi end callback.
99    PositionMultiEnd = 72,
100    /// Account update multi callback.
101    AccountUpdateMulti = 73,
102    /// Account update multi end callback.
103    AccountUpdateMultiEnd = 74,
104    /// Security definition option parameter callback.
105    SecurityDefinitionOptionParameter = 75,
106    /// Security definition option parameter end callback.
107    SecurityDefinitionOptionParameterEnd = 76,
108    /// Soft-dollar tiers callback.
109    SoftDollarTiers = 77,
110    /// Family codes callback.
111    FamilyCodes = 78,
112    /// Symbol samples callback.
113    SymbolSamples = 79,
114    /// Market depth exchanges callback.
115    MarketDepthExchanges = 80,
116    /// Tick request parameters callback.
117    TickRequestParameters = 81,
118    /// Smart components callback.
119    SmartComponents = 82,
120    /// News article callback.
121    NewsArticle = 83,
122    /// Tick news callback.
123    TickNews = 84,
124    /// News providers callback.
125    NewsProviders = 85,
126    /// Historical news callback.
127    HistoricalNews = 86,
128    /// Historical news end callback.
129    HistoricalNewsEnd = 87,
130    /// Head timestamp callback.
131    HeadTimestamp = 88,
132    /// Histogram data callback.
133    HistogramData = 89,
134    /// Historical data update callback.
135    HistoricalDataUpdate = 90,
136    /// Reroute market data request callback.
137    RerouteMarketDataRequest = 91,
138    /// Reroute market depth request callback.
139    RerouteMarketDepthRequest = 92,
140    /// Market rule callback.
141    MarketRule = 93,
142    /// PnL callback.
143    Pnl = 94,
144    /// PnL single callback.
145    PnlSingle = 95,
146    /// Historical ticks callback.
147    HistoricalTicks = 96,
148    /// Historical bid/ask ticks callback.
149    HistoricalTicksBidAsk = 97,
150    /// Historical last ticks callback.
151    HistoricalTicksLast = 98,
152    /// Tick-by-tick callback.
153    TickByTick = 99,
154    /// Order bound callback.
155    OrderBound = 100,
156    /// Completed order callback.
157    CompletedOrder = 101,
158    /// Completed orders end callback.
159    CompletedOrdersEnd = 102,
160    /// Replace FA end callback.
161    ReplaceFaEnd = 103,
162    /// WSH metadata callback.
163    WshMetaData = 104,
164    /// WSH event data callback.
165    WshEventData = 105,
166    /// Historical schedule callback.
167    HistoricalSchedule = 106,
168    /// User info callback.
169    UserInfo = 107,
170    /// Historical data end callback.
171    HistoricalDataEnd = 108,
172    /// Current time in milliseconds callback.
173    CurrentTimeInMillis = 109,
174    /// Config response callback.
175    ConfigResponse = 110,
176    /// Update config response callback.
177    UpdateConfigResponse = 111,
178}
179
180impl Incoming {
181    /// All known incoming TWS API message ids.
182    pub const ALL: &'static [Self] = &[
183        Self::TickPrice,
184        Self::TickSize,
185        Self::OrderStatus,
186        Self::ErrorMessage,
187        Self::OpenOrder,
188        Self::AccountValue,
189        Self::PortfolioValue,
190        Self::AccountUpdateTime,
191        Self::NextValidId,
192        Self::ContractData,
193        Self::ExecutionData,
194        Self::MarketDepth,
195        Self::MarketDepthL2,
196        Self::NewsBulletins,
197        Self::ManagedAccounts,
198        Self::ReceiveFa,
199        Self::HistoricalData,
200        Self::BondContractData,
201        Self::ScannerParameters,
202        Self::ScannerData,
203        Self::TickOptionComputation,
204        Self::TickGeneric,
205        Self::TickString,
206        Self::TickEfp,
207        Self::CurrentTime,
208        Self::RealTimeBars,
209        Self::ContractDataEnd,
210        Self::OpenOrderEnd,
211        Self::AccountDownloadEnd,
212        Self::ExecutionDataEnd,
213        Self::DeltaNeutralValidation,
214        Self::TickSnapshotEnd,
215        Self::MarketDataType,
216        Self::CommissionAndFeesReport,
217        Self::PositionData,
218        Self::PositionEnd,
219        Self::AccountSummary,
220        Self::AccountSummaryEnd,
221        Self::VerifyMessageApi,
222        Self::VerifyCompleted,
223        Self::DisplayGroupList,
224        Self::DisplayGroupUpdated,
225        Self::VerifyAndAuthMessageApi,
226        Self::VerifyAndAuthCompleted,
227        Self::PositionMulti,
228        Self::PositionMultiEnd,
229        Self::AccountUpdateMulti,
230        Self::AccountUpdateMultiEnd,
231        Self::SecurityDefinitionOptionParameter,
232        Self::SecurityDefinitionOptionParameterEnd,
233        Self::SoftDollarTiers,
234        Self::FamilyCodes,
235        Self::SymbolSamples,
236        Self::MarketDepthExchanges,
237        Self::TickRequestParameters,
238        Self::SmartComponents,
239        Self::NewsArticle,
240        Self::TickNews,
241        Self::NewsProviders,
242        Self::HistoricalNews,
243        Self::HistoricalNewsEnd,
244        Self::HeadTimestamp,
245        Self::HistogramData,
246        Self::HistoricalDataUpdate,
247        Self::RerouteMarketDataRequest,
248        Self::RerouteMarketDepthRequest,
249        Self::MarketRule,
250        Self::Pnl,
251        Self::PnlSingle,
252        Self::HistoricalTicks,
253        Self::HistoricalTicksBidAsk,
254        Self::HistoricalTicksLast,
255        Self::TickByTick,
256        Self::OrderBound,
257        Self::CompletedOrder,
258        Self::CompletedOrdersEnd,
259        Self::ReplaceFaEnd,
260        Self::WshMetaData,
261        Self::WshEventData,
262        Self::HistoricalSchedule,
263        Self::UserInfo,
264        Self::HistoricalDataEnd,
265        Self::CurrentTimeInMillis,
266        Self::ConfigResponse,
267        Self::UpdateConfigResponse,
268    ];
269}
270
271/// Outgoing TWS API message identifiers.
272#[derive(Debug, Clone, Copy, PartialEq, Eq)]
273#[repr(i32)]
274pub enum Outgoing {
275    /// Request market data.
276    ReqMktData = 1,
277    /// Cancel market data.
278    CancelMktData = 2,
279    /// Place order.
280    PlaceOrder = 3,
281    /// Cancel order.
282    CancelOrder = 4,
283    /// Request open orders.
284    ReqOpenOrders = 5,
285    /// Request account data.
286    ReqAcctData = 6,
287    /// Request executions.
288    ReqExecutions = 7,
289    /// Request ids.
290    ReqIds = 8,
291    /// Request contract data.
292    ReqContractData = 9,
293    /// Request market depth.
294    ReqMktDepth = 10,
295    /// Cancel market depth.
296    CancelMktDepth = 11,
297    /// Request news bulletins.
298    ReqNewsBulletins = 12,
299    /// Cancel news bulletins.
300    CancelNewsBulletins = 13,
301    /// Set server log level.
302    SetServerLogLevel = 14,
303    /// Request auto-open orders.
304    ReqAutoOpenOrders = 15,
305    /// Request all open orders.
306    ReqAllOpenOrders = 16,
307    /// Request managed accounts.
308    ReqManagedAccounts = 17,
309    /// Request FA data.
310    ReqFa = 18,
311    /// Replace FA data.
312    ReplaceFa = 19,
313    /// Request historical data.
314    ReqHistoricalData = 20,
315    /// Exercise options.
316    ExerciseOptions = 21,
317    /// Request scanner subscription.
318    ReqScannerSubscription = 22,
319    /// Cancel scanner subscription.
320    CancelScannerSubscription = 23,
321    /// Request scanner parameters.
322    ReqScannerParameters = 24,
323    /// Cancel historical data.
324    CancelHistoricalData = 25,
325    /// Request current time.
326    ReqCurrentTime = 49,
327    /// Request real-time bars.
328    ReqRealTimeBars = 50,
329    /// Cancel real-time bars.
330    CancelRealTimeBars = 51,
331    /// Request implied volatility calculation.
332    ReqCalcImpliedVolat = 54,
333    /// Request option price calculation.
334    ReqCalcOptionPrice = 55,
335    /// Cancel implied volatility calculation.
336    CancelCalcImpliedVolat = 56,
337    /// Cancel option price calculation.
338    CancelCalcOptionPrice = 57,
339    /// Request global cancel.
340    ReqGlobalCancel = 58,
341    /// Request market data type.
342    ReqMarketDataType = 59,
343    /// Request positions.
344    ReqPositions = 61,
345    /// Request account summary.
346    ReqAccountSummary = 62,
347    /// Cancel account summary.
348    CancelAccountSummary = 63,
349    /// Cancel positions.
350    CancelPositions = 64,
351    /// Verify request.
352    VerifyRequest = 65,
353    /// Verify message.
354    VerifyMessage = 66,
355    /// Query display groups.
356    QueryDisplayGroups = 67,
357    /// Subscribe to group events.
358    SubscribeToGroupEvents = 68,
359    /// Update display group.
360    UpdateDisplayGroup = 69,
361    /// Unsubscribe from group events.
362    UnsubscribeFromGroupEvents = 70,
363    /// Start API.
364    StartApi = 71,
365    /// Verify and auth request.
366    VerifyAndAuthRequest = 72,
367    /// Verify and auth message.
368    VerifyAndAuthMessage = 73,
369    /// Request positions multi.
370    ReqPositionsMulti = 74,
371    /// Cancel positions multi.
372    CancelPositionsMulti = 75,
373    /// Request account updates multi.
374    ReqAccountUpdatesMulti = 76,
375    /// Cancel account updates multi.
376    CancelAccountUpdatesMulti = 77,
377    /// Request security definition option parameters.
378    ReqSecDefOptParams = 78,
379    /// Request soft dollar tiers.
380    ReqSoftDollarTiers = 79,
381    /// Request family codes.
382    ReqFamilyCodes = 80,
383    /// Request matching symbols.
384    ReqMatchingSymbols = 81,
385    /// Request market depth exchanges.
386    ReqMktDepthExchanges = 82,
387    /// Request smart components.
388    ReqSmartComponents = 83,
389    /// Request news article.
390    ReqNewsArticle = 84,
391    /// Request news providers.
392    ReqNewsProviders = 85,
393    /// Request historical news.
394    ReqHistoricalNews = 86,
395    /// Request head timestamp.
396    ReqHeadTimestamp = 87,
397    /// Request histogram data.
398    ReqHistogramData = 88,
399    /// Cancel histogram data.
400    CancelHistogramData = 89,
401    /// Cancel head timestamp.
402    CancelHeadTimestamp = 90,
403    /// Request market rule.
404    ReqMarketRule = 91,
405    /// Request PnL.
406    ReqPnl = 92,
407    /// Cancel PnL.
408    CancelPnl = 93,
409    /// Request PnL single.
410    ReqPnlSingle = 94,
411    /// Cancel PnL single.
412    CancelPnlSingle = 95,
413    /// Request historical ticks.
414    ReqHistoricalTicks = 96,
415    /// Request tick-by-tick data.
416    ReqTickByTickData = 97,
417    /// Cancel tick-by-tick data.
418    CancelTickByTickData = 98,
419    /// Request completed orders.
420    ReqCompletedOrders = 99,
421    /// Request WSH metadata.
422    ReqWshMetaData = 100,
423    /// Cancel WSH metadata.
424    CancelWshMetaData = 101,
425    /// Request WSH event data.
426    ReqWshEventData = 102,
427    /// Cancel WSH event data.
428    CancelWshEventData = 103,
429    /// Request user info.
430    ReqUserInfo = 104,
431    /// Request current time in milliseconds.
432    ReqCurrentTimeInMillis = 105,
433    /// Cancel contract data.
434    CancelContractData = 106,
435    /// Cancel historical ticks.
436    CancelHistoricalTicks = 107,
437    /// Request config.
438    ReqConfig = 108,
439    /// Update config.
440    UpdateConfig = 109,
441}
442
443impl Outgoing {
444    /// Numeric protocol id.
445    pub const fn id(self) -> i32 {
446        self as i32
447    }
448
449    /// Numeric protobuf protocol id.
450    pub const fn protobuf_id(self) -> i32 {
451        self.id() + PROTOBUF_MSG_ID
452    }
453}
454
455impl TryFrom<i32> for Incoming {
456    type Error = ();
457
458    fn try_from(value: i32) -> Result<Self, Self::Error> {
459        Ok(match value {
460            1 => Self::TickPrice,
461            2 => Self::TickSize,
462            3 => Self::OrderStatus,
463            4 => Self::ErrorMessage,
464            5 => Self::OpenOrder,
465            6 => Self::AccountValue,
466            7 => Self::PortfolioValue,
467            8 => Self::AccountUpdateTime,
468            9 => Self::NextValidId,
469            10 => Self::ContractData,
470            11 => Self::ExecutionData,
471            12 => Self::MarketDepth,
472            13 => Self::MarketDepthL2,
473            14 => Self::NewsBulletins,
474            15 => Self::ManagedAccounts,
475            16 => Self::ReceiveFa,
476            17 => Self::HistoricalData,
477            18 => Self::BondContractData,
478            19 => Self::ScannerParameters,
479            20 => Self::ScannerData,
480            21 => Self::TickOptionComputation,
481            45 => Self::TickGeneric,
482            46 => Self::TickString,
483            47 => Self::TickEfp,
484            49 => Self::CurrentTime,
485            50 => Self::RealTimeBars,
486            52 => Self::ContractDataEnd,
487            53 => Self::OpenOrderEnd,
488            54 => Self::AccountDownloadEnd,
489            55 => Self::ExecutionDataEnd,
490            56 => Self::DeltaNeutralValidation,
491            57 => Self::TickSnapshotEnd,
492            58 => Self::MarketDataType,
493            59 => Self::CommissionAndFeesReport,
494            61 => Self::PositionData,
495            62 => Self::PositionEnd,
496            63 => Self::AccountSummary,
497            64 => Self::AccountSummaryEnd,
498            65 => Self::VerifyMessageApi,
499            66 => Self::VerifyCompleted,
500            67 => Self::DisplayGroupList,
501            68 => Self::DisplayGroupUpdated,
502            69 => Self::VerifyAndAuthMessageApi,
503            70 => Self::VerifyAndAuthCompleted,
504            71 => Self::PositionMulti,
505            72 => Self::PositionMultiEnd,
506            73 => Self::AccountUpdateMulti,
507            74 => Self::AccountUpdateMultiEnd,
508            75 => Self::SecurityDefinitionOptionParameter,
509            76 => Self::SecurityDefinitionOptionParameterEnd,
510            77 => Self::SoftDollarTiers,
511            78 => Self::FamilyCodes,
512            79 => Self::SymbolSamples,
513            80 => Self::MarketDepthExchanges,
514            81 => Self::TickRequestParameters,
515            82 => Self::SmartComponents,
516            83 => Self::NewsArticle,
517            84 => Self::TickNews,
518            85 => Self::NewsProviders,
519            86 => Self::HistoricalNews,
520            87 => Self::HistoricalNewsEnd,
521            88 => Self::HeadTimestamp,
522            89 => Self::HistogramData,
523            90 => Self::HistoricalDataUpdate,
524            91 => Self::RerouteMarketDataRequest,
525            92 => Self::RerouteMarketDepthRequest,
526            93 => Self::MarketRule,
527            94 => Self::Pnl,
528            95 => Self::PnlSingle,
529            96 => Self::HistoricalTicks,
530            97 => Self::HistoricalTicksBidAsk,
531            98 => Self::HistoricalTicksLast,
532            99 => Self::TickByTick,
533            100 => Self::OrderBound,
534            101 => Self::CompletedOrder,
535            102 => Self::CompletedOrdersEnd,
536            103 => Self::ReplaceFaEnd,
537            104 => Self::WshMetaData,
538            105 => Self::WshEventData,
539            106 => Self::HistoricalSchedule,
540            107 => Self::UserInfo,
541            108 => Self::HistoricalDataEnd,
542            109 => Self::CurrentTimeInMillis,
543            110 => Self::ConfigResponse,
544            111 => Self::UpdateConfigResponse,
545            _ => return Err(()),
546        })
547    }
548}