Example #1
0
        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();
        }
Example #2
0
        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);
                }
            });
        }
Example #3
0
 public static void startDBWriter()
 {
     SystemFlg.setDBWriterFlg(true);
     initialize();
     dbWriterThread();
     //var th = new Thread(dbWriterThread);
     //th.Start();
 }
Example #4
0
 public static void finishMasterThread()
 {
     SystemFlg.setMMFlg(false);
     SystemFlg.setMarketDataFlg(false);
     SystemFlg.setMasterFlg(false);
     SystemFlg.setDBWriterFlg(false);
     Form1.Form1Instance.setLabel12("Finished master thread");
 }
Example #5
0
 public static void startMarketData()
 {
     SystemFlg.setMarketDataFlg(true);
     MarketDataLog.initialize();
     //BoardDataUpdate.getCurrentBoard();
     BoardDataUpdate.startBoardUpdate();
     marketDataThread();
     DBWriter.startDBWriter();
     //System.Diagnostics.Debug.WriteLine("kita2");
 }
Example #6
0
 private async static void dbWriterThread()
 {
     await Task.Run(async() =>
     {
         while (SystemFlg.getDBWriterFlg())
         {
             writeExecutionsData();
             writeBoardData();
             await Task.Delay(500);
         }
     });
 }
Example #7
0
 public async static void startExchangeMonitor()
 {
     while (SystemFlg.getMarketDataFlg())
     {
         await Task.Run(async() =>
         {
             if (FlyerAPI2.getApiAccessProhibition() == false)
             {
                 Form1.Form1Instance.Invoke((Action)(() =>
                 {
                 }));
             }
             await Task.Delay(10000);
             return(0);
         });
     }
 }
Example #8
0
        private async void buttonExitAll_Click(object sender, EventArgs e)
        {
            Log.initialize();
            if (SystemFlg.getMarketDataFlg() == false)
            {
                MasterThread.startMasterThread();
                await Task.Delay(1000);

                MarketData.startMarketData();
                await Task.Delay(3000);
            }

            Account ac = new Account();
            await ac.updateCurrentPositions();

            await ac.startExitPriceTracingOrder();
        }
Example #9
0
        public async static void startMasterThread()
        {
            SystemFlg.setMasterFlg(true);
            start_dt = new DateTime();
            start_dt = DateTime.Now;

            FlyerAPI2.startFlyerAPIMonitoring();

            while (SystemFlg.getMasterFlg())
            {
                await Task.Run(async() =>
                {
                    Form1.Form1Instance.Invoke((Action)(() =>
                    {
                        Form1.Form1Instance.setLabel10("system time elapsed (min)=" + Math.Round((DateTime.Now - start_dt).TotalMinutes, 2));
                    }));
                    await Task.Delay(100);
                    return(0);
                });
            }
        }
Example #10
0
        public static async Task <string> startFlyerAPIMonitoring()
        {
            setApiAccessProhibition(false);
            num_private_minit = new List <long>();
            num_public_minit  = new List <long>();
            int num = 0;

            while (SystemFlg.getMasterFlg())
            {
                await Task.Run(async() =>
                {
                    num_public_minit.Add(num_public_called);
                    num_private_minit.Add(num_private_called);

                    if (num_public_minit.Count > 60 && num_private_minit.Count > 60)
                    {
                        if ((num_private_called - num_private_minit[0]) > 150 || (num_public_called + num_private_called - num_public_minit[0] - num_private_minit[0]) > 450)
                        {
                            setApiAccessProhibition(true);
                            //Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.setLabel7("API Access Prohibited"); }));
                        }
                        else
                        {
                            setApiAccessProhibition(false);
                        }
                        num_private_minit.RemoveAt(0);
                        num_public_minit.RemoveAt(0);
                    }
                    Form1.Form1Instance.Invoke((Action)(() =>
                    {
                        Form1.Form1Instance.setLabel8("num private api acecss in a min=" + (num_private_called - num_private_minit[0]).ToString());
                        Form1.Form1Instance.setLabel9("num public api acecss in a min=" + (num_public_called - num_public_minit[0]).ToString());
                    }));
                    await Task.Delay(1000);
                    return(0);
                });
            }
            return("completed");
        }
Example #11
0
        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();
             */
        }
Example #12
0
 private void buttonMarketData_Click(object sender, EventArgs e)
 {
     SystemFlg.setMasterFlg(true);
     MarketData.startMarketData();
     FlyerAPI2.startFlyerAPIMonitoring();
 }
Example #13
0
 private void buttonStopMMBot_Click(object sender, EventArgs e)
 {
     SystemFlg.initialize();
 }
Example #14
0
        public async static Task <string> startMMBot(double entry_spread_width, double size)
        {
            string res = "";

            SystemFlg.setMMFlg(true);

            initialize();
            entry_spread = entry_spread_width;
            order_size   = size;
            var ac = new Account();

            await Task.Delay(3000);


            await Task.Run(async() =>
            {
                int num = 0;
                while (SystemFlg.getMMFlg())
                {
                    if (FlyerAPI2.getApiAccessProhibition() == false)
                    {
                        //Log.addLog("MMBOT", "num=" + num.ToString());
                        await MMStrategy(ac);

                        //string line2 = "";
                        //var ord = ac.getAllOrders();

                        /*for (int i = 0; i < ord.Count; i++)
                         *  line2 += ord[i].order_side + " - " + ord[i].order_lot + "@" + ord[i].order_price + ", ";
                         */
                        //ac.takeLog("orders:"+line2);
                        //ac.takeLog("holdings:"+ ac.holding_ave_side + ", hold ave price=" + ac.holding_ave_price + " x " + ac.holding_total_size);

                        Form1.Form1Instance.Invoke((Action)(() =>
                        {
                            Form1.Form1Instance.setLabel5(ac.holding_ave_side + ", hold ave price=" + ac.holding_ave_price + " x " + ac.holding_total_size);
                            Form1.Form1Instance.setLabel4("num trade=" + ac.num_trade);
                            //Form1.Form1Instance.setLabel6(line2);
                        }));
                        num++;
                    }
                    else
                    {
                        Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.setLabel4("api prohibited"); }));
                        await Task.Delay(1000);
                    }
                }
            });

            Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("Finishing MMBot"); }));
            await ac.cancelAllOrders();

            await ac.checkExecutionAndUpdateOrders();

            await ac.startExitPriceTracingOrder();

            Form1.Form1Instance.Invoke((Action)(() => { Form1.Form1Instance.addListBox2("Finished MMBot"); }));

            //ac.displayAllLog();
            ac.writeLog();

            return(res);
        }
Example #15
0
        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("");
        }