예제 #1
0
        private void DownloadFills()
        {
            // Perform REST request/response to download fill data, specifying our cached minimum timestamp as a starting point.
            // On a successful response the timestamp will be updated so we run no risk of downloading duplicate fills.

            List <TT_Fill> fills = new List <TT_Fill>();

            do
            {
                var min_param = new RestSharp.Parameter("minTimestamp", TT_Info.ToRestTimestamp(m_minTimeStamp).ToString(), RestSharp.ParameterType.QueryString);

                RestSharp.IRestResponse result = RestManager.GetRequest("ledger", "fills", min_param);

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception("Request for fills unsuccessful. Status:" + result.StatusCode.ToString() + "Error Message: " + result.ErrorMessage);
                }

                JObject json_data = JObject.Parse(result.Content);
                foreach (var fill in json_data["fills"])
                {
                    fills.Add(new TT_Fill(fill));
                }

                fills.Sort((f1, f2) => f1.UtcTimeStamp.CompareTo(f2.UtcTimeStamp));
                RaiseFillDownloadEvent(fills);

                if (fills.Count > 0 && m_running)
                {
                    m_minTimeStamp = new DateTime(fills[fills.Count - 1].UtcTimeStamp.Ticks + 1);
                }
            }while (fills.Count == TT_Info.MAX_RESPONSE_FILLS);
        }
예제 #2
0
        private static TT_User RequestUser(string user_id)
        {
            var          result = RestManager.GetRequest("risk", "user/" + user_id);
            UserResponse usr    = JsonConvert.DeserializeObject <UserResponse>(result.Content);

            return(usr.user[0]);
        }
예제 #3
0
        private static void GetProductData()
        {
            var         result       = RestManager.GetRequest("ttpds", "productdata");
            ProductData product_data = JsonConvert.DeserializeObject <ProductData>(result.Content);

            if (product_data.productTypes != null)
            {
                privInstance.dicts.Add("productTypes", CreateDictionary(product_data.productTypes));
            }
            else
            {
                throw new Exception("GET ProductData request failed" + Environment.NewLine + result.Content.ToString());
            }
        }
예제 #4
0
        private static void GetMarkets()
        {
            var       result  = RestManager.GetRequest("ttpds", "markets");
            TTMarkets markets = JsonConvert.DeserializeObject <TTMarkets>(result.Content);

            if (markets.Markets != null)
            {
                privInstance.dicts.Add("markets", CreateDictionary(markets.Markets));
            }
            else
            {
                throw new Exception("GET Market request failed" + Environment.NewLine + result.Content.ToString());
            }
        }
예제 #5
0
        private static TT_Account RequestAccount(string account_id)
        {
            var result = RestManager.GetRequest("risk", "account/" + account_id);

            try
            {
                AccountResponse acct = JsonConvert.DeserializeObject <AccountResponse>(result.Content);
                return(acct.account[0]);
            }
            catch (Exception)
            {
                TT_Account acct = new TT_Account();
                acct.id   = account_id;
                acct.name = "PLACEHOLDER";
                return(acct);
            }
        }
예제 #6
0
        private static void GetInstrumentData()
        {
            var            result          = RestManager.GetRequest("ttpds", "instrumentdata");
            InstrumentData instrument_data = JsonConvert.DeserializeObject <InstrumentData>(result.Content);

            if (instrument_data.optionCodes != null && instrument_data.optionSchemes != null && instrument_data.seriesTerms != null && instrument_data.comboTypes != null)
            {
                privInstance.dicts.Add("optionCodes", CreateDictionary(instrument_data.optionCodes));
                privInstance.dicts.Add("optionSchemes", CreateDictionary(instrument_data.optionSchemes));
                privInstance.dicts.Add("seriesTerms", CreateDictionary(instrument_data.seriesTerms));
                privInstance.dicts.Add("comboTypes", CreateDictionary(instrument_data.comboTypes));
            }
            else
            {
                throw new Exception("GET InstrumentData request failed" + Environment.NewLine + result.Content.ToString());
            }
        }
예제 #7
0
        private static void GetOrderData()
        {
            var       result     = RestManager.GetRequest("ttledger", "orderdata");
            OrderData order_data = JsonConvert.DeserializeObject <OrderData>(result.Content);

            if (order_data.data != null)
            {
                foreach (var entry in order_data.data)
                {
                    privInstance.dicts.Add(entry.Key, entry.Value);
                }
            }
            else
            {
                throw new Exception("GET OrderData request failed" + Environment.NewLine + result.Content.ToString());
            }
        }
