示例#1
0
        public static MarketOrder DecorateOrder(POSITION pos)
        {
            var mo = new MarketOrder
            {
                AccountID      = pos.AccountID,
                Comment        = pos.Comment,
                ExpertComment  = pos.ExpertComment,
                ID             = pos.ID,
                Magic          = pos.Magic,
                PendingOrderID = pos.PendingOrderID,
                PriceBest      = (float?)pos.PriceBest,
                PriceEnter     = (float)pos.PriceEnter,
                PriceWorst     = (float?)pos.PriceWorst,
                Side           = pos.Side,
                State          = ((PositionState)pos.State),
                StopLoss       = (float?)(pos.Stoploss == 0 ? null : pos.Stoploss),
                TakeProfit     = (float?)(pos.Takeprofit == 0 ? null : pos.Takeprofit),
                Symbol         = pos.Symbol,
                TimeEnter      = pos.TimeEnter,
                Volume         = pos.Volume,
                TrailLevel1    = (float?)pos.TrailLevel1,
                TrailLevel2    = (float?)pos.TrailLevel2,
                TrailLevel3    = (float?)pos.TrailLevel3,
                TrailLevel4    = (float?)pos.TrailLevel4,
                TrailTarget1   = (float?)pos.TrailTarget1,
                TrailTarget2   = (float?)pos.TrailTarget2,
                TrailTarget3   = (float?)pos.TrailTarget3,
                TrailTarget4   = (float?)pos.TrailTarget4,
                MasterOrder    = pos.MasterOrder
            };

            return(mo);
        }
示例#2
0
        public static POSITION UndecorateOpenedPosition(MarketOrder pos)
        {
            // ReSharper disable PossibleInvalidOperationException
            var p = new POSITION
            {
                //ID = pos.ID,
                AccountID      = pos.AccountID,
                Comment        = pos.Comment,
                ExpertComment  = pos.ExpertComment,
                Magic          = pos.Magic,
                PendingOrderID = pos.PendingOrderID,
                PriceBest      = (decimal?)pos.PriceBest,
                PriceWorst     = (decimal?)pos.PriceWorst,
                PriceEnter     = (decimal)pos.PriceEnter,
                Side           = pos.Side,
                Stoploss       = (decimal?)pos.StopLoss,
                Symbol         = pos.Symbol,
                Takeprofit     = (decimal?)pos.TakeProfit,
                TimeEnter      = pos.TimeEnter,
                Volume         = pos.Volume,
                State          = (int)pos.State,
                MasterOrder    = pos.MasterOrder
            };

            // ReSharper restore PossibleInvalidOperationException
            return(p);
        }
示例#3
0
        public static List<POSITION> MakePositions(TradeSharpConnectionPersistent conn, int accountId)
        {
            var allPositions = new List<POSITION>();

            // создать сделки по счету
            var tickers = new[] { "EURUSD", "USDJPY", "AUDUSD", "EURCAD" };
            var timeNow = DateTime.Now;
            var times = new[] { timeNow.AddDays(-2), timeNow.AddDays(-1), timeNow.AddHours(-1) };
            var sides = new[] { -1, 1 };

            foreach (var ticker in tickers)
            foreach (var side in sides)
            foreach (var time in times)
            {
                var quote = QuoteStorage.Instance.ReceiveValue(ticker);
                var pos = new POSITION
                {
                    AccountID = accountId,
                    Symbol = ticker,
                    Side = side,
                    TimeEnter = time,
                    State = (int)PositionState.Opened,
                    PriceEnter = (decimal)(side == 1 ? quote.ask : quote.bid),
                    Volume = 100000
                };
                allPositions.Add(pos);
                conn.POSITION.Add(pos);
            }

            return allPositions;
        }
        /// <summary>
        /// Открывает новую сделку в ручную
        /// </summary>
        /// <param name="positionItem">экземпляр модели данных</param>
        /// <returns>Удачно ли выполнено добавления сделки</returns>
        public bool NewDeal(PositionItem positionItem)
        {
            try
            {
                using (var ctx = DatabaseContext.Instance.Make())
                {
                    var acc = ctx.ACCOUNT.FirstOrDefault(a => a.ID == positionItem.AccountID);
                    Logger.Error("NewDeal - ошибка открытия сделки в ручную. Не удалось найти счёт с номером " +
                                 positionItem.AccountID);
                    if (acc == null) return false;

                    var newPosition = new POSITION
                        {
                            AccountID = positionItem.AccountID,
                            Symbol = positionItem.Symbol,
                            TimeEnter = positionItem.TimeEnter,
                            PriceEnter = positionItem.PriceEnter,
                            Volume = positionItem.Volume,
                            Side = positionItem.Side,
                            State = (int) positionItem.State
                        };

                    ctx.POSITION.Add(newPosition);
                    ctx.SaveChanges();
                }
            }
            catch (UpdateException ex)
            {
                Logger.Error("NewDeal - ошибка открытия сделки в ручную. Ошибка добавления в базу данных. Возможно сущьность с таким идентификатором уже существует или указаны некорректные параметры", ex);
                return false;
            }
            catch (Exception ex)
            {
                Logger.Error("NewDeal - ошибка открытия сделки в ручную", ex);
                return false;
            }
            return true;
        }
