Exemple #1
0
 /// <summary>
 /// The lowest low for the barlist, considering so many bars back.
 /// </summary>
 /// <param name="b">The barlist.</param>
 /// <param name="barsback">The barsback to consider.</param>
 /// <returns></returns>
 public static decimal LL(BarList b, int barsback)
 { // gets lowest low
     if (barsback > b.Count) barsback = b.Count;
     decimal ll = decimal.MaxValue;
     for (int i = b.Last; i > (b.Count - barsback); i--) ll = (b.Get(i).Low < ll) ? b.Get(i).Low : ll;
     return ll;
 }
Exemple #2
0
 /// <summary>
 /// Returns the highest-high of the barlist, for so many bars back.
 /// </summary>
 /// <param name="b">The barlist.</param>
 /// <param name="barsback">The barsback to consider.</param>
 /// <returns></returns>
 public static decimal HH(BarList b, int barsback)
 {// gets highest high
     if (barsback > b.Count) barsback = b.Count;
     decimal hh = decimal.MinValue;
     for (int i = b.Last; i > (b.Count - barsback); i--) hh = (b.Get(i).High > hh) ? b.Get(i).High : hh;
     return hh;
 }
Exemple #3
0
        /// <summary>
        /// The lowest low for the barlist, considering so many bars back.
        /// </summary>
        /// <param name="b">The barlist.</param>
        /// <param name="barsback">The barsback to consider.</param>
        /// <returns></returns>
        public static decimal LL(BarList b, int barsback)
        { // gets lowest low
            if (barsback > b.Count)
            {
                barsback = b.Count;
            }
            decimal ll = decimal.MaxValue;

            for (int i = b.Last; i > (b.Count - barsback); i--)
            {
                ll = (b.Get(i).Low < ll) ? b.Get(i).Low : ll;
            }
            return(ll);
        }
Exemple #4
0
        /// <summary>
        /// Returns the highest-high of the barlist, for so many bars back.
        /// </summary>
        /// <param name="b">The barlist.</param>
        /// <param name="barsback">The barsback to consider.</param>
        /// <returns></returns>
        public static decimal HH(BarList b, int barsback)
        {// gets highest high
            if (barsback > b.Count)
            {
                barsback = b.Count;
            }
            decimal hh = decimal.MinValue;

            for (int i = b.Last; i > (b.Count - barsback); i--)
            {
                hh = (b.Get(i).High > hh) ? b.Get(i).High : hh;
            }
            return(hh);
        }
Exemple #5
0
        public bool newBar(BarList bl)
        {
            if (!bl.isValid) return false;
            Bar obar = bl.Get(bl.Last);
            if (!isbarcons) throw new Exception("You can't call a newBar method without using the right constructor.");
            if (bl.NewBar)
                {
                    count++;
                    decimal open = obar.Open;

                    l.Add(open);

                    if (l.Count > lookback)
                    {
                        l.RemoveAt(0);
                        count--;
                    }

                    sum = 0;
                    sumsqtrade = 0;
                    foreach (decimal v in l)
                    {
                        sum += v;
                        sumsqtrade = sumsqtrade + v * v;
                    }
                    mean = sum / count;
                    devavg = (sumsqtrade - sum * sum / count) / count;
                }


                sd = Math.Sqrt((double)devavg);
                ub = mean + sds * (decimal)sd;
                lb = mean - sds * (decimal)sd;
                return true;
        }
Exemple #6
0
        public int Test(List<FileInfo> tf) 
        {
            show("Starting run "+name+" containing "+ tf.Count + " symbols."+Environment.NewLine);
            int totfills = 0;
            int totalticks = approxticks(tf);

            for (int i = 1; i <= tf.Count; i++) // loop through the list
            {
                FileInfo f = tf[i-1];
                Match m = Regex.Match(f.Name, "([0-9]+)", RegexOptions.IgnoreCase);
                int date = Convert.ToInt32(m.Result("$1"));
                LoadIndexFiles(date);
                TickFile(f.Name); // set current file
                if (f.Length == 0) continue; // ignore if tick file is empty
                show(Environment.NewLine);
                show("Symbol " + this.symbol + " (" + i + " of " + tf.Count + ") ");

                // reset per-symbol statistics
                if (mybox!=null) mybox.Reset();
                bl = new BarList((BarInterval)bint, symbol);
                int fills = 0;
                tick = new eSigTick(); // reset our tick
                int itime = 0;
                BoxInfo bi = new BoxInfo();
                

                while (this.getTick() && tick.hasTick)
                { // process the ticks
                    line++;
                    if ((line % 5000) == 0) show(".");
                    if (((itime==0) || (itime!=tick.time)))
                    {
                        // load all the indicies for this time
                        List<Index> itix = FetchIdx(tick.time);
                        itime = tick.time;
                        // send them to the box (before we send the tix)
                        for (int id = 0; id < itix.Count; id++)
                            mybox.NewIndex(itix[id]);
                    }

                    if ((this.exfilter != "") &&
                        ((!tick.isTrade && (!tick.be.Contains(exfilter) || !tick.oe.Contains(exfilter))) ||
                         (tick.isTrade && tick.ex.Contains(exfilter)))) continue;

                    if ((bi.Open == 0) && tick.isTrade) 
                        bi.Open = tick.trade;

                    if (tick.trade > bi.High) bi.High = tick.trade;
                    if (tick.trade < bi.Low) bi.Low = tick.trade;
                    bl.AddTick(tick); // save our tick to a bar

                    if (bl.Has(2) && bl.Get(bl.Last-1).DayEnd)
                    { // we hit a new day in the same file, reset day stuff and set our DayEndTime
                        bl.Reset();
                        bl.AddTick(tick); // put our tick back

                        if (mybox!=null) mybox.Reset();
                        SetDayClose(); // this has to be run after mybox.Reset!!!
                    }


                    // execute any pending orders on this tick
                    if (mybroker.GetOrderList().Count>0) fills += mybroker.Execute(tick); 
                    // trade box on this tick, if he generates any orders then send them
                    if (mybox != null)
                    {
                        mybroker.sendOrder(
                            mybox.Trade(tick, bl, mybroker.GetOpenPosition(this.symbol), bi));
                        // quit early if box shuts itself off and no pending orders
                        if (mybox.Off && (mybroker.GetOrderList().Count == 0)) break;
                    }


                    if (this.delay != 0)
                    {
                        System.Threading.Thread.Sleep(this.delay);
                        System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Lowest;
                    }
                    this.ReportProgress((int)((100*line) / totalticks));
                }
                show(fills+" executions.");
                totfills += fills;
                this.cf.Close();
            }
            show(Environment.NewLine);
            show(name+" complete: "+tf.Count+" symbols, "+ totfills + " executions."+Environment.NewLine);
            return totfills;
        }
