Exemplo n.º 1
0
        /// <summary>
        /// Loads the markets.
        /// </summary>
        private void LoadMarkets()
        {
            if (marketsLoaded == null) marketsLoaded = new Dictionary<String, Market>();
            if (marketList == null) marketList = new MarketList();
            if (!IsRunning) return;
            IsRunning = false;

            try
            {
                // Remove markets that have expired
                marketsLoaded = (Dictionary<String, Market>)CleanUpMarketsLoadedList(marketsLoaded);

                // Notify the user
                SendMessage("AutoMarketLoader: Market loader looking for new markets.");

                foreach (var exchangeId in getAllMarkets.ExchangeIds)
                {
                    marketList = null;
                    marketList = Core.betfairAPI.GetAllMarketsObject(exchangeId, getAllMarkets.Countries,
                                                                     getAllMarkets.EventIds,
                                                                     Convert.ToInt32(getAllMarkets.EventDateTo),
                                                                     Convert.ToInt32(getAllMarkets.EventDateFrom));

                    if (marketList == null) continue;

                    SendMessage("AutoMarketLoader: " + marketList.Count + " potensial markets found.");

                    foreach (Market market in marketList)
                    {
                        if (marketsLoaded.ContainsKey((market.exchangeId + ":" + market.marketId)) ||
                            market.status != MarketStatus.ACTIVE) continue;

                        var marketProperties = GetObjectValues(market);

                        foreach (var strategy in strategies)
                        {
                            if (strategy.FilterGetAllMarketsResults == null) continue;

                            var loadMarket = new bool[strategy.FilterGetAllMarketsResults.Length];

                            for (var x = 0; x < strategy.FilterGetAllMarketsResults.Length; x++)
                            {
                                var endReached = false;
                                loadMarket[x] = true;
                                var query = strategy.FilterGetAllMarketsResults[x];

                                while (!endReached)
                                {
                                    string propertyValue;
                                    if (!marketProperties.ContainsKey(query.Field.ToLower()))
                                    {
                                        loadMarket[x] = false;
                                    }
                                    else
                                    {
                                        propertyValue =
                                            (marketProperties[query.Field.ToLower()].ToString()).ToLower
                                                ();

                                        switch (query.Operator)
                                        {
                                            case QueryOperator.CONTAINS:
                                                if (!propertyValue.Contains(query.Value.ToLower()))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.NOT_CONTAINS:
                                                if (propertyValue.Contains(query.Value.ToLower()))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.EQUAL:
                                                if (propertyValue != query.Value.ToLower())
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.NOT_EQUAL:
                                                if (propertyValue == query.Value.ToLower())
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.GREATER_THAN:
                                                if (Convert.ToDouble(propertyValue) >
                                                    Convert.ToDouble(query.Value))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.LESS_THAN:
                                                if (Convert.ToDouble(propertyValue) <
                                                    Convert.ToDouble(query.Value))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.REGEX_IS_MATCH:
                                                if (
                                                    !Regex.IsMatch(
                                                         (marketProperties[query.Field.ToLower()].ToString()),
                                                         query.Value))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                            case QueryOperator.REGEX_NOT_MATCH:
                                                if (
                                                    Regex.IsMatch(
                                                        (marketProperties[query.Field.ToLower()].ToString()),
                                                        query.Value))
                                                {
                                                    loadMarket[x] = false;
                                                }
                                                break;
                                        }
                                    }

                                    if (query.And != null)
                                    {
                                        query = query.And;
                                    }
                                    else
                                    {
                                        endReached = true;
                                    }
                                }
                            }

                            for (var x = 0; x < loadMarket.Length; x++)
                            {
                                if (!loadMarket[x]) continue;

                                marketsLoaded.Add((market.exchangeId + ":" + market.marketId),
                                                  market);
                                Broker.Execute(EventNames.LoadNewMarketAction, this,
                                               new NewMarketEventArgs(exchangeId, market.marketId,
                                                                      strategy));
                                continue;
                            }
                        }
                    }
                }

                wakeUpTime = DateTime.Now.AddMilliseconds(getAllMarkets.RunMarketsQueryEvery);
                SendMessage("AutoMarketLoader: Going to sleep until " + wakeUpTime);
            }
            catch (Exception ex)
            {
                SendMessage("EXCEPTION: AutoMarketLoader: Message:" + ex.Message);
                SendMessage("EXCEPTION: AutoMarketLoader: Stack Trace:" + ex);
            }

            IsRunning = true;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extract the results of a Betfair GetAllMarketsResp.data string to a MarketList object
        /// </summary>
        /// <param name="marketData"></param>
        /// <returns></returns>
        public MarketList ConvertToObject(string marketData)
        {
            if (marketData != null)
            {
                //Step 1 - Create the response object
                var marketList = new MarketList();

                //Step 2 - Clean up the break characters
                marketData = HelperMethods.ProtectBreakChars(marketData);

                //Step 3 - Split the markets out
                string[] marketStringArray = marketData.Split(":".ToCharArray());

                //Step 4 - Loop through the markets and split out the market items
                for (int x = 0; x < marketStringArray.Length; x++)
                {
                    //Check that the array is not empty
                    if (marketStringArray[x].Length > 0)
                    {
                        //Create the market object
                        var market = new Market();

                        //Step 5 - Split out the markets component items
                        string[] marketItemsStringArray = marketStringArray[x].Split("~".ToCharArray());

                        //Step 6 - Check that the array is equal or greater than 15
                        if (marketItemsStringArray.Length >= 15)
                        {
                            //Step 7 - Restore the break characters
                            for (int y = 0; y < marketItemsStringArray.Length; y++)
                            {
                                marketItemsStringArray[y] = HelperMethods.RestoreBreakChars(marketItemsStringArray[y]);
                            }

                            //Step 8 - Update the market values
                            market.marketId = Convert.ToInt32(marketItemsStringArray[0]);
                            market.name = marketItemsStringArray[1];
                            market.type = marketItemsStringArray[2];
                            market.status = (MarketStatus) Enum.Parse(typeof (MarketStatus), marketItemsStringArray[3]);
                            market.eventDate =
                                new DateTimeCalculations().UnixTimeStampToDateTime(
                                    Convert.ToDouble(marketItemsStringArray[4]));
                            market.menuPath = marketItemsStringArray[5];
                            market.eventHierarchy = HelperMethods.SplitToInt32Array("/", marketItemsStringArray[6]);
                            market.betDelay = Convert.ToInt32(marketItemsStringArray[7]);
                            market.exchangeId = Convert.ToInt32(marketItemsStringArray[8]);
                            market.country = marketItemsStringArray[9];
                            market.apiMarketDataLastRefresh = Convert.ToInt64(marketItemsStringArray[10]);
                            market.numberOfRunners = Convert.ToInt32(marketItemsStringArray[11]);
                            market.numberOfWinners = Convert.ToInt32(marketItemsStringArray[12]);
                            market.totalAmountMatched = Convert.ToDouble(marketItemsStringArray[13]);
                            if (marketItemsStringArray[14].ToLower() == "y") market.bspMarket = true;
                            if (marketItemsStringArray[15].ToLower() == "y") market.turningInPlay = true;

                            //Step 9 - Add the new market item to the response object
                            marketList.Add(market);
                        }
                    }
                }

                //Step 10 - Sort the list items
                marketList.Sort(new MarketDateComparer());

                //Step 11 - Done
                return marketList;
            }
            return null;
        }