Пример #1
0
        private static void CopyBalanceTrade(TradeRecordResult trade,
                                             int mt4_from, IEnumerable <dynamic> items,
                                             MT4Wrapper api, DockServer dock, int source)
        {
            var key = string.Empty;

            foreach (var i in items)
            {
                var copy_order = 0;
                try
                {
                    copy_order = i.order_to;
                    var volume    = i.volume;
                    var to_source = i.source == 0 ? "real" : "demo";
                    var mt4_to    = i.mt4_to;
                    Utils.CommonLog.Info("自动平掉复制订单{0}的copy订单{1},成交量{2},位置{3}",
                                         trade.order, copy_order, volume, to_source);
                    var args = new TradeTransInfoArgsResult
                    {
                        type   = TradeTransInfoTypes.TT_BR_ORDER_CLOSE,
                        cmd    = (short)trade.cmd,
                        order  = copy_order,
                        price  = trade.close_price,
                        volume = volume,
                    };
                    var result = api.TradeTransaction(ref args);
                    if (result == RET_CODE.RET_OK)
                    {
                        var sql = string.Empty;
                        sql = "INSERT INTO pre_delete(order_from) VALUES(@order_from)";
                        using (var cmd = new SQLiteCommand(sql, dock.CopySource))
                        {
                            cmd.Parameters.AddWithValue("@order_from", trade.order);
                            cmd.ExecuteNonQuery();
                        }
                        double amount = 0.0;
                        sql = "SELECT amount FROM copy WHERE from_mt4 = @mt4_from AND to_mt4 = @mt4_to";
                        using (var cmd = new SQLiteCommand(sql, dock.CopySource))
                        {
                            cmd.Parameters.AddWithValue("@mt4_from", mt4_from);
                            cmd.Parameters.AddWithValue("@mt4_to", mt4_to);
                            var amount_obj = cmd.ExecuteScalar();
                            if (amount_obj == null)
                            {
                                Utils.CommonLog.Warn("收到一个很奇怪的需要结算盈利的copy单,找不到copy表记录" +
                                                     "单号{0}, MT4{1}", trade.order, trade.login);
                                continue;
                            }
                            else
                            {
                                amount = Convert.ToDouble(amount_obj);
                            }
                        }
                        //算钱模式
                        var order = api.AdmTradesRequest(copy_order, false);
                        if (order.profit + order.storage + amount < 0)
                        {
                            amount = 0.0;
                        }
                        else
                        {
                            amount += trade.profit + trade.storage;
                        }
                        sql = "UPDATE copy SET amount=@amount WHERE from_mt4=@mt4_from AND to_mt4=@mt4_to";
                        using (var cmd = new SQLiteCommand(sql, dock.CopySource))
                        {
                            cmd.Parameters.AddWithValue("@mt4_from", mt4_from);
                            cmd.Parameters.AddWithValue("@mt4_to", mt4_to);
                            cmd.Parameters.AddWithValue("@amount", amount);
                            cmd.ExecuteNonQuery();
                        }
                        sql = "SELECT user_code FROM user WHERE mt4=@mt4_id";
                        using (var cmd = new SQLiteCommand(sql, dock.CopySource))
                        {
                            cmd.Parameters.AddWithValue("@mt4_id", mt4_to);
                            var user_code_obj = cmd.ExecuteScalar();
                            if (user_code_obj != null)
                            {
                                key = string.Format(RedisCopyActivityTemplate, trade.login);
                                dock.RedisCopy.ZAdd(key, TimeServer.Now.ToTime32().ToString(),
                                                    user_code_obj.ToString());
                                Utils.CommonLog.Info("自动平掉复制订单{0}的copy订单{1}成功",
                                                     trade.order, copy_order);
                            }
                            else
                            {
                                Utils.CommonLog.Info("Copy平仓复制订单{0}的copy订单{1}无法反查邀请码信息,MT4账号{2}",
                                                     trade.order, copy_order, mt4_to);
                            }
                        }
                    }
                    else
                    {
                        Utils.CommonLog.Warn("自动平掉复制订单{0}的copy订单{1}失败,原因{2}",
                                             trade.order, copy_order, result);
                    }
                }
                catch (Exception e)
                {
                    Utils.CommonLog.Error("子订单{0}平仓出现问题{1},{2}",
                                          copy_order, e.Message, e.StackTrace);
                }
            }
        }
