Esempio n. 1
0
        /// <summary>
        /// Fetch an order book for an instrument.
        /// </summary>
        /// <param name="pairs"></param>
        /// <param name="queryNVC"></param>
        /// <returns></returns>
        public async Task <OrderBook> GetInstrumentOrderBook(InstrumentName pair, DateTime?time = null)
        {
            NameValueCollection queryNVC = new NameValueCollection();

            if (time != null)
            {
                queryNVC.Add("time", time.Value.ToUniversalTime().ToString());
            }

            string path  = string.Format("instruments/{0}/orderBook", pair.ToString());
            string query = QueryFromNVC(queryNVC);

            using (HttpResponseMessage response = await RequestAsync(EHostAPIType.REST, HttpMethod.Get, path, query))
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException(JObject.Parse(await response.Content.ReadAsStringAsync())["errorMessage"].ToString());
                }

                using (Stream stream = await response.Content.ReadAsStreamAsync())
                {
                    string jsonString = JObject.Parse(await new StreamReader(stream).ReadToEndAsync())["orderBook"].ToString();
                    return(JsonConvert.DeserializeObject <OrderBook>(jsonString, ErrorHandlingSetting));
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// A MarketOrderRequest specifies the parameters that may be set when creating a Market Order.
 /// </summary>
 /// <param name="instrument">The Market Order’s Instrument.</param>
 /// <param name="units">The quantity requested to be filled by the Market Order. A posititive number of units results in a long Order, and a negative number of units results in a short Order.</param>
 public MarketOrderRequest(InstrumentName instrument, double units)
 {
     this.Type         = EOrderType.MARKET.ToString();
     this.Instrument   = instrument.ToString();
     this.Units        = units;
     this.TimeInForce  = ETimeInForce.FOK.ToString();
     this.PositionFill = EOrderPositionFill.DEFAULT.ToString();
 }
Esempio n. 3
0
 /// <summary>
 /// A StopOrderRequest specifies the parameters that may be set when creating a Stop Order.
 /// </summary>
 /// <param name="instrument">The Stop Order’s Instrument.</param>
 /// <param name="units">The quantity requested to be filled by the Stop Order. A posititive number of units results in a long Order, and a negative number of units results in a short Order.</param>
 /// <param name="price">The price threshold specified for the Stop Order. The Stop Order will only be filled by a market price that is equal to or worse than this price.</param>
 public StopOrderRequest(InstrumentName instrument, double units, PriceValue price)
 {
     this.Type             = EOrderType.STOP.ToString();
     this.Instrument       = instrument.ToString();
     this.Units            = units;
     this.Price            = price;
     this.TimeInForce      = ETimeInForce.GTC.ToString();
     this.PositionFill     = EOrderPositionFill.DEFAULT.ToString();
     this.TriggerCondition = EOrderTriggerCondition.DEFAULT.ToString();
 }
Esempio n. 4
0
 /// <summary>
 /// A MarketIfTouchedOrderRequest specifies the parameters that may be set when creating a Market-if-Touched Order.
 /// </summary>
 /// <param name="instrument">The MarketIfTouched Order’s Instrument.</param>
 /// <param name="units">The quantity requested to be filled by the MarketIfTouched Order. A posititive number of units results in a long Order, and a negative number of units results in a short Order.</param>
 /// <param name="price">The price threshold specified for the MarketIfTouched Order. The MarketIfTouched Order will only be filled by a market price that crosses this price from the direction of the market price at the time when the Order was created (the initialMarketPrice). Depending on the value of the Order’s price and initialMarketPrice, the MarketIfTouchedOrder will behave like a Limit or a Stop Order.</param>
 public MarketIfTouchedOrderRequest(InstrumentName instrument, double units, PriceValue price)
 {
     this.Type             = EOrderType.MARKET_IF_TOUCHED.ToString();
     this.Instrument       = instrument.ToString();
     this.Units            = units;
     this.Price            = price;
     this.TimeInForce      = ETimeInForce.GTC.ToString();
     this.PositionFill     = EOrderPositionFill.DEFAULT.ToString();
     this.TriggerCondition = EOrderTriggerCondition.DEFAULT.ToString();
 }
Esempio n. 5
0
 //
 // *************************************************************
 // ****                      AddBook()                      ****
 // *************************************************************
 //
 /// <summary>
 /// Strategy wants to get a book for a single instrument.  This method is usually called by the Strategy
 /// or pricing model during SetupComplete().
 /// </summary>
 /// <param name="instrumentName"></param>
 public void AddBook(InstrumentName instrumentName)
 {
     if (m_Instrument != null)
     {   // a new instrument we should get a book for.
         m_Instrument = instrumentName;
         m_OrderBook  = m_OrderHub.CreateOrderBook(instrumentName);
         m_StrategyHub.SubscribeToFills(m_Strategy, m_OrderBook);                                  // tell StrategyHub what events i want, and who they belong to.
         m_StrategyHub.SubscribeToMajorOrderStatusEvents(m_Strategy, m_OrderBook);
         m_StrategyHub.SubscribeToOrderSubmitted(m_Strategy, m_OrderBook);
         if (m_IsMarketReady && m_StrategyHub.m_Market.TryGetInstrumentDetails(instrumentName, out m_InstrumentDetails))
         {
             m_FillBook = new FillBook(instrumentName.ToString(), m_InstrumentDetails.Multiplier);
         }
     }
 }// AddBook()
Esempio n. 6
0
        /// <summary>
        /// Get a list of Orders for an Account
        /// </summary>
        /// <param name="orderIDs">List of Order IDs to retrieve.</param>
        /// <param name="orderStateFilter">The state to filter the requested Orders by [default=PENDING].</param>
        /// <param name="instrument">The instrument to filter the requested orders by.</param>
        /// <param name="count">The maximum number of Orders to return [default=50, maximum=500].</param>
        /// <param name="beforeID">The maximum Order ID to return. If not provided the most recent Orders in the Account are returned.</param>
        /// <returns></returns>
        public async Task <List <Order> > GetOrders
        (
            InstrumentName instrument,
            List <OrderID> orderIDs,
            OrderStateFilter orderStateFilter,
            int count        = 50,
            OrderID beforeID = null
        )
        {
            #region queryNVC
            NameValueCollection queryNVC = new NameValueCollection()
            {
                { "state", ((EOrderStateFilter)orderStateFilter).ToString() },
                { "count", count.ToString() }
            };
            if (orderIDs != null)
            {
                queryNVC.Add("ids", string.Join(",", orderIDs));
            }
            if (instrument != null)
            {
                queryNVC.Add("instrument", instrument.ToString());
            }
            if (beforeID != null)
            {
                queryNVC.Add("beforeID", beforeID);
            }
            #endregion

            string path  = string.Format("accounts/{0}/orders", AccountID);
            string query = QueryFromNVC(queryNVC);

            using (HttpResponseMessage response = await RequestAsync(EHostAPIType.REST, HttpMethod.Get, path, query))
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException(JObject.Parse(await response.Content.ReadAsStringAsync())["errorMessage"].ToString());
                }

                using (Stream stream = await response.Content.ReadAsStreamAsync())
                {
                    string jsonString = JObject.Parse(await new StreamReader(stream).ReadToEndAsync())["orders"].ToString();
                    return(JsonConvert.DeserializeObject <List <Order> >(jsonString, ErrorHandlingSetting));
                }
            }
        }
