static void Main(string[] args) { BrokerErrorCode errCode = BrokerErrorCode.Unknown; var upstoxBroker = new MyUpstoxWrapper(apiKey, apiSecret, redirectUrl); #if DEBUG Trace("DEBUG MODE"); List <UpstoxTradeParams> stocksConfig1 = ReadTradingConfigFile(); errCode = upstoxBroker.Login1(); #else Trace("RELEASE MODE"); errCode = upstoxBroker.Login(); #endif // Check for Holiday today if (IsHolidayToday()) { Trace("Exchange Holiday today.. exiting."); return; } // Wait 2 seconds if contended – in case another instance // of the program is in the process of shutting down. if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false)) { var message = "Another instance of the app is running. Only one instance for a userId is allowed! UserId: " + userId + ". Exiting.."; Trace(message); return; } Trace(userId); #if !DEBUG MarketUtils.WaitUntilMarketOpen(); #endif //Thread.Sleep(5000);// Let the rates etc update on server // Read the config file List <UpstoxTradeParams> stocksConfig = ReadTradingConfigFile(); var threads = new List <Thread>(stocksConfig.Count); var mktTrendConfig = new UpstoxMarketTrendParams(); var monthNow = DateTime.Now.Month + (DateTime.Now.Day > 20 ? 1 : 0); var yearNow = DateTime.Now.Year + (monthNow > 12 ? 1 : 0); if (monthNow > 12) { monthNow = 1; } var months = new[] { "DUMMY", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; var niftyFutCode = string.Format("NIFTY{0}{1}FUT", (yearNow - 2000), months[monthNow]); mktTrendConfig.stockCode = niftyFutCode; mktTrendConfig.exchangeStr = "NSE_FO"; mktTrendConfig.upstox = upstoxBroker; var mktTrend = new MarketTrend(mktTrendConfig); var mktThread = new Thread(mktTrend.StartCapturingMarketTrend); threads.Add(mktThread); foreach (var stockConfig in stocksConfig) { stockConfig.upstox = upstoxBroker; stockConfig.mktTrend = mktTrend; upstoxBroker.AddStock(stockConfig.stockCode); var t = new Thread(new UpstoxAverageTheBuyThenSell(stockConfig).StockBuySell); threads.Add(t); } threads.ForEach(t => { t.Start(); /*Thread.Sleep(200);*/ }); threads.ForEach(t => t.Join()); Trace("Update PnL files"); WritePnL(upstoxBroker, stocksConfig); // Send out the log in email and chat Trace("All stock threads completed. Emailing today's log file"); MessagingUtils.Init(); var log = GetLogContent(); MessagingUtils.SendAlertMessage("SimpleTrader log", log); Trace("Exiting.."); }
static void Main(string[] args) { BrokerErrorCode errCode = BrokerErrorCode.Unknown; // Check for Holiday today if (IsHolidayToday()) { Trace("Exchange Holiday today.. exiting."); return; } // Wait 2 seconds if contended – in case another instance // of the program is in the process of shutting down. if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false)) { var message = "Another instance of the app is running. Only one instance for a userId is allowed! UserId: " + userId + ". Exiting.."; Trace(message); //Console.WriteLine("Press any key to exit.."); //Console.ReadKey(); return; } Trace(userId); MarketUtils.WaitUntilMarketOpen(); // Broker var broker = new IciciDirectBroker(userId, "AMIT1978", "08071986"); //var broker = new IciciDirectBroker(userId, "amit1978", "07111982"); broker.LogIn(); //// Separate Login thread in background //BrokingAccountThread loginThread = new BrokingAccountThread(); //Thread thread = new Thread(new ParameterizedThreadStart(LoginUtils.Background_LoginCheckerThread)); //BrokingAccountObject iciciAccObj = new BrokingAccountObject(broker); //loginThread.brokerAccountObj = iciciAccObj; //loginThread.thread = thread; //loginThread.thread.Name = "Main Login Thread of MUNISGcS"; //loginThread.thread.IsBackground = true; //loginThread.thread.Start(iciciAccObj); // ************ TESTING Code starts ****************** // //Trace("Testing starts"); //string ordertestref = ""; //double funds; //errCode = broker.GetFundsAvailable(out funds); //errCode = broker.AllocateFunds(FundAllocationCategory.IpoMf, funds); //errCode = broker.AllocateFunds(FundAllocationCategory.IpoMf, -4.67); //var str = string.Format("[Holding EOD] {0} {1}", OrderPositionTypeEnum.Btst, ""); //errCode = CancelEquityOrder(string.Format("[Holding EOD] {0} {1}", HoldingTypeEnum.Btst, ""), ref ordertestref, OrderDirection.SELL); //var newTrades = new Dictionary<string, EquityTradeBookRecord>(); //List<EquityDematHoldingRecord> holdings; //List<EquityBTSTTradeBookRecord> btstHoldings; //errCode = broker.GetBTSTListings("CAPFIR", out btstHoldings); //List<EquityPendingPositionForDelivery> pendingPositions; //errCode = broker.GetOpenPositionsPendingForDelivery("BAJFI", out pendingPositions); //errCode = broker.GetOpenPositionsPendingForDelivery("MOTSUM", out pendingPositions); //Dictionary<string, EquityOrderBookRecord> orders; //errCode = broker.GetEquityOrderBookToday(false, false, "MOTSUM", out orders); //errCode = broker.PlaceEquityMarginSquareOffOrder("BAJFI", 34, 34, "1733", OrderPriceType.LIMIT, OrderDirection.SELL, EquityOrderType.DELIVERY, "2017232", Exchange.NSE, out ordertestref); //errCode = broker.PlaceEquityMarginDeliveryFBSOrder("BAJFI", 5, "1733", OrderPriceType.LIMIT, OrderDirection.SELL, EquityOrderType.DELIVERY, Exchange.NSE, out ordertestref); //errCode = broker.GetDematAllocation("BAJFI", out holdings); //errCode = broker.GetDematAllocation("CAPFIR", out holdings); //errCode = broker.GetDematAllocation("CAPPOI", out holdings); //errCode = broker.ConvertToDeliveryFromMarginOpenPositions("BAJFI", 3, 1, "2017223", OrderDirection.BUY, Exchange.NSE); //errCode = broker.GetEquityTradeBookToday(true, "BAJFI", out newTrades); //errCode = broker.ConvertToDeliveryFromPendingForDelivery("CAPFIR", 4, 1, "2017217", Exchange.NSE); //errCode = broker.CancelEquityOrder("20171120N900017136", EquityOrderType.DELIVERY); //while (errCode != BrokerErrorCode.Success) //{ // errCode = broker.CancelAllOutstandingEquityOrders("BAJFI", out Dictionary<string, BrokerErrorCode> cancelledOrders); // errCode = broker.PlaceEquityDeliveryBTSTOrder("BAJFI", 1, "1831", OrderPriceType.LIMIT, OrderDirection.SELL, EquityOrderType.DELIVERY, Exchange.NSE, "2017221", out ordertestref); //} ////errCode = broker.PlaceEquityMarginDeliveryFBSOrder("BAJFI", 1, "1831", OrderPriceType.LIMIT, OrderDirection.SELL, EquityOrderType.DELIVERY, Exchange.NSE, out ordertestref); //Trace("Testing ends"); // ************ TESTING Code ends ****************** // // Ensure all funds are allocated double funds; errCode = BrokerErrorCode.Unknown; errCode = broker.GetFundsAvailable(out funds); funds -= 1000; funds = funds < 0 ? 0 : funds; if (funds > 0) { errCode = broker.AllocateFunds(FundAllocationCategory.Equity, funds); Trace(string.Format("Ensure free funds [{0}] are allocated to Equity: {1}", funds, errCode)); } // Read the config file List <TradeParams> stocksConfig = ReadTradingConfigFile(); var threads = new List <Thread>(stocksConfig.Count); foreach (var stockConfig in stocksConfig) { stockConfig.broker = broker; var t = new Thread(new AverageTheBuyThenSell(stockConfig).StockBuySell); threads.Add(t); } threads.ForEach(t => { t.Start(); Thread.Sleep(1000); }); threads.ForEach(t => t.Join()); // Send out the log in email and chat Trace("All stock threads completed. Emailing today's log file"); MessagingUtils.Init(); var log = GetLogContent(); MessagingUtils.SendAlertMessage("SimpleTrader log", log); Trace("Exiting.."); }