// Try min profit squareoff first between 3 - 3.10 time. // From 3.10 to 3.15 time if squareoff of all positions is set to true and the ltp diff meets threshold for max loss pct, then do a market order squareoff public void TrySquareOffNearEOD(AlgoType algoType) { // if after 3 pm, then try to square off in at least no profit no loss if possible. cancel the outstanding buys anyway if (MarketUtils.IsTimeAfter310()) { var ordPriceType = OrderPriceType.LIMIT; var doUpdateOrders = false; // 3.20 - 3.25 pm time. market order type for forced square off given pct loss is within acceptable range // do it before 3.15, otherwise broker will try to squareoff on its own anytime between 3.15 - 3.30 if (MarketUtils.IsTimeAfter320() && !MarketUtils.IsTimeAfter325() && squareOffAllPositionsAtEOD && !isEODMinLossSquareOffMarketOrderUpdated) { double ltp; var errCode = GetLTP(out ltp); if (errCode != BrokerErrorCode.Success) { return; } ordPriceType = OrderPriceType.MARKET; var diff = (ltp - todayOutstandingPrice) / ltp; Trace(string.Format("[Margin EOD]: diff {0} ltp {1} outstandingprice {2} pctMaxLossSquareOffPositions {3} goodPrice {4} ", diff, ltp, todayOutstandingPrice, pctMaxLossSquareOffPositions, goodPrice)); if ((Math.Abs(diff) < pctMaxLossSquareOffPositions || diff > 0) && todayOutstandingPrice > goodPrice) { Trace(string.Format("[Margin EOD]: max loss {0}% is less than {1}% and avg outstanding price {2} is greater than good price of {3}. LTP is {4}. Place squareoff @ MARKET.", diff, pctMaxLossSquareOffPositions, todayOutstandingPrice, goodPrice, ltp)); doUpdateOrders = true; isEODMinLossSquareOffMarketOrderUpdated = true; } } // 3.10 - 3.20 pm time. try simple limit order with min profit price. watch until 3.10 pm else if (!isEODMinProfitSquareOffLimitOrderUpdated) { Trace(string.Format("[Margin EOD]: MinProfit Squareoff and cancel outstanding buy orders")); ordPriceType = OrderPriceType.LIMIT; isEODMinProfitSquareOffLimitOrderUpdated = true; doUpdateOrders = true; } if (doUpdateOrders) { if (algoType == AlgoType.AverageTheBuyThenSell) { // just cancel the outstanding buy order if (!string.IsNullOrEmpty(todayOutstandingBuyOrderRef)) { // cancel existing buy order errCode = CancelEquityOrder("[Margin EOD]", ref todayOutstandingBuyOrderRef, EquityOrderType.MARGIN, OrderDirection.BUY); } // bought qty needs square off. there is outstanding sell order, revise the price to try square off if (!string.IsNullOrEmpty(todayOutstandingSellOrderRef)) { // cancel existing sell order errCode = CancelEquityOrder("[Margin EOD]", ref todayOutstandingSellOrderRef, EquityOrderType.MARGIN, OrderDirection.SELL); if (errCode == BrokerErrorCode.Success) { // place new sell order, update sell order ref var sellPrice = GetSellPrice(todayOutstandingPrice, false, true); errCode = PlaceEquityOrder(stockCode, OrderPositionTypeEnum.Margin, todayOutstandingQty, todayOutstandingQty, sellPrice.ToString(), ordPriceType, OrderDirection.SELL, EquityOrderType.MARGIN, exchange, "", out todayOutstandingSellOrderRef); } } } } } }
// Try min profit squareoff first between 3 - 3.10 time. // From 3.10 to 3.15 time if squareoff of all positions is set to true and the ltp diff meets threshold for max loss pct, then do a market order squareoff public void TrySquareOffNearEOD(AlgoType algoType) { // if after 3 pm, then try to square off in at least no profit no loss if possible. either buy or sell is outstanding if (MarketUtils.IsTimeAfter3()) { var ordPriceType = OrderPriceType.LIMIT; var doUpdateOrders = false; // 3.10 - 3.15 pm time. market order type for forced square off given pct loss is within acceptable range // do it before 3.15, otherwise broker will try to squareoff on its own anytime between 3.15 - 3.30 if (MarketUtils.IsTimeAfter310() && !MarketUtils.IsTimeAfter315() && squareOffAllPositionsAtEOD && !isEODMinLossSquareOffMarketOrderUpdated) { var ltp = GetLTP(); ordPriceType = OrderPriceType.MARKET; var diff = Math.Abs((ltp - todayOutstandingPrice) / ltp); if (diff < pctMaxLossSquareOffPositions && todayOutstandingPrice > goodPrice) { Trace(string.Format("TrySquareOffNearEOD: max loss % {0} is within acceptable range of {1} and avg outstanding price {2} is greater than good price of {3}. Update squareoff at MARKET price type.", diff, pctMaxLossSquareOffPositions, todayOutstandingPrice, goodPrice)); doUpdateOrders = true; isEODMinLossSquareOffMarketOrderUpdated = true; } } // 3 - 3.10 pm time. try simple limit order with min profit price else if (!isEODMinProfitSquareOffLimitOrderUpdated) { Trace(string.Format("TrySquareOffNearEOD: Update squareoff LIMIT order with min profit % price or cancel outstanding orders")); ordPriceType = OrderPriceType.LIMIT; isEODMinProfitSquareOffLimitOrderUpdated = true; doUpdateOrders = true; } if (doUpdateOrders) { if (algoType == AlgoType.AverageTheBuyThenSell) { // just cancel the outstanding buy order if (!string.IsNullOrEmpty(todayOutstandingBuyOrderRef)) { // cancel existing buy order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingBuyOrderRef, OrderDirection.BUY); } // bought qty needs square off. there is outstanding sell order, revise the price to try square off if (!string.IsNullOrEmpty(todayOutstandingSellOrderRef)) { // cancel existing sell order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingSellOrderRef, OrderDirection.SELL); if (errCode == BrokerErrorCode.Success) { todayOutstandingSellOrderRef = ""; // place new sell order, update sell order ref Trace("Placing EOD squareoff updated order"); var sellPrice = GetSellPrice(todayOutstandingPrice, false, true); errCode = PlaceEquityOrder(stockCode, todayOutstandingQty, sellPrice.ToString(), ordPriceType, OrderDirection.SELL, EquityOrderType.MARGIN, exchange, out todayOutstandingSellOrderRef); } } } else if (algoType == AlgoType.SimultaneousBuySell) { // if there is an outstanding order pair, just cancel both if (!string.IsNullOrEmpty(todayOutstandingBuyOrderRef) && !string.IsNullOrEmpty(todayOutstandingSellOrderRef)) { // cancel existing sell order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingSellOrderRef, OrderDirection.SELL); // cancel existing buy order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingBuyOrderRef, OrderDirection.BUY); } // bought qty needs square off. there is outstanding sell order, revise the price to try square off else if (string.IsNullOrEmpty(todayOutstandingBuyOrderRef) && !string.IsNullOrEmpty(todayOutstandingSellOrderRef)) { // cancel existing sell order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingSellOrderRef, OrderDirection.SELL); if (errCode == BrokerErrorCode.Success) { todayOutstandingSellOrderRef = ""; // place new sell order, update sell order ref Trace("Placing EOD squareoff updated order"); var sellPrice = GetSellPrice(todayOutstandingPrice, false, true); errCode = PlaceEquityOrder(stockCode, ordQty, sellPrice.ToString(), ordPriceType, OrderDirection.SELL, EquityOrderType.MARGIN, exchange, out todayOutstandingSellOrderRef); } } // sold qty needs square off. there is outstanding buy order, revise the price to try square off else if (string.IsNullOrEmpty(todayOutstandingSellOrderRef) && !string.IsNullOrEmpty(todayOutstandingBuyOrderRef)) { // cancel existing buy order errCode = CancelEquityOrder("(EOD squareoff) @ " + ordPriceType, ref todayOutstandingBuyOrderRef, OrderDirection.BUY); if (errCode == BrokerErrorCode.Success) { todayOutstandingBuyOrderRef = ""; // place new buy order, update buy order ref Trace("Placing EOD squareoff updated order"); var buyPrice = GetBuySquareOffPrice(todayOutstandingPrice); errCode = PlaceEquityOrder(stockCode, ordQty, buyPrice.ToString(), ordPriceType, OrderDirection.BUY, EquityOrderType.MARGIN, exchange, out todayOutstandingBuyOrderRef); } } } } } }