Example #1
0
 void EventHelper_2_type2(object[] param, ref List <ProbaStruct> MinValueClose, ref List <ProbaStruct> MaxValueClose)
 {
     if (MinValueClose.Count == divergenceClose + 1)
     {
         MinValueClose.RemoveAt(0);
     }
     if (MaxValueClose.Count == divergenceClose + 1)
     {
         MaxValueClose.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
         };
         if (MinValueClose.Count != 0)
         {
             if (openpos == OpenPosition.LONG)
             {
                 FunTradeWorker.DivergenceCheck(false, MinValueClose, Temp_value, ref divergence_min_close, ref closelong, logger, name, time, LoggNeeded, false);
             }
         }
         MinValueClose.Add(Temp_value);
         if (divergenceClose == 0)
         {
             if (OpenPosition.LONG == openpos)
             {
                 CloseLong = Temp_value;
                 closelong = true;
             }
         }
         else
         if (divergence_min_close == divergenceClose)
         {
             closelong = true;
             CloseLong = MinValueClose.Last();
             string message = String.Format("{0} Решение закрыть позиции Лонг. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_min_close = 0;
         }
     }
     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
         };
         if (MaxValueClose.Count != 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 FunTradeWorker.DivergenceCheck(true, MaxValueClose, Temp_value, ref divergence_max_close, ref closeshort, logger, name, time, LoggNeeded, false);
             }
         }
         MaxValueClose.Add(Temp_value);
         if (divergenceClose == 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 CloseShort = Temp_value;
                 closelong  = true;
             }
         }
         else
         if (divergence_max_close == divergenceClose)
         {
             closeshort = true;
             CloseShort = MaxValueClose.Last();
             string message = String.Format("{0} Решение закрыть позиции Шорт. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_max_close = 0;
         }
     }
 }
