// Stock trading lot(qty) for orders //private int mStockTradingLot; //protected int StockTradingLot //{ // get { lock (lockObjectProperties) { return mStockTradingLot; } } // set { lock (lockObjectProperties) { mStockTradingLot = value; } } //} public BrokerErrorCode RunCoreAlgo() { #region SimpleBuySellPairSquareAlgo bool bCancelPairOrder = false; string stockCode = stockInfo.StockCode; int stockExchange = stockInfo.MainExchange == Exchange.NSE ? 0 : 1; BrokerErrorCode errorCode = BrokerErrorCode.Unknown; int tryCnt = 0; while (errorCode != BrokerErrorCode.Success && tryCnt <= 5) { // refresh stock state (doesnt have price info) errorCode = EquityStockState.EquityRefreshStockStateToday(mBroker, stockInfo); if (errorCode == BrokerErrorCode.NotLoggedIn) { mBroker.LogOut(); errorCode = mBroker.CheckAndLogInIfNeeded(true); tryCnt++; } } string traceString = "RefreshEquityStockStateToday: " + errorCode.ToString(); FileTracing.TraceOut(traceString); if (errorCode.Equals(BrokerErrorCode.Success)) { // If no buy or sell order pending, i.e. the square-off pair orders have executed // then place a pair of fresh buy-sell orders if (!(stockInfo.IsAnyOutstandingBuyOrder() || stockInfo.IsAnyOutstandingSellOrder() )) { // Place buy-sell order pair // Get latest quote EquitySymbolQuote[] stockPrice; errorCode = mBroker.GetEquityQuote(stockCode, out stockPrice); if (!errorCode.Equals(BrokerErrorCode.Success)) { return(errorCode); } // Generate orders List <EquityOrderRecord> generatedOrders = new List <EquityOrderRecord>(); string BidPrice; string OfferPrice; // price decision // First orders, only if market not open and it is first order pair if ((stockInfo.NumTrades == 0) && !MarketUtils.IsMarketOpen()) { double closePrice = stockPrice[stockExchange].PreviousClosePriceDouble; if (algoParams.initBidPrice > 0) { BidPrice = algoParams.initBidPrice.ToString(); } else { decimal buy = (decimal)(closePrice - (2.0 / 100 * closePrice));//prevclose - 3% buy = decimal.Subtract(buy, 0.05M); buy = decimal.Round(buy, 1); BidPrice = buy.ToString(); } if (algoParams.initOfferPrice > 0) { OfferPrice = algoParams.initOfferPrice.ToString(); } else { decimal sell = (decimal)(closePrice + (2.0 / 100 * closePrice));//prevclose + 3% sell = decimal.Add(sell, 0.05M); sell = decimal.Round(sell, 1, MidpointRounding.AwayFromZero); OfferPrice = sell.ToString(); } } // Subsequent orders // TODO: May put time conditions or market volatility conditions here in else if else { double ltp = stockPrice[stockExchange].LastTradePriceDouble; decimal buy = (decimal)(ltp - ((double)(0.85 / 100) * ltp));//prevclose - 0.85% buy = decimal.Subtract(buy, 0.05M); buy = decimal.Round(buy, 1); BidPrice = buy.ToString(); decimal sell = (decimal)(ltp + ((double)(0.85 / 100) * ltp));//prevclose + 0.85% sell = decimal.Add(sell, 0.05M); sell = decimal.Round(sell, 1, MidpointRounding.AwayFromZero); OfferPrice = sell.ToString(); } // Buy generatedOrders.Add(new EquityOrderRecord(stockCode, algoParams.StockTradingLot, BidPrice, OrderPriceType.LIMIT, OrderDirection.BUY, stockInfo.MainExchange, EquityOrderType.DELIVERY, "0", "0")); if (!bGenerateASingleBuyOrderAtStart) { // Sell generatedOrders.Add(new EquityOrderRecord(stockCode, algoParams.StockTradingLot, OfferPrice, OrderPriceType.LIMIT, OrderDirection.SELL, stockInfo.MainExchange, EquityOrderType.DELIVERY, "0", "0")); } List <BrokerErrorCode> errorCodes = null; // Validate orders List <EquityOrderRecord> ordersToPlace = stockInfo.ReturnFilteredValidOrders(generatedOrders); // Place validated orders errorCodes = mBroker.PlaceMultipleEquityOrders(ordersToPlace, 5); traceString = "Stock: " + stockInfo.StockCode + " :PlaceMultipleEquityOrders: 2\n"; int o = 0; foreach (BrokerErrorCode errCodeEach in errorCodes) { EquityOrderRecord order = ordersToPlace[o++]; traceString = traceString + order.OrderDirection.ToString() + "-" + order.Quantity.ToString() + " at " + order.Price + ": " + errCodeEach.ToString() + "\n"; // Place Order failed if (!errCodeEach.Equals(BrokerErrorCode.Success)) { // Cancel both the orders bCancelPairOrder = true; } } FileTracing.TraceOut(traceString); // Even if any one of the orders failed to place, cancel the pair if (bCancelPairOrder) { errorCode = BrokerUtils.CancelStockOutstandingOrdersAndTrace(mBroker, stockCode); } } } return(errorCode); #endregion }