예제 #8
0
        private static TT_Instrument RequestInstrument(string instr_id)
        {
            var result = RestManager.GetRequest("pds", "instrument/" + instr_id);

            InstrumentResponse response = JsonConvert.DeserializeObject <InstrumentResponse>(result.Content);

            if (response.instrument != null)
            {
                return(response.instrument[0]);
            }
            else
            {
                TT_Instrument instr = new TT_Instrument();
                instr.id            = instr_id;
                instr.alias         = "Permission Denied";
                instr.productSymbol = "Permission Denied";
                instr.productTypeId = 34;
                instr.optionCodeId  = 2;
                return(instr);
            }
        }
예제 #9
0
        private static TT_User RequestUser(string user_id)
        {
            var          result   = RestManager.GetRequest("risk", "user/" + user_id);
            UserResponse response = JsonConvert.DeserializeObject <UserResponse>(result.Content);
            TT_User      usr      = null;

            if (response.Status == "Ok")
            {
                usr = response.user[0];
            }
            else
            {
                // If the REST API user cannot view this user, make a dummy variable
                usr                    = new TT_User();
                usr.id                 = user_id;
                usr.alias              = "user_id:" + user_id;
                usr.company            = new TT_Company();
                usr.company.name       = "user_id:" + user_id;
                usr.company.id         = -1;
                usr.company.abbrevName = "user_id:" + user_id;
            }
            return(usr);
        }
예제 #10
0
        private void DownloadFills()
        {
            // Perform REST request/response to download fill data, specifying our cached minimum timestamp as a starting point.
            // On a successful response the timestamp will be updated so we run no risk of downloading duplicate fills.

            bool           should_continue = false;
            List <TT_Fill> fills           = new List <TT_Fill>();

            do
            {
                should_continue = false;

                var min_param = new RestSharp.Parameter("minTimestamp", TT_Info.ToRestTimestamp(m_minTimeStamp).ToString(), RestSharp.ParameterType.QueryString);

                RestSharp.IRestResponse result = RestManager.GetRequest("ledger", "fills", min_param);

                if (result.StatusCode == System.Net.HttpStatusCode.GatewayTimeout)
                {
                    should_continue = true;
                    DateTime max_time = DateTime.Now;

                    int retry_count = 0;
                    for (retry_count = 0; retry_count < max_retries; ++retry_count)
                    {
                        FDLog.LogMessage("Fill request timed out. Retrying....");

                        max_time = m_minTimeStamp + TimeSpan.FromTicks((max_time - m_minTimeStamp).Ticks / 2);
                        var max_param = new RestSharp.Parameter("maxTimestamp", TT_Info.ToRestTimestamp(max_time).ToString(), RestSharp.ParameterType.QueryString);

                        result = RestManager.GetRequest("ledger", "fills", min_param, max_param);

                        if (result.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            m_minTimeStamp = max_time;
                            break;
                        }
                        else if (result.StatusCode != System.Net.HttpStatusCode.GatewayTimeout)
                        {
                            throw new Exception(String.Format("Request for fills unsuccessful. (minTimestamp={0}) - Status: {1} - Error Message: {2}", min_param.Value.ToString(), result.StatusCode.ToString(), result.ErrorMessage));
                        }

                        if (retry_count == max_retries)
                        {
                            throw new Exception("Request for fills unsuccessful. Max Retries exceeded.");
                        }
                    }
                }
                else if (result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception(String.Format("Request for fills unsuccessful. (minTimestamp={0}) - Status: {1} - Error Message: {2}", min_param.Value.ToString(), result.StatusCode.ToString(), result.ErrorMessage));
                }

                JObject json_data = JObject.Parse(result.Content);
                FDLog.LogMessage(String.Format("Downloaded {0} fills.", json_data["fills"].Count()));
                foreach (var fill in json_data["fills"])
                {
                    fills.Add(new TT_Fill(fill));
                }

                fills.Sort((f1, f2) => f1.UtcTimeStamp.CompareTo(f2.UtcTimeStamp));
                RaiseFillDownloadEvent(fills);

                if (fills.Count > 0)
                {
                    m_minTimeStamp = new DateTime(fills[fills.Count - 1].UtcTimeStamp.Ticks + 1);
                }

                should_continue |= (fills.Count == TT_Info.MAX_RESPONSE_FILLS);
                should_continue &= m_running;
            }while (should_continue);
        }
예제 #11
0
 public static IRestResponse GetRequest(string target, string call, params Parameter[] parameters)
 {
     return(RestManager.GetRequest(target, call, 1, parameters));
 }