Example #2
0
 void EventHelper(object[] param, List <ProbaStruct> MinValueOpen, List <ProbaStruct> MinValueClose, List <ProbaStruct> MaxValueOpen, List <ProbaStruct> MaxValueClose)
 {
     if (MinValueOpen.Count == divergenceOpen + 1)
     {
         MinValueOpen.RemoveAt(0);
     }
     if (MinValueClose.Count == divergenceClose + 1)
     {
         MinValueClose.RemoveAt(0);
     }
     if (MaxValueOpen.Count == divergenceOpen + 1)
     {
         MaxValueOpen.RemoveAt(0);
     }
     if (MaxValueClose.Count == divergenceClose + 1)
     {
         MaxValueClose.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
         };
         if (MinValueOpen.Count != 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 if (can_expand)
                 {
                     if (Temp_value.GapValue > MinValueOpen.Last().GapValue&& Temp_value.price > MinValueOpen.Last().price)
                     {
                         expand_position_short = true;
                     }
                     else
                     {
                         expand_position_short = false;
                     }
                 }
             }
             FunTradeWorker.CorvergenceCheck(MinValueOpen, MaxValueOpen, Temp_value, gapValue, price, time,
                                             ref ConvergenMin, ref convergen_min, Model.Project.Outputs[gappos].Value,
                                             ratio, ref PositionShort, locker, tradeTerminal, ref openpos, logger, name, LoggNeeded);
             if (openpos == OpenPosition.NONE)
             {
                 FunTradeWorker.DivergenceCheck(false, MinValueOpen, Temp_value, ref divergence_min_open, ref openshort, logger, name, time, LoggNeeded, true);
             }
         }
         if (MinValueClose.Count != 0)
         {
             if (openpos == OpenPosition.LONG)
             {
                 FunTradeWorker.DivergenceCheck(false, MinValueClose, Temp_value, ref divergence_min_close, ref closelong, logger, name, time, LoggNeeded, false);
             }
         }
         MinValueOpen.Add(Temp_value);
         MinValueClose.Add(Temp_value);
         if (divergenceOpen == 0)
         {
             if (openpos == OpenPosition.NONE)
             {
                 openshort = true;
                 OpenShort = Temp_value;
             }
             if (openpos == OpenPosition.SHORT)
             {
                 expand_position_short = true;
             }
         }
         else
         if (divergence_min_open == divergenceOpen)
         {
             openshort  = true;
             OpenShort  = MinValueOpen.Last();
             CrashShort = MinValueOpen[0];
             string message = String.Format("{0} Решение открытия позиции Шорт. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_min_open = 0;
         }
         if (divergenceClose == 0)
         {
             if (openpos == OpenPosition.LONG)
             {
                 CloseLong = Temp_value;
                 closelong = true;
             }
         }
         else
         if (divergence_min_close == divergenceClose)
         {
             closelong = true;
             CloseLong = MinValueClose.Last();
             string message = String.Format("{0} Решение закрыть позиции Лонг. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_min_close = 0;
         }
     }
     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
         };
         if (MaxValueOpen.Count != 0)
         {
             if (openpos == OpenPosition.LONG)
             {
                 if (can_expand)
                 {
                     if (Temp_value.GapValue < MaxValueOpen.Last().GapValue&& Temp_value.price < MaxValueOpen.Last().price)
                     {
                         expand_position_long = true;
                     }
                     else
                     {
                         expand_position_long = false;
                     }
                 }
             }
             FunTradeWorker.CorvergenceCheck(MaxValueOpen, MinValueOpen, Temp_value, gapValue, price, time, ref ConvergenMax, ref convergen_max,
                                             Model.Project.Outputs[gappos].Value, ratio, ref PositionLong, locker, tradeTerminal, ref openpos, logger, name, LoggNeeded);
             if (openpos == OpenPosition.NONE)
             {
                 FunTradeWorker.DivergenceCheck(true, MaxValueOpen, Temp_value, ref divergence_max_open, ref openlong, logger, name, time, LoggNeeded, true);
             }
         }
         if (MaxValueClose.Count != 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 FunTradeWorker.DivergenceCheck(true, MaxValueClose, Temp_value, ref divergence_max_close, ref closeshort, logger, name, time, LoggNeeded, false);
             }
         }
         MaxValueOpen.Add(Temp_value);
         MaxValueClose.Add(Temp_value);
         if (divergenceOpen == 0)
         {
             if (openpos == OpenPosition.NONE)
             {
                 openlong = true;
                 OpenLong = Temp_value;
             }
             if (openpos == OpenPosition.LONG)
             {
                 expand_position_short = true;
             }
         }
         else
         if (divergence_max_open == divergenceOpen)
         {
             openlong  = true;
             OpenLong  = MaxValueOpen.Last();
             CrashLong = MaxValueOpen[0];
             string message = String.Format("{0} Решение открытия позиции Лонг. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_max_open = 0;
         }
         if (divergenceClose == 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 CloseShort = Temp_value;
                 closelong  = true;
             }
         }
         else
         if (divergence_max_close == divergenceClose)
         {
             closeshort = true;
             CloseShort = MaxValueClose.Last();
             string message = String.Format("{0} Решение закрыть позиции Шорт. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_max_close = 0;
         }
     }
 }
