wvietor / ibkr_rust

A Rust port of the Interactive Brokers TWS API.
Apache License 2.0
8 stars 3 forks source link

Open Order not being fully deserialized -> missing fields. #8

Open fraschm1998 opened 3 days ago

fraschm1998 commented 3 days ago

deserialize_contract_proxy not fully deserializing open_order_msg. Not deserializing whether it's a MKT or LMT order, and if it's a LMT order, the cost. Trying to implement it myself but am confused as to what order_types is supposed to be. You have it set as a default string vec, howver shouldn't it be an OrderType enum between MKT and LMT and other OrderTypes?

https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#fa-orders

order = Order()
order.action = "BUY"
order.orderType = "MKT"
order.totalQuantity = 50
order.faGroup = "MyTestProfile"

I see you have some of this and am confused as to what it covers.

    #[inline]
    fn order_types(&self) -> &Vec<String> {
        match_poly!(self;
            Self::Forex(t)
            | Self::Crypto(t)
            | Self::Stock(t)
            | Self::Index(t)
            | Self::SecFuture(t)
            | Self::SecOption(t)
            | Self::Commodity(t) => t.order_types()
        )
    }

What I did so far replicated from OrderSide:

executions.rs

#[derive(Debug, Clone, Copy, Ord, PartialOrd, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// The possible order types for an order
pub enum OrderType {
    #[serde(rename = "MKT")]
    /// A market order
    Mkt,
    #[serde(rename = "LMT")]
    /// A limit order
    Lmt,
    #[serde(rename = "MIDPRICE")]
    /// A mid-price order
    MidPrice,
    #[serde(rename = "MTL")]
    /// A market-to-limit order
    Mtl,
    #[serde(rename = "STP")]
    /// A stop order
    Stp,
    #[serde(rename = "STP LMT")]
    /// A stop-limit order
    StpLmt,
    #[serde(rename = "TRAIL")]
    /// A trailing stop order
    Trail,
    #[serde(rename = "TRAIL LIMIT")]
    /// A trailing stop limit order
    TrailLimit,
    #[serde(rename = "REL")]
    /// A relative order
    Rel,
    #[serde(rename = "RPI")]
    /// A retail price improvement order
    Rpi,
    #[serde(rename = "PEG MID")]
    /// A pegged to midpoint order
    PegMid,
    #[serde(rename = "PEG BEST")]
    /// A pegged to best order
    PegBest,
    #[serde(rename = "SNAP MKT")]
    /// A snap market order
    SnapMkt,
    #[serde(rename = "SNAP MID")]
    /// A snap mid order
    SnapMid,
    #[serde(rename = "SNAP PRIM")]
    /// A snap primary order
    SnapPrim,
    #[serde(rename = "MOC")]
    /// A market-on-close order
    Moc,
    #[serde(rename = "LOC")]
    /// A limit-on-close order
    Loc,
    #[serde(rename = "IBALGO")]
    /// An IB algorithmic order
    IbAlgo,
    #[serde(rename = "Adaptive (IBALGO)")]
    /// An adaptive IB algorithmic order
    AdaptiveIbAlgo,
}

#[derive(Debug, Default, Clone, thiserror::Error)]
#[error("Invalid value encountered when attempting to parse an order type. No such order type: {0}. Valid order types include \"MKT\", \"LMT\", \"MIDPRICE\", etc.")]
/// An error returned when parsing an [`OrderType`] fails.
pub struct ParseOrderTypeError(String);

impl std::str::FromStr for OrderType {
    type Err = ParseOrderTypeError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "MKT" => Ok(Self::Mkt),
            "LMT" => Ok(Self::Lmt),
            "MIDPRICE" => Ok(Self::MidPrice),
            "MTL" => Ok(Self::Mtl),
            "STP" => Ok(Self::Stp),
            "STP LMT" => Ok(Self::StpLmt),
            "TRAIL" => Ok(Self::Trail),
            "TRAIL LIMIT" => Ok(Self::TrailLimit),
            "REL" => Ok(Self::Rel),
            "RPI" => Ok(Self::Rpi),
            "PEG MID" => Ok(Self::PegMid),
            "PEG BEST" => Ok(Self::PegBest),
            "SNAP MKT" => Ok(Self::SnapMkt),
            "SNAP MID" => Ok(Self::SnapMid),
            "SNAP PRIM" => Ok(Self::SnapPrim),
            "MOC" => Ok(Self::Moc),
            "LOC" => Ok(Self::Loc),
            "IBALGO" => Ok(Self::IbAlgo),
            "Adaptive (IBALGO)" => Ok(Self::AdaptiveIbAlgo),
            other => Err(ParseOrderTypeError(other.to_owned())),
        }
    }
}
fraschm1998 commented 3 days ago

Or did you do Vec to decode all of them instead of an enum for as right now the order_type: Vec for the open_order_msg returns empty.

image

wvietor commented 3 days ago

Yeah, I've struggled on the implementation here. It's somewhat complicated to reassemble the order fields. It's not necessarily clear what information is guaranteed to be included. Working on a fix

fraschm1998 commented 3 days ago

Yeah, I've struggled on the implementation here. It's somewhat complicated to reassemble the order fields. It's not necessarily clear what information is guaranteed to be included. Working on a fix

Maybe just replicate the output of the rust-ibapi library on lib.rs?

https://docs.rs/ibapi/latest/ibapi/orders/enum.OrderDataResult.html

wvietor commented 3 days ago

Yeah I'll take a look. It looks like their solution is quite "dynamic" (uses lots of Option), but I think that's find for this case. Will work on this shortly.

fraschm1998 commented 3 days ago

Here's the output of an open order:

SECOND Intod2Iter(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 12:55:50 [239/1000]
    [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        "320227571",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        "QQQ",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "STK",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "?",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "SMART",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        "USD",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "QQQ",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "NMS",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "BUY",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "100",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "LMT",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "482.21",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
        "0.0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "DAY",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "DU5614130",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "1462182896",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "1462182896.0/DU5614130/100",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "-1",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "2147483647",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "None",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "0",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        "",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        "",                                                                                                                                  
        "",                                                                                                                                  
        "?",                                                                                                                                 
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "2147483647",                                                                                                                        
        "2147483647",                                                                                                                        
        "",                                                                                                                                  
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "IB",                                                                                                                                
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "Submitted",                                                                                                                         
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "None",                                                                                                                              
        "1.7976931348623157E308",                                                                                                            
        "495.83",                                                                                                                            
        "1.7976931348623157E308",
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "1.7976931348623157E308",                                                                                                            
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "1",                                                                                                                                 
        "0",                                                                                                                                 
        "0",                                                                                                                                 
        "1",                                                                                                                                 
        "",                                                                                                                                  
        "",                                                                                                                                  
        "0",                                                                                                                                 
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
        "",                                                                                                                                  
    ],                                                                                                                                       
)
fraschm1998 commented 3 days ago

Taken from:

        async move {
            decode_fields!(
                fields =>
                    order_id @ 1: i64,
            );

            println!("SECOND {:#?}", fields);

            let proxy = deserialize_contract_proxy::<crate::contract::proxy_indicators::HasExchange>(
                fields,
            )?;