Ejemplo n.º 1
0
    /// <summary>
    /// 刷新历史数据
    /// </summary>
    public virtual async Task RunHistory()
    {
        Console.WriteLine(V_Instrument_id + ":获取历史数据");

        Debugger.Log(V_Instrument_id + ":获取历史数据");

        if (V_HistoryCache == null)
        {
            V_HistoryCache = new KLineCache();
        }

        List <KLine> history_data = new List <KLine>();

        SwapApi api = CommonData.Ins.V_SwapApi;

        int length = V_Min;

        DateTime t_start = DateTime.Now.AddMinutes(-length * 2000);

        DateTime t_end = DateTime.Now;

        while (t_start.AddMinutes(length * 200) < t_end)
        {
            JContainer con = await api.getCandlesDataAsync(V_Instrument_id, t_start, t_start.AddMinutes(length * 200), length * 60);

            List <KLine> d = KLine.GetListFormJContainer(con);

            d.AddRange(history_data);

            history_data.Clear();

            history_data.AddRange(d);

            t_start = t_start.AddMinutes(length * 200);
        }

        Console.WriteLine(V_Instrument_id + ":历史数据 " + history_data.Count + "条");

        Debugger.Log(V_Instrument_id + ":历史数据 " + history_data.Count + "条");

        V_HistoryCache.RefreshData(history_data);
        V_LastOpTime = history_data[history_data.Count - 1].V_Timestamp;
    }
Ejemplo n.º 2
0
 void UpdateTestData()
 {
     if (kLineDataDic.Count <= 0)
     {
         for (int i = 0; i < V_MinList.Count; i++)
         {
             KLineCache cache = new KLineCache();
             cache.RefreshData(V_Cache.GetMergeKLine(V_MinList[i] / 60));
             kLineDataDic.Add(V_MinList[i], cache);
         }
     }
     else
     {
         for (int i = 0; i < V_MinList.Count; i++)
         {
             kLineDataDic[V_MinList[i]].RefreshData(V_Cache.GetMergeKLine(V_MinList[i] / 60));
         }
     }
 }
Ejemplo n.º 3
0
    public override async Task F_AfterHandleOrder(AccountInfo info)
    {
        for (int i = 0; i < V_MinList.Count; i++)
        {
            int        value = V_MinList[i];
            JContainer con   = await CommonData.Ins.V_SwapApi.getCandlesDataAsync(V_Instrument_id, DateTime.Now.AddMinutes(-value * 200), DateTime.Now, 60 *value);

            if (kLineDataDic.ContainsKey(value))
            {
                kLineDataDic[value].RefreshData(con);
            }
            else
            {
                KLineCache kLineCache = new KLineCache();
                kLineCache.RefreshData(con);

                kLineDataDic.Add(value, kLineCache);
            }
        }
        await base.F_AfterHandleOrder(info);
    }
Ejemplo n.º 4
0
        async Task GetKLineValue(bool debug = true)
        {
            index = 0;
            foreach (var item in m_USDTList.Keys)
            {
                //获取K线数据
                //获取近200条K线

                try
                {
                    SpotData spotData = m_USDTList[item];

                    //Debugger.Log("获取K线数据:" + spotData.SpotName);

                    JContainer con = await CommonData.Ins.V_SpotApi.getCandlesAsync(spotData.SpotName, DateTime.Now.AddMinutes(-24 * 60 * 200), DateTime.Now, 24 * 60 * 60);

                    spotData.dayData.RefreshData(con);

                    for (int i = 0; i < MinList.Count; i++)
                    {
                        int value = MinList[i];

                        await Task.Run(delegate
                        {
                            Thread.Sleep(500);
                        });

                        con = await CommonData.Ins.V_SpotApi.getCandlesAsync(spotData.SpotName, DateTime.Now.AddMinutes(-value * 200), DateTime.Now, 60 *value);

                        if (spotData.kLineDataDic.ContainsKey(value))
                        {
                            spotData.kLineDataDic[value].RefreshData(con);
                        }
                        else
                        {
                            KLineCache kLineCache = new KLineCache();
                            kLineCache.RefreshData(con);
                            spotData.kLineDataDic.Add(value, kLineCache);
                        }
                    }

                    spotData.hourData    = spotData.kLineDataDic[60];
                    spotData.sixHourData = spotData.kLineDataDic[360];

                    //计算推荐值
                    spotData.RefreshCommandValue(debug);
                }
                catch (Exception ex)
                {
                    Debugger.Error(ex.ToString());
                }
                index++;
            }


            foreach (var key in m_ResultDic.Keys)
            {
                if (m_USDTList.ContainsKey(key))
                {
                    m_ResultDic[key].V_CurPrice          = m_USDTList[key].V_CurPrice;
                    m_ResultDic[key].V_AllPrice          = m_USDTList[key].V_AllPrice;
                    m_ResultDic[key].recommandValue      = m_USDTList[key].recommandValue;
                    m_ResultDic[key].shortRecommandValue = m_USDTList[key].shortRecommandValue;
                }
            }
        }
