예제 #1
0
        /// <summary>
        /// Generate the exposure plot using the python libraries.
        /// </summary>
        public override string Render()
        {
            var longBacktestFrame  = Metrics.Exposure(_backtestPortfolios, OrderDirection.Buy);
            var shortBacktestFrame = Metrics.Exposure(_backtestPortfolios, OrderDirection.Sell);
            var longLiveFrame      = Metrics.Exposure(_livePortfolios, OrderDirection.Buy);
            var shortLiveFrame     = Metrics.Exposure(_livePortfolios, OrderDirection.Sell);

            var backtestFrame = longBacktestFrame.Join(shortBacktestFrame)
                                .FillMissing(Direction.Forward)
                                .FillMissing(0.0);

            var liveFrame = longLiveFrame.Join(shortLiveFrame)
                            .FillMissing(Direction.Forward)
                            .FillMissing(0.0);

            longBacktestFrame  = Frame.CreateEmpty <DateTime, Tuple <SecurityType, OrderDirection> >();
            shortBacktestFrame = Frame.CreateEmpty <DateTime, Tuple <SecurityType, OrderDirection> >();
            longLiveFrame      = Frame.CreateEmpty <DateTime, Tuple <SecurityType, OrderDirection> >();
            shortLiveFrame     = Frame.CreateEmpty <DateTime, Tuple <SecurityType, OrderDirection> >();

            foreach (var key in backtestFrame.ColumnKeys)
            {
                longBacktestFrame[key]  = backtestFrame[key].SelectValues(x => x < 0 ? 0 : x);
                shortBacktestFrame[key] = backtestFrame[key].SelectValues(x => x > 0 ? 0 : x);
            }

            foreach (var key in liveFrame.ColumnKeys)
            {
                longLiveFrame[key]  = liveFrame[key].SelectValues(x => x < 0 ? 0 : x);
                shortLiveFrame[key] = liveFrame[key].SelectValues(x => x > 0 ? 0 : x);
            }

            longBacktestFrame  = longBacktestFrame.DropSparseColumnsAll();
            shortBacktestFrame = shortBacktestFrame.DropSparseColumnsAll();
            longLiveFrame      = longLiveFrame.DropSparseColumnsAll();
            shortLiveFrame     = shortLiveFrame.DropSparseColumnsAll();

            var base64 = "";

            using (Py.GIL())
            {
                var time                = backtestFrame.RowKeys.ToList().ToPython();
                var longSecurities      = longBacktestFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
                var shortSecurities     = shortBacktestFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
                var longData            = longBacktestFrame.ColumnKeys.Select(x => longBacktestFrame[x].Values.ToList().ToPython()).ToPython();
                var shortData           = shortBacktestFrame.ColumnKeys.Select(x => shortBacktestFrame[x].Values.ToList().ToPython()).ToPython();
                var liveTime            = liveFrame.RowKeys.ToList().ToPython();
                var liveLongSecurities  = longLiveFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
                var liveShortSecurities = shortLiveFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
                var liveLongData        = longLiveFrame.ColumnKeys.Select(x => longLiveFrame[x].Values.ToList().ToPython()).ToPython();
                var liveShortData       = shortLiveFrame.ColumnKeys.Select(x => shortLiveFrame[x].Values.ToList().ToPython()).ToPython();

                base64 = Charting.GetExposure(
                    time,
                    longSecurities,
                    shortSecurities,
                    longData,
                    shortData,
                    liveTime,
                    liveLongSecurities,
                    liveShortSecurities,
                    liveLongData,
                    liveShortData
                    );
            }

            return(base64);
        }