예제 #1
0
        public void EventHandlerSmall(object[] param)
        {
            if (MinValueSmall.Count == Size)
            {
                MinValueSmall.RemoveAt(0);
            }
            if (MaxValueSmall.Count == Size)
            {
                MaxValueSmall.RemoveAt(0);
            }
            if (param[0].ToString() == "Min")
            {
                lastvalue = LastZigZagValue.MIN;
                double      gapValue   = (double)param[1];
                double      price      = (double)param[2];
                DateTime    time       = (DateTime)param[3];
                ProbaStruct Temp_value = new ProbaStruct()
                {
                    GapValue = gapValue, price = price, time = time
                };
                MinValueSmall.Add(Temp_value);
                if (MinValueSmall.Count == Size)
                {
                    int convergence = 0;
                    int divergence  = 0;
                    lock (locker)
                    {
                        for (int i = 1; i < Size; i++)
                        {
                            if (MinValueSmall[i].GapValue <MinValueSmall[i - 1].GapValue && MinValueSmall[i].price> MinValueSmall[i - 1].price)
                            {
                                convergence++;
                            }
                            if (MinValueSmall[i].GapValue > MinValueSmall[i - 1].GapValue && MinValueSmall[i].price > MinValueSmall[i - 1].price)
                            {
                                divergence++;
                            }
                        }
                        if (MinValueSmall.Last().GapValue < lvl_dwn_last * 0.8)
                        {
                            longlock = true;
                        }
                        else
                        {
                            longlock = false;
                        }
                        if (convergence == Size)
                        {
                            convergenceshort = true;
                        }
                        else
                        {
                            convergenceshort = false;
                        }
                        if (divergence == Size)
                        {
                            divergenceshort = true;
                        }
                        else
                        {
                            divergenceshort = false;
                        }
                    }
                }
            }
            if (param[0].ToString() == "Max")
            {
                lastvalue = LastZigZagValue.MAX;
                double      gapValue   = (double)param[1];
                double      price      = (double)param[2];
                DateTime    time       = (DateTime)param[3];
                ProbaStruct Temp_value = new ProbaStruct()
                {
                    GapValue = gapValue, price = price, time = time
                };
                MaxValueSmall.Add(Temp_value);

                if (MaxValueSmall.Count == Size)
                {
                    int convergence = 0;
                    int divergence  = 0;
                    lock (locker)
                    {
                        if (!longlock)
                        {
                            openlongcloseshort = true;
                        }
                        for (int i = 1; i < Size; i++)
                        {
                            if (MaxValueSmall[i].GapValue > MaxValueSmall[i - 1].GapValue && MaxValueSmall[i].price < MaxValueSmall[i - 1].price)
                            {
                                convergence++;
                            }
                            if (MaxValueSmall[i].GapValue < MaxValueSmall[i - 1].GapValue && MaxValueSmall[i].price < MaxValueSmall[i - 1].price)
                            {
                                divergence++;
                            }
                        }
                        if (MaxValueSmall.Last().GapValue > lvl_up_last * 0.8)
                        {
                            shortlock = true;
                        }
                        else
                        {
                            shortlock = false;
                        }
                        if (convergence == Size)
                        {
                            convergencelong = true;
                        }
                        else
                        {
                            convergencelong = false;
                        }
                        if (divergence == Size)
                        {
                            divergencelong = true;
                        }
                        else
                        {
                            divergencelong = false;
                        }
                    }
                }
            }
        }