Example #3
0
 public void EventHandler_1_type2(object[] param)
 {
     if (MinValueOpen_1.Count == divergenceOpen + 1)
     {
         MinValueOpen_1.RemoveAt(0);
     }
     if (MaxValueOpen_1.Count == divergenceOpen + 1)
     {
         MaxValueOpen_1.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
         };
         if (MinValueOpen_1.Count != 0)
         {
             if (openpos == OpenPosition.SHORT)
             {
                 if (can_expand)
                 {
                     if (Temp_value.GapValue > MinValueOpen_1.Last().GapValue&& Temp_value.price > MinValueOpen_1.Last().price)
                     {
                         expand_position_short = true;
                     }
                     else
                     {
                         expand_position_short = false;
                     }
                 }
             }
             if (openpos == OpenPosition.NONE)
             {
                 FunTradeWorker.DivergenceCheck(false, MinValueOpen_1, Temp_value, ref divergence_min_open, ref openshort, logger, name, time, LoggNeeded, true);
             }
         }
         MinValueOpen_1.Add(Temp_value);
         MinValueClose_1.Add(Temp_value);
         if (divergenceOpen == 0)
         {
             if (openpos == OpenPosition.NONE)
             {
                 openshort = true;
                 OpenShort = Temp_value;
             }
             if (openpos == OpenPosition.SHORT)
             {
                 expand_position_short = true;
             }
         }
         else
         if (divergence_min_open == divergenceOpen)
         {
             openshort  = true;
             OpenShort  = MinValueOpen_1.Last();
             CrashShort = MinValueOpen_1[0];
             string message = String.Format("{0} Решение открытия позиции Шорт. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_min_open = 0;
         }
     }
     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
         };
         if (MaxValueOpen_1.Count != 0)
         {
             if (openpos == OpenPosition.LONG)
             {
                 if (can_expand)
                 {
                     if (Temp_value.GapValue < MaxValueOpen_1.Last().GapValue&& Temp_value.price < MaxValueOpen_1.Last().price)
                     {
                         expand_position_long = true;
                     }
                     else
                     {
                         expand_position_long = false;
                     }
                 }
             }
             if (openpos == OpenPosition.NONE)
             {
                 FunTradeWorker.DivergenceCheck(true, MaxValueOpen_1, Temp_value, ref divergence_max_open, ref openlong, logger, name, time, LoggNeeded, true);
             }
         }
         MaxValueOpen_1.Add(Temp_value);
         if (divergenceOpen == 0)
         {
             if (openpos == OpenPosition.NONE)
             {
                 OpenLong = Temp_value;
                 openlong = true;
             }
             if (openpos == OpenPosition.LONG)
             {
                 expand_position_short = true;
             }
         }
         else
         if (divergence_max_open == divergenceOpen)
         {
             openlong  = true;
             OpenLong  = MaxValueOpen_1.Last();
             CrashLong = MaxValueOpen_1[0];
             string message = String.Format("{0} Решение открытия позиции Лонг. Ждем локальную дивергенцию... ", name);
             //if (LoggNeeded)
             //    logger.LogEvent(Temp_value.time, message);
             divergence_max_open = 0;
         }
     }
 }
