Exemplo n.º 1
0
        private void LoadSavedOrders(DBAdviser adviser, ref ExpertInfo expert)
        {
            var terminals = Container.Resolve <ITerminalEvents>();
            List <PositionInfo> positions = terminals.GetPositions4Adviser(adviser.Id);

            if (Utils.HasAny(positions))
            {
                expert.Orders = JsonConvert.SerializeObject(positions);
            }
        }
Exemplo n.º 2
0
        public ExpertInfo InitExpert(ExpertInfo expert)
        {
            try
            {
                using (var Session = ConnectionHelper.CreateNewSession())
                {
                    var msg       = "";
                    var accNumber = long.Parse(expert.Account);
                    var terminal  = data.getTerminalByNumber(Session, accNumber);
                    if (terminal == null)
                    {
                        msg = $"Unknown AccountNumber {expert.Account}. Please Register Account in DB.";
                        log.Log(msg);
                        expert.Magic  = 0;
                        expert.Reason = msg;
                        return(expert);
                    }

                    var strSymbol = expert.Symbol;
                    if (strSymbol.Contains("_i"))
                    {
                        strSymbol = strSymbol.Substring(0, strSymbol.Length - 2);
                    }
                    var symbol = data.getSymbolByName(strSymbol);
                    if (symbol == null)
                    {
                        msg = $"Unknown Symbol {strSymbol}. Please register Symbol in DB.";
                        log.Log(msg);
                        expert.Reason = msg;
                        return(expert);
                    }

                    var adviser = data.getAdviser(Session, terminal.Id, symbol.Id, expert.EAName);
                    if (adviser == null)
                    {
                        adviser           = new DBAdviser();
                        adviser.Name      = expert.EAName;
                        adviser.Timeframe = expert.ChartTimeFrame;
                        adviser.Retired   = false;
                        var dbt = new DBTerminal();
                        dbt.Id           = terminal.Id;
                        adviser.Terminal = dbt;
                        adviser.Symbol   = symbol;
                    }
                    else
                    {
                        expert.IsMaster = adviser.IsMaster;
                    }

                    adviser.Running = true;
                    if (adviser.Id <= 0)
                    {
                        data.SaveInsertAdviser(Session, adviser);
                    }

                    var dynProps = data.GetPropertiesInstance((short)EntitiesEnum.Adviser, adviser.Id);
                    if (dynProps == null || Utils.HasAny(dynProps.Vals) == false)
                    {
                        expert.Data = "";
                    }
                    else
                    {
                        var res   = JsonConvert.DeserializeObject <Dictionary <string, DynamicProperty> >(dynProps.Vals);
                        var state = DefaultProperties.transformProperties(res);
                        expert.Data = JsonConvert.SerializeObject(state);
                    }
                    LoadSavedOrders(adviser, ref expert);
                    expert.Magic = adviser.Id;
                    SubscribeToSignals(adviser.Id);

                    log.Info($"Expert On <{adviser.Symbol.Name}> On TF=<{expert.ChartTimeFrame}> loaded successfully!");
                }

                return(expert);
            }
            catch (Exception e)
            {
                log.Error(e);
                expert.Magic = 0;
            }

            return(expert);
        }