示例#5
0
 private bool ModifyClosedPosition(MarketOrder order, TradeSharpConnection ctx, POSITION_CLOSED pos,
     out string errorString)
 {
     errorString = string.Empty;
     if (order.IsClosed)
     {
         var opened = new POSITION
         {
             AccountID = pos.AccountID,
             ID = pos.ID,
             Comment = order.Comment,
             Magic = order.Magic,
             ExpertComment = order.ExpertComment,
             PendingOrderID = order.PendingOrderID,
             PriceBest = (decimal?)order.PriceBest,
             PriceEnter = (decimal)order.PriceEnter,
             PriceWorst = (decimal?)order.PriceWorst,
             Side = order.Side,
             Stoploss = (decimal?)order.StopLoss,
             Symbol = order.Symbol,
             Takeprofit = (decimal?)order.TakeProfit,
             TimeEnter = order.TimeEnter,
             Volume = order.Volume,
             State = (int) order.State,
             MasterOrder = order.MasterOrder
         };
         ctx.POSITION_CLOSED.Remove(pos);
         ctx.POSITION.Add(opened);
         ctx.SaveChanges();
         Logger.InfoFormat("Closed order {0} {1} {2} {3} was reopened",
             pos.ID, pos.Side > 0 ? "BUY" : "SELL", pos.Volume, pos.Symbol);
         return true;
     }
     pos.Comment = order.Comment;
     pos.Magic = order.Magic;
     pos.ExpertComment = order.ExpertComment;
     pos.PendingOrderID = order.PendingOrderID;
     pos.PriceBest = (decimal?)order.PriceBest;
     pos.PriceEnter = (decimal)order.PriceEnter;
     pos.PriceWorst = (decimal?)order.PriceWorst;
     pos.Side = order.Side;
     pos.Stoploss = (decimal?)order.StopLoss;
     pos.Symbol = order.Symbol;
     pos.Takeprofit = (decimal?)order.TakeProfit;
     if (order.TimeEnter != default(DateTime))
         pos.TimeEnter = order.TimeEnter;
     pos.Volume = order.Volume;
     pos.ExitReason = (int) order.ExitReason;
     pos.PriceExit = (decimal) (order.PriceExit ?? 0);
     pos.PriceWorst = (decimal?) order.PriceWorst;
     pos.ResultBase = (decimal)order.ResultBase;
     pos.ResultDepo = (decimal)order.ResultDepo;
     pos.ResultPoints = (decimal)order.ResultPoints;
     pos.TimeExit = order.TimeExit ?? default(DateTime);
     pos.Swap = (decimal) (order.Swap ?? 0);
     Logger.InfoFormat("Closed order {0} {1} {2} {3} was modified",
             pos.ID, pos.Side > 0 ? "BUY" : "SELL", pos.Volume, pos.Symbol);
     ctx.SaveChanges();
     return true;
 }