Ejemplo n.º 5
0
    //public void Start() {
    //    Console.WriteLine("start {0}",V_Instrument_id);
    //    Update();
    //}

    public virtual async void Update()
    {
        if (V_TacticsState == EM_TacticsState.Stop)
        {
            return;
        }

        if (DateTime.Now.Minute % 30 == 0 && DateTime.Now.Second < 5)
        {
            debug = true;
        }

        try
        {
            SwapApi api = CommonData.Ins.V_SwapApi;

            //更新账号信息
            JObject obj = await api.getAccountsByInstrumentAsync(V_Instrument_id);

            V_AccountInfo.RefreshData(obj["info"].ToString());

            //更新持仓信息
            obj = await api.getPositionByInstrumentAsync(V_Instrument_id);

            V_AccountInfo.RefreshPositions(Position.GetPositionList(obj["holding"].ToString()));

            //更新未完成订单信息,全部撤销掉
            await V_AccountInfo.ClearOrders();

            if (V_AccountInfo.V_Position == null && (DateTime.Now - m_LastRefreshTime).Ticks > (long)m_TaticsHelper.V_Min * Util.Minute_Ticks * AppSetting.Ins.GetInt("RefreshSettingTime"))//更新设置操作
            {
                //更新参数
                await m_TaticsHelper.RunHistory();

                m_LastRefreshTime = DateTime.Now;

                Console.WriteLine("{0} {1}:更新设置", DateTime.Now, V_Instrument_id);
            }
            else
            {
                if (debug)
                {
                    Console.WriteLine("{0} {1}:获取数据", DateTime.Now, V_Instrument_id);
                }

                //获取近200条K线
                JContainer con = await api.getCandlesDataAsync(V_Instrument_id, DateTime.Now.AddMinutes(-m_TaticsHelper.V_Min * 200), DateTime.Now, m_TaticsHelper.V_Min * 60);

                cache.RefreshData(con);

                V_AccountInfo.V_CurPrice = cache.V_KLineData[0].V_ClosePrice;

                if (!V_AccountInfo.HasEnoughMoney(orderPercent) && V_AccountInfo.V_Position == null)
                {
                    Console.WriteLine("{0} 钱不够。。。。", V_Instrument_id);
                    Debugger.Error(string.Format("{0} 钱不够。。。。", V_Instrument_id));
                    TimeEventHandler.Ins.AddEvent(new TimeEventModel(600, 1, Update));
                    return;
                }

                await Handle();
            }
        }
        catch (Exception ex)
        {
            //Console.WriteLine(ex.ToString());
            Console.WriteLine("{0} {1}:处理数据异常", DateTime.Now, V_Instrument_id);
            error = true;
        }

        debug = false;
        Update();

        //TimeEventHandler.Ins.AddEvent(new TimeEventModel(1, 1, Update));
    }
Ejemplo n.º 6
0
    public virtual void Handle(List <KLine> data)
    {
        KLine line = data[0];

        Cur_Cache.RefreshData(data);
        helper.V_Cache = Cur_Cache;

        if (helper.V_HandleOrderSelf)
        {
            helper.F_HandleOrderTest(this);
        }
        else
        {
            bool  hasLong = false, hasShort = false;
            float longPercent = 0, shortPrecent = 0;

            if (V_Positions != null && V_Positions.Count > 0)
            {
                List <int> closeList = new List <int>();
                foreach (var item in V_Positions)
                {
                    if (item.V_Dir > 0)
                    {
                        hasLong = true;
                    }
                    else
                    {
                        hasShort = true;
                    }

                    //有单就算下是否需要平仓
                    float v = item.GetPercentTest(data[0], helper.V_LossPercent);

                    if (item.V_Dir > 0)
                    {
                        longPercent = v;
                    }
                    else
                    {
                        shortPrecent = v;
                    }

                    if (helper.ShouldCloseOrderTest(item.V_Dir, v, Cur_Cache.V_KLineData[0]))
                    {
                        if (item.V_Dir > 0)
                        {
                            hasLong = false;
                        }
                        else
                        {
                            hasShort = false;
                        }
                        closeList.Add(item.V_Dir);
                    }
                }

                if (closeList.Count > 0)
                {
                    foreach (var item in closeList)
                    {
                        CloseOrder(data[0], item);
                    }
                    return;
                }
            }

            bool makeOrder = false;
            if (helper.V_IsDoubleSide)
            {
                makeOrder = !hasShort || !hasLong;
            }
            else
            {
                makeOrder = !hasShort && !hasLong;
            }


            if (makeOrder)
            {
                long leave = helper.GetCoolDownTest();
                if (leave < 0)
                {
                    return;
                }

                int o = helper.MakeOrder(true);

                if (o > 0 && !hasLong)
                {
                    if (hasShort && shortPrecent > 0)
                    {
                        return;
                    }
                    //多单
                    helper.V_LastOpTime = helper.V_Cache.V_KLineData[0].V_Timestamp;
                    OpenOrder(1, Cur_Cache.V_KLineData[0], orderPercent);
                }
                else if (o < 0 && !hasShort)
                {
                    if (hasLong && longPercent > 0)
                    {
                        return;
                    }
                    //空单
                    helper.V_LastOpTime = helper.V_Cache.V_KLineData[0].V_Timestamp;
                    OpenOrder(-1, Cur_Cache.V_KLineData[0], orderPercent);
                }
            }
        }
    }
Ejemplo n.º 7
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);
        }