private List <TB_OPT10085> stockList = new List <TB_OPT10085>(); //계좌를 하나만 사용할거라고 가정한다. public void UpdateStockList(TB_REALTIME_CONTRACT real10002_data) { // FileLog.PrintF("MyStock UpdateStockList real10002_data.종목코드=>"+ real10002_data.종목코드); //FileLog.PrintF("MyStock UpdateStockList stockList.Count()=>" + stockList.Count()); lock (_lockStockList) { foreach (TB_OPT10085 stock in stockList) { // FileLog.PrintF("MyStock UpdateStockList stock.종목코드=>" + stock.종목코드); if (stock.stock_cd.Trim().Equals(real10002_data.stock_cd)) { // FileLog.PrintF("MyStock UpdateStockList change"); int 현재가 = Math.Abs(real10002_data.curr_amt); /*디비서 조회*/ int 보유수량 = stock.possession_qty; int 매입금액 = stock.tot_purchase_amt; int 평가금액 = 현재가 * 보유수량; int 매입수수료 = Commission.GetKiwoomCommissionBuy(매입금액); int 매도수수료 = Commission.GetKiwoomCommissionSell(평가금액); int 수수료 = 매입수수료 + 매도수수료; int 매도세금 = Commission.GetTaxSell(평가금액); int 손익분기매입가 = 0; if (보유수량 != 0) //이게 0일경우가 있다 매도를 한상태일경우 보유수량이 0으로 리스트에 계속 존재한다. { 손익분기매입가 = (매입수수료 + 매도수수료 + 매도세금 + 매입금액) / 보유수량; // 무조건오림 } int 평가손익 = 평가금액 - (매입금액 + 수수료 + 매도세금); // float 수익률 = (평가손익 / 매입금액) * 100; //int끼리 나눠서... 소수점을 버리는구나.. 이런.. float 수익률 = 0; if (매입금액 != 0) { 수익률 = ((float)평가손익 / (float)매입금액) * 100; } int 손익금액 = (평가금액 - 매입금액); float 손익율 = 0; if (매입금액 != 0) { 손익율 = ((float)손익금액 / (float)매입금액) * 100; } stock.curr_amt = 현재가; stock.evaluated_amt = 평가금액; stock.buying_commission = 매도수수료; stock.commission = 수수료; stock.selling_tax = 매도세금; stock.will_profit_amt = 손익분기매입가; stock.valuation_profit_loss = 평가손익; stock.earnings_rt = 수익률; stock.not_commission_profit_loss = 손익금액; stock.profit_loss_rt = 손익율; dbUpdate(stock); autoSale(stock); } } } }
public override void ReceivedData(AxKHOpenAPILib.AxKHOpenAPI axKHOpenAPI, AxKHOpenAPILib._DKHOpenAPIEvents_OnReceiveRealDataEvent e) { /* * * [20] = 체결시간 //(0) * [10] = 현재가 //(1) * [11] = 전일대비 //(2) * [12] = 등락율 //(3) * [27] = (최우선)매도호가 //(4) * [28] = (최우선)매수호가 //(5) * [15] = 거래량 //(6) * [13] = 누적거래량 //(7) * [14] = 누적거래대금 //(8) * [16] = 시가 //(9) * [17] = 고가 //(10) * [18] = 저가 //(11) * [25] = 전일대비기호 //(12) * [26] = 전일거래량대비(계약,주) //(13) * [29] = 거래대금증감 //(14) * [30] = 전일거래량대비(비율) //(15) * [31] = 거래회전율 //(16) * [32] = 거래비용 //(17) * [228] = 체결강도 //(18) * [311] = 시가총액(억) //(19) * [290] = 장구분 //(20) * [691] = KO접근도 //(21) * [567] = 상한가발생시간 //(22) * [568] = 하한가발생시간 //(23) * //[2020-04-02 10:18:30]종목코드 : 005930 | RealType : 주식체결 | * RealData : 101829 -45600 -200 -0.44 -45600 -45550 +240 7648579 351335 +46200 +46250 -45350 5 -19610953 -931147601650 -28.06 0.13 129 78.06 2722221 2 0 -27.39 000000 000000 10825 090018 090526 090159 4001191 3123474 -43.84 10171 23587 +10944 0 +240 +240 4560 3648 45935 252 * [2020-04-02 10:40:28]체결시간=> 104027 * [2020-04-02 10:40:28]현재가=> 45800 * [2020-04-02 10:40:28]전일대비=> 0 * [2020-04-02 10:40:28]등락율=> 0.00 * [2020-04-02 10:40:28](최우선)매도호가=> 45800 * [2020-04-02 10:40:28](최우선)매수호가=> -45750 * [2020-04-02 10:40:28]거래량=> +1 * [2020-04-02 10:40:28]누적거래량=> 8718082 * [2020-04-02 10:40:28]누적거래대금=> 400224 * [2020-04-02 10:40:28]시가=> +46200 * [2020-04-02 10:40:28]고가=> +46250 * [2020-04-02 10:40:28]저가=>-45350 * [2020-04-02 10:40:28]전일대비기호=> 3 * [2020-04-02 10:40:28]전일거래량대비_계약_주=> -18541450 * [2020-04-02 10:40:28]거래대금증감=> -882258926600 * [2020-04-02 10:40:28]전일거래량대비_비율=> -31.98 * [2020-04-02 10:40:28]거래회전율=> 0.15 * [2020-04-02 10:40:28]거래비용=> 129 * [2020-04-02 10:40:28]체결강도=> 82.85 * [2020-04-02 10:40:28]시가총액_억=> 2734160 * [2020-04-02 10:40:28]장구분=> 2 * [2020-04-02 10:40:28]KO접근도=> 0 * [2020-04-02 10:40:28]상한가발생시간=> 000000 * [2020-04-02 10:40:28]하한가발생시간=> 000000 * [2020-04-02 10:40:28]종목코드=> 005930 * [2020-04-02 10:40:28]RealName=> 주식체결 */ /* * FileLog.PrintF(String.Format("체결시간=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 20).Trim())); //[0] * FileLog.PrintF(String.Format("현재가=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 10).Trim())); //[1] * FileLog.PrintF(String.Format("전일대비=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 11).Trim())); //[2] * FileLog.PrintF(String.Format("등락율=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 12).Trim())); //[3] * FileLog.PrintF(String.Format("(최우선)매도호가=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 27).Trim())); //[4] * FileLog.PrintF(String.Format("(최우선)매수호가=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 28).Trim())); //[5] * FileLog.PrintF(String.Format("거래량=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 15).Trim())); //[6] * FileLog.PrintF(String.Format("누적거래량=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 13).Trim())); //[7] * FileLog.PrintF(String.Format("누적거래대금=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 14).Trim())); //[8] * FileLog.PrintF(String.Format("시가=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 16).Trim())); //[9] * FileLog.PrintF(String.Format("고가=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 17).Trim())); //[10] * FileLog.PrintF(String.Format("저가=>{0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 18).Trim())); //[11] * FileLog.PrintF(String.Format("전일대비기호=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 25).Trim())); //[12] * FileLog.PrintF(String.Format("전일거래량대비_계약_주=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 26).Trim())); //[13] * FileLog.PrintF(String.Format("거래대금증감=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 29).Trim())); //[14] * FileLog.PrintF(String.Format("전일거래량대비_비율=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 30).Trim())); //[15] * FileLog.PrintF(String.Format("거래회전율=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 31).Trim())); //[16] * FileLog.PrintF(String.Format("거래비용=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 32).Trim())); //[17] * FileLog.PrintF(String.Format("체결강도=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 228).Trim())); //[18] * FileLog.PrintF(String.Format("시가총액_억=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 311).Trim())); //[19] * FileLog.PrintF(String.Format("장구분=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 290).Trim())); //[20] * FileLog.PrintF(String.Format("KO접근도=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 691).Trim())); //[21] * FileLog.PrintF(String.Format("상한가발생시간=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 567).Trim())); //[22] * FileLog.PrintF(String.Format("하한가발생시간=> {0} ", axKHOpenAPI.GetCommRealData(e.sRealType, 568).Trim())); //[23] * FileLog.PrintF(String.Format("종목코드=> {0} ", e.sRealKey.ToString().Trim())); * FileLog.PrintF(String.Format("RealName=> {0} ", e.sRealType.ToString().Trim())); * FileLog.PrintF(String.Format("sRealData=> {0} ", e.sRealData.ToString().Trim())); */ try { String 현재일자 = DateTime.Now.ToString("yyyyMMdd"); String 체결시간TMP = axKHOpenAPI.GetCommRealData(e.sRealType, 20).Trim(); //[0] //체결시간이 6자리이므로 HHMMSS ==> HH:MM:SS로 바꿔야한다. TB_REALTIME_CONTRACT real10002_data = new TB_REALTIME_CONTRACT(); //String 현재시간 = DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff"); real10002_data.real_name = e.sRealType.ToString().Trim(); real10002_data.stock_cd = e.sRealKey.ToString().Trim(); real10002_data.stock_dt = 현재일자; real10002_data.contract_time = 체결시간TMP; real10002_data.curr_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 10).Trim()); //[1] real10002_data.contrast_yesterday = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 11).Trim()); //[2] real10002_data.fluctuation_rt = float.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 12).Trim()); //[3] real10002_data.offered_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 27).Trim()); //[4] real10002_data.bid_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 28).Trim()); //[5] real10002_data.trade_qty = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 15).Trim()); //[6] real10002_data.accumulated_trade_qty = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 13).Trim()); //[7] real10002_data.accumulated_trade_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 14).Trim()); //[8] real10002_data.start_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 16).Trim()); //[9] real10002_data.high_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 17).Trim()); //[10] real10002_data.low_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 18).Trim()); //[11] real10002_data.contrast_yesterday_symbol = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 25).Trim()); //[12] real10002_data.yesterday_contrast_trade_qty = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 26).Trim()); //[13] real10002_data.trade_amount_variation = float.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 29).Trim()); //[14] real10002_data.yesterday_contrast_trade_rt = float.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 30).Trim()); //[15] real10002_data.trade_turnover_ratio = float.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 31).Trim()); //[16] real10002_data.trade_cost = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 32).Trim()); //[17] real10002_data.contract_strength = float.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 228).Trim()); //[18] real10002_data.total_market_amt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 311).Trim()); //[19] real10002_data.market_gubun = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 290).Trim()); //[20] real10002_data.ko_accessibility_rt = Int32.Parse(axKHOpenAPI.GetCommRealData(e.sRealType, 691).Trim()); //[21] real10002_data.upper_amt_limit_time = axKHOpenAPI.GetCommRealData(e.sRealType, 567).Trim(); //[22] real10002_data.lower_amt_limit_time = axKHOpenAPI.GetCommRealData(e.sRealType, 568).Trim(); //[23] //MyStock.getClass1Instance().UpdateStockList(real10002_data); RealTimeData realTimeData = new RealTimeData(); realTimeData.insertRealtimeContract(real10002_data); } catch (Exception ex) { FileLog.PrintF("[REAL10002]Exception ex=" + ex.Message); //{ "42883: insert_tb_stock(p_market_cd => text, p_stock_cd => text, p_stock_nm => text, p_stock_dt => text, p_cnt => integer, p_last_price => text, p_stock_state => text, p_construction => text) 이름의 함수가 없음"} // Message "42601: 구문 오류, 입력 끝부분" string } }
/*주식체결*/ public void insertRealtimeContract(TB_REALTIME_CONTRACT tmp) { using (var conn = new NpgsqlConnection(Config.GetDbConnStr())) { try { conn.Open(); String sql = @"CALL insert_tb_realtime_contract( @p_stock_dt, @p_stock_cd, @p_contract_time, @p_curr_amt, @p_fluctuation_rt, @p_offered_amt, @p_bid_amt, @p_trade_qty, @p_accumulated_trade_qty, @p_accumulated_trade_amt, @p_start_amt, @p_high_amt, @p_low_amt, @p_contrast_yesterday_symbol, @p_contrast_yesterday, @p_yesterday_contrast_trade_qty, @p_trade_amount_variation, @p_yesterday_contrast_trade_rt, @p_trade_turnover_ratio, @p_trade_cost, @p_contract_strength, @p_total_market_amt, @p_market_gubun, @p_ko_accessibility_rt, @p_upper_amt_limit_time, @p_lower_amt_limit_time, @p_real_name )"; using (var cmd = new NpgsqlCommand(sql, conn)) { //cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@p_stock_dt", NpgsqlTypes.NpgsqlDbType.Text, tmp.stock_dt); cmd.Parameters.AddWithValue("@p_stock_cd", NpgsqlTypes.NpgsqlDbType.Text, tmp.stock_cd); cmd.Parameters.AddWithValue("@p_contract_time", NpgsqlTypes.NpgsqlDbType.Text, tmp.contract_time); cmd.Parameters.AddWithValue("@p_curr_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.curr_amt); cmd.Parameters.AddWithValue("@p_fluctuation_rt", NpgsqlTypes.NpgsqlDbType.Real, tmp.fluctuation_rt); cmd.Parameters.AddWithValue("@p_offered_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.offered_amt); cmd.Parameters.AddWithValue("@p_bid_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.bid_amt); cmd.Parameters.AddWithValue("@p_trade_qty", NpgsqlTypes.NpgsqlDbType.Integer, tmp.trade_qty); cmd.Parameters.AddWithValue("@p_accumulated_trade_qty", NpgsqlTypes.NpgsqlDbType.Integer, tmp.accumulated_trade_qty); cmd.Parameters.AddWithValue("@p_accumulated_trade_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.accumulated_trade_amt); cmd.Parameters.AddWithValue("@p_start_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.start_amt); cmd.Parameters.AddWithValue("@p_high_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.high_amt); cmd.Parameters.AddWithValue("@p_low_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.low_amt); cmd.Parameters.AddWithValue("@p_contrast_yesterday_symbol", NpgsqlTypes.NpgsqlDbType.Integer, tmp.contrast_yesterday_symbol); cmd.Parameters.AddWithValue("@p_contrast_yesterday", NpgsqlTypes.NpgsqlDbType.Integer, tmp.contrast_yesterday); cmd.Parameters.AddWithValue("@p_yesterday_contrast_trade_qty", NpgsqlTypes.NpgsqlDbType.Integer, tmp.yesterday_contrast_trade_qty); cmd.Parameters.AddWithValue("@p_trade_amount_variation", NpgsqlTypes.NpgsqlDbType.Real, tmp.trade_amount_variation); cmd.Parameters.AddWithValue("@p_yesterday_contrast_trade_rt", NpgsqlTypes.NpgsqlDbType.Real, tmp.yesterday_contrast_trade_rt); cmd.Parameters.AddWithValue("@p_trade_turnover_ratio", NpgsqlTypes.NpgsqlDbType.Real, tmp.trade_turnover_ratio); cmd.Parameters.AddWithValue("@p_trade_cost", NpgsqlTypes.NpgsqlDbType.Integer, tmp.trade_cost); cmd.Parameters.AddWithValue("@p_contract_strength", NpgsqlTypes.NpgsqlDbType.Real, tmp.contract_strength); cmd.Parameters.AddWithValue("@p_total_market_amt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.total_market_amt); cmd.Parameters.AddWithValue("@p_market_gubun", NpgsqlTypes.NpgsqlDbType.Integer, tmp.market_gubun); cmd.Parameters.AddWithValue("@p_ko_accessibility_rt", NpgsqlTypes.NpgsqlDbType.Integer, tmp.ko_accessibility_rt); cmd.Parameters.AddWithValue("@p_upper_amt_limit_time", NpgsqlTypes.NpgsqlDbType.Text, tmp.upper_amt_limit_time); cmd.Parameters.AddWithValue("@p_lower_amt_limit_time", NpgsqlTypes.NpgsqlDbType.Text, tmp.lower_amt_limit_time); cmd.Parameters.AddWithValue("@p_real_name", NpgsqlTypes.NpgsqlDbType.Text, tmp.real_name); cmd.ExecuteNonQuery(); } } catch (Exception ex) { FileLog.PrintF("[insertRealtimeContract]Exception ex=" + ex.Message); //{ "42883: insert_tb_stock(p_market_cd => text, p_stock_cd => text, p_stock_nm => text, p_stock_dt => text, p_cnt => integer, p_last_price => text, p_stock_state => text, p_construction => text) 이름의 함수가 없음"} // Message "42601: 구문 오류, 입력 끝부분" string } } }