Exemplo n.º 1
0
        public bool IsMatch(Dictionary <int, KLineCache> klineDataDic, float btcLSPercent, List <int> MACycleList)
        {
            switch (type)
            {
            case MatchConditionType.MA:

                int value = (int)args1;

                if (!klineDataDic.ContainsKey(value) && value % 1440 == 0)
                {
                    int        merge = value / 1440;
                    KLineCache cache = new KLineCache();
                    cache.RefreshData(klineDataDic[1440].GetMergeKLine(merge));
                    klineDataDic[value] = cache;
                }

                if (klineDataDic.ContainsKey(value))
                {
                    KLineCache kLineCache = klineDataDic[value];

                    List <float> maList = new List <float>();

                    List <float> perList = new List <float>();

                    for (int i = 0; i < paramsList1.Count; i++)
                    {
                        int maIndex = (int)paramsList1[i];

                        float maValue = 0;

                        if (maIndex == 0)
                        {
                            maValue = kLineCache.V_KLineData[0].V_ClosePrice;
                        }
                        else
                        {
                            if (MACycleList[maIndex - 1] > kLineCache.V_KLineData.Count)
                            {
                                return(false);
                            }

                            maValue = MA.GetMA(MACycleList[maIndex - 1], kLineCache.V_KLineData);
                        }


                        maList.Add(maValue);


                        float perValue = MathF.Abs((kLineCache.V_KLineData[0].V_ClosePrice - maValue) / maValue * 100);

                        perList.Add(perValue);
                    }

                    bool match = true;
                    for (int i = 0; i < maList.Count - 1; i++)
                    {
                        float perValue = MathF.Abs((maList[i] - maList[i + 1]) / maList[i + 1] * 100);

                        if (perValue > 1 && maList[i] < maList[i + 1])
                        {
                            match = false;
                            break;
                        }
                    }

                    if (match)
                    {
                        for (int i = 0; i < paramsList2.Count; i++)
                        {
                            if (paramsList2[i] < 0)
                            {
                                if (perList[i] < MathF.Abs(paramsList2[i]))
                                {
                                    match = false;
                                    break;
                                }
                            }
                            else
                            {
                                if (perList[i] > paramsList2[i])
                                {
                                    match = false;
                                    break;
                                }
                            }
                        }
                    }

                    return(match);
                }

                break;

            case MatchConditionType.EMA:

                value = (int)args1;

                if (klineDataDic.ContainsKey(value))
                {
                    KLineCache kLineCache = klineDataDic[value];

                    List <float> maList = new List <float>();

                    List <float> perList = new List <float>();

                    for (int i = 0; i < paramsList1.Count; i++)
                    {
                        int maIndex = (int)paramsList1[i];

                        float maValue = 0;

                        if (maIndex == 0)
                        {
                            maValue = kLineCache.V_KLineData[0].V_ClosePrice;
                        }
                        else
                        {
                            if (MACycleList[maIndex - 1] > kLineCache.V_KLineData.Count)
                            {
                                return(false);
                            }
                            maValue = EMA.GetEMA(MACycleList[maIndex - 1], kLineCache.V_KLineData);
                        }

                        maList.Add(maValue);


                        float perValue = MathF.Abs((kLineCache.V_KLineData[0].V_ClosePrice - maValue) / maValue * 100);

                        perList.Add(perValue);
                    }

                    bool match = true;
                    for (int i = 0; i < maList.Count - 1; i++)
                    {
                        if (maList[i] < maList[i + 1])
                        {
                            match = false;
                            break;
                        }
                    }

                    if (match)
                    {
                        for (int i = 0; i < paramsList2.Count; i++)
                        {
                            if (paramsList2[i] < 0)
                            {
                                if (perList[i] < MathF.Abs(paramsList2[i]))
                                {
                                    match = false;
                                    break;
                                }
                            }
                            else
                            {
                                if (perList[i] > paramsList2[i])
                                {
                                    match = false;
                                    break;
                                }
                            }
                        }
                    }

                    return(match);
                }
                break;

            case MatchConditionType.BtcLSPercent:

                if (args1 < 0)
                {
                    if (btcLSPercent < MathF.Abs(args1))
                    {
                        return(false);
                    }

                    return(true);
                }
                else
                {
                    if (btcLSPercent > args1)
                    {
                        return(false);
                    }

                    return(true);
                }

                break;

            case MatchConditionType.PriceList:

                value = (int)args1;

                if (klineDataDic.ContainsKey(value))
                {
                    KLineCache kLineCache = klineDataDic[value];

                    bool match = true;

                    int count = (int)paramsList1[0];

                    if (Math.Abs(count) + 1 > kLineCache.V_KLineData.Count)
                    {
                        return(false);
                    }

                    if (count > 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (kLineCache.V_KLineData[i].GetAvg() < kLineCache.V_KLineData[i + 1].GetAvg())
                            {
                                match = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < -count; i++)
                        {
                            if (kLineCache.V_KLineData[i].GetAvg() > kLineCache.V_KLineData[i + 1].GetAvg())
                            {
                                match = false;
                                break;
                            }
                        }
                    }
                    return(match);
                }
                break;

            case MatchConditionType.OldPrice:

                value = (int)args1;

                if (klineDataDic.ContainsKey(value))
                {
                    KLineCache kLineCache = klineDataDic[value];

                    bool match = false;

                    int startInedx = (int)paramsList1[0];
                    int dir        = (int)paramsList1[1];
                    int count      = Math.Abs(dir);

                    float curValue = kLineCache.V_KLineData[0].V_ClosePrice;

                    if (startInedx < kLineCache.V_KLineData.Count)
                    {
                        float p1 = kLineCache.V_KLineData[startInedx].GetAvg();
                        float p2 = kLineCache.V_KLineData[startInedx - count].GetAvg();

                        float percent = MathF.Abs((p1 - p2) / p2 * 100);

                        if (percent > 2)
                        {
                            if (dir > 0)
                            {
                                if (p1 < p2 && curValue < p1)
                                {
                                    match = true;
                                }
                            }
                            else
                            {
                                if (p1 > p2 && curValue > p1)
                                {
                                    match = true;
                                }
                            }
                        }
                    }
                    return(match);
                }
                break;

            default:
                break;
            }

            return(false);
        }
