コード例 #1
0
ファイル: CopyServer.cs プロジェクト: rebider/MT4Proxy.NET
        private void CopyProc()
        {
            TradeInfoEventArgs item = null;
            var sql = string.Empty;

            while (Utils.SignalWait(ref EnableRunning, _signal))
            {
                _queNewTrades.TryDequeue(out item);
                try
                {
                    var trade_type = item.TradeType;
                    var trade      = item.Trade;
                    var key        = string.Empty;
                    var handler    = OnNewTrade;
                    //if (handler != null)
                    //    handler(this, item);
                    //continue;
                    try
                    {
                        if (string.IsNullOrWhiteSpace(InnerServer))
                        {
                            sql = "SELECT user_code FROM user WHERE mt4=@mt4_id";
                            using (var cmd = new SQLiteCommand(sql, Source.CopySource))
                            {
                                cmd.Parameters.AddWithValue("@mt4_id", item.Trade.login);
                                var user_code_obj = cmd.ExecuteScalar();
                                var user_code     = string.Empty;
                                if (user_code_obj != null)
                                {
                                    user_code = user_code_obj.ToString();
                                }
                                item.Usercode = user_code;
                            }
                            sql = "SELECT mt4_from FROM copy_order WHERE order_to=@order_id";
                            using (var cmd = new SQLiteCommand(sql, Source.CopySource))
                            {
                                cmd.Parameters.AddWithValue("@order_id", item.Trade.order);
                                var from_obj = cmd.ExecuteScalar();
                                if (from_obj != null)
                                {
                                    item.FromUsercode = from_obj.ToString();
                                    item.ToUsercode   = item.Usercode;
                                }
                            }
                        }
                        else
                        {
                            using (var reqSocket = _zmqContext.CreateSocket(SocketType.Req))
                            {
                                reqSocket.Connect(InnerServer);
                                dynamic reqBody = new ExpandoObject();
                                reqBody.__api    = "InnerSearchUsercode";
                                reqBody.mt4_id   = trade.login;
                                reqBody.order_id = trade.order;
                                var req = JsonConvert.SerializeObject(reqBody) as string;
                                reqSocket.Send(req);
                                var repBody = _jss.Deserialize <dynamic>(reqSocket.RecvString());
                                var code    = Convert.ToInt32(repBody["ucode"]);
                                if (code != 0)
                                {
                                    item.Usercode = code.ToString();
                                }
                                var from_code = Convert.ToInt32(repBody["from_ucode"]);
                                if (from_code != 0)
                                {
                                    item.FromUsercode = from_code.ToString();
                                    item.ToUsercode   = code.ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Utils.CommonLog.Error("组装订单出现问题,订单号{0},{1},{2}",
                                              trade.order, e.Message, e.StackTrace);
                    }
                    if (handler != null)
                    {
                        handler(this, item);
                    }
                    if ((trade_type == TRANS_TYPE.TRANS_ADD || trade_type == TRANS_TYPE.TRANS_UPDATE) &&
                        trade.cmd <= (int)Utils.TRADE_COMMAND.OP_SELL)
                    {
                        var apiEquity = Poll.New();
                        var equity    = 0.0;
                        var balance   = 0.0;
                        var free      = 0.0;
                        apiEquity.GetEquity(trade.login, ref equity, ref free, ref balance);
                        Poll.Release(apiEquity);
                        dynamic reqBody = new ExpandoObject();
                        reqBody.mt4_id  = trade.login;
                        reqBody.balance = balance;
                        reqBody.__api   = "InnerCheckCopyBalance";
                        using (var reqSocket = _zmqContext.CreateSocket(SocketType.Req))
                        {
                            reqSocket.Connect(CopyCheckAddr);
                            var req = JsonConvert.SerializeObject(reqBody) as string;
                            reqSocket.Send(req);
                            reqSocket.RecvString();
                        }
                    }
                    if (!Enable)
                    {
                        continue;
                    }
                    if (trade_type == TRANS_TYPE.TRANS_ADD &&
                        trade.comment == "copy")
                    {
                        continue;
                    }
                    Task.Factory.StartNew(() => { StartLink(trade_type, trade); });
                }
                catch (Exception e)
                {
                    Utils.CommonLog.Error(string.Format("复制服务出错,{0},{1}",
                                                        e.Message, e.StackTrace));
                }
            }
            ServerContainer.FinishStop();
        }
コード例 #2
0
ファイル: CopyServer.cs プロジェクト: rebider/MT4Proxy.NET
        private void StartLink(TRANS_TYPE trade_type, TradeRecordResult trade)
        {
            var dock = new DockServer();

            try
            {
                using (var db = dock.CopySource)
                {
                    var sql      = string.Empty;
                    var mt4_from = trade.login;
                    var jss      = new JavaScriptSerializer();
                    if ((trade_type == TRANS_TYPE.TRANS_ADD || trade_type == TRANS_TYPE.TRANS_UPDATE) &&
                        trade.cmd <= (int)Utils.TRADE_COMMAND.OP_SELL)
                    {
                        var trade_date = trade.timestamp.FromTime32();
                        Utils.CommonLog.Info("MT4:{0}的订单{1}已收到",
                                             mt4_from, trade.order);
                        try
                        {
                            sql = "SELECT * FROM copy WHERE from_mt4=@mt4_id";
                            var lst = new List <dynamic>();
                            using (var cmd = new SQLiteCommand(sql, db))
                            {
                                cmd.Parameters.AddWithValue("@mt4_id", mt4_from);
                                using (var copy_reader = cmd.ExecuteReader())
                                {
                                    while (copy_reader.Read())
                                    {
                                        dynamic copy_item = new ExpandoObject();
                                        copy_item.from_mt4 = int.Parse(copy_reader["from_mt4"]
                                                                       .ToString());
                                        copy_item.to_mt4 = int.Parse(copy_reader["to_mt4"]
                                                                     .ToString());
                                        copy_item.source = int.Parse(copy_reader["source"]
                                                                     .ToString());
                                        copy_item.amount = double.Parse(copy_reader["amount"]
                                                                        .ToString());
                                        lst.Add(copy_item);
                                    }
                                }
                            }
                            var real_copy = lst.Where(i => i.source == 0);
                            var demo_copy = lst.Where(i => i.source != 0);
                            if (lst.Count > 0)
                            {
                                MT4Wrapper api     = null;
                                var        equity  = 0.0;
                                var        balance = 0.0;
                                var        free    = 0.0;
                                using (api = Poll.New())
                                {
                                    api.GetEquity(mt4_from, ref equity, ref free, ref balance);
                                }

                                if (real_copy.Count() > 0)
                                {
                                    using (api = Poll.New())
                                    {
                                        CopyNewTrade(trade, mt4_from, real_copy,
                                                     trade_date, api, balance, dock, 0);
                                    }
                                }
                                if (demo_copy.Count() > 0)
                                {
                                    using (api = Poll.DemoAPI())
                                    {
                                        CopyNewTrade(trade, mt4_from, demo_copy,
                                                     trade_date, api, balance, dock, 1);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Utils.CommonLog.Error("执行copy功能出现故障,订单号{0},{1},{2}",
                                                  trade.order, e.Message, e.StackTrace);
                        }
                    }
                    if (trade_type == TRANS_TYPE.TRANS_DELETE && !string.IsNullOrWhiteSpace(trade.symbol))
                    {
                        try
                        {
                            sql = "SELECT * FROM copy_order WHERE order_from=@order_id";
                            var lst = new List <dynamic>();
                            using (var cmd = new SQLiteCommand(sql, db))
                            {
                                cmd.Parameters.AddWithValue("@order_id", trade.order);
                                using (var copy_reader = cmd.ExecuteReader())
                                {
                                    while (copy_reader.Read())
                                    {
                                        dynamic copy_item = new ExpandoObject();
                                        copy_item.mt4_from = int.Parse(copy_reader["mt4_from"]
                                                                       .ToString());
                                        copy_item.mt4_to = int.Parse(copy_reader["mt4_to"]
                                                                     .ToString());
                                        copy_item.source = int.Parse(copy_reader["source"]
                                                                     .ToString());
                                        copy_item.order_from = int.Parse(copy_reader["order_from"]
                                                                         .ToString());
                                        copy_item.order_to = int.Parse(copy_reader["order_to"]
                                                                       .ToString());
                                        copy_item.direction = int.Parse(copy_reader["direction"]
                                                                        .ToString());
                                        copy_item.volume = int.Parse(copy_reader["volume"]
                                                                     .ToString());
                                        lst.Add(copy_item);
                                    }
                                }
                            }
                            if (lst.Count > 0)
                            {
                                Utils.CommonLog.Info("可复制订单{0}已平仓,copy数量{1}",
                                                     trade.order, lst.Count);
                            }
                            var        real_copy = lst.Where(i => i.source == 0);
                            var        demo_copy = lst.Where(i => i.source != 0);
                            MT4Wrapper api       = null;
                            if (real_copy.Count() > 0)
                            {
                                using (api = Poll.New())
                                {
                                    CopyBalanceTrade(trade, mt4_from, real_copy, api,
                                                     dock, 0);
                                }
                            }
                            if (demo_copy.Count() > 0)
                            {
                                using (api = Poll.DemoAPI())
                                {
                                    CopyBalanceTrade(trade, mt4_from, demo_copy, api,
                                                     dock, 1);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Utils.CommonLog.Error("执行copy平仓功能出现故障,订单号{0},{1},{2}",
                                                  trade.order, e.Message, e.StackTrace);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Utils.CommonLog.Error(string.Format("复制StartLink出错,{0},{1}",
                                                    e.Message, e.StackTrace));
            }
        }
コード例 #3
0
        public void SyncEquity()
        {
            var logger = Utils.CommonLog;

            logger.Info("准备开始同步equity信息到MySQL");
            string sql = string.Format(
                "SELECT DISTINCT {0} FROM user WHERE {0} IS NOT NULL;",
                AccountMT4FieldName);
            var users = new List <int>();

            using (var cmd = new MySqlCommand(sql, Source.MysqlAccount))
            {
                using (var mt4_reader = cmd.ExecuteReader())
                {
                    while (mt4_reader.Read())
                    {
                        var id = int.Parse(mt4_reader[AccountMT4FieldName].ToString());
                        users.Add(id);
                    }
                }
            }
            using (var mt4 = Poll.New())
            {
                var    dict    = new Dictionary <int, double>();
                double result  = 0;
                double free    = 0;
                double balance = 0;
                foreach (var id in users)
                {
                    var status = mt4.GetEquity(id, ref result, ref free, ref balance);
                    if (status != RET_CODE.RET_OK)
                    {
                        logger.Error(string.Format(
                                         "同步MT4账户{0}的个人资产信息出现了问题", id));
                        continue;
                    }
                    dict[id] = result;
                }
                var mt4date = DateTime.UtcNow.AddHours(3).Date;
                sql = "INSERT INTO equity(mt4_id, date, value) VALUES(@mt4id, @date, @value)" +
                      "ON DUPLICATE KEY UPDATE value = @value";
                foreach (var kv in dict)
                {
                    try
                    {
                        using (var cmd = new MySqlCommand(sql, Source.MysqlSource))
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@mt4id", kv.Key);
                            cmd.Parameters.AddWithValue("@date", mt4date);
                            cmd.Parameters.AddWithValue("@value", kv.Value);
                            cmd.ExecuteNonQuery();
                        }
                        logger.Info(string.Format("已经同步用户{0}的资产", kv.Key));
                    }
                    catch
                    {
                        logger.Error(string.Format(
                                         "同步MT4账户{0}的个人资产({1})信息出现了问题", kv.Key, kv.Value));
                    }
                }
            }
        }