Esempio n. 1
0
        public void NowDeals(DateTime nowTime, MqlRates nowRate)
        {
            if (!TestParameters2.RealTimeMode)
            {
                WekaUtils.Instance.WriteLog("in not realTimeMode, NowDeals should not be called.");
                return;
            }

            m_nowTime = nowTime;

            m_nowPrice = nowRate;

            foreach (var i in m_deals)
            {
                if (i.OpenTime >= m_nowTime)
                {
                    continue;
                }

                if (!i.CloseTime.HasValue)
                {
                    CalculateDealCloseTimeOnly(i);
                }
            }
        }
 private void ExecuteCandidateNowDeals(DateTime nowDate, MqlRates nowRate)
 {
     foreach (var kvp in m_classifierInfos)
     {
         var clsInfo = kvp.Value;
         clsInfo.Deals.NowDeals(nowDate, nowRate);
     }
 }
Esempio n. 3
0
        //private ParameterdCandidateStrategy _realDealsCandidate;
        public void RunOnTick(long nowTime, MqlRates nowRate)
        {
            // We now updateHpData on every bar
            //DateTime nowDate = WekaUtils.GetDateFromTime(nowTime);

            //if (!TestParameters2.UseFutureHpData)
            //{
            //    realDealsCandidate.ExecuteCandidateNowDeals(nowDate, nowRate);
            //}
        }
