public bool StartUpdateCandles()
        {
            try
            {
                TCandlesUpdate(null);
                if (timerCandlesUpdate != null)
                {
                    timerCandlesUpdate.Dispose();
                }
                timerCandlesUpdate = new Timer(TCandlesUpdate);
                timerCandlesUpdate.Change(StrategyTool.NextTimerTick(Param.Interval, Print), StrategyTool.TimerPeriod(Param.Interval));

                if (timerLastCUpdate != null)
                {
                    timerLastCUpdate.Dispose();
                }
                timerLastCUpdate = new Timer(TLastCUpdate);
                timerLastCUpdate.Change(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(Param.FreqUpdate));
                return(true);
            }
            catch (Exception ex)
            {
                Print(string.Format("Ошибка StartUpdates: {0}", ex.Message), true);
                return(false);
            }
        }
        public void AddNewStrategy(StrategyParam SParam)
        {
            if (SParam == null)
            {
                return;
            }
            Strategy nStrategy = StrategyTool.GetStrategyByName(SParam.StrategyType, "");

            if (nStrategy == null)
            {
                System.Media.SystemSounds.Beep.Play();
                Print("Ошибка: неправильный тип стратегии - " + SParam.StrategyType);
                return;
            }
            StrategyTool.ChangeBaseParam(ref nStrategy, SParam);
            StrategiesList.Add(nStrategy.Param.StrategyName, nStrategy);
            nStrategy.SaveData();
        }
        /// <summary>
        /// Cохранить
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button12_Click(object sender, EventArgs e)
        {
            var CurStrategy = SManager.ActiveStrategy;

            if (CurStrategy == null)
            {
                return;
            }

            var ReadParam = ReadStrategyParam(true);

            if (ReadParam == null)
            {
                return;
            }

            string OldName = CurStrategy.Param.StrategyName;

            StrategyTool.ChangeBaseParam(ref CurStrategy, ReadParam);
            CheckStrategyRename(OldName, CurStrategy.Param.StrategyName);
            CurStrategy.SaveData();
        }
