示例#1
0
    /// <summary>
    /// 分析历史数据
    /// </summary>
    /// <returns></returns>
    public override async Task RunHistory()
    {
        await base.RunHistory();

        Console.WriteLine(V_Instrument_id + ":分析结果");
        Debugger.Warn(V_Instrument_id + ":分析结果");
        TaticsTestRunner.TestRun(this);
        Console.WriteLine(V_Instrument_id + ":分析历史数据完毕");
        Debugger.Warn(V_Instrument_id + ":分析历史数据完毕");
    }
示例#2
0
    public override void F_HandleOrderTest(TaticsTestRunner testRunner)
    {
        List <OrderOperation> result = HandleOrder(testRunner.V_Positions, true);

        if (result != null && result.Count > 0)
        {
            foreach (var item in result)
            {
                if (item.Operation != 0 && item.Dir != 0)
                {
                    if (item.Operation == 1)
                    {
                        //开仓
                        testRunner.OpenOrder(item.Dir, V_Cache.V_KLineData[0], item.Percent);
                    }
                    else
                    {
                        //平仓
                        testRunner.CloseOrder(V_Cache.V_KLineData[0], item.Dir, item.Percent, false);
                    }
                }
            }
        }
    }
示例#3
0
    private static float OnTestRun(BaseTaticsHelper helper, ref int loss_result, ref int win_result, ref float avg_win, ref int orderCount, ref int allWinCount, ref int allCount)
    {
        Dictionary <int, List <float> > winResultDic  = new Dictionary <int, List <float> >();
        Dictionary <int, List <float> > lossResultDic = new Dictionary <int, List <float> >();


        Dictionary <int, Dictionary <int, float> > all_ResultDic = new Dictionary <int, Dictionary <int, float> >();

        Dictionary <int, Dictionary <int, int> > all_CountDic = new Dictionary <int, Dictionary <int, int> >();

        allWinCount = 0;
        allCount    = 0;

        int minLoss = -30;
        int maxLoss = -60;
        int minWin  = 30;
        int maxWin  = 100;

        string[] stopValue = AppSetting.Ins.GetValue("StopValue").Split('_');
        if (stopValue.Length >= 4)
        {
            minLoss = int.Parse(stopValue[0]);
            maxLoss = int.Parse(stopValue[1]);
            minWin  = int.Parse(stopValue[2]);
            maxWin  = int.Parse(stopValue[3]);
        }

        for (int loss = minLoss; loss >= maxLoss; loss -= 5)
        {
            for (int win = minWin; win <= maxWin; win += 5)
            {
                allCount++;
                TaticsTestRunner run = new TaticsTestRunner();
                run.Cur_Cache = new KLineCache();
                helper.SetStopPercent(loss, win);
                run.Data_All = helper.V_HistoryCache.V_KLineData;
                run.helper   = helper;

                run.ResetData();
                float money = run.Run();
                if (money > TaticsTestRunner.Init_Money)
                {
                    allWinCount++;
                }

                if (!winResultDic.ContainsKey(win))
                {
                    winResultDic[win] = new List <float>();
                }
                winResultDic[win].Add(money);

                if (!lossResultDic.ContainsKey(loss))
                {
                    lossResultDic[loss] = new List <float>();
                }
                lossResultDic[loss].Add(money);


                if (!all_ResultDic.ContainsKey(loss))
                {
                    Dictionary <int, float> temp = new Dictionary <int, float>();
                    all_ResultDic[loss] = temp;
                }
                all_ResultDic[loss][win] = money;

                if (!all_CountDic.ContainsKey(loss))
                {
                    Dictionary <int, int> temp = new Dictionary <int, int>();
                    all_CountDic[loss] = temp;
                }
                all_CountDic[loss][win] = run.V_OrderCount;
            }
        }

        int   loss_final = 0, win_final = 0, orderCount_final = 0;
        float maxMoney = 0;

        foreach (var item in winResultDic)
        {
            List <float> resultList = item.Value;
            float        value      = resultList.Sum();
            if (maxMoney < value)
            {
                maxMoney  = value;
                win_final = item.Key;
            }
        }

        maxMoney = 0;
        foreach (var item in lossResultDic)
        {
            List <float> resultList = item.Value;
            float        value      = resultList.Sum();
            if (maxMoney < value)
            {
                maxMoney   = value;
                loss_final = item.Key;
            }
        }

        orderCount_final = all_CountDic[loss_final][win_final];

        helper.SetStopPercent(loss_final, win_final);
        helper.ClearTempData();

        float allWinMoney = 0;

        foreach (var loss in all_ResultDic)
        {
            foreach (var win in loss.Value)
            {
                allWinMoney += win.Value;
                //Console.WriteLine("止损 {0} 止盈 {1} 开单次数 {2}  模拟剩余 {3}", loss.Key, win.Key, all_CountDic[loss.Key][win.Key], win.Value);
            }
        }

        try
        {
            if (!(helper is ICycleTatics))
            {
                Console.WriteLine("{0}:盈利情况:{1}/{2}   盈利平均值:{3}", helper.V_Instrument_id, allWinCount, allCount, allWinMoney / allCount);

                Debugger.Warn(string.Format("{0}:盈利情况:{1}/{2}   盈利平均值:{3}", helper.V_Instrument_id, allWinCount, allCount, allWinMoney / allCount));

                Console.WriteLine("{0}:最佳止盈止损百分比值: {1} {2} 开单次数 {3} \n模拟剩余资金: {4}", helper.V_Instrument_id, loss_final, win_final, all_CountDic[loss_final][win_final], all_ResultDic[loss_final][win_final]);

                Debugger.Warn(string.Format("{0}:最佳止盈止损百分比值: {1} {2} 开单次数 {3} \n模拟剩余资金: {4}", helper.V_Instrument_id, loss_final, win_final, all_CountDic[loss_final][win_final], all_ResultDic[loss_final][win_final]));
            }

            avg_win = allWinMoney / allCount;

            loss_result = loss_final;
            win_result  = win_final;
            orderCount  = orderCount_final;
        }
        catch (Exception ex)
        {
            avg_win = 5;

            loss_result = -80;
            win_result  = 120;
            orderCount  = 0;
            Debugger.Error("没有合适的止损止盈,用默认值 -80 120");
        }



        if (loss_final == 0 || win_final == 0)
        {
            return(0);
        }
        return(all_ResultDic[loss_final][win_final]);
    }
 public virtual void  F_HandleOrderTest(TaticsTestRunner testRunner)
 {
 }
    /// <summary>
    /// 刷新历史数据
    /// </summary>
    public override async Task RunHistory()
    {
        await base.RunHistory();

        Console.WriteLine(V_Instrument_id + ":分析结果");

        Debugger.Warn(V_Instrument_id + ":分析结果");

        List <float> resultList_add = new List <float>();
        List <float> resultList_mul = new List <float>();

        List <float> klineList_add = new List <float>();
        List <float> klineList_mul = new List <float>();

        int          start    = 150;
        List <KLine> all_data = V_HistoryCache.V_KLineData;

        for (int i = start; i < all_data.Count - start; i++)
        {
            List <KLine> data = all_data.GetRange(all_data.Count - 1 - start - i, start);

            if (V_Cache == null)
            {
                V_Cache = new KLineCache();
            }
            V_Cache.RefreshData(data);

            float result = GetResult();

            if ((MathF.Abs(result) - 0.01f) > 0)
            {
                if (result > 0)
                {
                    resultList_add.Add(result);
                }
                else
                {
                    resultList_mul.Add(result);
                }
            }

            //KLine line = data[0];
            //float p = (line.V_ClosePrice - line.V_OpenPrice) / line.V_OpenPrice * V_Leverage * 100;
            //if (MathF.Abs(p) >= 0.1f)
            //{
            //    if (line.V_OpenPrice > line.V_ClosePrice)
            //    {
            //        //跌
            //        klineList_mul.Add(p);
            //    }
            //    else
            //    {
            //        //涨
            //        klineList_add.Add(p);
            //    }
            //}
        }

        result_add_avg = Util.GetAvg(resultList_add);
        result_mul_avg = Util.GetAvg(resultList_mul);

        resultList_add.AddRange(resultList_mul);

        result_avg = Util.GetAvg(resultList_add);

        TaticsTestRunner.TestRun(this);

        Console.WriteLine(V_Instrument_id + ":分析历史数据完毕");

        Debugger.Warn(V_Instrument_id + ":分析历史数据完毕");
    }