private static void writeBoardData() { if (MarketDataLog.getNumBoardData() > 100) { DBManager.insertBoardData(MarketDataLog.getAllBoardData()); } }
private async static void marketDataThread() { A: disconnect_flg = false; //var api = new API(); //var api2 = new API(); var api3 = new API(); //api.Subscribe<Tick>(api.TickerFxBtcJpy, OnReceive, OnConnect, OnError); //api2.Subscribe<BoardDiff>(api.boardFxBtcJpy, OnReceiveBoard, OnConnectBoard, OnError); api3.Subscribe <List <Executions> >(api3.ExecutionsFxBtcJpy, OnReceiveExecutions, OnConnectExecutions, OnErrorExecutions); while (SystemFlg.getMarketDataFlg()) { await Task.Delay(300); //Thread.Sleep(10); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.setLabel("num executions log=" + MarketDataLog.getNumExecutionsLog().ToString()); Form1.Form1Instance.setLabel2("num board data log=" + MarketDataLog.getNumBoardData().ToString()); })); if (disconnect_flg) { goto A;//make new instance when unexpectedlly disconnected } } api3.unsubscribe(); }
private static void writeExecutionsData() { if (MarketDataLog.getNumExecutionsLog() > 10000) { DBManager.insertExecutions(MarketDataLog.getExecutionsData()); } }
public static async void startBoardUpdate() { initialize(); await Task.Run(async() => { while (SystemFlg.getMarketDataFlg()) { if (FlyerAPI2.getApiAccessProhibition() == false) { var board = await FlyerAPI2.getBoardAsync("FX_BTC_JPY"); if (board.MidPrice != 0) { board.dt = DateTime.Now; var ask_p = board.Asks.Select(c => c.Price).ToArray(); var bid_p = board.Bids.Select(c => c.Price).ToArray(); var ask_min = ask_p.Min(); var bid_max = bid_p.Max(); board.spread = ask_min - bid_max; setCurrentBoard(board); MarketDataLog.addBoardData(board.dt, new double[] { bid_max, ask_min, board.spread }); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.setLabel3(board.spread.ToString()); })); } } //await Task.Delay(0); } }); }
private static void OnReceiveExecutions(List <Executions> data) { MarketDataLog.addExecutionsData(data); foreach (var v in data) { Form1.Form1Instance.addListBox3(v.exec_date + ", id=" + v.id + ", side=" + v.side + ", price=" + v.price + ", size=" + v.size); } }
public static void startMarketData() { SystemFlg.setMarketDataFlg(true); MarketDataLog.initialize(); //BoardDataUpdate.getCurrentBoard(); BoardDataUpdate.startBoardUpdate(); marketDataThread(); DBWriter.startDBWriter(); //System.Diagnostics.Debug.WriteLine("kita2"); }
public async Task <string> checkExecutionAndUpdateOrders() { var res = ""; if (MarketDataLog.getNumExecutionsLog() < last_ind_marketdata) { last_ind_marketdata = 0; } var exe_data = MarketDataLog.getExecutionsDataRange(last_ind_marketdata); last_ind_marketdata += exe_data.Count; var ord = getAllOrders(); for (int i = 0; i < ord.Count; i++) { for (int j = 0; j < exe_data.Count; j++) { if (exe_data[j].buy_child_order_acceptance_id == ord[i].order_id || exe_data[j].sell_child_order_acceptance_id == ord[i].order_id) { addHolding(ord[i].order_price, exe_data[j].size, ord[i].order_side); takeLog(DateTime.Now + ": executed " + ord[i].order_side + " for " + exe_data[j].price + " x " + exe_data[j].size); Log.addLog("Account-checkExecutionAndUpdateOrders", ": executed " + ord[i].order_side + " for " + exe_data[j].price + " x " + exe_data[j].size); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("executed " + ord[i].order_side + " for " + exe_data[j].price + " x " + exe_data[j].size); })); if ((ord[i].order_lot - exe_data[j].size) <= 0) { takeLog(DateTime.Now + ": order " + ord[i].order_id + " was full filled and removed"); Log.addLog("Account-checkExecutionAndUpdateOrders", ": order " + ord[i].order_id + " was full filled and removed"); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("order " + ord[i].order_id + " was full filled and removed"); })); removeOrder(ord[i].order_id); num_trade++; } else { Log.addLog("Account-checkExecutionAndUpdateOrders", ": order " + ord[i].order_id + " was partially executed"); updateOrderSize(ord[i].order_lot - exe_data[j].size, ord[i].order_id); } } } } return(res); }
private static void OnReceiveBoard(BoardDiff data) { //if (BoardDataUpdate.getCurrentBoard().MidPrice > 0) { var current_board = BoardDataUpdate.getCurrentBoard(); current_board.dt = DateTime.Now; var ask_min = current_board.Asks.Select(x => x.Price).ToList().Min(); var bid_max = current_board.Bids.Select(x => x.Price).ToList().Max(); foreach (var v in data.Asks) { if (v.Price < ask_min) { ask_min = v.Price; } break; } foreach (var v in data.Bids) { if (v.Price > bid_max) { bid_max = v.Price; } break; } current_board.Asks[0].Price = ask_min; current_board.Bids[0].Price = bid_max; current_board.spread = ask_min - bid_max; MarketDataLog.addBoardData(current_board.dt, new double[] { bid_max, ask_min, current_board.spread }); BoardDataUpdate.setCurrentBoard(current_board); Form1.Form1Instance.setLabel3(current_board.spread.ToString()); } /* * foreach (var v in data.Asks) * line += v.Price + " x " + v.Size; * Form1.Form1Instance.addListBox(data.MidPrice+" : " + line); */ }
private static void OnReceive(Tick data) { MarketDataLog.addTickData(data); //Form1.Form1Instance.addListBox2(data.Timestamp + ", price=" + data.LatestPrice + ", vol=" + data.Volume + ", vol product=" + data.VolumeByProduct); }
private async void buttonSendOrder_Click(object sender, EventArgs e) { if (SystemFlg.getMarketDataFlg() == false) { MarketData.startMarketData(); await Task.Delay(3000); } var board = await FlyerAPI2.getBoardAsync("FX_BTC_JPY"); double price = board.Asks.Select(x => x.Price).ToList().Min() - 1; var res = await FlyerAPI2.sendChiledOrderAsync("SELL", price, 0.01, 1); if (res.order_id != "") { addListBox2("completed send order:" + res.order_id); } if (id == "") { id = res.order_id; } System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); bool flg = true; string dotti = ""; while (flg) { var exe = await FlyerAPI2.getExecutionsAcceptanceIDAsync(res.order_id); System.Diagnostics.Debug.WriteLine(exe); if (exe.Count > 0) { if (exe[0].child_order_accesptance_id == res.order_id) { flg = false; dotti = "getExecutionsAcceptanceIDAsync"; } } if (MarketDataLog.getExecutionStatus(res.order_id)) { flg = false; dotti = "getExecutionStatus"; } } sw.Stop(); id = res.order_id; Form1Instance.addListBox2(res.order_id + ":time=" + sw.ElapsedMilliseconds + ", dotti=" + dotti); SystemFlg.setMarketDataFlg(false); SystemFlg.setDBWriterFlg(false); /* * var board2 = await FlyerAPI2.getBoardAsync("FX_BTC_JPY"); * var res2 = await FlyerAPI2.sendChiledOrderAsync("SELL", board2.Asks.Select(x => x.Price).ToList().Max()-1, 0.01, 1); * * var exe2 = await FlyerAPI2.getExecutionsAcceptanceIDAsync(res2.order_id); * * var posi2 = await FlyerAPI2.getPositionsAsync(); */ }
private async Task <string> checkCancel() { await Task.Run(async() => { while (SystemFlg.getMMFlg()) { if (cancelling) { var order = await FlyerAPI2.sendChiledOrderAsync("BUY", MarketDataLog.getLastExecutionsData().price - 100000, 0.01, 1); //send dummy order Log.addLog("Account-CheckCancel", "sendChiledOrderAsync-dummy"); var ord_dt = DateTime.Now; if (order.order_id.Contains("JRF")) { bool flg = true; do { if (FlyerAPI2.getApiAccessProhibition() == false) { var orders = await FlyerAPI2.getChildOrderAsync("ACTIVE");//get current active orders Log.addLog("Account-CheckCancel", "getChildOrderAsync-ACTIVE"); var dt = DateTime.Now; var orders_id = orders.Select(x => x.child_order_acceptance_id).ToList(); if (orders_id.Contains(order.order_id)) //when the dummy order is in the active order list { //treat all cancelling status orders not exist in order list as properlly cancelled var ord = getAllOrders(); int num_cancelling = 0; for (int i = 0; i < ord.Count; i++) { if (ord[i].order_status == "CANCELLING" && dt > ord[i].order_dt) { num_cancelling++; //if (orders_id.Contains(ord[i].order_id))// { takeLog(DateTime.Now + ": cancelled " + ord[i].order_side + ", price=" + ord[i].order_price + " x " + ord[i].order_lot); Log.addLog("Account-CheckCancel", "cancelled " + ord[i].order_side + ", price=" + ord[i].order_price + " x " + ord[i].order_lot); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("cancelled " + ord[i].order_side + ", price=" + ord[i].order_price + " x " + ord[i].order_lot); })); removeOrder(ord[i].order_id); } } } if (num_cancelling == 0)//cancel dummy order and stop current checking { string res = "error"; do { res = await FlyerAPI2.cancelChildOrdersAsync(order.order_id); flg = false; cancelling = false; Log.addLog("Account-CheckCancel", "num cancelling = 0, res=" + res); } while (res == "error"); } else if ((DateTime.Now - ord_dt).Seconds > 60) { flg = false; } } } await Task.Delay(500); } while (flg); } } else { Log.addLog("Account-CheckCancel", "cancelling = false, num orders=" + getNumCurrentOrders()); await Task.Delay(1000); } } }); return(""); }