Example #4
0
        /// <summary>
        /// Формирование последней свечи по MarketHistory
        /// </summary>
        /// <returns></returns>
        private async Task <List <BaseTypes.Candle> > CandleByMarketHistory(BaseTypes.Market Market, Candle_Interval Interval, InvokePrint Print)
        {
            try
            {
                List <Trade> mhist = await GetMarketHistory(Market, Print);

                Candle lastCandle = await GetLastCandle(Market, Interval, Print);

                List <BaseTypes.Candle> resCandles = new List <BaseTypes.Candle>();
                DateTime NTime = StrategyTool.AddPeriod(lastCandle.Time, Interval);
                if (mhist[0].Timestamp > NTime && ConvertTime(mhist[0].Timestamp) != ConvertTime(NTime))
                {
                    resCandles.Add(new BaseTypes.Candle
                    {
                        Open  = mhist[0].Price,
                        Low   = mhist[0].Price,
                        Hight = mhist[0].Price,
                        Close = mhist[0].Price,
                        Time  = NTime
                    });
                }
                else
                {
                    return(null);
                }

                List <DateTime> times = new List <DateTime>();
                times.Add(NTime);
                while (times.Last() < mhist[0].Timestamp)
                {
                    times.Add(StrategyTool.AddPeriod(times.Last(), Interval));
                }
                times.RemoveAt(times.Count - 1);
                int Cpos = times.Count - 1;

                decimal Hval = mhist[0].Price;
                decimal Lval = mhist[0].Price;
                for (int i = 1; i < mhist.Count - 1; i++)
                {
                    if (mhist[i].Timestamp < times[Cpos]) //оформление свечи
                    {
                        resCandles.Last().Hight = Hval;
                        resCandles.Last().Low   = Lval;
                        resCandles.Last().Open  = mhist[i - 1].Price;

                        Hval = mhist[i].Price;
                        Lval = mhist[i].Price;

                        Cpos = Cpos - 1;
                        if (Cpos < 0)
                        {
                            break;
                        }
                        resCandles.Add(new BaseTypes.Candle
                        {
                            Open  = mhist[i].Price,
                            Low   = mhist[i].Price,
                            Hight = mhist[i].Price,
                            Close = mhist[i].Price,
                            Time  = times[Cpos]
                        });
                    }
                    else //обновление max/min текущей свечи
                    {
                        if (Hval < mhist[i].Price)
                        {
                            Hval = mhist[i].Price;
                        }
                        if (Lval > mhist[i].Price)
                        {
                            Lval = mhist[i].Price;
                        }
                    }
                }
                if (Cpos >= 0)
                {
                    resCandles.Last().Hight = Hval;
                    resCandles.Last().Low   = Lval;
                    resCandles.Last().Open  = mhist[mhist.Count - 1].Price;
                }
                resCandles.Reverse();
                if (resCandles.Count > 0)
                {
                    var tmp = await bittrex.GetTicker(Market.MarketName);

                    decimal lastValue = tmp.Last.Value;

                    if (lastValue > resCandles.Last().Hight)
                    {
                        resCandles.Last().Hight = lastValue;
                    }
                    else
                    if (lastValue < resCandles.Last().Low)
                    {
                        resCandles.Last().Low = lastValue;
                    }
                    else
                    {
                        resCandles.Last().Close = lastValue;
                    }
                }

                return(resCandles);
            }
            catch (Exception ex)
            {
                System.Media.SystemSounds.Beep.Play();
                Print("Ошибка BittrexApi CandleByMarketHistory: " + ex.Message);
                return(null);
            }
        }
        /// <summary>
        /// Прочитать стратегии из ini файла
        /// </summary>
        public void ReadStrategies()
        {
            if (!File.Exists(IniTool.IniFnameStates))
            {
                FileIniDataParser tfileIniData  = new FileIniDataParser();
                IniData           newParsedData = new IniData();
                tfileIniData.WriteFile(IniTool.IniFnameStates, newParsedData);
            }

            FileIniDataParser         fileIniData      = new FileIniDataParser();
            IniData                   parsedDataParams = fileIniData.ReadFile(IniTool.IniFnameParams);
            IniData                   parsedDataStates = fileIniData.ReadFile(IniTool.IniFnameStates);
            IEnumerable <SectionData> StratSectParams  = parsedDataParams.Sections.Where((x) => { return(x.SectionName.Contains("Strategy_")); });

            foreach (SectionData sdata in StratSectParams)
            {
                try
                {
                    Dictionary <string, string> ResDictParam = new Dictionary <string, string>();
                    for (int i = 0; i < sdata.Keys.Count; i++)
                    {
                        ResDictParam.Add(sdata.Keys.ElementAt(i).KeyName, sdata.Keys.ElementAt(i).Value);
                    }
                    Strategy nStrategy = StrategyTool.GetStrategyByName(ResDictParam["StrategyType"], sdata.SectionName);
                    nStrategy.ChangeState += OnStartegyChangeState;
                    nStrategy.Param.LoadData(ResDictParam);

                    if (parsedDataStates.Sections.ContainsSection(sdata.SectionName))
                    {
                        SectionData SDataState = parsedDataStates.Sections.GetSectionData(sdata.SectionName);
                        Dictionary <string, string> ResDictState = new Dictionary <string, string>();
                        for (int i = 0; i < SDataState.Keys.Count; i++)
                        {
                            ResDictState.Add(SDataState.Keys.ElementAt(i).KeyName, SDataState.Keys.ElementAt(i).Value);
                        }
                        nStrategy.State.LoadData(ResDictState);
                    }
                    else
                    {
                        nStrategy.SaveData(false);
                    }

                    StrategiesList.Add(nStrategy.Param.StrategyName, nStrategy);

                    if (nStrategy.State.IsStartegyRun)
                    {
                        nStrategy.State.IsStartegyRun = false;
                        try
                        {
                            ThreadPool.QueueUserWorkItem((x) => nStrategy.Start(false));
                            Print(string.Format("Запущено стратегию: [{0}]", nStrategy.Param.StrategyName));
                        }
                        catch (Exception ex)
                        {
                            System.Media.SystemSounds.Beep.Play();
                            Print(string.Format("Ошибка при запуске стратегии [{0}]: {1}", nStrategy.Param.StrategyName, ex.Message));
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Media.SystemSounds.Beep.Play();
                    Print("Ошибка при возобновлении стратегии из ini файла: " + ex.Message);
                }
            }
        }