Esempio n. 7
0
        //
        #endregion//Properties

        #region Public Methods
        // *****************************************************************
        // ****                 Public Methods                         ****
        // *****************************************************************
        //
        //
        // *************************************************************
        // ****             MarketInstrumentInitialized()           ****
        // *************************************************************
        //
        /// <summary>
        /// called once the market for all instruments is subscribed to and we have instrument
        /// details
        /// </summary>
        public override void MarketInstrumentInitialized(Lib.BookHubs.Book marketBook)
        {
            if (m_IsMarketReady)
            {
                return;
            }
            m_IsMarketReady = true;
            if (m_StrategyHub.m_Market.TryGetInstrumentDetails(m_Instrument, out m_InstrumentDetails))
            {
                if (m_FillBook == null)
                {
                    m_FillBook = new FillBook(m_Instrument.ToString(), m_InstrumentDetails.Multiplier);
                }
                if (double.IsNaN(m_QuoteTickSize))                      // if our user hasn't defined this yet
                {
                    m_QuoteTickSize = m_InstrumentDetails.TickSize;     // set it to the default tick size here
                }
            }
            else
            {
                Log.NewEntry(LogLevel.Error,
                             "OrderEngine:MarketInstrumentInitialized failed to get instrument details and create order book for {0}", m_Instrument);
            }
        }
Esempio n. 8
0
        }//TryCreateNewBook()

        //
        //
        // *************************************************************
        // ****                     GetRequest()                    ****
        // *************************************************************
        // <summary>
        // This utility method uses the RequestFactory for request to create
        // and clear a request for new use.
        // </summary>
        // <param name="request"></param>
        // <returns></returns>

        /*
         * protected MarketHubRequest GetRequest(RequestCode request)
         * {
         *  MarketHubRequest eventArg = m_RequestFactory.Get();
         *  eventArg.Request = request;
         *  eventArg.Data.Clear();
         *  return eventArg;
         * }//GetRequest()
         * //
         * //
         * protected void RecycleRequest(MarketHubRequest requestToRecycle)
         * {
         *  //m_RequestFactory.Recycle(requestToRecycle);
         * }
         */
        //
        //
        // *************************************************************
        // ****          TryAddInstrumentDetails()                  ****
        // *************************************************************
        /// <summary>
        /// Hub thread calls to try and add instrument details to the dictionary table.
        /// </summary>
        /// <param name="instrName"></param>
        /// <param name="instrDetails"></param>
        protected bool TryAddInstrumentDetails(InstrumentName instrName, InstrumentDetails instrDetails)
        {
            if (m_InstrumentDetailsTable.TryAdd(instrName, instrDetails))           // try and add it
            {
                return(true);
            }
            if (!m_InstrumentDetailsTable.ContainsKey(instrName))                    // if we fail, and we don't already have this instrument...something went wrong!
            {
                Log.NewEntry(LogLevel.Major, "MarketHub.TryAddInstrumentDetails: {0} Failed to Add to Instrument Details Dictionary.", instrName.ToString());
            }
            return(false);
        }
 /// <summary>
 /// Get the instrument and granularity based blob folder
 /// </summary>
 /// <param name="instrument"></param>
 /// <param name="granularity"></param>
 /// <returns></returns>
 private string GetInstrumentFolder(InstrumentName instrument, Granularity granularity) => $"{instrument.ToString().ToUpper()}/{granularity.ToString().ToUpper()}";