Example #4
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.Last().value + profit_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to TakeProfit condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionLong, tradeTerminal, true, bid, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
                if (lose_to_close > 0)
                {
                    if (bid <= PositionLong.Last().value - lose_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to StopLose condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionLong, tradeTerminal, true, bid, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
            }
            if (openpos == OpenPosition.SHORT)
            {
                if (profit_to_close > 0)
                {
                    if (PositionShort.Last().value - profit_to_close >= ask)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to TakeProfit condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionShort, tradeTerminal, false, ask, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
                if (lose_to_close > 0)
                {
                    if (ask >= PositionShort.Last().value + lose_to_close)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to StopLose condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionShort, tradeTerminal, false, ask, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
            }
            //if(convergen_min)
            if (lastvalue == LastZigZagValue.MIN)
            {
                if (openpos == OpenPosition.LONG)
                {
                    if (gap > CrashLong.GapValue)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to crash divergence condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionLong, tradeTerminal, true, bid, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
                if (convergen_max == convergen_counter)
                {
                    if (gap > ConvergenMax.GapValue && ask > ConvergenMax.price)
                    {
                        //if (LoggNeeded)
                        //    logger.LogEvent(time, name + " Вторая конвергенция Long");
                        convergen_max = 0;
                        //FunTradeWorker.ClosePosition(true, PositionLong, tradeTerminal, true, bid, ref all_profit, locker, logger, time, name, LoggNeeded);
                        //openpos = OpenPosition.NONE;
                    }
                }
                if (openpos == OpenPosition.LONG)
                {
                    FunTradeWorker.AddPositionVolume_Old(true, ref last_pos, gap, ask, PositionLong, ref expand_position_long, locker, tradeTerminal, logger, time, name, LoggNeeded);
                    //FunTradeWorker.AddPositionVolume_New(true, MaxValueOpen, MinValueOpen, gap, ask, ratio_exp, PositionLong, ref expand_position_long, locker, tradeTerminal, logger, time, name, LoggNeeded);
                }
                if (openlong)
                {
                    if (openpos == OpenPosition.NONE)
                    {
                        if (gap < OpenLong.GapValue && ask < OpenLong.price)
                        {
                            Position pos = new Position();
                            pos.index      = ind++;
                            pos.value      = ask;
                            last_pos.gap   = gap;
                            last_pos.price = ask;
                            PositionLong.Add(pos);
                            openpos = OpenPosition.LONG;
                            lock (locker)
                            {
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Buy, pos.index);
                            }
                            if (LoggNeeded)
                            {
                                logger.LogEvent(time, name + " send buy order " + pos.index + " req price : " + pos.value);
                            }
                            openlong = false;
                            return;
                        }
                    }
                }
                if (closeshort)
                {
                    if (gap < CloseShort.GapValue && ask < CloseShort.price)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to divergence condition");
                        }
                        if (FunTradeWorker.ClosePosition(false, PositionShort, tradeTerminal, false, ask, ref all_profit, locker, logger, time, name, LoggNeeded))
                        {
                            openpos = OpenPosition.NONE;
                        }
                        closeshort = false;
                        return;
                    }
                }
            }
            if (lastvalue == LastZigZagValue.MAX)
            {
                if (openpos == OpenPosition.SHORT)
                {
                    if (gap < CrashShort.GapValue)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close short due to crash divergence condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionShort, tradeTerminal, false, ask, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos = OpenPosition.NONE;
                        return;
                    }
                }
                if (convergen_min == convergen_counter)
                {
                    if (gap < ConvergenMin.GapValue && bid < ConvergenMin.price)
                    {
                        //if (LoggNeeded)
                        //    logger.LogEvent(time, name + " Вторая конвергенция Short");
                        convergen_min = 0;
                        //if(FunTradeWorker.ClosePosition(true, PositionShort, tradeTerminal, false, ask, ref all_profit, locker, logger, time, name, LoggNeeded))
                        //    openpos = OpenPosition.NONE;
                    }
                }
                if (openpos == OpenPosition.SHORT)
                {
                    FunTradeWorker.AddPositionVolume_Old(false, ref last_pos, gap, bid, PositionShort, ref expand_position_short, locker, tradeTerminal, logger, time, name, LoggNeeded);
                    //FunTradeWorker.AddPositionVolume_New(false, MinValueOpen, MaxValueOpen, gap, bid, ratio_exp, PositionShort, ref expand_position_short, locker, tradeTerminal, logger, time, name, LoggNeeded);
                }
                if (closelong)
                {
                    if (gap > CloseLong.GapValue && bid > CloseLong.price)
                    {
                        if (LoggNeeded)
                        {
                            logger.LogEvent(time, "Close long due to divergence condition");
                        }
                        FunTradeWorker.ClosePosition(false, PositionLong, tradeTerminal, true, bid, ref all_profit, locker, logger, time, name, LoggNeeded);
                        openpos   = OpenPosition.NONE;
                        closelong = false;
                        return;
                    }
                }
                if (openshort)
                {
                    if (openpos == OpenPosition.NONE)
                    {
                        if (gap > OpenShort.GapValue && bid > OpenShort.price)
                        {
                            Position pos = new Position();
                            pos.index      = ind++;
                            pos.value      = bid;
                            last_pos.gap   = gap;
                            last_pos.price = bid;
                            PositionShort.Add(pos);
                            openpos = OpenPosition.SHORT;
                            lock (locker)
                            {
                                tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Sell, pos.index);
                            }
                            if (LoggNeeded)
                            {
                                logger.LogEvent(time, name + " send sell order " + pos.index + " req price : " + pos.value);
                            }
                            openshort = false;
                            return;
                        }
                    }
                }
            }
        }