示例#6
0
 private bool ModifyOpenedPosition(MarketOrder order, TradeSharpConnection ctx, POSITION pos, 
     out string errorString)
 {
     errorString = string.Empty;
     if (order.IsClosed)
     {
         var closed = new POSITION_CLOSED
         {
             AccountID = pos.AccountID,
             ID = pos.ID,
             Comment = order.Comment,
             ExitReason = (int) order.ExitReason,
             Magic = order.Magic,
             ExpertComment = order.ExpertComment,
             PendingOrderID = order.PendingOrderID,
             PriceBest = (decimal?) order.PriceBest,
             PriceEnter = (decimal) order.PriceEnter,
             PriceExit = (decimal) (order.PriceExit ?? 0),
             PriceWorst = (decimal?) order.PriceWorst,
             ResultBase = (decimal) order.ResultBase,
             ResultDepo = (decimal) order.ResultDepo,
             ResultPoints = (decimal) order.ResultPoints,
             Side = order.Side,
             Stoploss = (decimal?) order.StopLoss,
             Swap = (decimal) (order.Swap ?? 0),
             Symbol = order.Symbol,
             Takeprofit = (decimal?) order.TakeProfit,
             TimeEnter = order.TimeEnter,
             TimeExit = order.TimeExit ?? default(DateTime),
             Volume = order.Volume
         };
         ctx.POSITION.Remove(pos);
         ctx.POSITION_CLOSED.Add(closed);
         return true;
     }
     pos.Comment = order.Comment;
     pos.Magic = order.Magic;
     pos.ExpertComment = order.ExpertComment;
     pos.PendingOrderID = order.PendingOrderID;
     pos.PriceBest = (decimal?) order.PriceBest;
     pos.PriceEnter = (decimal) order.PriceEnter;
     pos.PriceWorst = (decimal?) order.PriceWorst;
     pos.Side = order.Side;
     pos.Stoploss = (decimal?) order.StopLoss;
     pos.Symbol = order.Symbol;
     pos.Takeprofit = (decimal?) order.TakeProfit;
     pos.TimeEnter = order.TimeEnter;
     pos.Volume = order.Volume;
     pos.State = (int) order.State;
     pos.MasterOrder = order.MasterOrder;
     ctx.SaveChanges();
     return true;
 }
示例#7
0
 public static POSITION UndecorateOpenedPosition(MarketOrder pos)
 {
     // ReSharper disable PossibleInvalidOperationException
     var p = new POSITION
     {
         //ID = pos.ID,
         AccountID = pos.AccountID,
         Comment = pos.Comment,
         ExpertComment = pos.ExpertComment,
         Magic = pos.Magic,
         PendingOrderID = pos.PendingOrderID,
         PriceBest = (decimal?)pos.PriceBest,
         PriceWorst = (decimal?)pos.PriceWorst,
         PriceEnter = (decimal)pos.PriceEnter,
         Side = pos.Side,
         Stoploss = (decimal?)pos.StopLoss,
         Symbol = pos.Symbol,
         Takeprofit = (decimal?)pos.TakeProfit,
         TimeEnter = pos.TimeEnter,
         Volume = pos.Volume,
         State = (int)pos.State,
         MasterOrder = pos.MasterOrder
     };
     // ReSharper restore PossibleInvalidOperationException
     return p;
 }
示例#8
0
 public static MarketOrder DecorateOrder(POSITION pos)
 {
     var mo = new MarketOrder
     {
         AccountID = pos.AccountID,
         Comment = pos.Comment,
         ExpertComment = pos.ExpertComment,
         ID = pos.ID,
         Magic = pos.Magic,
         PendingOrderID = pos.PendingOrderID,
         PriceBest = (float?)pos.PriceBest,
         PriceEnter = (float)pos.PriceEnter,
         PriceWorst = (float?)pos.PriceWorst,
         Side = pos.Side,
         State = ((PositionState)pos.State),
         StopLoss = (float?)(pos.Stoploss == 0 ? null : pos.Stoploss),
         TakeProfit = (float?)(pos.Takeprofit == 0 ? null : pos.Takeprofit),
         Symbol = pos.Symbol,
         TimeEnter = pos.TimeEnter,
         Volume = pos.Volume,
         TrailLevel1 = (float?)pos.TrailLevel1,
         TrailLevel2 = (float?)pos.TrailLevel2,
         TrailLevel3 = (float?)pos.TrailLevel3,
         TrailLevel4 = (float?)pos.TrailLevel4,
         TrailTarget1 = (float?)pos.TrailTarget1,
         TrailTarget2 = (float?)pos.TrailTarget2,
         TrailTarget3 = (float?)pos.TrailTarget3,
         TrailTarget4 = (float?)pos.TrailTarget4,
         MasterOrder = pos.MasterOrder
     };
     return mo;
 }