Пример #1
0
        public static void TryBuildPayload()
        {
            if (IsSubscribed)
            {
                Current++;
                if (Current >= Frequency)
                {
                    Current = 0;
                    lock (LockObj)
                    {
                        Dictionary <string, string> items = new Dictionary <string, string>();

                        if (Global.State.AlgoTraderSubscriptions["main"] == true)
                        {
                            AlgoTraderMain main = new AlgoTraderMain();
                            main.CurrentTime          = AlgoTraderState.CurrentTime.ToString();
                            main.SelectedSymbol       = SelectedSymbol;
                            main.SelectedStrategyName = AlgoTraderMethods.GetStrategyName(SelectedStrategyId);
                            main.SelectedStrategyId   = SelectedStrategyId;

                            using (var output = new StringWriter())
                            {
                                JSON.Serialize(main, output, Options.PrettyPrint);
                                items.Add("algoTraderMain", output.ToString());
                            }
                        }


                        if (Global.State.AlgoTraderSubscriptions["log"] == true)
                        {
                            UIObjects.AlgoTraderLog log = new UIObjects.AlgoTraderLog();
                            log.Log = Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose);
                            //log.Log = System.Web.HttpUtility.JavaScriptStringEncode(Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose));

                            using (var output = new StringWriter())
                            {
                                JSON.Serialize(log, output, Options.PrettyPrint);
                                items.Add("algoTraderLog", output.ToString());
                            }
                        }

                        if (Global.State.AlgoTraderSubscriptions["overview"] == true)
                        {
                            AlgoTraderOverview overview = buildOverviewObject();

                            using (var output = new StringWriter())
                            {
                                JSON.Serialize(overview, output, Options.PrettyPrint);
                                items.Add("algoTraderOverview", output.ToString());
                            }
                        }

                        if (Global.State.AlgoTraderSubscriptions["chart"] == true)
                        {
                            AlgoTraderChart chart = buildChartObject();

                            using (var output = new StringWriter())
                            {
                                JSON.Serialize(chart, output, Options.Default);
                                items.Add("algoTraderChart", output.ToString());
                            }
                        }


                        Payload = SerializerMethods.DictionarySerializedValuesToJSON(items);
                    }
                }
            }
        }