예제 #2
0
        public void PositionHandler(object[] param)
        {
            double   gap  = (double)param[0];
            double   ask  = (double)param[1];
            double   bid  = (double)param[2];
            DateTime time = (DateTime)param[3];

            if (openpos == OpenPosition.LONG)
            {
                if (profit_to_close > 0)
                {
                    if (bid >= PositionLong.value + profit_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to TakeProfit condition");
                        }
                        openpos = OpenPosition.NONE;
                        lock (locker)
                        {
                            tradeTerminal.ClosePosition(PositionLong.index);
                        }
                        double profit = bid - PositionLong.value;
                        all_profit += profit;
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, name + " send sell order " + PositionLong.index + " req price : " + bid + " Profit: " + profit);
                        }
                        return;
                    }
                }
                if (lose_to_close > 0)
                {
                    if (bid <= PositionLong.value - lose_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to StopLose condition");
                        }
                        openpos = OpenPosition.NONE;
                        lock (locker)
                        {
                            tradeTerminal.ClosePosition(PositionLong.index);
                        }
                        double profit = bid - PositionLong.value;
                        all_profit += profit;
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, name + " send sell order " + PositionLong.index + " req price : " + bid + " Profit: " + profit);
                        }
                        return;
                    }
                }
            }
            if (openpos == OpenPosition.SHORT)
            {
                if (profit_to_close > 0)
                {
                    if (PositionShort.value - profit_to_close >= ask)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to TakeProfit condition");
                        }
                        openpos = OpenPosition.NONE;
                        lock (locker)
                        {
                            tradeTerminal.ClosePosition(PositionShort.index);
                        }
                        double profit = PositionShort.value - ask;
                        all_profit += profit;
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, name + " send buy order " + PositionShort.index + " req price : " + ask + " Profit: " + profit);
                        }
                        return;
                    }
                }
                if (lose_to_close > 0)
                {
                    if (ask >= PositionShort.value + lose_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to StopLose condition");
                        }
                        openpos = OpenPosition.NONE;
                        lock (locker)
                        {
                            tradeTerminal.ClosePosition(PositionShort.index);
                        }
                        double profit = PositionShort.value - ask;
                        all_profit += profit;
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, name + " send buy order " + PositionShort.index + " req price : " + ask + " Profit: " + profit);
                        }
                        return;
                    }
                }
            }
            if (lastvalue == LastZigZagValue.MIN)
            {
                if (openpos == OpenPosition.LONG)
                {
                }
                if (convergencelong)
                {
                    double last_knee = Math.Abs(MaxValueSmall.Last().GapValue - MinValueSmall.Last().GapValue);
                    double temp_knee = Math.Abs(MinValueSmall.Last().GapValue - gap);
                    double r_exp     = temp_knee / last_knee;
                    if (r_exp >= ratio)
                    {
                        if (!shortlock)
                        {
                            Position pos = new Position();
                            pos.index    = ind++;
                            pos.value    = ask;
                            PositionLong = pos;
                            openpos      = OpenPosition.LONG;
                            lock (locker)
                            {
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Buy, ind);
                            }
                            if (LoggNeeded)
                            {
                                logger.LogEvent(time, name + " Открываем long по дивергенции " + ind + " req price : " + ask);
                            }
                            return;
                        }
                    }
                }
                if (divergencelong)
                {
                    openpos = OpenPosition.NONE;
                    tradeTerminal.ClosePosition(PositionShort.index);
                    double profit = PositionShort.value - ask;
                    all_profit += profit;
                    if (LoggNeeded)
                    {
                        logger.LogEvent(time, name + " send buy order " + PositionShort.index + " req price : " + ask + " Profit: " + profit);
                    }
                    return;
                }
                if (openlongcloseshort)
                {
                    lock (locker)
                    {
                        if (gap > lvl_up_last)
                        {
                            openlongcloseshort = false;
                            if (openpos == OpenPosition.SHORT)
                            {
                                openpos = OpenPosition.NONE;
                                tradeTerminal.ClosePosition(PositionShort.index);
                                double profit = PositionShort.value - ask;
                                all_profit += profit;
                                if (LoggNeeded)
                                {
                                    logger.LogEvent(time, name + " send buy order " + PositionShort.index + " req price : " + ask + " Profit: " + profit);
                                }
                                return;
                            }
                            if (openpos == OpenPosition.NONE)
                            {
                                Position pos = new Position();
                                pos.index    = ind++;
                                pos.value    = ask;
                                PositionLong = pos;
                                openpos      = OpenPosition.LONG;
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Buy, pos.index);
                                if (LoggNeeded)
                                {
                                    logger.LogEvent(time, name + " send buy order " + pos.index + " req price : " + pos.value);
                                }

                                return;
                            }
                        }
                    }
                }
            }
            if (lastvalue == LastZigZagValue.MAX)
            {
                if (openpos == OpenPosition.SHORT)
                {
                }
                if (convergenceshort)
                {
                    double last_knee = Math.Abs(MinValueSmall.Last().GapValue - MaxValueSmall.Last().GapValue);
                    double temp_knee = Math.Abs(MaxValueSmall.Last().GapValue - gap);
                    double r_exp     = temp_knee / last_knee;
                    if (r_exp >= ratio)
                    {
                        if (!shortlock)
                        {
                            Position pos = new Position();
                            pos.index     = ind++;
                            pos.value     = bid;
                            PositionShort = pos;
                            openpos       = OpenPosition.SHORT;
                            lock (locker)
                            {
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Sell, ind);
                            }
                            if (LoggNeeded)
                            {
                                logger.LogEvent(time, name + " Открываем short по дивергенции " + ind + " req price : " + bid);
                            }
                            return;
                        }
                    }
                }
                if (divergenceshort)
                {
                    openpos = OpenPosition.NONE;
                    lock (locker)
                    {
                        tradeTerminal.ClosePosition(PositionLong.index);
                    }
                    double profit = bid - PositionLong.value;
                    all_profit += profit;
                    if (LoggNeeded)
                    {
                        logger.LogEvent(time, name + " send sell order " + PositionLong.index + " req price : " + bid + " Profit: " + profit);
                    }
                    return;
                }
                if (openshortcloselong)
                {
                    lock (locker)
                    {
                        if (gap < lvl_dwn_last)
                        {
                            openlongcloseshort = false;
                            if (openpos == OpenPosition.LONG)
                            {
                                openpos = OpenPosition.NONE;
                                tradeTerminal.ClosePosition(PositionLong.index);
                                double profit = bid - PositionLong.value;
                                all_profit += profit;
                                if (LoggNeeded)
                                {
                                    logger.LogEvent(time, name + " send sell order " + PositionLong.index + " req price : " + bid + " Profit: " + profit);
                                }
                                return;
                            }
                            if (openpos == OpenPosition.NONE)
                            {
                                Position pos = new Position();
                                pos.index     = ind++;
                                pos.value     = bid;
                                PositionShort = pos;
                                openpos       = OpenPosition.SHORT;
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Sell, pos.index);
                                if (LoggNeeded)
                                {
                                    logger.LogEvent(time, name + " send sell order " + pos.index + " req price : " + pos.value);
                                }
                                return;
                            }
                        }
                    }
                }
            }
        }