public static void startMarketData() { SystemFlg.setMarketDataFlg(true); MarketDataLog.initialize(); //BoardDataUpdate.getCurrentBoard(); BoardDataUpdate.startBoardUpdate(); marketDataThread(); DBWriter.startDBWriter(); //System.Diagnostics.Debug.WriteLine("kita2"); }
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 async Task <string> MMStrategy(Account ac) { string res = ""; var board = BoardDataUpdate.getCurrentBoard(); //if (board.Asks.Length > 0) { var bids = board.Bids.Select(x => x.Price).ToList(); var asks = board.Asks.Select(x => x.Price).ToList(); double bid_max = bids.Max(); double ask_min = asks.Min(); await ac.checkExecutionAndUpdateOrders(); var ord = ac.getAllOrders(); if (ac.holding_total_size == 0 && ord.Count == 0) //no positions, no orders { if (board.spread >= entry_spread) { //entry for both of bid and ask var sell_order = await ac.entry(ask_min - 1, order_size, "SELL"); if (sell_order.order_id != "") { Log.addLog("MMStrategy", "ordered sell @" + (ask_min - 1)); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("ordered sell @" + (ask_min - 1)); })); } var buy_order = await ac.entry(bid_max + 1, order_size, "BUY"); if (buy_order.order_id != "") { Log.addLog("MMStrategy", "ordered buy @" + (bid_max + 1)); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("ordered buy @" + (bid_max + 1)); })); } //check execution and start exit price tracing when executed bool flg = true; do { await ac.checkExecutionAndUpdateOrders(); if (ac.holding_ave_side != "") { await ac.startExitPriceTracingOrder(); } else if (BoardDataUpdate.getCurrentBoard().spread < entry_spread) { //Log.addLog("MMStrategy", "cancelAllOrders-hold=0, order=0"); await ac.cancelAllOrders(); flg = false; } } while (flg); } } else if (ac.holding_total_size == 0 && ord.Count > 0) //no positions but some orders { if (board.spread < entry_spread) { //Log.addLog("MMStrategy", "cancelAllOrders-hold=0, order>0, board.spread < entry_spread"); var res_cancel = await ac.cancelAllOrders(); if (res_cancel != "error") { //ac.takeLog("cancelling all orders"); //Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("cancelling all orders"); })); } } else { for (int i = 0; i < ord.Count; i++) { if (ord[i].order_side == "BUY") { if (ord[i].order_price <= bid_max) { double size = ord[i].order_lot; Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("cancelling buy order, id=" + ord[i].order_id); })); //Log.addLog("MMStrategy", "hold=0, order>0, board.spread > entry_spread, cancelling buy order, id=" + ord[i].order_id); var res_cancel = await ac.cancelOrder(ord[i].order_id); if (res_cancel != "error") { //Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("ordered buy @" + (bid_max + 1)); })); //await ac.entry(bid_max + 1, size, "BUY"); } } } else { if (ord[i].order_price >= ask_min) { double size = ord[i].order_lot; Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("cancelling sell order, id=" + ord[i].order_id); })); //Log.addLog("MMStrategy", "hold=0, order>0, board.spread > entry_spread, cancelling sell order, id=" + ord[i].order_id); var res_cancel = await ac.cancelOrder(ord[i].order_id); if (res_cancel != "error") { //Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("ordered sell @" + (ask_min - 1)); })); //await ac.entry(ask_min - 1, size, "SELL"); } } } } } } else if (ac.holding_total_size > 0)//holding positions, and orders { Log.addLog("MMStrategy", "ac.holding_total_size > 0"); var com = await ac.startExitPriceTracingOrder(); res = "completed exit price tracing order"; } } return(res); }
public async Task <string> startExitPriceTracingOrder() { string res = ""; takeLog(DateTime.Now + ": started exit price tracing order"); Log.addLog("Account-startExitPriceTracingOrder", "startExitPriceTracingOrder"); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("started price tracing order"); })); await Task.Run(async() => { do { await checkExecutionAndUpdateOrders(); if (holding_total_size > 0) //if holding position { var board = BoardDataUpdate.getCurrentBoard(); double bid_max = board.Bids.Select(x => x.Price).ToList().Max(); double ask_min = board.Asks.Select(x => x.Price).ToList().Min(); var ord = getAllOrders(); if (holding_ave_side == "BUY") //hodling long position { var orders = ord.Where(x => x.order_side == "SELL").ToList(); if (orders.Count > 0) //exit order is already exist { for (int i = 0; i < orders.Count; i++) { if (orders[i].order_status != "CANCELLING") { if (orders[i].order_price >= ask_min) { var res_cancel = await cancelOrder(orders[i].order_id); if (res_cancel != "error") { takeLog(DateTime.Now + ": PirceTracingOrder - cancelling sell order " + orders[i].order_price + " x " + orders[i].order_lot); Log.addLog("Account-startExitPriceTracingOrder", ": PirceTracingOrder - cancelling sell order " + orders[i].order_price + " x " + orders[i].order_lot); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("PirceTracingOrder - cancelling sell order : " + orders[i].order_price + " x " + orders[i].order_lot); })); } } } } } else //if no exit order { //var order = await FlyerAPI2.sendChiledOrderAsync("SELL", ask_min - 1, holding_total_size, 1); var order = await entry(ask_min - 1, holding_total_size, "SELL"); /*if (order.order_id != "") * { * addOrder(DateTime.Now, ask_min - 1, holding_total_size, order.order_id, "ACTIVE", "SELL"); * takeLog(DateTime.Now + ": PirceTracingOrder - entry sell order " + (ask_min - 1).ToString() + " x " + holding_total_size); * Log.addLog("Account-startExitPriceTracingOrder", ": PirceTracingOrder - entry sell order " + (ask_min - 1).ToString() + " x " + holding_total_size); * Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("PirceTracingOrder - ordered sell @" + (ask_min - 1)); })); * }*/ } } else if (holding_ave_side == "SELL") //holding short position { var orders = ord.Where(x => x.order_side == "BUY").ToList(); if (orders.Count > 0) //exit order is already exist { for (int i = 0; i < orders.Count; i++) { if (orders[i].order_status != "CANCELLING") { if (orders[i].order_price <= bid_max) { var res_cancel = await cancelOrder(orders[i].order_id); if (res_cancel != "error") { takeLog(DateTime.Now + ": PirceTracingOrder - cancelling buy order " + orders[i].order_price + " x " + orders[i].order_lot); Log.addLog("Account-startExitPriceTracingOrder", ": PirceTracingOrder - cancelling buy order " + orders[i].order_price + " x " + orders[i].order_lot); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("PirceTracingOrder - cancelling buy order : " + orders[i].order_price + " x " + orders[i].order_lot); })); } } } } } else //exit order it not yet exit { //var order = await FlyerAPI2.sendChiledOrderAsync("BUY", bid_max + 1, holding_total_size, 1); var order = await entry(bid_max + 1, holding_total_size, "BUY"); /*if (order.order_id != "") * { * addOrder(DateTime.Now, bid_max + 1, holding_total_size, order.order_id, "ACTIVE", "BUY"); * takeLog(DateTime.Now + ": PirceTracingOrder - entry buy order " + (bid_max + 1).ToString() + " x " + holding_total_size); * Log.addLog("Account-startExitPriceTracingOrder", ": PirceTracingOrder - entry buy order " + (bid_max + 1).ToString() + " x " + holding_total_size); * Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("PirceTracingOrder - ordered buy @" + (bid_max + 1)); })); * }*/ } } } } while (holding_total_size > 0 || getNumCurrentOrders() > 0); }); takeLog("Completed exit price tracing order"); Log.addLog("Account-startExitPriceTracingOrder", "Completed exit price tracing order"); Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("Completed exit price tracing order"); })); return(res); }