Esempio n. 4
0
        public void OnTick(MqlRates mqlRate, double zigzag, ReturnActionInfo returnAction)
        {
            //if (mqlRate.time != (new DateTime(2010, 5, 3, 16, 25, 0) - this.MtStartTime).TotalSeconds)
            //    return;

            //if (mqlRate.time < (new DateTime(2009, 08, 10, 07, 25, 0) - this.MtStartTime).TotalSeconds)
            //    return;
            //if (mqlRate.time > (new DateTime(2010, 08, 10, 07, 25, 0) - this.MtStartTime).TotalSeconds)
            //    return;

            if (zigzag == 0)
            {
                return;
            }

            if (m_lastTime == mqlRate.time && m_lastZigzag == zigzag)
            {
                return;
            }

            if (m_isCalculating)
            {
                return;
            }

            m_isCalculating = true;
            m_lastTime      = mqlRate.time;
            m_lastZigzag    = zigzag;

            try
            {
                int nowRatePos   = m_simulator.Rates.Count - 1;
                int nowZigzagPos = this.ZigzagValues.Count - 1;

                // Add Data
                if ((m_simulator.Rates[m_simulator.Rates.Count - 1].time - Parameters.MtStartTime).TotalSeconds < mqlRate.time)
                {
                    m_simulator.Rates.Add(new ZigzagRate
                    {
                        time        = Parameters.MtStartTime.AddSeconds(mqlRate.time),
                        open        = mqlRate.open,
                        high        = mqlRate.high,
                        low         = mqlRate.low,
                        close       = mqlRate.close,
                        tick_volume = mqlRate.tick_volume,
                        spread      = mqlRate.spread,
                        real_volume = mqlRate.real_volume,
                        zigzag      = zigzag
                    });
                    nowRatePos++;

                    if (zigzag != 0)
                    {
                        this.ZigzagValues.Add(zigzag);
                        this.ZigzagToRatePos.Add(m_simulator.Rates.Count - 1);

                        nowZigzagPos++;
                    }
                }
                else if ((m_simulator.Rates[m_simulator.Rates.Count - 1].time - Parameters.MtStartTime).TotalSeconds == mqlRate.time)
                {
                    var v = m_simulator.Rates[m_simulator.Rates.Count - 1];
                    v.open        = mqlRate.open;
                    v.high        = mqlRate.high;
                    v.low         = mqlRate.low;
                    v.close       = mqlRate.close;
                    v.tick_volume = mqlRate.tick_volume;
                    v.spread      = mqlRate.spread;
                    v.real_volume = mqlRate.real_volume;
                    v.zigzag      = zigzag;

                    if (zigzag != this.ZigzagValues[this.ZigzagValues.Count - 1])
                    {
                        this.ZigzagValues[this.ZigzagValues.Count - 1] = zigzag;
                    }
                }
                else
                {
                    for (int i = 0; i < m_simulator.Rates.Count; ++i)
                    {
                        if ((m_simulator.Rates[i].time - Parameters.MtStartTime).TotalSeconds == mqlRate.time)
                        {
                            nowRatePos = i;
                            break;
                        }
                    }

                    for (int i = 0; i < this.ZigzagToRatePos.Count; ++i)
                    {
                        if (this.ZigzagToRatePos[i] > nowRatePos)
                        {
                            nowZigzagPos = i - 1;
                            break;
                        }
                    }
                    //throw new ArgumentException("Invalid mqlRate's Time in OnTick!");
                }

                if (nowZigzagPos < m_zigzagPatternLength)
                {
                    return;
                }

                if (nowZigzagPos == m_lastZigzagPos)
                {
                    return;
                }

                m_lastZigzagPos = nowZigzagPos;

                Dictionary <int, int>     priceStatIn   = new Dictionary <int, int>();
                Dictionary <int, int>     priceStatOut  = new Dictionary <int, int>();
                List <List <ZigzagRate> > likeRatesList = new List <List <ZigzagRate> >();

                double[] nowZigzag  = new double[m_zigzagPatternLength];
                double[] prevZigzag = new double[m_zigzagPatternLength];

                for (int i = 0; i < m_zigzagPatternLength; ++i)
                {
                    nowZigzag[i] = this.ZigzagValues[nowZigzagPos + i + 1 - m_zigzagPatternLength]; //
                }
                Normalize(nowZigzag);


                for (int j = m_zigzagPatternLength - 1; j < nowZigzagPos; ++j)
                {
                    // 加 1 - m_zigzagPatternLength为朝后看
                    for (int i = 0; i < m_zigzagPatternLength; ++i)
                    {
                        prevZigzag[i] = this.ZigzagValues[j + i + 1 - m_zigzagPatternLength];
                    }
                    Normalize(prevZigzag);

                    double[] ret;
                    if (m_simiCache[nowZigzagPos, j, 0] != 0)
                    {
                        ret = new double[] { m_simiCache[nowZigzagPos, j, 0], m_simiCache[nowZigzagPos, j, 1] };
                    }
                    else
                    {
                        //ret = weka.timeseries.SimilarityAnalysis.GetSimilarity(nowZigzag, prevZigzag);
                        //m_simiCache[nowZigzagPos, j, 0] = ret[0];
                        //m_simiCache[nowZigzagPos, j, 1] = ret[1];
                        ret = new double[1] {
                            0
                        };
                    }

                    if (ret[0] < m_zigzagSimilarityFreqE && ret[1] < m_zigzagSimilarityTimeE)
                    {
                        //GetPriceStat(this.ZigzagToRatePos[j], 0, 50, priceStatIn);
                        //GetPriceStat(this.ZigzagToRatePos[j], 50, 200, priceStatOut);

                        var l = new List <ZigzagRate>();
                        likeRatesList.Add(l);
                        for (int i = 0; i < 120; ++i)
                        {
                            l.Add(m_simulator.Rates[this.ZigzagToRatePos[j] + i]);
                        }
                    }
                }

                if (likeRatesList.Count == 0)
                {
                    return;
                }

                double maxP = double.MinValue;
                int    maxDealType = 0, maxBl = 0, maxTp = 0, maxSl = 0;

                for (int i = 0; i <= 20; i += 5)
                {
                    for (int tp = 20; tp <= 100; tp += 5)
                    {
                        for (int sl = 10; sl < 50; sl += 5)
                        {
                            double profit = 0;
                            foreach (List <ZigzagRate> l in likeRatesList)
                            {
                                bool   hasPosition = false;
                                double dealPrice   = 0;
                                double nowPrice    = l[0].open;
                                foreach (ZigzagRate r in l)
                                {
                                    if (!hasPosition)
                                    {
                                        double price = nowPrice - i * 0.0001;
                                        if (price >= r.low && price < r.high)
                                        {
                                            hasPosition = true;
                                            dealPrice   = price;
                                        }
                                    }
                                    else
                                    {
                                        if (dealPrice + tp * 0.0001 >= r.low && dealPrice + tp * 0.0001 < r.high)
                                        {
                                            profit += tp;
                                            break;
                                        }
                                        else if (dealPrice - sl * 0.0001 + this.Spread * 0.0001 >= r.low && dealPrice - sl * 0.0001 + this.Spread * 0.0001 < r.high)
                                        {
                                            profit -= sl;
                                            break;
                                        }
                                    }
                                }
                            }

                            if (profit > 0 && profit > maxP)
                            {
                                maxDealType = 1;
                                maxP        = profit;
                                maxBl       = i;
                                maxTp       = tp;
                                maxSl       = sl;
                            }

                            // sell
                            profit = 0;
                            foreach (List <ZigzagRate> l in likeRatesList)
                            {
                                bool   hasPosition = false;
                                double dealPrice   = 0;
                                double nowPrice    = l[0].open;
                                foreach (ZigzagRate r in l)
                                {
                                    if (!hasPosition)
                                    {
                                        double price = nowPrice + i * 0.0001;
                                        if (price + this.Spread * 0.0001 >= r.low && price + this.Spread * 0.0001 < r.high)
                                        {
                                            hasPosition = true;
                                            dealPrice   = price;
                                        }
                                    }
                                    else
                                    {
                                        if (dealPrice - tp * 0.0001 - this.Spread * 0.0001 >= r.low && dealPrice - tp * 0.0001 - this.Spread * 0.0001 < r.high)
                                        {
                                            profit += tp;
                                            break;
                                        }
                                        else if (dealPrice + sl * 0.0001 >= r.low && dealPrice + sl * 0.0001 < r.high)
                                        {
                                            profit -= sl;
                                            break;
                                        }
                                    }
                                }
                            }

                            if (profit > 0 && profit > maxP)
                            {
                                maxDealType = -1;
                                maxP        = profit;
                                maxBl       = i;
                                maxTp       = tp;
                                maxSl       = sl;
                            }
                        }
                    }
                }

                using (StreamWriter sw = new StreamWriter("c:\\forex\\ea_order_detail.txt", true))
                {
                    sw.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", nowZigzagPos, maxDealType, maxBl, maxTp, maxSl, maxP, Parameters.MtStartTime.AddSeconds(mqlRate.time).ToString("yyyy.MM.dd HH:mm")));
                }

                using (StreamWriter sw = new StreamWriter("c:\\forex\\ea_order.txt", true))
                {
                    if (maxDealType == 1)
                    {
                        sw.WriteLine(string.Format("Buy, {0}, {1}, {2}, {3}, {4}", Parameters.MtStartTime.AddSeconds(mqlRate.time).ToString("yyyy.MM.dd HH:mm"), maxBl, maxTp, maxSl, maxP));
                    }
                    else if (maxDealType == -1)
                    {
                        sw.WriteLine(string.Format("Sell, {0}, {1}, {2}, {3}, {4}", Parameters.MtStartTime.AddSeconds(mqlRate.time).ToString("yyyy.MM.dd HH:mm"), maxBl, maxTp, maxSl, maxP));
                    }
                }

                //if (priceStatIn.Count == 0 && priceStatOut.Count == 0)
                //    return;

                //double count = 0, count2 = 0;
                //foreach (var dealIn in priceStatIn)
                //    count += dealIn.Value;
                //foreach (var dealOut in priceStatOut)
                //    count2 += dealOut.Value;

                //double maxPSell = double.MinValue, minPSell = double.MaxValue, allPSell = 0;
                //double maxPBuy = double.MinValue, minPBuy = double.MaxValue, allPBuy = 0;
                //int maxDealInBuy = 0, maxDealOutBuy = 0;
                //int maxDealInSell = 0, maxDealOutSell = 0;
                //foreach (var dealIn in priceStatIn)
                //{
                //    foreach (var dealOut in priceStatOut)
                //    {
                //        int profitSell = 0, profitBuy = 0;
                //        profitBuy = dealOut.Key - dealIn.Key - this.Spread;
                //        profitSell = dealIn.Key - dealOut.Key - this.Spread;

                //        double prob = (dealIn.Value / count) * (dealOut.Value / count2);
                //        double p1 = prob * profitBuy;
                //        double p2 = prob * profitSell;
                //        if (p1 > maxPBuy)
                //        {
                //            maxPBuy = p1;
                //            maxDealInBuy = dealIn.Key;
                //            maxDealOutBuy = dealOut.Key;
                //        }
                //        if (p1 < minPBuy)
                //        {
                //            minPBuy = p1;
                //        }

                //        if (p2 > maxPSell)
                //        {
                //            maxPSell = p2;
                //            maxDealInSell = dealIn.Key;
                //            maxDealOutSell = dealOut.Key;
                //        }
                //        if (p2 < minPSell)
                //        {
                //            minPSell = p2;
                //        }

                //        allPBuy += p1;
                //        allPSell += p2;
                //    }
                //}

                //if (allPBuy > allPSell && allPBuy > 0)
                ////if (maxPBuy > maxPSell && maxPBuy > 0)
                //{
                //    returnAction.DealType = 1;
                //    returnAction.DealIn = maxDealInBuy;
                //    returnAction.DealOut = maxDealOutBuy;
                //    returnAction.WinProb = allPBuy;
                //    returnAction.LoseProb = minPBuy;

                //    using (StreamWriter sw = new StreamWriter("c:\\forex\\ea_order.txt", true))
                //    {
                //        sw.WriteLine(string.Format("Buy, {0}, {1}, {2}, {3}, {4}", this.MtStartTime.AddSeconds(mqlRate.time).ToString("yyyy.MM.dd HH:mm"), maxDealInBuy, maxDealOutBuy, allPBuy.ToString("N2"), minPBuy.ToString("N2")));
                //    }
                //}
                //else if (allPSell > allPBuy && allPSell > 0)
                ////else if (maxPSell > maxPBuy && maxPSell > 0)
                //{
                //    returnAction.DealType = -1;
                //    returnAction.DealIn = maxDealInSell;
                //    returnAction.DealOut = maxDealOutSell;
                //    returnAction.WinProb = allPSell;
                //    returnAction.LoseProb = minPSell;

                //    using (StreamWriter sw = new StreamWriter("c:\\forex\\ea_order.txt", true))
                //    {
                //        sw.WriteLine(string.Format("Sell, {0}, {1}, {2}, {3}, {4}", this.MtStartTime.AddSeconds(mqlRate.time).ToString("yyyy.MM.dd HH:mm"), maxDealInSell, maxDealOutSell, allPSell.ToString("N2"), minPSell.ToString("N2")));
                //    }
                //}
                //else
                //{
                //    returnAction.DealType = 0;
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                m_isCalculating = false;
            }
        }