Esempio n. 1
0
        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..");
        }
Esempio n. 2
0
 public MarketTrend(UpstoxMarketTrendParams @params)
 {
     myUpstoxWrapper = @params.upstox;
     stockCode       = @params.stockCode;
     exchStr         = @params.exchangeStr;
 }