Пример #2
0
 private static void CopyNewTrade(TradeRecordResult trade,
                                  int mt4_from, IEnumerable <dynamic> items, DateTime trade_date,
                                  MT4Wrapper api, double balance, DockServer dock, int source)
 {
     foreach (var i in items)
     {
         var mt4_to = 0;
         try
         {
             mt4_to = i.to_mt4;
             var to_source = i.source;
             var amount    = i.amount;
             Utils.CommonLog.Info("将[MT4:{0}]的订单{1}复制给[MT4:{2}]",
                                  mt4_from, trade.order, mt4_to);
             var volume = ForexMath.CopyTransform(balance, trade.volume, amount);
             var now    = TimeServer.Now;
             if (to_source == 0 && Math.Abs((now - trade_date).TotalSeconds) > 1)
             {
                 Utils.CommonLog.Info("将[MT4:{0}]的订单{1}复制给[MT4:{2}],时间过长,单据时间:{3},此时:{4}",
                                      mt4_from, trade.order, mt4_to, trade_date, now);
                 continue;
             }
             if (volume <= 0)
             {
                 Utils.CommonLog.Info("将[MT4:{0}]的订单{1}复制给[MT4:{2}],复制单成交量过小,跳过复制",
                                      mt4_from, trade.order, mt4_to);
                 continue;
             }
             var args = new TradeTransInfoArgsResult
             {
                 type    = TradeTransInfoTypes.TT_BR_ORDER_OPEN,
                 cmd     = (short)trade.cmd,
                 orderby = mt4_to,
                 price   = trade.open_price,
                 symbol  = trade.symbol,
                 volume  = volume,
                 comment = "copy",
             };
             var result = api.TradeTransaction(ref args);
             if (result == RET_CODE.RET_OK)
             {
                 var sql =
                     "INSERT INTO copy_order(mt4_from, mt4_to, order_from, order_to," +
                     "direction, volume, source) VALUES(@mt4_from, @mt4_to, @order_from," +
                     "@order_to, @direction, @volume, @source)";
                 using (var cmd = new SQLiteCommand(sql, dock.CopySource))
                 {
                     cmd.Parameters.AddWithValue("@mt4_from", mt4_from);
                     cmd.Parameters.AddWithValue("@mt4_to", mt4_to);
                     cmd.Parameters.AddWithValue("@order_from", trade.order);
                     cmd.Parameters.AddWithValue("@order_to", args.order);
                     cmd.Parameters.AddWithValue("@direction", args.cmd);
                     cmd.Parameters.AddWithValue("@volume", volume);
                     cmd.Parameters.AddWithValue("@source", source);
                     cmd.ExecuteNonQuery();
                 }
                 Utils.CommonLog.Info("将[MT4:{0}]的订单{1}复制给[MT4:{2}],复制成功",
                                      mt4_from, trade.order, mt4_to);
             }
             else
             {
                 Utils.CommonLog.Warn("将[MT4:{0}]的订单{1}复制给[MT4:{2}],复制结果失败,原因:{5}",
                                      mt4_from, trade.order, mt4_to, result);
             }
         }
         catch (Exception e)
         {
             Utils.CommonLog.Error("Copy订单{0}复制给MT4[{1}]出现问题{2},{3}",
                                   trade.order, mt4_to, e.Message, e.StackTrace);
         }
     }
 }
Пример #3
0
        public void OnRequest(IInputOutput aIO, dynamic aJson)
        {
            var from_mt4    = Convert.ToInt32(aJson["from_mt4"]);
            var to_mt4      = Convert.ToInt32(aJson["to_mt4"]);
            var auto_delete = Convert.ToBoolean(aJson["auto_delete"]);
            var sql         = string.Empty;
            var dock        = new DockServer();
            var db          = dock.CopySource;

            if (auto_delete)
            {
                sql = "SELECT * FROM copy_order WHERE mt4_to = @mt4_to";
                using (var cmd = new SQLiteCommand(sql, db))
                {
                    cmd.Parameters.AddWithValue("@mt4_to", to_mt4);
                    using (var order_reader = cmd.ExecuteReader())
                    {
                        while (order_reader.Read())
                        {
                            dynamic item = new ExpandoObject();
                            item.mt4       = int.Parse(order_reader["mt4_to"].ToString());
                            item.volume    = int.Parse(order_reader["volume"].ToString());
                            item.direction =
                                int.Parse(order_reader["direction"].ToString());
                            item.order  = int.Parse(order_reader["order_to"].ToString());
                            item.source = int.Parse(order_reader["source"].ToString());
                            var args = new TradeTransInfoArgsResult
                            {
                                type   = TradeTransInfoTypes.TT_BR_ORDER_DELETE,
                                cmd    = (short)item.direction,
                                order  = item.order,
                                volume = item.volume,
                            };
                            MT4Wrapper api = null;
                            if (item.source == 0)
                            {
                                api = Poll.New();
                            }
                            else
                            {
                                api = Poll.DemoAPI();
                            }
                            using (api)
                            {
                                var result = api.TradeTransaction(ref args);
                                if (result == RET_CODE.RET_OK)
                                {
                                    Utils.CommonLog.Info("用户{0}关闭copy功能成功平仓{1}订单,报价{2}",
                                                         args.orderby, args.order, args.price);
                                }
                            }
                        }
                    }
                }
            }
            sql = "DELETE FROM copy WHERE from_mt4=@mt4_from AND to_mt4=@mt4_to";
            using (var cmd = new SQLiteCommand(sql, db))
            {
                cmd.Parameters.AddWithValue("@mt4_from", from_mt4);
                cmd.Parameters.AddWithValue("@mt4_to", to_mt4);
                cmd.ExecuteNonQuery();
            }
            sql = "DELETE FROM copy_order WHERE mt4_to=@mt4_to";
            using (var cmd = new SQLiteCommand(sql, db))
            {
                cmd.Parameters.AddWithValue("@mt4_to", to_mt4);
                cmd.ExecuteNonQuery();
            }
            aIO.Output = JsonConvert.SerializeObject(
                Utils.MakeResponseObject(true, 0, string.Empty));
        }