Пример #2
0
        public static AlgoTraderOverview buildOverviewObject()
        {
            AlgoTraderOverview result = new AlgoTraderOverview();


            /* ###################### SYMBOLS ###################### */

            AlgoTraderDataTable symbolsDataTable = new AlgoTraderDataTable();

            symbolsDataTable.Name = "overview-symbols";

            if (String.IsNullOrEmpty(SelectedSymbol))
            {
                symbolsDataTable.HideColumn = (!String.IsNullOrEmpty(SelectedStrategyId) ? 1 : -1);
                symbolsDataTable.Show       = true;
                symbolsDataTable.Title      = "Symbols (" + AlgoTraderShared.WatchList.Count.ToString() + ")";

                StringBuilder sb = new StringBuilder();

                // name, strategies, positions, orders, actions, realized, unrealized

                List <DataTableItem> dataTableItems = new List <DataTableItem>();
                for (int n = 0; n < AlgoTraderShared.WatchList.Count; n++)
                {
                    WatchItem wi = AlgoTraderShared.WatchList[n];

                    string symbol = wi.Symbol;

                    DataTableItem dataTableItem = new DataTableItem();

                    dataTableItem.ColumnValues.Add("symbol", wi.Symbol);
                    dataTableItem.ColumnValues.Add("strategies", wi.Strategies.Count);



                    //dataTableItem.Name = wi.Symbol;
                    //dataTableItem.SubItems = wi.Strategies.Count;

                    dataTableItem.ColumnValues.Add("orders", AlgoTraderShared.Orders.FindAll(o => o.Symbol == symbol).Count);
                    dataTableItem.ColumnValues.Add("actions", AlgoTraderShared.StrategyActions.FindAll(a => a.Symbol == symbol).Count);

                    List <Position> positions = AlgoTraderShared.Positions.FindAll(p => p.Symbol == symbol);

                    dataTableItem.ColumnValues.Add("positions", positions.Count);

                    // do realized
                    List <Position> closedPositions = positions.FindAll(p => p.Status == PositionStatus.Closed);
                    decimal         netRealized     = 0M;
                    for (int m = 0; m < closedPositions.Count; m++)
                    {
                        netRealized += closedPositions[m].SoldAt - closedPositions[m].BoughtAt;
                    }
                    dataTableItem.ColumnValues.Add("realized", netRealized);

                    // do unrealized
                    List <Position> openPositions  = positions.FindAll(p => p.Status == PositionStatus.Open);
                    decimal         latestAskPrice = -1M;
                    decimal         latestBidPrice = -1M;
                    if (openPositions.Count > 0)
                    {
                        List <Node> nodes = AlgoTraderShared.NodesData[symbol].Nodes;
                        latestAskPrice = nodes[nodes.Count - 1].AskPrice;
                        latestBidPrice = nodes[nodes.Count - 1].BidPrice;
                    }
                    decimal netUnrealized = 0M;


                    for (int m = 0; m < openPositions.Count; m++)
                    {
                        Position p         = openPositions[m];
                        decimal  sellPrice = 0M;
                        decimal  buyPrice  = 0M;

                        if (p.Side == PositionSide.Long)
                        {
                            // if long, find the sell price by getting the latest bid price
                            sellPrice = latestBidPrice;
                            buyPrice  = p.BoughtAt;
                        }
                        else if (p.Side == PositionSide.Short)
                        {
                            // if short, find the buy price by getting the latest ask price
                            buyPrice  = latestAskPrice;
                            sellPrice = p.SoldAt;
                        }
                        else
                        {
                            throw new Exception("a what?");
                        }
                        netUnrealized += sellPrice - buyPrice;
                    }
                    dataTableItem.ColumnValues.Add("unrealized", netUnrealized);

                    dataTableItems.Add(dataTableItem);
                }


                TrySortDataTable(ref dataTableItems, "overview-symbols", "symbol");

                // // name, strategies, positions, orders, actions, realized, unrealized
                // <tr><td></td><td class=\"sub-items-col\"></td><td></td><td></td><td></td><td></td><td></td></tr>
                for (int n = 0; n < dataTableItems.Count; n++)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["symbol"] + "</td>");
                    sb.Append("<td class=\"sub-items-col\">" + dataTableItems[n].ColumnValues["strategies"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["positions"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["orders"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["actions"] + "</td>");
                    sb.Append("<td data-sort-value=\"" + dataTableItems[n].ColumnValues["realized"] + "\">" + UC.DecimalToUSD((decimal)dataTableItems[n].ColumnValues["realized"], 4) + "</td>");
                    sb.Append("<td data-sort-value=\"" + dataTableItems[n].ColumnValues["unrealized"] + "\">" + UC.DecimalToUSD((decimal)dataTableItems[n].ColumnValues["unrealized"], 4) + "</td>");
                    sb.Append("</tr>");
                }

                symbolsDataTable.TBodyHtml = sb.ToString();
            }

            result.DataTables.Add(symbolsDataTable);

            /* ###################### STRATEGIES ###################### */

            AlgoTraderDataTable strategiesDataTable = new AlgoTraderDataTable();

            strategiesDataTable.Name = "overview-strategies";

            if (String.IsNullOrEmpty(SelectedStrategyId))
            {
                strategiesDataTable.HideColumn = (!String.IsNullOrEmpty(SelectedSymbol) ? 1 : -1);
                strategiesDataTable.Show       = true;

                string[] usableStrategiesKeys = Global.State.UsableStrategies.Keys.ToArray();

                strategiesDataTable.Title = "Strategies (" + usableStrategiesKeys.Length.ToString() + ")";

                StringBuilder sb = new StringBuilder();

                // name, strategies, positions, orders, actions, realized, unrealized

                List <DataTableItem> dataTableItems = new List <DataTableItem>();
                if (AlgoTraderShared.WatchList.Count > 0)
                {
                    for (int n = 0; n < usableStrategiesKeys.Length; n++)
                    {
                        string strategy = Global.State.UsableStrategies[usableStrategiesKeys[n]];

                        DataTableItem dataTableItem = new DataTableItem();

                        dataTableItem.ColumnValues.Add("strategy", strategy);

                        // probably super inefficient
                        dataTableItem.ColumnValues.Add("symbols", AlgoTraderShared.WatchList.Count(w => w.Strategies.Exists(s => s.GetType().Name == strategy)));



                        //dataTableItem.Name = wi.Symbol;
                        //dataTableItem.SubItems = wi.Strategies.Count;

                        dataTableItem.ColumnValues.Add("orders", AlgoTraderShared.Orders.FindAll(o => o.StrategyName == strategy && (String.IsNullOrEmpty(SelectedSymbol) || o.Symbol == SelectedSymbol)).Count);
                        dataTableItem.ColumnValues.Add("actions", AlgoTraderShared.StrategyActions.FindAll(a => a.StrategyName == strategy && (String.IsNullOrEmpty(SelectedSymbol) || a.Symbol == SelectedSymbol)).Count);

                        List <Position> positions = AlgoTraderShared.Positions.FindAll(p => p.StrategyName == strategy && (String.IsNullOrEmpty(SelectedSymbol) || p.Symbol == SelectedSymbol));

                        dataTableItem.ColumnValues.Add("positions", positions.Count);

                        // do realized
                        List <Position> closedPositions = positions.FindAll(p => p.Status == PositionStatus.Closed);
                        decimal         netRealized     = 0M;
                        for (int m = 0; m < closedPositions.Count; m++)
                        {
                            netRealized += closedPositions[m].SoldAt - closedPositions[m].BoughtAt;
                        }
                        dataTableItem.ColumnValues.Add("realized", netRealized);

                        // do unrealized
                        List <Position> openPositions = positions.FindAll(p => p.Status == PositionStatus.Open);

                        decimal netUnrealized = 0M;


                        for (int m = 0; m < openPositions.Count; m++)
                        {
                            Position p         = openPositions[m];
                            decimal  sellPrice = 0M;
                            decimal  buyPrice  = 0M;

                            List <Node> nodes = AlgoTraderShared.NodesData[p.Symbol].Nodes;

                            if (p.Side == PositionSide.Long)
                            {
                                // if long, find the sell price by getting the latest bid price
                                sellPrice = nodes[nodes.Count - 1].BidPrice;
                                buyPrice  = p.BoughtAt;
                            }
                            else if (p.Side == PositionSide.Short)
                            {
                                // if short, find the buy price by getting the latest ask price
                                buyPrice  = nodes[nodes.Count - 1].AskPrice;
                                sellPrice = p.SoldAt;
                            }
                            else
                            {
                                throw new Exception("a what?");
                            }
                            netUnrealized += sellPrice - buyPrice;
                        }
                        dataTableItem.ColumnValues.Add("unrealized", netUnrealized);

                        dataTableItems.Add(dataTableItem);
                    }
                }
                TrySortDataTable(ref dataTableItems, "overview-strategies", "strategy");

                for (int n = 0; n < dataTableItems.Count; n++)
                {
                    sb.Append("<tr>");
                    sb.Append("<td style=\"text-align:left;\">" + dataTableItems[n].ColumnValues["strategy"] + "</td>");
                    sb.Append("<td class=\"sub-items-col\">" + dataTableItems[n].ColumnValues["symbols"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["positions"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["orders"] + "</td>");
                    sb.Append("<td>" + dataTableItems[n].ColumnValues["actions"] + "</td>");
                    sb.Append("<td data-sort-value=\"" + dataTableItems[n].ColumnValues["realized"] + "\">" + UC.DecimalToUSD((decimal)dataTableItems[n].ColumnValues["realized"], 4) + "</td>");
                    sb.Append("<td data-sort-value=\"" + dataTableItems[n].ColumnValues["unrealized"] + "\">" + UC.DecimalToUSD((decimal)dataTableItems[n].ColumnValues["unrealized"], 4) + "</td>");
                    sb.Append("</tr>");
                }

                strategiesDataTable.TBodyHtml = sb.ToString();
            }

            result.DataTables.Add(strategiesDataTable);

            return(result);
        }