Exemple #7
0
        void Chart_Paint(object sender, PaintEventArgs e)
        {
            if (bl != null)
            {
                highesth = BarMath.HH(bl);
                lowestl  = BarMath.LL(bl);
                barc     = bl.Count;
            }
            if ((bl == null) || (bl.Count == 0))
            {
                return;
            }
            Text         = Title;
            r            = ClientRectangle;
            pixperbar    = (((decimal)r.Width - (decimal)border - ((decimal)border / 3)) / (decimal)barc);
            pixperdollar = (((decimal)r.Height - (decimal)border * 2) / (highesth - lowestl));
            Pen p = new Pen(Color.Black);

            g = e.Graphics;
            Form f = (Form)sender;

            g.Clear(f.BackColor);

            Color fgcol = (f.BackColor == Color.Black) ? Color.White : Color.Black;

            // x-axis
            g.DrawLine(new Pen(fgcol), (int)(border / 3), r.Height - border, r.Width - border, r.Height - border);
            // y-axis
            g.DrawLine(new Pen(fgcol), r.Width - border, r.Y + border, r.Width - border, r.Height - border);

            const int minxlabelwidth = 15;

            int lastlabelcoord = -500;

            for (int i = 0; i < barc; i++)
            {
                Color bcolor = (bl.Get(i).Close > bl.Get(i).Open) ? Color.Green : Color.Red;
                p = new Pen(bcolor);
                // draw high/low bar
                g.DrawLine(p, getX(i), getY(bl.Get(i).Low), getX(i), getY(bl.Get(i).High));
                // draw open bar
                g.DrawLine(p, getX(i), getY(bl.Get(i).Open), getX(i) - (int)(pixperbar / 3), getY(bl.Get(i).Open));
                // draw close bar
                g.DrawLine(p, getX(i), getY(bl.Get(i).Close), getX(i) + (int)(pixperbar / 3), getY(bl.Get(i).Close));
                // draw time labels (time @30min and date@noon)

                if (bl.Int != BarInterval.Day)
                {
                    if ((i % 6) == 0)
                    {
                        g.DrawString(bl.Get(i).Bartime.ToString(), f.Font, new SolidBrush(fgcol), getX(i), r.Height - (f.Font.GetHeight() * 3));
                    }
                    if (bl.Get(i).Bartime == 1200)
                    {
                        g.DrawString(bl.Get(i).Bardate.ToString(), f.Font, new SolidBrush(fgcol), getX(i), r.Height - (float)(f.Font.GetHeight() * 1.5));
                    }
                }
                else
                {
                    int[] date        = BarMath.Date(bl.Get(i).Bardate);
                    int[] lastbardate = date;
                    if ((i - 1) > 0)
                    {
                        lastbardate = BarMath.Date(bl.Get(i - 1).Bardate);
                    }
                    if ((getX(lastlabelcoord) + minxlabelwidth) <= getX(i))
                    {
                        lastlabelcoord = i;
                        g.DrawString(date[2].ToString(), f.Font, new SolidBrush(fgcol), getX(i), r.Height - (f.Font.GetHeight() * 3));
                    }
                    if ((i == 0) || (lastbardate[1] != date[1]))
                    {
                        string ds = date[1].ToString();
                        if ((i == 0) || (lastbardate[0] != date[0]))
                        {
                            ds += '/' + date[0].ToString();
                        }
                        g.DrawString(ds, f.Font, new SolidBrush(fgcol), getX(i), r.Height - (float)(f.Font.GetHeight() * 1.5));
                    }
                }
            }

            // DRAW YLABELS
            // max number of even intervaled ylabels possible on yaxis
            int numlabels = (int)((r.Height - border * 2) / (f.Font.GetHeight() * 1.5));
            // nearest price units giving "pretty" even intervaled ylabels
            decimal priceunits = NearestPrettyPriceUnits(highesth - lowestl, numlabels);
            // starting price point from low end of range, including lowest low in barlist
            decimal lowstart = lowestl - ((lowestl * 100) % (priceunits * 100)) / 100;
            // original "non-pretty" price units calc
            //decimal priceunits = (highesth-lowestl)/numlabels;
            Pen priceline = new Pen(Color.BlueViolet);

            priceline.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

            for (decimal i = 0; i <= numlabels; i++)
            {
                decimal price = lowstart + (i * priceunits);
                g.DrawString(price.ToString("C"), f.Font, new SolidBrush(fgcol), r.Width - border, getY(price) - f.Font.GetHeight());
                g.DrawLine(priceline, border / 3, getY(price), r.Width - border, getY(price));
            }
            DrawLabels();
        }