Пример #1
0
        /// <summary>
        /// Calculate the absolute differences between each successive tick.
        /// Each date is the date at which the difference has been observed,
        /// so the b in "a - b".
        /// </summary>
        /// <returns>A TickList containing the dates at which the differences
        /// have been observed, and the differences.</returns>
        public TickList AbsoluteDifferences()
        {
            TickList returnlist = new TickList();

            for (int i = 1; i < list.Count; i++)
            {
                DateTime observeddate = list[i].getDate();
                decimal  difference   = list[i].getValue() - list[i - 1].getValue();
                returnlist.Add(new Tick(observeddate, difference));
            }
            return(returnlist);
        }
Пример #2
0
        /// <summary>
        /// A simple hold strategy.
        /// When the value of the web data has (absolutely or relatively), increased
        /// by a certain number or percentage, the asset is held for the specified holdtime.
        /// </summary>
        /// <param name="valuedata">A TickList containing the value data.</param>
        /// <param name="webdata">A TickList containing the Google search data.</param>
        /// <param name="relative">If true, the change is interpreted as relative change.
        /// Otherwise, the change is absolute.</param>
        /// <param name="change">A number representing the change treshold.</param>
        /// <param name="holdtime">The time for which to hold the asset in hours.
        /// (this might change into the future when testing high-frequency strategies).</param>
        /// <returns>A touple containing: a decimal representing the remaing value
        /// after using this strategy. A TickList containing the buy- and -sell
        /// values and prices.</returns>
        public static Tuple <decimal, TickList> SimpleHoldStrategy(TickList valuedata,
                                                                   TickList webdata, bool relative, decimal change, int holdtime)
        {
            decimal  value     = 1;
            TickList portfolio = new TickList();

            TickList    absticklist = webdata.AbsoluteDifferences();
            List <Tick> absdif      = absticklist.getList();

            TickList    relticklist = webdata.RelativeDifferences();
            List <Tick> reldif      = relticklist.getList();

            for (int i = 0; i < reldif.Count && i != -1;)
            {
                if ((relative && reldif[i].getValue() > change) ||
                    (!relative && absdif[i].getValue() > change))
                {
                    DateTime buydate  = reldif[i].getDate();
                    DateTime selldate = buydate.AddHours(holdtime);
                    try
                    {
                        decimal buyprice  = valuedata.valueAt(buydate);
                        decimal sellprice = valuedata.valueAt(selldate);
                        value *= sellprice / buyprice;
                        portfolio.Add(new Tick(buydate, buyprice));
                        portfolio.Add(new Tick(selldate, sellprice));
                    }
                    catch (InvalidOperationException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    i = relticklist.findDate(selldate);
                }
                else
                {
                    i++;
                }
            }
            return(Tuple.Create(value, portfolio));
        }
Пример #3
0
        /// <summary>
        /// Calculate the relative differences between each successive tick.
        /// This means each absolute difference is scaled by the original value.
        /// For example from 10 to 8 the absolute difference is -2, the relative
        /// difference is -2/10 = 0.2.
        /// </summary>
        /// <returns>A TickList containing the dates at which the differences
        /// have been observed, and the differences.</returns>
        public TickList RelativeDifferences()
        {
            TickList    returnlist   = new TickList();
            List <Tick> absolutelist = this.AbsoluteDifferences().getList();

            for (int i = 0; i < absolutelist.Count; i++)
            {
                DateTime observeddate       = list[i].getDate();
                decimal  relativedifference = absolutelist[i].getValue() / list[i].getValue();
                returnlist.Add(new Tick(observeddate, relativedifference));
            }
            return(returnlist);
        }