Beispiel #1
0
 private void TaskCompleted(Task task)
 {
     try
     {
         using (TradePlatformEntities platformEntities = new TradePlatformEntities())
         {
             ScheduledTask scheduledTask = Queryable.SingleOrDefault <ScheduledTask>(Queryable.Where <ScheduledTask>((IQueryable <ScheduledTask>)platformEntities.ScheduledTasks, (Expression <Func <ScheduledTask, bool> >)(y => y.Name == ((TaskInfo)task.AsyncState).Name)));
             if (scheduledTask == null)
             {
                 return;
             }
             Thread.CurrentThread.Name   = "Task: '" + scheduledTask.Name + "' Callback";
             scheduledTask.LastExecution = new DateTime?(DateTime.Now);
             string taskExecutionResult = ((TaskInfo)task.AsyncState).TaskExecutionResult;
             Trace.Write((object)new LogInfo(LogType.Workflow, (Exception)null, "Execution Result: " + taskExecutionResult));
             scheduledTask.LastMessage = taskExecutionResult;
             if (scheduledTask.Type == "Once")
             {
                 scheduledTask.State    = "Completed";
                 scheduledTask.IsActive = false;
                 Trace.Write((object)new LogInfo(LogType.Initializations, (Exception)null, "Single execution completed.\n"));
             }
             if (scheduledTask.Type == "Infinity")
             {
                 scheduledTask.State = "Waiting";
                 Trace.Write((object)new LogInfo(LogType.Initializations, (Exception)null, "Next execution completed.\n"));
             }
             if (scheduledTask.Type == "Countdown")
             {
                 scheduledTask.State = "Waiting";
                 --scheduledTask.Executions;
                 if (scheduledTask.Executions == 0)
                 {
                     scheduledTask.State    = "Completed";
                     scheduledTask.IsActive = false;
                     Trace.Write((object)new LogInfo(LogType.Initializations, (Exception)null, "Countdown executions completed\n"));
                 }
                 else
                 {
                     Trace.Write((object)new LogInfo(LogType.Initializations, (Exception)null, "Next execution completed. Left: " + (object)scheduledTask.Executions + "\n"));
                 }
             }
             if (task.IsFaulted)
             {
                 scheduledTask.LastError = task.Exception.InnerException.Message;
                 Trace.Write((object)new LogInfo(LogType.HandlerExecutionError, (Exception)null, "Task execution failed with an error: " + task.Exception.InnerException.Message + "\n"));
             }
             platformEntities.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Trace.Write((object)new LogInfo(LogType.Execption, ex, ""));
     }
 }
Beispiel #2
0
 private void TaskCompleted(Task task)
 {
   try
   {
     using (TradePlatformEntities platformEntities = new TradePlatformEntities())
     {
       ScheduledTask scheduledTask = Queryable.SingleOrDefault<ScheduledTask>(Queryable.Where<ScheduledTask>((IQueryable<ScheduledTask>) platformEntities.ScheduledTasks, (Expression<Func<ScheduledTask, bool>>) (y => y.Name == ((TaskInfo) task.AsyncState).Name)));
       if (scheduledTask == null)
         return;
       Thread.CurrentThread.Name = "Task: '" + scheduledTask.Name + "' Callback";
       scheduledTask.LastExecution = new DateTime?(DateTime.Now);
       string taskExecutionResult = ((TaskInfo) task.AsyncState).TaskExecutionResult;
       Trace.Write((object) new LogInfo(LogType.Workflow, (Exception) null, "Execution Result: " + taskExecutionResult));
       scheduledTask.LastMessage = taskExecutionResult;
       if (scheduledTask.Type == "Once")
       {
         scheduledTask.State = "Completed";
         scheduledTask.IsActive = false;
         Trace.Write((object) new LogInfo(LogType.Initializations, (Exception) null, "Single execution completed.\n"));
       }
       if (scheduledTask.Type == "Infinity")
       {
         scheduledTask.State = "Waiting";
         Trace.Write((object) new LogInfo(LogType.Initializations, (Exception) null, "Next execution completed.\n"));
       }
       if (scheduledTask.Type == "Countdown")
       {
         scheduledTask.State = "Waiting";
         --scheduledTask.Executions;
         if (scheduledTask.Executions == 0)
         {
           scheduledTask.State = "Completed";
           scheduledTask.IsActive = false;
           Trace.Write((object) new LogInfo(LogType.Initializations, (Exception) null, "Countdown executions completed\n"));
         }
         else
           Trace.Write((object) new LogInfo(LogType.Initializations, (Exception) null, "Next execution completed. Left: " + (object) scheduledTask.Executions + "\n"));
       }
       if (task.IsFaulted)
       {
         scheduledTask.LastError = task.Exception.InnerException.Message;
         Trace.Write((object) new LogInfo(LogType.HandlerExecutionError, (Exception) null, "Task execution failed with an error: " + task.Exception.InnerException.Message + "\n"));
       }
       platformEntities.SaveChanges();
     }
   }
   catch (Exception ex)
   {
     Trace.Write((object) new LogInfo(LogType.Execption, ex, ""));
   }
 }
Beispiel #3
0
 public void CheckNow()
 {
     using (TradePlatformEntities platformEntities = new TradePlatformEntities())
     {
         ObjectSet <ScheduledTask> scheduledTasks           = platformEntities.ScheduledTasks;
         Expression <Func <ScheduledTask, bool> > predicate = (Expression <Func <ScheduledTask, bool> >)(x => x.IsActive && x.State != "Running");
         foreach (ScheduledTask scheduledTask1 in Enumerable.ToList <ScheduledTask>((IEnumerable <ScheduledTask>)Queryable.Where <ScheduledTask>((IQueryable <ScheduledTask>)scheduledTasks, predicate)))
         {
             ScheduledTask scheduledTask = scheduledTask1;
             TaskInfo      taskInfo      = Enumerable.SingleOrDefault <TaskInfo>(Enumerable.Where <TaskInfo>(this._tasks, (Func <TaskInfo, bool>)(x => x.Name == scheduledTask.Name)));
             if (taskInfo != null)
             {
                 int num = Enumerable.Max((IEnumerable <int>) new int[2]
                 {
                     scheduledTask.Interval,
                     (int)taskInfo.MinInterval.TotalMilliseconds
                 });
                 if (scheduledTask.LastExecution.HasValue)
                 {
                     DateTime?lastExecution = scheduledTask.LastExecution;
                     TimeSpan timeSpan      = TimeSpan.FromMilliseconds((double)num);
                     DateTime?nullable      = lastExecution.HasValue ? new DateTime?(lastExecution.GetValueOrDefault() + timeSpan) : new DateTime?();
                     DateTime now           = DateTime.Now;
                     if ((nullable.HasValue ? (nullable.GetValueOrDefault() < now ? 1 : 0) : 0) == 0)
                     {
                         continue;
                     }
                 }
                 scheduledTask.State = "Running";
                 Trace.Write((object)new LogInfo(LogType.Initializations, (Exception)null, "Running '" + scheduledTask.Name + "' task..."));
                 taskInfo.Patameters = Enumerable.ToList <ScheduledTaskParameter>((IEnumerable <ScheduledTaskParameter>)scheduledTask.ScheduledTaskParameters);
                 Task task = new Task((Action <object>)(x => ((TaskInfo)x).ExecuteTask()), (object)taskInfo);
                 task.ContinueWith(new Action <Task>(this.TaskCompleted));
                 task.Start();
             }
             else
             {
                 scheduledTask.State = "Missing";
                 Trace.Write((object)new LogInfo(LogType.Notifications, (Exception)null, "Missing implementation for task '" + scheduledTask.Name + "'"));
             }
         }
         platformEntities.SaveChanges();
     }
 }
Beispiel #4
0
 public void CheckNow()
 {
   using (TradePlatformEntities platformEntities = new TradePlatformEntities())
   {
     ObjectSet<ScheduledTask> scheduledTasks = platformEntities.ScheduledTasks;
     Expression<Func<ScheduledTask, bool>> predicate = (Expression<Func<ScheduledTask, bool>>) (x => x.IsActive && x.State != "Running");
     foreach (ScheduledTask scheduledTask1 in Enumerable.ToList<ScheduledTask>((IEnumerable<ScheduledTask>) Queryable.Where<ScheduledTask>((IQueryable<ScheduledTask>) scheduledTasks, predicate)))
     {
       ScheduledTask scheduledTask = scheduledTask1;
       TaskInfo taskInfo = Enumerable.SingleOrDefault<TaskInfo>(Enumerable.Where<TaskInfo>(this._tasks, (Func<TaskInfo, bool>) (x => x.Name == scheduledTask.Name)));
       if (taskInfo != null)
       {
         int num = Enumerable.Max((IEnumerable<int>) new int[2]
         {
           scheduledTask.Interval,
           (int) taskInfo.MinInterval.TotalMilliseconds
         });
         if (scheduledTask.LastExecution.HasValue)
         {
           DateTime? lastExecution = scheduledTask.LastExecution;
           TimeSpan timeSpan = TimeSpan.FromMilliseconds((double) num);
           DateTime? nullable = lastExecution.HasValue ? new DateTime?(lastExecution.GetValueOrDefault() + timeSpan) : new DateTime?();
           DateTime now = DateTime.Now;
           if ((nullable.HasValue ? (nullable.GetValueOrDefault() < now ? 1 : 0) : 0) == 0)
             continue;
         }
         scheduledTask.State = "Running";
         Trace.Write((object) new LogInfo(LogType.Initializations, (Exception) null, "Running '" + scheduledTask.Name + "' task..."));
         taskInfo.Patameters = Enumerable.ToList<ScheduledTaskParameter>((IEnumerable<ScheduledTaskParameter>) scheduledTask.ScheduledTaskParameters);
         Task task = new Task((Action<object>) (x => ((TaskInfo) x).ExecuteTask()), (object) taskInfo);
         task.ContinueWith(new Action<Task>(this.TaskCompleted));
         task.Start();
       }
       else
       {
         scheduledTask.State = "Missing";
         Trace.Write((object) new LogInfo(LogType.Notifications, (Exception) null, "Missing implementation for task '" + scheduledTask.Name + "'"));
       }
     }
     platformEntities.SaveChanges();
   }
 }
Beispiel #5
0
        protected override int Start()
        {
            using (TradePlatformEntities model = new TradePlatformEntities())
            {
                int currentNumber         = this.AccountNumber();
                MetaTraderAccount account = model.MetaTraderAccounts.SingleOrDefault(x => x.AccountNumber == currentNumber);

                if (account == null)
                {
                    account = new MetaTraderAccount();
                    model.MetaTraderAccounts.AddObject(account);
                }

                account.AccountBalance        = (decimal)this.AccountBalance();
                account.AccountCredit         = this.AccountCredit();
                account.AccountCompany        = this.AccountCompany();
                account.AccountCurrency       = this.AccountCurrency();
                account.AccountEquity         = (decimal)this.AccountEquity();
                account.AccountFreeMargin     = (decimal)this.AccountFreeMargin();
                account.AccountFreeMarginMode = this.AccountFreeMarginMode();
                account.AccountLeverage       = this.AccountLeverage();
                account.AccountMargin         = (decimal)this.AccountMargin();
                account.AccountName           = this.AccountName();
                account.AccountNumber         = this.AccountNumber();
                account.AccountProfit         = (decimal)this.AccountProfit();
                account.AccountServer         = this.AccountServer();
                account.AccountStopoutLevel   = this.AccountStopoutLevel();
                account.AccountStopoutMode    = this.AccountStopoutMode();

                List <MetaTraderOrder> openedOrders = account.MetaTraderOrders.Where(x => x.IsClosed == false).ToList();

                openedOrders.ForEach(x => model.MetaTraderOrders.DeleteObject(x));

                for (int i = this.OrdersTotal() - 1; i >= 0; i--)
                {
                    if (this.OrderSelect(i, SELECT_BY.SELECT_BY_POS, POOL_MODES.MODE_TRADES))
                    {
                        MetaTraderOrder order = new MetaTraderOrder();

                        order.ClosePrice  = this.OrderClosePrice();
                        order.CloseTime   = this.OrderCloseTime();
                        order.Comment     = this.OrderComment();
                        order.Ticket      = this.OrderTicket();
                        order.OpenPrice   = this.OrderOpenPrice();
                        order.OpenTime    = this.OrderOpenTime();
                        order.Profit      = (decimal)this.OrderProfit();
                        order.Size        = this.OrderLots();
                        order.StopLoss    = this.OrderStopLoss();
                        order.Swap        = (decimal)this.OrderSwap();
                        order.Symbol      = this.OrderSymbol();
                        order.TakeProfit  = this.OrderTakeProfit();
                        order.Type        = this.OrderType().ToString();
                        order.MagicNumber = this.OrderMagicNumber();
                        order.Commission  = (decimal)this.OrderCommission();
                        order.IsClosed    = false;

                        account.MetaTraderOrders.Add(order);
                    }
                }

                List <int> list = account.MetaTraderOrders.Select(order => order.Ticket).ToList();

                for (int i = this.HistoryTotal() - 1; i >= 0; i--)
                {
                    if (this.OrderSelect(i, SELECT_BY.SELECT_BY_POS, POOL_MODES.MODE_HISTORY))
                    {
                        if (!list.Contains(this.OrderTicket()))
                        {
                            MetaTraderOrder order = new MetaTraderOrder();
                            order.ClosePrice  = this.OrderClosePrice();
                            order.CloseTime   = this.OrderCloseTime();
                            order.Comment     = this.OrderComment();
                            order.Ticket      = this.OrderTicket();
                            order.OpenPrice   = this.OrderOpenPrice();
                            order.OpenTime    = this.OrderOpenTime();
                            order.Profit      = (decimal)this.OrderProfit();
                            order.Size        = this.OrderLots();
                            order.StopLoss    = this.OrderStopLoss();
                            order.Swap        = (decimal)this.OrderSwap();
                            order.Symbol      = this.OrderSymbol();
                            order.TakeProfit  = this.OrderTakeProfit();
                            order.Type        = this.OrderType().ToString();
                            order.MagicNumber = this.OrderMagicNumber();
                            order.Commission  = (decimal)this.OrderCommission();
                            order.IsClosed    = true;

                            account.MetaTraderOrders.Add(order);
                        }
                    }
                }

                model.SaveChanges();
            }

            return(1);
        }
        protected override int Start()
        {
            using (TradePlatformEntities model = new TradePlatformEntities())
            {
                int currentNumber = this.AccountNumber();
                MetaTraderAccount account = model.MetaTraderAccounts.SingleOrDefault(x => x.AccountNumber == currentNumber);

                if (account == null)
                {
                    account = new MetaTraderAccount();
                    model.MetaTraderAccounts.AddObject(account);
                }

                account.AccountBalance = (decimal) this.AccountBalance();
                account.AccountCredit = this.AccountCredit();
                account.AccountCompany = this.AccountCompany();
                account.AccountCurrency = this.AccountCurrency();
                account.AccountEquity = (decimal)this.AccountEquity();
                account.AccountFreeMargin = (decimal)this.AccountFreeMargin();
                account.AccountFreeMarginMode = this.AccountFreeMarginMode();
                account.AccountLeverage = this.AccountLeverage();
                account.AccountMargin = (decimal)this.AccountMargin();
                account.AccountName = this.AccountName();
                account.AccountNumber = this.AccountNumber();
                account.AccountProfit = (decimal)this.AccountProfit();
                account.AccountServer = this.AccountServer();
                account.AccountStopoutLevel = this.AccountStopoutLevel();
                account.AccountStopoutMode = this.AccountStopoutMode();

                List<MetaTraderOrder> openedOrders = account.MetaTraderOrders.Where(x => x.IsClosed == false).ToList();

                openedOrders.ForEach(x => model.MetaTraderOrders.DeleteObject(x));

                for (int i = this.OrdersTotal() - 1; i >= 0; i--)
                {
                    if (this.OrderSelect(i, SELECT_BY.SELECT_BY_POS, POOL_MODES.MODE_TRADES))
                    {
                        MetaTraderOrder order = new MetaTraderOrder();

                        order.ClosePrice = this.OrderClosePrice();
                        order.CloseTime = this.OrderCloseTime();
                        order.Comment = this.OrderComment();
                        order.Ticket = this.OrderTicket();
                        order.OpenPrice = this.OrderOpenPrice();
                        order.OpenTime = this.OrderOpenTime();
                        order.Profit = (decimal)this.OrderProfit();
                        order.Size = this.OrderLots();
                        order.StopLoss = this.OrderStopLoss();
                        order.Swap = (decimal)this.OrderSwap();
                        order.Symbol = this.OrderSymbol();
                        order.TakeProfit = this.OrderTakeProfit();
                        order.Type = this.OrderType().ToString();
                        order.MagicNumber = this.OrderMagicNumber();
                        order.Commission = (decimal)this.OrderCommission();
                        order.IsClosed = false;

                        account.MetaTraderOrders.Add(order);
                    }
                }

                List<int> list = account.MetaTraderOrders.Select(order => order.Ticket).ToList();

                for (int i = this.HistoryTotal() - 1; i >= 0; i--)
                {
                    if (this.OrderSelect(i, SELECT_BY.SELECT_BY_POS, POOL_MODES.MODE_HISTORY))
                    {
                        if (!list.Contains(this.OrderTicket()))
                        {
                            MetaTraderOrder order = new MetaTraderOrder();
                            order.ClosePrice = this.OrderClosePrice();
                            order.CloseTime = this.OrderCloseTime();
                            order.Comment = this.OrderComment();
                            order.Ticket = this.OrderTicket();
                            order.OpenPrice = this.OrderOpenPrice();
                            order.OpenTime = this.OrderOpenTime();
                            order.Profit = (decimal)this.OrderProfit();
                            order.Size = this.OrderLots();
                            order.StopLoss = this.OrderStopLoss();
                            order.Swap = (decimal)this.OrderSwap();
                            order.Symbol = this.OrderSymbol();
                            order.TakeProfit = this.OrderTakeProfit();
                            order.Type = this.OrderType().ToString();
                            order.MagicNumber = this.OrderMagicNumber();
                            order.Commission = (decimal)this.OrderCommission();
                            order.IsClosed = true;

                            account.MetaTraderOrders.Add(order);
                        }
                    }
                }

                model.SaveChanges();
            }

            return 1;
        }