public static void AddPositionVolume_New(bool LongShort, List <ProbaStruct> MainZigZagCollection, List <ProbaStruct> OfZigZagCollection, double gap, double price, double ratio_exp, List <Position> Position, ref bool expand_position, object locker, ModelsAT.BrokerMT tradeTerminal, ILogger logger, DateTime time, string name, bool LoggNeeded) { if (expand_position) { double last_knee = Math.Abs(MainZigZagCollection.Last().GapValue - OfZigZagCollection.Last().GapValue); double temp_knee = Math.Abs(OfZigZagCollection.Last().GapValue - gap); double r_exp = temp_knee / last_knee; if (r_exp >= ratio_exp) { if (LoggNeeded) { logger.LogEvent(time, name + " Размер ratio_O " + r_exp); } int ind = Position.Last().index; lock (locker) { if (!LongShort) { tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Sell, ind); } else { tradeTerminal.OpenOrder(ArbitrageTradeWF.OrderType.MKT_Buy, ind); } } expand_position = false; if (LoggNeeded) { logger.LogEvent(time, name + " Увеличиваем объем позиции " + ind + " req price : " + price); } } } }
static void AddPositionVolumeAssist(ref LastPosition last_pos, double gap, double price, List <Position> Position, object locker, ModelsAT.BrokerMT tradeTerminal, ArbitrageTradeWF.OrderType order_type, ILogger logger, DateTime time, string name, bool LoggNeeded) { last_pos.gap = gap; last_pos.price = price; int ind = Position.Last().index; lock (locker) { tradeTerminal.OpenOrder(order_type, ind); } Position.Add(new Processors.Position() { index = ind, value = price }); if (LoggNeeded) { logger.LogEvent(time, name + " Увеличиваем объем позиции " + ind + " req price : " + price); } }
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; } } } } } }
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; } } } } }