Beispiel #1
0
        protected override void CalcBar()
        {
            Ratio.Value = Bars.CloseValue / BarsOfData(2).CloseValue;
            if (Bars.CurrentBar < Length)
            {
                return;
            }
            double contracts = this.PortfolioEquity() * PercentEquity / 100;

            if (Bars.CurrentBar > 1)
            {
                if (PublicFunctions.DoubleLess(m_expAvgRatioVal + m_stdDevRatioVal, Ratio.Value))
                {
                    if (!PublicFunctions.DoubleEquals(m_cur_pos, -1))
                    {
                        m_cur_pos = -1;
                        m_short.Send((int)contracts);
                    }
                }
                else
                if (PublicFunctions.DoubleGreater(m_expAvgRatioVal - m_stdDevRatioVal, Ratio.Value))
                {
                    if (!PublicFunctions.DoubleEquals(m_cur_pos, 1))
                    {
                        m_cur_pos = 1;
                        m_long.Send((int)contracts);
                    }
                }
                else
                {
                    m_cur_pos = 0;
                    m_lx.Send();
                    m_sx.Send();
                }
            }

            m_expAvgRatioVal = m_expAvgRatio.Value;
            m_stdDevRatioVal = Ratio.StandardDeviationCustom(Length, 1);

            if (Environment.ApplicationCode == EApplicationCode.Portfolio)
            {
                int slaveIdx = this.GetFirstStrategyIndexBySymbolName(BarsOfData(2).Info.Name);
                if (slaveIdx < 0)
                {
                    ExecControl.Abort(@"specified slave trader on instrument ""{0}"" not found", BarsOfData(2).Info.Name);
                }

                double money = Math.Abs(m_cur_pos * contracts) * Bars.CloseValue * Bars.Info.BigPointValue;

                if (PublicFunctions.DoubleGreater(money, 0))
                {
                    money = this.FromSymbolToPortfolioCurrency(money);
                }

                PortfolioStrategies[slaveIdx].PortfolioData[MasterMoney] = -m_cur_pos * money;
            }
        }
Beispiel #2
0
        protected override void CalcBar()
        {
            deviation.Value = Bars.Close[0] - current_regression[0];

            double standard_deviation = deviation.StandardDeviationCustom(length, 1);

            if (standard_deviation > 0.01)
            {
                double z_score = deviation.Value / standard_deviation;

                bool close_long, close_short, open_long, open_short;

                if (contrarian)
                {
                    open_short = z_score > trigger;
                    open_long  = z_score < -1 * trigger;

                    close_long  = z_score > 0;
                    close_short = z_score < 0;
                }
                else
                {
                    open_long  = z_score > trigger;
                    open_short = z_score < -1 * trigger;

                    close_short = z_score > 0;
                    close_long  = z_score < 0;
                }

                if (StrategyInfo.MarketPosition == 0)
                {
                    if (open_long)
                    {
                        long_order.Send();
                    }
                    if (open_short)
                    {
                        short_order.Send();
                    }
                }
                else if (StrategyInfo.MarketPosition < 0)
                {
                    if (close_short)
                    {
                        close_short_order.Send();
                    }
                }
                else if (StrategyInfo.MarketPosition > 0)
                {
                    if (close_long)
                    {
                        close_long_order.Send();
                    }
                }
            }
        }
        protected override void CalcBar()
        {
            this.StrategiesDenyEntriesAll();

            int strategiesCount = PortfolioStrategies.Count;

            avgReturn.Value = 0;

            var ranks = new Dictionary <int, double>();

            for (int idx = 0; idx < strategiesCount; idx++)
            {
                ranks[idx]       = PortfolioStrategies[idx].PortfolioData["RankStrategyR"].safe_cast2double();
                avgReturn.Value += ranks[idx];
            }

            avgReturn.Value /= strategiesCount;
            double standardDeviation = avgReturn.StandardDeviationCustom(StdDevLength, 1);

            if (standardDeviation == 0)
            {
                standardDeviation = 1;
            }

            ranks = ranks.ToDictionary(x => x.Key, y => (y.Value - avgReturn.Value) / standardDeviation);

            ranks = ranks.OrderByDescending(elem => elem.Value).ToDictionary(x => x.Key, y => y.Value);

            List <int> strategies = new List <int>();
            int        inLong     = this.StrategiesInLong(ref strategies);

            for (int idx = 0; idx < BuyBestX - inLong; idx++)
            {
                PortfolioStrategies[ranks.Keys.ElementAt(idx)].AllowEntriesLong = true;
                if (UsePercentFromPortfolioForEntry)
                {
                    PortfolioStrategies[ranks.Keys.ElementAt(idx)].EntryContracts =
                        this.CalcContractsForEntry(PortfolioBalancePercent, ranks.Keys.ElementAt(idx));
                }

                if (TraceOutput)
                {
                    Output.WriteLine("CurrentBar = {0}. Allow LONG for symbol {1}, Contracts = {2}",
                                     Bars.CurrentBar, PortfolioStrategies[ranks.Keys.ElementAt(idx)].Signals[0].Bars.Info.Name,
                                     PortfolioStrategies[ranks.Keys.ElementAt(idx)].EntryContracts);
                }
            }

            int inShort = this.StrategiesInShort(ref strategies);

            for (int idx = strategiesCount - 1; idx > strategiesCount - SellWorstY + inShort + 1; idx--)
            {
                PortfolioStrategies[ranks.Keys.ElementAt(idx)].AllowEntriesShort = true;
                if (UsePercentFromPortfolioForEntry)
                {
                    PortfolioStrategies[ranks.Keys.ElementAt(idx)].EntryContracts =
                        this.CalcContractsForEntry(PortfolioBalancePercent, ranks.Keys.ElementAt(idx));
                }

                if (TraceOutput)
                {
                    Output.WriteLine("CurrentBar = {0}. Allow SHORT for symbol {1}, Contracts = {2}",
                                     Bars.CurrentBar, PortfolioStrategies[ranks.Keys.ElementAt(idx)].Signals[0].Bars.Info.Name,
                                     PortfolioStrategies[ranks.Keys.ElementAt(idx)].EntryContracts);
                }
            }
        }