示例#1
0
        public TraderMongoDB(MongoFacade mongoDbFacade)
        {
            mongodb = mongoDbFacade;

            pricesCollection = mongodb.getDB().GetCollection("prices");
            dataCollection   = mongodb.getDB().GetCollection("data");
        }
示例#2
0
        public void importData(string data)
        {
            var options = new MongoInsertOptions()
            {
                Flags = InsertFlags.ContinueOnError
            };

            BsonDocument doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(data);

            foreach (string name in doc.Names)
            {
                try
                {
                    var result = mongodb.getDB().GetCollection(name).InsertBatch(doc[name].AsBsonArray, options);
                }
                catch { errors++; }
                //.Insert(doc[name].AsBsonArray); //Untested
            }

            /*int errors = 0;
             * foreach(string name in doc.Names)
             * {
             *  foreach (BsonDocument entry in doc[name].AsBsonArray)
             *      try
             *      {
             *          mongodb.getCollection(name).Insert(entry);
             *      }
             *      catch { errors++;  }
             * }*/
        }
示例#3
0
        void IDataminingDatabase.addOutcome(long timeframe, string instrument)
        {
            //Do in walker? Faster? Todo...
            //{ outcome_max_1800: { $exists: true } }

            List <Thread> threads = new List <Thread>();

            long start = database.getFirstTimestamp();
            long end   = database.getLastTimestamp();

            long oneThreadTimeframe = (end - start) / threadsCount;
            int  threadId           = 0;

            var collection = mongodb.getDB().GetCollection("prices");

            while (threadId < threadsCount)
            {
                long threadBeginning = start + (oneThreadTimeframe * threadId);
                long threadEnd       = threadBeginning + oneThreadTimeframe;

                Thread thread = new Thread(delegate()
                {
                    string name = "outcome " + timeframe + " ID_" + threadBeginning + ":" + threadEnd;
                    progress.setProgress(name, 0);
                    int done   = 0;
                    long count = 0;

                    var docs = collection.FindAs <BsonDocument>(Query.And(Query.EQ("instrument", instrument), Query.NotExists("outcome_max_" + timeframe), Query.LT("timestamp", threadEnd), Query.GTE("timestamp", threadBeginning))).SetSortOrder(SortBy.Ascending("timestamp"));
                    docs.SetFlags(QueryFlags.NoCursorTimeout);

                    count = docs.Count();
                    foreach (BsonDocument doc in docs)
                    {
                        done++;
                        progress.setProgress(name, Convert.ToInt32(Convert.ToDouble(done) / Convert.ToDouble(count) * 100d));

                        try
                        {
                            TickData inTimeframe = database.getPrice(doc["timestamp"].AsInt64 + timeframe, doc["instrument"].AsString);

                            var min_doc = collection.FindAs <BsonDocument>(Query.And(Query.EQ("instrument", doc["instrument"].AsString), Query.LT("timestamp", doc["timestamp"].AsInt64 + timeframe), Query.GT("timestamp", doc["timestamp"].AsInt64)))
                                          .SetSortOrder(SortBy.Ascending("bid"))
                                          .SetLimit(1)
                                          .SetFields("bid")
                                          .Single();

                            var max_doc = collection.FindAs <BsonDocument>(Query.And(Query.EQ("instrument", doc["instrument"].AsString), Query.LT("timestamp", doc["timestamp"].AsInt64 + timeframe), Query.GT("timestamp", doc["timestamp"].AsInt64)))
                                          .SetSortOrder(SortBy.Descending("ask"))
                                          .SetLimit(1)
                                          .SetFields("ask")
                                          .Single();

                            string s = max_doc["ask"].AsDouble.ToString();
                            s        = s + "";

                            collection.FindAndModify(new FindAndModifyArgs()
                            {
                                Query  = Query.EQ("_id", doc["_id"]),
                                Update = Update.Combine(
                                    Update.Set("outcome_max_" + timeframe, max_doc["ask"]),
                                    Update.Set("outcome_min_" + timeframe, min_doc["bid"]),
                                    Update.Set("outcome_actual_" + timeframe, inTimeframe.getAvgPrice())
                                    )
                            });
                        }
                        catch { }
                    }

                    progress.remove(name);
                });

                thread.Start();
                threads.Add(thread);

                threadId++;
            }

            waitForThreads(threads);
        }