Example #1
0
        public void ComputeStrategy(object selectedItem)
        {
            string filename     = selectedItem + "." + ResultFileExtension;
            var    bestReturn   = -120.0;
            var    candlesCount = Candles.Count;
            //var bestCalmar = 0.0;
            //var bestMa = 0;
            //var bestparamAVolLength = 0;
            //var bestparamADuration = 0;
            //var bestparamABuffer = 0.0;
            //var bestparamAVolThreshold = 0.0;
            //var bestparamAsl = 0.0;
            const double spread = 0.00016;

            using (var writer = new StreamWriter(filename, false))
            {
                writer.WriteLine("results");
            }
            //paramALength: 9 paramAVolLength: 26 paramABuffer: -0.00416 paramAVolThreshold: 100 paramADuration: 16 paramASL: 0.00576 Calmar: 2.6312
            //const int paramALength=9;
            for (var paramALength = 9; paramALength <= 19; paramALength += 1)//liczba swiec wstecz do obliczenia maksimum
            {
                var maxes = new List <double>(candlesCount)
                {
                    Candles.First().Close
                };
                var kon = candlesCount - 1;
                for (var i = 1; i < kon; i++)
                {
                    maxes.Add(GetCandlesRange(i, paramALength).Max(candle => candle.Close));//TODO: Change to High
                }
                const int paramAVolLength = 26;
                //for (int paramAVolLength = 26; paramAVolLength <= 26/*38*/; paramAVolLength++)
                {
                    //CreateStatus("paramALength: {0}", paramALength);
                    //AppendStatus("  paramAVolLength: {0}\r\n", paramAVolLength);
                    var volAverages = new List <double>(candlesCount)
                    {
                        0
                    };
                    for (var i = 1; i < kon; i++)
                    {
                        volAverages.Add(GetCandlesRange(i, paramALength).Average(candle => candle.Volume) - Candles[i].Volume);
                    }
                    //const int paramADuration = 16;
                    for (var paramADuration = 6; paramADuration <= 16; paramADuration++)//liczba kroków wprzód do zamkniecia pozycji
                    {
                        //for (var paramAVolThreshold = -100; paramAVolThreshold <= 100; paramAVolThreshold += 100)//próg dla sredniego wolumenu
                        const int paramAVolThreshold = 100;
                        {
                            for (var paramABuffer = -spread * 28; paramABuffer <= spread; paramABuffer += spread)
                            //dla kwadrantu a i b szukamy ujemnych wartości, dla kwadrantów c i d dodatnich wartości bufora
                            //const double paramABuffer = -0.00416;
                            {
                                //const double paramAsl = 0.00576;
                                for (var paramAsl = spread * 35; paramAsl <= spread * 48; paramAsl += spread)//Stop Loss minimalnie może być równy 8*spread 0.00384%
                                {
                                    var sumRa        = new List <double>(candlesCount);
                                    var returnValues = new List <double>(candlesCount);
                                    var drawdowns    = new List <double>(candlesCount);
                                    var candlesBegin = Math.Max(paramAVolLength, paramALength) + 1;
                                    //var positionCount = 0; //liczba otwieranych pozycji
                                    //var stopLossCount = 0; //liczba stop lossów
                                    var lastCandle = kon - Math.Max(paramALength, paramAVolLength) - paramADuration;
                                    for (int i = 0; i < candlesBegin; i++)
                                    {
                                        sumRa.Add(0);
                                        returnValues.Add(0);
                                    }
                                    for (int i = candlesBegin; i < lastCandle; i++)
                                    {
                                        returnValues.Add(0);
                                        if (maxes[i] + paramABuffer < Candles[i].Close && volAverages[i] < paramAVolThreshold)
                                        {
                                            returnValues[i] = Candles[i + paramADuration].Close - Candles[i + 1].Open - spread;
                                            //zysk z i-tej pozycji long zamykanej na zamknięciu po paramADuration kroku
                                            var minLow = Candles.GetRange(i + 1, paramADuration - 1).Min(candle => candle.Low);
                                            if ((-Candles[i + 1].Open + minLow) < -paramAsl)
                                            {
                                                returnValues[i] = -paramAsl;
                                                //stopLossCount++;
                                            }
                                            //positionCount++;
                                        }
                                        sumRa.Add(sumRa[i - 1] + returnValues[i]);  //krzywa narastania kapitału
                                    }


                                    var recordReturn   = 0.0; //rekord zysku
                                    var recordDrawdown = 0.0; //rekord obsuniecia
                                    for (int j = 0; j < lastCandle; j++)
                                    {
                                        if (sumRa[j] > recordReturn)
                                        {
                                            recordReturn = sumRa[j];
                                        }
                                        drawdowns.Add(sumRa[j] - recordReturn);//róznica pomiedzy bieżącą wartoscia kapitału skumulowanego a dotychczasowym rekordem
                                        if (drawdowns[j] < recordDrawdown)
                                        {
                                            recordDrawdown = drawdowns[j];  //obsuniecie maksymalne
                                        }
                                    }

                                    //wyniki końcowe
                                    var sumReturn = sumRa[lastCandle - 1];
                                    //var calmar = -sumReturn / recordDrawdown;  //wskaznik Calmara
                                    if (bestReturn < sumReturn)
                                    {
                                        bestReturn = sumReturn; //bestCalmar = calmar;
                                        //bestMa = paramALength;
                                        //bestparamAVolLength = paramAVolLength;
                                        //bestparamADuration = paramADuration;
                                        //bestparamABuffer = paramABuffer;
                                        //bestparamAVolThreshold = paramAVolThreshold;
                                        //bestparamAsl = paramAsl;
                                        //using (var writer = new StreamWriter(filename, true))
                                        //{
                                        //    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
                                        //            getDouble(sumReturn), getDouble(paramALength),
                                        //            getDouble(paramAVolLength), getDouble(paramADuration), getDouble(paramAVolThreshold),
                                        //            getDouble(paramABuffer), getDouble(paramAsl), getDouble(calmar));
                                        //}
                                        //CreateStatus("  \r\nzysk: {0}", sumReturn);
                                        //AppendStatus("  paramALength: {0}", paramALength);
                                        //AppendStatus("  paramAVolLength: {0}", paramAVolLength);
                                        //AppendStatus("  paramADuration: {0}", paramADuration);
                                        //AppendStatus("  paramAVolThreshold: {0}", paramAVolThreshold);
                                        //AppendStatus("  paramABuffer: {0}", paramABuffer);
                                        //AppendStatus("  paramASL: {0}", paramAsl);
                                        //AppendStatus("  Calmar: {0}", calmar);
                                        //Application.DoEvents();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //CreateStatus("zysk: {0}", 0);
            //AppendStatus("  paramALength: {0}", bestMa);
            //AppendStatus("  paramAVolLength: {0}", bestparamAVolLength);
            //AppendStatus("  paramABuffer: {0}", bestparamABuffer);
            //AppendStatus("  paramAVolThreshold: {0}", bestparamAVolThreshold);
            //AppendStatus("  paramADuration: {0}", bestparamADuration);
            //AppendStatus("  paramASL: {0}", bestparamAsl);
            //AppendStatus("  Calmar: {0}", bestCalmar);
        }
Example #2
0
        public void LoadStockDailyOrMinute()
        {
            Candles.Clear();
            CandlesBasic.Clear();
            CandlesHistory.Clear();

            try
            {
                string fName = LoadPathFileCsv();

                bool bDate1 = false;
                bool bDate2 = false;
                bool fileOrderDescending;

                DateTime date1 = DateTime.Today;
                DateTime date2 = DateTime.Today;
                string[] time1 = null;
                string[] time2 = null;
                int      candleInfoIndex;

                StreamReader stream = new StreamReader(fName);
                string       row;
                bool         goodSoFar = false;

                while ((row = stream.ReadLine()) != null)
                {
                    string[] splitRow = row.Split(';');

                    if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                    {
                        continue;
                    }

                    if (!bDate1)
                    {
                        DateTime.TryParse(splitRow[1], out date1);
                        bDate1 = true;
                        time1  = splitRow[2].Split(':');
                        continue;
                    }

                    if (!bDate2)
                    {
                        DateTime.TryParse(splitRow[1], out date2);
                        bDate2    = true;
                        time2     = splitRow[2].Split(':');
                        goodSoFar = true;
                        stream.Close();
                        break;
                    }
                }

                if (!goodSoFar)
                {
                    throw new Exception("Invalid CSV File - 1 Each row must be Symbol, Date, Time (optional), Open, High, Low, Close, and Volume (optional).");
                }

                if (time1.Length == 3 && time2.Length == 3)
                {
                    candleInfoIndex = 3;

                    DateTime date1Tmp = new DateTime(date1.Year, date1.Month, date1.Day,
                                                     Convert.ToInt32(time1[0]), Convert.ToInt32(time1[1]), Convert.ToInt32(time1[2]));
                    DateTime date2Tmp = new DateTime(date2.Year, date2.Month, date2.Day,
                                                     Convert.ToInt32(time2[0]), Convert.ToInt32(time2[1]), Convert.ToInt32(time2[2]));

                    fileOrderDescending = (date1Tmp > date2Tmp) ? false : true;
                }
                else if (time1.Length == 1 && time2.Length == 1)
                {
                    candleInfoIndex     = 2;
                    fileOrderDescending = (date1 > date2) ? false : true;
                }
                else
                {
                    throw new Exception("Invalid CSV File" + " 2 Each row must be Symbol, Date, Time (optional), Open, High, Low, Close, and Volume (optional).");
                }

                int hr;
                int mn;
                int sc;

                stream = new StreamReader(fName);

                List <string> listRowCollection = new List <string>();

                while ((row = stream.ReadLine()) != null)
                {
                    listRowCollection.Add(row);
                }

                stream.Close();

                if (fileOrderDescending)
                {
                    foreach (string t in listRowCollection)
                    {
                        string[] splitRow = t.Split(';');

                        if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                        {
                            continue;
                        }

                        DateTime dt;
                        if (!DateTime.TryParseExact(splitRow[1], "dd/MM/yyyy", null, DateTimeStyles.None, out dt))
                        {
                            continue;
                        }

                        hr = dt.Hour;
                        mn = dt.Minute;
                        sc = dt.Second;
                        if (dt.Hour == 0)
                        {
                            hr = 0;
                            mn = 0;
                            sc = 0;
                        }

                        if (candleInfoIndex == 3)
                        {
                            string[] strTime = splitRow[2].Split(':');
                            if (strTime.Length == 3)
                            {
                                hr = Convert.ToInt16(strTime[0]);
                                mn = Convert.ToInt16(strTime[1]);
                                sc = Convert.ToInt16(strTime[2]);
                            }
                        }

                        CandlesBasic.Add(new RegCandle
                        {
                            Date   = new DateTime(dt.Year, dt.Month, dt.Day, hr, mn, sc),
                            Open   = Convert.ToDouble(splitRow[candleInfoIndex]),
                            High   = Convert.ToDouble(splitRow[candleInfoIndex + 1]),
                            Low    = Convert.ToDouble(splitRow[candleInfoIndex + 2]),
                            Close  = Convert.ToDouble(splitRow[candleInfoIndex + 3]),
                            Volume = Convert.ToDouble(splitRow[candleInfoIndex + 4])
                        });
                    }
                }
                else
                {
                    for (int i = listRowCollection.Count - 1; i >= 0; i--)
                    {
                        string[] splitRow = listRowCollection[i].Split(';');

                        if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                        {
                            continue;
                        }

                        DateTime dt;
                        if (!DateTime.TryParseExact(splitRow[1], "dd/MM/yyyy", null, DateTimeStyles.None, out dt))
                        {
                            continue;
                        }

                        hr = dt.Hour;
                        mn = dt.Minute;
                        sc = dt.Second;

                        if (dt.Hour == 0)
                        {
                            hr = 0;
                            mn = 0;
                            sc = 0;
                        }

                        if (candleInfoIndex == 3)
                        {
                            string[] strTime = splitRow[2].Split(':');
                            if (strTime.Length == 3)
                            {
                                hr = Convert.ToInt16(strTime[0]);
                                mn = Convert.ToInt16(strTime[1]);
                                sc = Convert.ToInt16(strTime[2]);
                            }
                        }

                        CandlesBasic.Add(new RegCandle
                        {
                            Date   = new DateTime(dt.Year, dt.Month, dt.Day, hr, mn, sc),
                            Open   = Convert.ToDouble(splitRow[candleInfoIndex]),
                            High   = Convert.ToDouble(splitRow[candleInfoIndex + 1]),
                            Low    = Convert.ToDouble(splitRow[candleInfoIndex + 2]),
                            Close  = Convert.ToDouble(splitRow[candleInfoIndex + 3]),
                            Volume = Convert.ToDouble(splitRow[candleInfoIndex + 4])
                        });
                    }
                }

                if (((Periodicity == Periodicity.Daily) || ((Periodicity == Periodicity.Minutely))) &&
                    (Interval == 1))
                {
                    Candles.AddRange(CandlesBasic);
                }
                else
                {
                    Transform();
                }

                CandlesHistory.AddRange((Candles.Count - History) > 0
                                            ? Candles.GetRange(Candles.Count - History, History)
                                            : Candles);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }