public void Save(MainWindow MainF, List <FData> FootData)
        {
            XmlTextWriter xmlfile = new XmlTextWriter(pathAllMatches, Encoding.UTF8);

            xmlfile.Formatting = Formatting.Indented;
            xmlfile.WriteStartElement("Data");
            for (int i = FootData.Count - 1; i >= 0; i--)
            {
                FData item = FootData[i];
                xmlfile.WriteStartElement("Match");

                xmlfile.WriteStartElement("League");
                xmlfile.WriteString(item.League);
                xmlfile.WriteEndElement();

                xmlfile.WriteStartElement("Team1");
                xmlfile.WriteString(item.Team1);
                xmlfile.WriteEndElement();

                xmlfile.WriteStartElement("Team2");
                xmlfile.WriteString(item.Team2);
                xmlfile.WriteEndElement();

                xmlfile.WriteStartElement("Time");
                xmlfile.WriteString(item.time.ToString());
                xmlfile.WriteEndElement();

                xmlfile.WriteStartElement("Match");
                xmlfile.WriteString(item.Match);
                xmlfile.WriteEndElement();
                xmlfile.WriteStartElement("BookieOdds");

                foreach (KeyValuePair <int, X> it in FootData[i].XKoef)
                {
                    BasicBrokerData data = MainF.Bookmakers[it.Key];
                    xmlfile.WriteStartElement("B_" + data.Name);

                    xmlfile.WriteStartElement("LastUpdate");
                    xmlfile.WriteString(it.Value.LastUpdate);
                    xmlfile.WriteEndElement();

                    xmlfile.WriteStartElement("X1");
                    xmlfile.WriteString(it.Value.Koef[1].ToString());
                    xmlfile.WriteEndElement();

                    xmlfile.WriteStartElement("X0");
                    xmlfile.WriteString(it.Value.Koef[0].ToString());
                    xmlfile.WriteEndElement();

                    xmlfile.WriteStartElement("X2");
                    xmlfile.WriteString(it.Value.Koef[2].ToString());
                    xmlfile.WriteEndElement();



                    if (MainF.Bookmakers[it.Key].Type == BrokerType.LAY)
                    {
                        xmlfile.WriteStartElement("LX1");
                        xmlfile.WriteString(item.Lay[1].ToString());
                        xmlfile.WriteEndElement();

                        xmlfile.WriteStartElement("LX0");
                        xmlfile.WriteString(item.Lay[0].ToString());
                        xmlfile.WriteEndElement();

                        xmlfile.WriteStartElement("LX2");
                        xmlfile.WriteString(item.Lay[2].ToString());
                        xmlfile.WriteEndElement();
                    }



                    xmlfile.WriteEndElement();
                }

/*
 *              for (int id = 0; id < MainF.Bookmakers.Count; id++)
 *              {
 *                  if (item.Koef[id, 1] == 0) continue;
 *                  BasicBrokerData data = MainF.Bookmakers[id];
 *                  xmlfile.WriteStartElement("B_" + data.Name);
 *
 *                  xmlfile.WriteStartElement("LastUpdate");
 *                  xmlfile.WriteString(item.LastUpdate[id]);
 *                  xmlfile.WriteEndElement();
 *
 *                  xmlfile.WriteStartElement("X1");
 *                  xmlfile.WriteString(item.Koef[id, 1].ToString());
 *                  xmlfile.WriteEndElement();
 *
 *                  xmlfile.WriteStartElement("X0");
 *                  xmlfile.WriteString(item.Koef[id, 0].ToString());
 *                  xmlfile.WriteEndElement();
 *
 *                  xmlfile.WriteStartElement("X2");
 *                  xmlfile.WriteString(item.Koef[id, 2].ToString());
 *                  xmlfile.WriteEndElement();
 *
 *                  xmlfile.WriteEndElement();
 *              }
 */

                xmlfile.WriteEndElement();
                xmlfile.WriteEndElement();
            }
            xmlfile.WriteEndElement();
            xmlfile.Close();
        }
        public void Load(List <FData> FootData, List <BasicBrokerData> Bookmakers)
        {
            if (File.Exists(pathAllMatches))
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(pathAllMatches);
                FootData.Clear();
                foreach (XmlNode item in xd.SelectNodes("Data/Match"))
                {
                    FData f = new FData();



                    if (item.SelectSingleNode(".//Team1") == null)
                    {
                        continue;
                    }
                    f.Team1 = item.SelectSingleNode(".//Team1").InnerText;

                    if (item.SelectSingleNode(".//Team2") == null)
                    {
                        continue;
                    }
                    f.Team2 = item.SelectSingleNode(".//Team2").InnerText;


                    if (item.SelectSingleNode(".//Match") == null)
                    {
                        continue;
                    }
                    f.Match = item.SelectSingleNode(".//Match").InnerText;

                    if (item.SelectSingleNode(".//League") == null)
                    {
                        continue;
                    }
                    f.League = item.SelectSingleNode(".//League").InnerText;


                    if (item.SelectSingleNode(".//Time") != null)
                    {
                        string time = time = item.SelectSingleNode(".//Time").InnerText;
                        try
                        {
                            DateTime buftime;
                            if (TryToParse.ParseDateTime(time, out buftime))
                            {
                                f.time = buftime;
                            }
                        }
                        catch (FormatException)
                        {
                            f.time = DateTime.Today;
                        }
                    }

                    for (int i = 0; i < Bookmakers.Count; i++)
                    {
                        BasicBrokerData data = Bookmakers[i];
                        if (item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/X0") == null)
                        {
                            continue;
                        }
                        f.AddNewItenToDictionary(i);
                        if (item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/LastUpdate") != null)
                        {
                            f.XKoef[i].LastUpdate = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/LastUpdate").InnerText;
                        }

                        double value = 1;
                        var    x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/X0");

                        if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                        {
                        }
                        f.XKoef[i].Koef[0] = value;

                        value = 1;
                        x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/X1");
                        if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                        {
                        }
                        f.XKoef[i].Koef[1] = value;

                        value = 1;
                        x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/X2");
                        if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                        {
                        }
                        f.XKoef[i].Koef[2] = value;

                        if (Bookmakers[i].Type == BrokerType.LAY)
                        {
                            value = 1;
                            x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/LX0");

                            if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                            {
                            }
                            f.Lay[0] = value;

                            value = 1;
                            x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/LX1");
                            if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                            {
                            }
                            f.Lay[1] = value;

                            value = 1;
                            x     = item.SelectSingleNode(".//BookieOdds/B_" + data.Name + "/LX2");
                            if (x != null && TryToParse.ParseDouble(x.InnerText, out value))
                            {
                            }
                            f.Lay[2] = value;
                        }
                    }

                    FootData.Add(f);
                }
            }
        }
        public void Calchedge(ref List <LayData> datalist, List <FData> FootData, CalcParameters cpar)
        {
            if (FootData.Count == 0)
            {
                return;
            }

            for (int id = FootData.Count - 1; id >= 0; id--)
            {
                FData item = FootData[id];
                if (cpar.today && item.time.Day != DateTime.Now.Day)
                {
                    continue;
                }


                foreach (KeyValuePair <int, X> it in item.XKoef)
                {
                    BasicBrokerData bookie = MainF.Bookmakers[it.Key];
                    if (cpar.preferedbookie != "All" && cpar.preferedbookie != bookie.Name)
                    {
                        continue;
                    }
                    if (!cpar.bookielimit.Contains(bookie.Name))
                    {
                        continue;
                    }

                    if (bookie.Type == BrokerType.BACK)
                    {
                        for (int q = 0; q < 3; q++)
                        {
                            if (it.Value.Koef[q] != 0 && item.Lay[q] != 0 && it.Value.Koef[q] >= bookie.Limit && CalcLayPL(it.Value.Koef[q], item.Lay[q]) > cpar.limit)
                            {
                                StringBuilder selection = new StringBuilder();
                                switch (q)
                                {
                                case 0: selection.Append("Draw"); break;

                                case 1: selection.Append(item.Team1); break;

                                case 2: selection.Append(item.Team2); break;

                                default: selection.Append("Uknown"); break;
                                }

                                LayData ld = new LayData();
                                ld.Match      = item.Team1 + " - " + item.Team2;
                                ld.Selection  = selection.ToString();
                                ld.Lay        = item.Lay[q];
                                ld.Back       = it.Value.Koef[q];
                                ld.Profit     = Math.Round(CalcLayPL(it.Value.Koef[q], item.Lay[q]), 3);
                                ld.Size       = item.SizeX[0];
                                ld.Bookie     = bookie.Name;
                                ld.League     = item.League;
                                ld.Time       = ChangeTimeToString(item.time);
                                ld.LastUpdate = it.Value.LastUpdate;
                                datalist.Add(ld);
                            }
                        }
                    } // end type.Back
                    if (bookie.Type == BrokerType.HANDICAP)
                    {
                        foreach (KeyValuePair <int, X> hi in item.XKoef)
                        {
                            if (MainF.Bookmakers[hi.Key].Type != BrokerType.BACK)
                            {
                                continue;
                            }
                            if (!cpar.bookielimit.Contains(MainF.Bookmakers[hi.Key].Name))
                            {
                                continue;
                            }
                            if (cpar.preferedbookie != "All" && cpar.preferedbookie != MainF.Bookmakers[hi.Key].Name)
                            {
                                continue;
                            }

                            if (hi.Value.Koef[1] != 0 && hi.Value.Koef[1] < hi.Value.Koef[2] && hi.Value.Koef[1] >= MainF.Bookmakers[hi.Key].Limit && CalcHandicapPL(hi.Value.Koef[1], it.Value.Koef[2]) > cpar.limit)
                            {
                                LayData ld = new LayData();
                                ld.Match      = item.Team1 + " - " + item.Team2;
                                ld.Selection  = item.Team1;
                                ld.Lay        = it.Value.Koef[2];
                                ld.Back       = hi.Value.Koef[1];
                                ld.Profit     = Math.Round(CalcHandicapPL(ld.Back, ld.Lay), 2);
                                ld.Bookie     = MainF.Bookmakers[hi.Key].Name;
                                ld.League     = item.League;
                                ld.Size       = -1;
                                ld.Time       = ChangeTimeToString(item.time);
                                ld.LastUpdate = hi.Value.LastUpdate + " Hand";
                                datalist.Add(ld);
                            }
                            if (hi.Value.Koef[2] != 0 && hi.Value.Koef[1] > hi.Value.Koef[2] && hi.Value.Koef[2] >= MainF.Bookmakers[hi.Key].Limit && CalcHandicapPL(hi.Value.Koef[2], it.Value.Koef[1]) > cpar.limit)
                            {
                                LayData ld = new LayData();
                                ld.Match      = item.Team1 + " - " + item.Team2;
                                ld.Selection  = item.Team2;
                                ld.Lay        = it.Value.Koef[1];
                                ld.Back       = hi.Value.Koef[2];
                                ld.Profit     = Math.Round(CalcHandicapPL(ld.Back, ld.Lay), 2);
                                ld.Bookie     = MainF.Bookmakers[hi.Key].Name;
                                ld.League     = item.League;
                                ld.Size       = -1;
                                ld.Time       = ChangeTimeToString(item.time);
                                ld.LastUpdate = hi.Value.LastUpdate + " Hand";
                                datalist.Add(ld);
                            }
                        }
                    }
                }
            }
        }