public ActionResult BuyTrade(double TradeAmount, Int32 PayoutPercentage, string SymbolId, string TradeType, Int32 ExpiryTimeInterval, string xpoints, string ypoints, string TradeWay)
        {
            CTrade objCTrade = new CTrade();
            Int32 UserId = Convert.ToInt32(Session["user"]);
            Int32 GmtInfo = Convert.ToInt32(Session["GMTInfo"]);
            try
            {
                if (UserId > -1)
                {
                    objCTrade.TradeId = -1;
                    objCTrade.TradeAmount = TradeAmount;
                    objCTrade.PayoutPerCentage = PayoutPercentage;
                    objCTrade.SymbolId = SymbolId;
                    objCTrade.TradeType = TradeType;
                    objCTrade.UserId = UserId;
                    objCTrade.ExpiryTimeInterval = ExpiryTimeInterval;
                    objCTrade.xpoints = xpoints;
                    objCTrade.ypoints = ypoints;
                    objCTrade.TradeWay = TradeWay;
                    StockModels objStockModels = new StockModels();

                    objCTrade = objStockModels.BuyTrade(objCTrade, GmtInfo);

                    if (objCTrade.TradeId > 0)
                    {
                        if (Convert.ToInt32(Session["user"]) > 0)
                        {
                            Session["TradeBalance"] = Math.Round(Convert.ToDouble(objStockModels.GetUserAccountBalance(Convert.ToInt32(Session["user"].ToString()))), 2);
                        }
                        else
                        {
                            Session["TradeBalance"] = -1;
                        }
                    }
                    else
                    {
                        return Json(objCTrade);
                    }
                }
                else
                {
                    objCTrade.TradeId = -1;
                    return Json(objCTrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return PartialView("_SingleOpenTrade", objCTrade);
        }
        public CTrade BuyTrade(CTrade objCTrade, int GMTInfo)
        {
            using (SqlConnection cnn = new SqlConnection(CommonCollection.ConnectionString))
            {
                try
                {

                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand("BuyTrade", cnn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        cmd.Parameters.Add("@userId", SqlDbType.Int).Value = objCTrade.UserId;
                        cmd.Parameters.Add("@TradeAmount", SqlDbType.Float).Value = objCTrade.TradeAmount;
                        cmd.Parameters.Add("@SymbolID", SqlDbType.NVarChar).Value = objCTrade.SymbolId;
                        cmd.Parameters.Add("@TradeType", SqlDbType.NVarChar).Value = objCTrade.TradeType;
                        cmd.Parameters.Add("@PayOutPercentage", SqlDbType.Int).Value = objCTrade.PayoutPerCentage;
                        cmd.Parameters.Add("@ExpiryTimeInterval", SqlDbType.Int).Value = objCTrade.ExpiryTimeInterval;
                        cmd.Parameters.Add("@xpoints", SqlDbType.NVarChar).Value = objCTrade.xpoints;
                        cmd.Parameters.Add("@ypoints", SqlDbType.NVarChar).Value = objCTrade.ypoints;
                        cmd.Parameters.Add("@TradeWay", SqlDbType.NVarChar).Value = objCTrade.TradeWay;
                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        objCTrade.TradeId = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString());
                        if (objCTrade.TradeId > 0)
                        {
                            StockModels objStockModels = new StockModels();
                            objStockModels.GetTradebyTradeId(objCTrade, GMTInfo, 0);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString(), ex);
                }
                finally
                {
                    cnn.Close();
                    cnn.Dispose();
                    GC.Collect();
                    SqlConnection.ClearPool(cnn);
                }
                return objCTrade;
            }
        }
        public ActionResult GetTradeTabsWithAllTradeHistory()
        {
            VMIndex objVMIndex = new VMIndex();

            StockModels objStockModels = new StockModels();

            Int32 UserId = Convert.ToInt32(Session["user"]);

            Int32 GmtInfo = Convert.ToInt32(Session["GMTInfo"]);

            try
            {
                if (UserId > -1)
                {
                    objVMIndex.LstOpenTrade = objStockModels.GetOpenTrades(UserId, GmtInfo);
                    objVMIndex.LstTradeHistory = objStockModels.GetAllTradeHistory(UserId, GmtInfo);
                    Session["TradeBalance"] = objStockModels.GetUserAccountBalance(UserId);
                    return PartialView("_TradeTabs", objVMIndex);
                }
                else
                {
                    CTrade objCTrade = new CTrade();
                    objCTrade.TradeId = -1;
                    return Json(objCTrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ActionResult GetTradebyTradeId(Int32 TradeId, string ExpPrice)
        {
            CTrade objCTrade = new CTrade();

            StockModels objStockModels = new StockModels();

            Int32 UserId = Convert.ToInt32(Session["user"]);

            Int32 GmtInfo = Convert.ToInt32(Session["GMTInfo"]);

            try
            {
                if (UserId > -1)
                {

                    objCTrade.TradeId = TradeId;

                    objCTrade.UserId = UserId;

                    objStockModels.TradeExpire(TradeId, ExpPrice);

                    objCTrade = objStockModels.GetTradebyTradeId(objCTrade, GmtInfo, 1);

                    ViewBag.IsClosed = 1;

                    Session["TradeBalance"] = objStockModels.GetUserAccountBalance(UserId);

                    ViewBag.IsClosed = 1;

                    if (objCTrade.TradeId == 0)
                    {
                        return Json(objCTrade);
                    }
                    return PartialView("_SingleOpenTrade", objCTrade);
                }
                else
                {
                    objCTrade.TradeId = -1;
                    return Json(objCTrade);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public List<CTrade> GetTradeHistory(int UserId, int GMTInfo)
 {
     List<CTrade> lstCTrade = new List<CTrade>();
     using (SqlConnection cnn = new SqlConnection(CommonCollection.ConnectionString))
     {
         try
         {
             cnn.Open();
             using (SqlCommand cmm = new SqlCommand("select top(15) [TradeId] ,[UserId],tradehistory.[SymbolId],[SymbolName],[TradeAmount],[BuyTimestamp],[ExpireTimestamp],dbo.F_DATETIME_TO_UNIX_TIME(BuyTimestamp) as [BuyTimestampUTC],dbo.F_DATETIME_TO_UNIX_TIME(ExpireTimestamp) as [ExpireTimestampUTC],[PriceAtBuy],[PriceAtExpire] ,[TradeType],TradeHistory.[PayoutPerCentage],[Description] from TradeHistory,Symbol_info where TradeHistory.SymbolId = Symbol_info.SymbolId and ExpireTimestamp < GETUTCDATE() and Status = 1 and UserId = " + UserId.ToString() + " order by BuyTimestamp desc", cnn))
             {
                 using (SqlDataAdapter a = new SqlDataAdapter(cmm))
                 {
                     DataTable t = new DataTable();
                     a.Fill(t);
                     if (t.Rows.Count > 0)
                     {
                         foreach (DataRow dtRow in t.Rows)
                         {
                             CTrade oCTrade = new CTrade();
                             oCTrade.BuyTimestamp = CommonCollection.GetLocalTime(Convert.ToInt64(dtRow["BuyTimestampUTC"]), GMTInfo);
                             oCTrade.ExpireTimestamp = CommonCollection.GetLocalTime(Convert.ToInt64(dtRow["ExpireTimestampUTC"]), GMTInfo);
                             oCTrade.Description = dtRow["Description"].ToString();
                             oCTrade.PayoutPerCentage = Convert.ToInt32(dtRow["PayoutPerCentage"].ToString());
                             oCTrade.PriceAtBuy = Convert.ToDouble(dtRow["PriceAtBuy"].ToString());
                             oCTrade.PriceAtExpire = Convert.ToDouble(dtRow["PriceAtExpire"].ToString());
                             oCTrade.SymbolId = dtRow["SymbolName"].ToString();
                             oCTrade.TradeType = dtRow["TradeType"].ToString();
                             oCTrade.TradeAmount = Convert.ToDouble(dtRow["TradeAmount"]);
                             oCTrade.TradeId = Convert.ToInt32(dtRow["TradeId"]);
                             oCTrade.UTCBuyTimestamp = Convert.ToInt64(dtRow["BuyTimestampUTC"]);
                             oCTrade.UTCExpireTimestamp = Convert.ToInt64(dtRow["ExpireTimestampUTC"]);
                             lstCTrade.Add(oCTrade);
                         }
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             throw new Exception(ex.ToString(), ex);
         }
         finally
         {
             cnn.Close();
             cnn.Dispose();
             GC.Collect();
             SqlConnection.ClearPool(cnn);
         }
         return lstCTrade;
     }
 }
 public CTrade GetTradebyTradeId(CTrade objCTrade, int GMTInfo, int status)
 {
     String Query = "";
     using (SqlConnection cnn = new SqlConnection(CommonCollection.ConnectionString))
     {
         try
         {
             cnn.Open();
             if (objCTrade.TradeId > 0)
             {
                 if (status == 0)
                 {
                     Query = "select [TradeId] ,[UserId],[xpoints],[ypoints],[TradeWay],tradehistory.[SymbolId],[SymbolName],[TradeAmount],[BuyTimestamp],[ExpireTimestamp],dbo.F_DATETIME_TO_UNIX_TIME(BuyTimestamp) as [BuyTimestampUTC],dbo.F_DATETIME_TO_UNIX_TIME(ExpireTimestamp) as [ExpireTimestampUTC],[PriceAtBuy],[PriceAtExpire] ,[TradeType],TradeHistory.[PayoutPerCentage],[Description] from TradeHistory,Symbol_info where TradeHistory.SymbolId = Symbol_info.SymbolId  and UserId =" + objCTrade.UserId + " and TradeId = " + objCTrade.TradeId + " ";
                 }
                 else
                 {
                     Query = "select [TradeId] ,[UserId],[xpoints],[ypoints],[TradeWay],tradehistory.[SymbolId],[SymbolName],[TradeAmount],[BuyTimestamp],[ExpireTimestamp],dbo.F_DATETIME_TO_UNIX_TIME(BuyTimestamp) as [BuyTimestampUTC],dbo.F_DATETIME_TO_UNIX_TIME(ExpireTimestamp) as [ExpireTimestampUTC],[PriceAtBuy],[PriceAtExpire] ,[TradeType],TradeHistory.[PayoutPerCentage],[Description] from TradeHistory,Symbol_info where TradeHistory.SymbolId = Symbol_info.SymbolId and status= 1 and UserId =" + objCTrade.UserId + " and TradeId = " + objCTrade.TradeId + " ";
                 }
                 using (SqlCommand cmm = new SqlCommand(Query, cnn))
                 {
                     using (SqlDataAdapter a = new SqlDataAdapter(cmm))
                     {
                         DataTable t = new DataTable();
                         a.Fill(t);
                         if (t.Rows.Count > 0)
                         {
                             objCTrade.Description = t.Rows[0]["Description"].ToString();
                             objCTrade.PayoutPerCentage = Convert.ToInt32(t.Rows[0]["PayoutPerCentage"].ToString());
                             objCTrade.PriceAtBuy = Convert.ToDouble((t.Rows[0]["ypoints"]).ToString());
                             objCTrade.SymbolId = t.Rows[0]["SymbolName"].ToString();
                             objCTrade.TradeType = t.Rows[0]["TradeType"].ToString();
                             objCTrade.TradeAmount = Convert.ToDouble(t.Rows[0]["TradeAmount"]);
                             objCTrade.TradeId = Convert.ToInt32(t.Rows[0]["TradeId"]);
                             objCTrade.BuyTimestamp = CommonCollection.GetLocalTime(Convert.ToInt64(t.Rows[0]["BuyTimestampUTC"]), GMTInfo);
                             objCTrade.ExpireTimestamp = CommonCollection.GetLocalTime(Convert.ToInt64(t.Rows[0]["ExpireTimestampUTC"]), GMTInfo);
                             objCTrade.UTCBuyTimestamp = Convert.ToInt64(t.Rows[0]["BuyTimestampUTC"]);
                             objCTrade.UTCExpireTimestamp = Convert.ToInt64(t.Rows[0]["ExpireTimestampUTC"]);
                             objCTrade.xpoints = (t.Rows[0]["xpoints"]).ToString();
                             objCTrade.ypoints = (t.Rows[0]["ypoints"]).ToString();
                             objCTrade.TradeWay = (t.Rows[0]["TradeWay"]).ToString();
                             objCTrade.PriceAtExpire = Convert.ToDouble((t.Rows[0]["PriceAtExpire"]).ToString());
                         }
                         else
                         {
                             objCTrade.TradeId = 0;
                         }
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             throw new Exception(ex.ToString(), ex);
         }
         finally
         {
             cnn.Close();
             cnn.Dispose();
             GC.Collect();
             SqlConnection.ClearPool(cnn);
         }
         return objCTrade;
     }
 }