Exemplo n.º 2
0
    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    public virtual async void Start()
    {
        try
        {
            await m_TaticsHelper.RunHistory();

            if (V_TacticsState == EM_TacticsState.Stop)
            {
                return;
            }

            m_LastRefreshTime = DateTime.Now;

            V_AccountInfo = new AccountInfo();

            V_AccountInfo.V_Leverage = m_TaticsHelper.V_Leverage;

            //获取一下合约面值
            JContainer con = await CommonData.Ins.V_SwapApi.getInstrumentsAsync();

            if (V_TacticsState == EM_TacticsState.Stop)
            {
                return;
            }

            DataTable t = JsonConvert.DeserializeObject <DataTable>(con.ToString());
            foreach (DataRow dr in t.Rows)
            {
                if (dr["instrument_id"].Equals(V_Instrument_id))
                {
                    V_AccountInfo.V_Contract_val = float.Parse(dr["contract_val"].ToString());
                    break;
                }
            }

            //设置合约倍数
            await CommonData.Ins.V_SwapApi.setLeverageByInstrumentAsync(V_Instrument_id, (int)m_TaticsHelper.V_Leverage, "3");
        }
        catch (Exception ex)
        {
            Console.WriteLine(V_Instrument_id + "  " + ex.ToString());
            Debugger.Error(V_Instrument_id + "  " + ex.ToString());

            Console.WriteLine(V_Instrument_id + "  ReStart");
            Debugger.Error(V_Instrument_id + "  ReStart");

            Start();
        }

        if (V_TacticsState == EM_TacticsState.Stop)
        {
            return;
        }

        cache = new KLineCache();

        if (V_TacticsState == EM_TacticsState.Start)
        {
            V_TacticsState = EM_TacticsState.Normal;
        }

        Console.WriteLine("start {0}", V_Instrument_id);
        Debugger.Warn(string.Format("start {0}", V_Instrument_id));
        Update();
    }
Exemplo n.º 3
0
    /// <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 + ":分析历史数据完毕");
    }