Example #1
0
    /// <summary>
    /// 自动处理
    /// </summary>
    /// <returns></returns>
    public async Task AutoHandle()
    {
        await m_TaticsHelper.F_AfterHandleOrder(V_AccountInfo);

        if (m_TaticsHelper.V_HandleOrderSelf)
        {
            await m_TaticsHelper.F_HandleOrder(V_AccountInfo);
        }
        else
        {
            await m_TaticsHelper.GetLongShortRatio(m_TaticsHelper.V_Cache.V_KLineData[0].V_Timestamp);

            bool  hasLong = false, hasShort = false;
            float longPercent = 0, shortPrecent = 0;
            if (V_AccountInfo.V_Positions != null && V_AccountInfo.V_Positions.Count > 0)
            {
                m_TaticsHelper.hasOrder = true;

                foreach (var item in V_AccountInfo.V_Positions)
                {
                    if (item.V_Dir > 0)
                    {
                        hasLong = true;
                    }
                    else
                    {
                        hasShort = true;
                    }

                    //有单就算下是否需要平仓
                    float v = item.GetPercent(V_AccountInfo.V_CurPrice);

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

                    if (m_TaticsHelper.ShouldCloseOrder(item.V_Dir, v))
                    {
                        if (v > 0)
                        {
                            if (V_OrderState == EM_OrderOperation.NoClose || V_OrderState == EM_OrderOperation.LongNoClose || V_OrderState == EM_OrderOperation.ShortNoClose)
                            {
                                return;
                            }
                        }

                        if (item.V_Dir > 0)
                        {
                            hasLong = false;
                        }
                        else
                        {
                            hasShort = false;
                        }

                        await V_AccountInfo.ClearPositions(item.V_Dir);
                    }
                }
            }
            else
            {
                if (m_TaticsHelper.hasOrder)
                {
                    m_TaticsHelper.hasOrder     = false;
                    m_TaticsHelper.V_LastOpTime = DateTime.UtcNow;
                }
            }

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

            bool makeOrder = false;

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


            if (makeOrder)
            {
                long leave = m_TaticsHelper.GetCoolDown();
                if (leave < 0)
                {
                    if (debug)
                    {
                        Console.WriteLine("{0} {1}:冷却中 cd {2}", DateTime.Now, V_Instrument_id, leave);
                        Debugger.Log(string.Format("{0} {1}:冷却中 cd {2}", DateTime.Now, V_Instrument_id, leave));
                        debug = false;
                    }
                    else
                    {
                        debugCount++;
                        if (debugCount >= 100)
                        {
                            debugCount = 0;
                            debug      = true;
                        }
                    }
                    return;
                }

                int o = m_TaticsHelper.MakeOrder();

                if (o > 0 && !hasLong)
                {
                    if (hasShort && shortPrecent > 0)
                    {
                        return;
                    }

                    //多单
                    if (V_OrderState != EM_OrderOperation.ShortOnly && V_OrderState != EM_OrderOperation.ShortNoClose)
                    {
                        m_TaticsHelper.V_LastOpTime = DateTime.UtcNow;

                        await V_AccountInfo.MakeOrder(1, V_AccountInfo.GetAvailMoney() *orderPercent);
                    }
                }
                else if (o < 0 && !hasShort)
                {
                    if (hasLong && longPercent > 0)
                    {
                        return;
                    }

                    //空单
                    if (V_OrderState != EM_OrderOperation.LongOnly && V_OrderState != EM_OrderOperation.LongNoClose)
                    {
                        m_TaticsHelper.V_LastOpTime = DateTime.UtcNow;
                        await V_AccountInfo.MakeOrder(-1, V_AccountInfo.GetAvailMoney() *orderPercent);
                    }
                }
            }
        }
    }
Example #2
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);
                }
            }
        }
    }