Ejemplo n.º 1
0
        public async Task <InitDataLiveDemoClientResponse> InitData(string clientId)
        {
            var marginTradingEnabled = await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId);

            if (!marginTradingEnabled.Demo && !marginTradingEnabled.Live)
            {
                throw new Exception("Margin trading is not available");
            }

            var initData        = new InitDataLiveDemoClientResponse();
            var clientIdRequest = new ClientIdBackendRequest {
                ClientId = clientId
            };

            var initDataResponses = await _httpRequestService.RequestIfAvailableAsync <InitDataBackendResponse>(clientIdRequest, "init.data", () => null, marginTradingEnabled);

            initData.Live = initDataResponses.Live?.ToClientContract();
            initData.Demo = initDataResponses.Demo?.ToClientContract();

            var initAssetsResponses = await _httpRequestService.RequestIfAvailableAsync(clientIdRequest, "init.assets", Array.Empty <AssetPairBackendContract>, marginTradingEnabled);

            initData.Assets = initAssetsResponses.Live.Concat(initAssetsResponses.Demo).GroupBy(a => a.Id)
                              .Select(g => g.First().ToClientContract()).ToArray();

            var initPricesResponse =
                await _httpRequestService.RequestWithRetriesAsync <Dictionary <string, InstrumentBidAskPairContract> >(
                    clientIdRequest, "init.prices");

            initData.Prices = initPricesResponse.ToDictionary(p => p.Key, p => p.Value.ToClientContract());

            return(initData);
        }
Ejemplo n.º 2
0
        private async Task Execute(string action, bool restartTimer = true)
        {
            // do action
            LogInfo($"Action: {action}");

            var command = action.Split(' ')[0].ToLower();

            switch (command)
            {
            case "initdata":
                var resinitdata = await Bot.InitData();

                _initData = (InitDataLiveDemoClientResponse)resinitdata.Result;
                _operations.Add(resinitdata);
                break;

            case "initaccounts":
                var resinitaccounts = await Bot.InitAccounts();

                _operations.Add(resinitaccounts);
                break;

            case "initgraph":
                var resinitGraph = await Bot.InitGraph();

                _initGraph = (InitChartDataClientResponse)resinitGraph.Result;
                _operations.Add(resinitGraph);
                foreach (var graphRow in _initGraph.ChartData)
                {
                    LogInfo($"ChartRow: {graphRow.Key}:{graphRow.Value}");
                }

                break;

            case "subscribe":
                var subscribeInstrument = action.Split(' ')[1].ToUpper();
                Bot.SubscribePrice(subscribeInstrument);
                break;

            case "unsubscribe":
                var unsubscribeInstrument = action.Split(' ')[1].ToUpper();
                Bot.UnsubscribePrice(unsubscribeInstrument);
                break;

            case "placeorder":
                #region placeorder
                if (_initData == null)
                {
                    LogInfo("PlaceOrder Failed. InitData not performed, please call InitData before placing orders");
                }
                else
                {
                    var placeOrderInstrument = action.Split(' ')[1].ToUpper();
                    int placeOrderCount;
                    if (action.Split(' ').Length > 2)
                    {
                        var orderCount = action.Split(' ')[2].ToUpper();
                        if (!int.TryParse(orderCount, out placeOrderCount))
                        {
                            placeOrderCount = 1;
                        }
                    }
                    else
                    {
                        placeOrderCount = 1;
                    }

                    var result = await Bot.PlaceOrders(_initData.Demo.Accounts[0].Id, placeOrderInstrument, placeOrderCount);

                    _operations.AddRange(result);
                }
                #endregion
                break;

            case "closeorder":
                #region closeorder
                if (_initData == null)
                {
                    LogInfo("CloseOrder Failed. InitData not performed, please call InitData before closing orders");
                }
                else
                {
                    var closeOrderInstrument = action.Split(' ')[1].ToUpper();
                    int closeOrderCount;
                    if (action.Split(' ').Length > 2)
                    {
                        var orderCount = action.Split(' ')[2].ToUpper();
                        if (!int.TryParse(orderCount, out closeOrderCount))
                        {
                            closeOrderCount = 1;
                        }
                    }
                    else
                    {
                        closeOrderCount = 1;
                    }

                    var result = await Bot.CloseOrders(_initData.Demo.Accounts[0].Id, closeOrderInstrument, closeOrderCount);

                    _operations.AddRange(result);
                }
                #endregion
                break;

            case "cancelorder":
                #region cancelorder
                if (_initData == null)
                {
                    LogInfo("CancelOrder Failed. InitData not performed, please call InitData before canceling orders");
                }
                else
                {
                    var cancelOrderInstrument = action.Split(' ')[1].ToUpper();
                    int cancelOrderCount;
                    if (action.Split(' ').Length > 2)
                    {
                        var orderCount = action.Split(' ')[2].ToUpper();
                        if (!int.TryParse(orderCount, out cancelOrderCount))
                        {
                            cancelOrderCount = 1;
                        }
                    }
                    else
                    {
                        cancelOrderCount = 1;
                    }

                    var result = await Bot.CancelOrders(_initData.Demo.Accounts[0].Id, cancelOrderInstrument, cancelOrderCount);

                    _operations.AddRange(result);
                }
                #endregion
                break;

            case "gethistory":
                var resgethistory = await Bot.GetHistory();

                _operations.Add(resgethistory);
                break;

            case "getaccounthistory":
                var resgetaccounthistory = await Bot.GetAccountHistory();

                _operations.Add(resgetaccounthistory);
                break;

            case "getaccountopenpositions":
                if (_initData == null)
                {
                    LogInfo("GetAccountOpenPositions Failed. InitData not performed, please call InitData before placing orders");
                }
                else
                {
                    var result = await Bot.GetAccountOpenPositions(_initData.Demo.Accounts[0].Id);

                    _operations.Add(result);
                }
                break;

            case "getclientorders":
                var getClientOrdersResult = await Bot.GetClientOrders();

                _operations.Add(getClientOrdersResult);
                break;

            case "reconnect":
                Bot.Reconnect();
                LogInfo("Reconnected...");
                break;

            case "placependingorder":
                #region placependingorder
                if (_initData == null)
                {
                    LogInfo("PlaceOrder Failed. InitData not performed, please call InitData before placing orders");
                }
                else
                {
                    var placeOrderInstrument = action.Split(' ')[1].ToUpper();
                    int placeOrderCount;
                    if (action.Split(' ').Length > 2)
                    {
                        var orderCount = action.Split(' ')[2].ToUpper();
                        if (!int.TryParse(orderCount, out placeOrderCount))
                        {
                            placeOrderCount = 1;
                        }
                    }
                    else
                    {
                        placeOrderCount = 1;
                    }
                    var currentBid = _initData.Prices[placeOrderInstrument].Bid;
                    var result     = await Bot.PlacePendingOrders(_initData.Demo.Accounts[0].Id, placeOrderInstrument, placeOrderCount, currentBid);

                    _operations.AddRange(result);
                }
                #endregion
                break;
            }


            // Wait for next action
            if (restartTimer)
            {
                _actionTimer.Change(Bot.ActionScriptInterval, 0);
            }
        }