Exemplo n.º 1
0
        void tl_gotTick(Tick t)
        {

            if (archivetickbox.Checked)
                ta.Save(t);
            if (!barlist.ContainsKey(t.sym)) barlist.Add(t.sym, new BarList(BarInterval.FiveMin, t.sym));
            else barlist[t.sym].newTick(t);

            if (boxlist.ContainsKey(t.sym) && (boxlist[t.sym] != null))
            {

                Box b = boxlist[t.sym];
                BoxInfo bi = new BoxInfo();
                Position p = new Position(t.sym);
                try
                {
                    p = poslist[t.sym];
                }
                catch (KeyNotFoundException) { }

                    
                Order o = b.Trade(t, barlist[t.sym], p, bi);
                o.Security = seclist[t.sym].Type;
                o.Exchange = seclist[t.sym].DestEx;
                o.LocalSymbol = seclist[t.sym].Name;
                tl.SendOrder(o);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Create a box-info instance from an existing BarList
 /// </summary>
 /// <param name="bl">The barlist.</param>
 /// <returns>a BoxInfo</returns>
 public static BoxInfo FromBarList(BarList bl)
 {
     BoxInfo bi = new BoxInfo();
     if (!bl.HasBar()) return bi;
     bi.High = BarMath.HH(bl);
     bi.Low = BarMath.LL(bl);
     bi.Open = bl[0].Open;
     return bi;
 }
Exemplo n.º 3
0
 protected override Order ReadOrder(Tick tick, BarList bl, BoxInfo boxinfo)
 {
     if (tick.sec % 5 != 0) return new Order();
     if (buyids.Count > 0)
         CancelOrders(true);
     else
         return new BuyLimit(Symbol, MinSize, 1);
     return new Order();
 }
Exemplo n.º 4
0
        /// <summary>
        /// Create a box-info instance from an existing BarList
        /// </summary>
        /// <param name="bl">The barlist.</param>
        /// <returns>a BoxInfo</returns>
        public static BoxInfo FromBarList(BarList bl)
        {
            BoxInfo bi = new BoxInfo();

            if (!bl.HasBar())
            {
                return(bi);
            }
            bi.High = BarMath.HH(bl);
            bi.Low  = BarMath.LL(bl);
            bi.Open = bl[0].Open;
            return(bi);
        }
Exemplo n.º 5
0
        protected override int Read(Tick tick, BarList bl,BoxInfo bi)
        {
            if (tick.isTrade)
            {
                sum += tick.trade;
                ticks++;
                MA = sum/ticks;

                if (((tick.time - starttime) % secperint) == 0) starttime = tick.time;
                if (starttime==0) return 0;

                bool pricecross = (((PosSize > 0) && (tick.trade < MA)) || ((PosSize < 0) && (tick.trade > MA)));
                return  (pricecross) ? Norm2Min(Flat*exitpercent) : 0;
            }
            return 0;
           
        }
Exemplo n.º 6
0
        const decimal s = -.11m; // stop loss
        
        // here are the trading rules that implement our strategy's intention
        protected override int Read(Tick tick, BarList bl,BoxInfo bi)
        {
            // indicator setup
            bars = bl;
            if (!bl.Has(2)) return 0; // must have at least one one bar

            // asymmetry tests
            if (((h - o) > a) && ((o - l) > a)) { Shutdown("Not enough Asymetry to trade."); return 0; }
            if ((h - l) < r) return 0; // must have the range
            if (((h-o)<=a) && ((h-tick.trade)>e)) return MaxSize;
            if (((o-l)<=a) && ((tick.trade-l)>e)) return MaxSize*-1;

            // profit and loss tests
            decimal PL = BoxMath.OpenPT(tick.trade, AvgPrice, PosSize);
            if (PL > p) return Flat;
            if (PL < s) return Flat;

            return 0;
        }
Exemplo n.º 7
0
        protected override int Read(Tick t, BarList barlist, BoxInfo bi)
        {
            this.tick = new Tick(t); // save tick to member for child classes
            this.bl   = barlist;     // save bars for same purpose
            _boxinfo  = bi;

            int adjust = 0;

            if (newTrade())
            {
                getStop();
            }
            else if (PosSize != 0) // if we have entry price, check exits
            {
                if (!IgnoreTick())
                {
                    checkMoveStop();
                }
                if (!IgnoreTick() && this.Exit())
                {
                    adjust = PosSize * -1;
                }
                else if (this.hitProfit())
                {
                    adjust = (PosSize > 0) ? ProfitSize : ProfitSize * -1;
                }
                else if (this.hitStop())
                {
                    adjust = PosSize * -1;
                }
            }
            else if (IgnoreTick())
            {
                adjust = 0;
            }
            else if (this.Enter()) // if haven't entered, check entry criteria
            {
                adjust = EntrySize * (Side ? 1 : -1);
            }
            return(adjust);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Called everytime a new tick is received by the box, if QuickOrder is true (default).  Reads from the user a position adjustment as an integer.
 /// </summary>
 /// <param name="tick">The current tick.</param>
 /// <param name="bl">The current barlist.</param>
 /// <param name="boxinfo">The boxinfo.</param>
 /// <returns>The number of shares to adjust the position up or down.</returns>
 protected virtual int Read(Tick tick, BarList bl, BoxInfo boxinfo)
 {
     D("No Read function provided"); return(0);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Called everytime a new tick is received by the box, if QuickOrder is true (default).  Reads from the user a position adjustment as an integer.
 /// </summary>
 /// <param name="tick">The current tick.</param>
 /// <param name="bl">The current barlist.</param>
 /// <param name="boxinfo">The boxinfo.</param>
 /// <returns>The number of shares to adjust the position up or down.</returns>
 protected virtual int Read(Tick tick, BarList bl,BoxInfo boxinfo) { D("No Read function provided"); return 0; }
Exemplo n.º 10
0
 public bool atHigh(BoxInfo bi) { return (isTrade && (trade >= bi.High)); }
Exemplo n.º 11
0
 public bool atLow(BoxInfo bi) { return (isTrade && (trade <= bi.Low)); }
Exemplo n.º 12
0
 public bool atLow(BoxInfo bi)
 {
     return(isTrade && (trade <= bi.Low));
 }
Exemplo n.º 13
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;
        }
Exemplo n.º 14
0
        /// <summary>
        /// Trades specified tick.  This method will call an inherited overridden member Read if QuickOrder is true or ReadOrder if QuickOrder is false.
        /// </summary>
        /// <param name="tick">The tick.</param>
        /// <param name="bl">The barlist.</param>
        /// <param name="pos">The current position in the stock or instrument.</param>
        /// <param name="bi">Other box information.</param>
        /// <returns></returns>
        public Order Trade(Tick tick, BarList bl, Position pos, BoxInfo bi)
        {
            Order o = new Order();

            if (Symbol == null)
            {
                if (tick.sym != "")
                {
                    symbol = tick.sym;
                }
                else
                {
                    throw new Exception("No symbol specified");
                }
                _pos = new Position(tick.sym);
            }
            if (!pos.isValid)
            {
                throw new Exception("Invalid Position Provided to Box" + pos.ToString());
            }
            if (tick.sym != Symbol)
            {
                return(o);
            }
            time = tick.time;
            date = tick.date;
            sec  = tick.sec;


            if ((Time < DayStart) || (Time > DayEnd))
            {
                return(o);                                    // is market open?
            }
            if (Off)
            {
                return(o);     // don't trade if shutdown
            }
            if (TradeCaps && pos.Flat && ((this.turns >= MAXTRADES) || (this.adjusts >= MAXADJUSTS)))
            {
                this.Shutdown("Trade limit reached.");
                return(o);
            }
            _pos = pos;

            if (QuickOrder) // user providing only size adjustment
            {
                // get our adjustment
                int adjust = this.Read(tick, bl, bi);

                // convert adjustment to an order
                o = this.Adjust(adjust);
            }
            else // user providing a complete order, so get it
            {
                o = ReadOrder(tick, bl, bi);
            }

            if (!OrdersAllowed) // if we're not allowed, mark order as invalid
            {
                o = new Order();
            }

            //flat us at the close
            if (!_sentshut && (Time >= (DayEnd - DayEndBuff)))
            {
                o      = this.Adjust(Flat);
                o.time = Time;
                o.date = Date;
                this.Shutdown("end-of-day");
                _sentshut = true;
            }
            if (o.isValid)
            {
                // if it's a valid order it counts as an adjustment
                adjusts++;
                // if we're going to flat from non-flat, this is a "trade"
                if ((Math.Abs(PosSize + o.SignedSize) == 0) && (PosSize != 0))
                {
                    turns++;
                }

                // final prep for good orders
                _expectedpossize += o.SignedSize;
                o.time            = Time;
                o.date            = Date;
            }

            if (o.isValid)
            {
                this.D("Sent order: " + o);
            }
            return(o); // send our order
        }
Exemplo n.º 15
0
 public bool atHigh(BoxInfo bi)
 {
     return(isTrade && (trade >= bi.High));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Called everytime a new tick is received by the box, if QuickOrder is false.  Reads from the user a position adjustment as an order.
 /// </summary>
 /// <param name="tick">The current tick.</param>
 /// <param name="bl">The current barlist.</param>
 /// <param name="boxinfo">The boxinfo.</param>
 /// <returns>The number of shares to adjust the position up or down.</returns>        protected virtual Order ReadOrder(Tick tick, BarList bl,BoxInfo boxinfo) { D("No ReadOrder function provided.");  return new Order(); }
 protected virtual Order ReadOrder(Tick tick, BarList bl, BoxInfo boxinfo) { D("No ReadOrder function provided."); return new Order(); }
Exemplo n.º 17
0
 /// <summary>
 /// Called everytime a new tick is received by the box, if QuickOrder is false.  Reads from the user a position adjustment as an order.
 /// </summary>
 /// <param name="tick">The current tick.</param>
 /// <param name="bl">The current barlist.</param>
 /// <param name="boxinfo">The boxinfo.</param>
 /// <returns>The number of shares to adjust the position up or down.</returns>        protected virtual Order ReadOrder(Tick tick, BarList bl,BoxInfo boxinfo) { D("No ReadOrder function provided.");  return new Order(); }
 protected virtual Order ReadOrder(Tick tick, BarList bl, BoxInfo boxinfo)
 {
     D("No ReadOrder function provided."); return(new Order());
 }
Exemplo n.º 18
0
        /// <summary>
        /// Trades specified tick.  This method will call an inherited overridden member Read if QuickOrder is true or ReadOrder if QuickOrder is false.
        /// </summary>
        /// <param name="tick">The tick.</param>
        /// <param name="bl">The barlist.</param>
        /// <param name="pos">The current position in the stock or instrument.</param>
        /// <param name="bi">Other box information.</param>
        /// <returns></returns>
        public Order Trade(Tick tick,BarList bl, Position pos,BoxInfo bi)
        {
            Order o = new Order();
            if (Symbol == null)
            {
                if (tick.sym != "") symbol = tick.sym;
                else throw new Exception("No symbol specified");
                _pos = new Position(tick.sym);
            }
            if (!pos.isValid) throw new Exception("Invalid Position Provided to Box" + pos.ToString());
            if (tick.sym != Symbol) return o;
            time = tick.time;
            date = tick.date;
            sec = tick.sec;

           
            if ((Time < DayStart) || (Time>DayEnd)) return o; // is market open?
            if (Off) return o; // don't trade if shutdown
            if (TradeCaps &&  pos.Flat && ((this.turns >= MAXTRADES) || (this.adjusts >= MAXADJUSTS)))
            {
                this.Shutdown("Trade limit reached.");
                return o;
            }
            _pos = pos;

            if (QuickOrder) // user providing only size adjustment
            {
                // get our adjustment
                int adjust = this.Read(tick, bl,bi);  

                // convert adjustment to an order
                o = this.Adjust(adjust); 
            }
            else // user providing a complete order, so get it
            {
                o = ReadOrder(tick, bl,bi);
            }

            if (!OrdersAllowed) // if we're not allowed, mark order as invalid
                o = new Order();

            //flat us at the close
            if (!_sentshut && (Time >= (DayEnd - DayEndBuff)))
            {
                o = this.Adjust(Flat);
                o.time = Time;
                o.date = Date;
                this.Shutdown("end-of-day");
                _sentshut = true;
            }
            if (o.isValid)
            {
                // if it's a valid order it counts as an adjustment
                adjusts++;
                // if we're going to flat from non-flat, this is a "trade"
                if ((Math.Abs(PosSize + o.SignedSize) == 0) && (PosSize != 0)) 
                    turns++;

                // final prep for good orders
                _expectedpossize += o.SignedSize;
                o.time = Time;
                o.date = Date;
            }
            
            if (o.isValid) this.D("Sent order: " + o);
            return o; // send our order
        }
Exemplo n.º 19
0
        protected override int Read(Tick t,BarList barlist,BoxInfo bi)
        {
            this.tick = new Tick(t); // save tick to member for child classes
            this.bl = barlist; // save bars for same purpose
            _boxinfo = bi;

            int adjust = 0;
            if (newTrade()) getStop(); 
            else if (PosSize != 0) // if we have entry price, check exits
            {
                if (!IgnoreTick()) checkMoveStop();
                if (!IgnoreTick() && this.Exit()) adjust = PosSize * -1;
                else if (this.hitProfit()) adjust = (PosSize > 0) ? ProfitSize : ProfitSize * -1;
                else if (this.hitStop()) adjust = PosSize * -1;
            }
            else if (IgnoreTick()) adjust = 0;
            else if (this.Enter()) // if haven't entered, check entry criteria
                adjust = EntrySize * (Side ? 1 : -1);
            return adjust;
        }
Exemplo n.º 20
0
 protected override int  Read(Tick tick, BarList bl, BoxInfo boxinfo)
 {
     D("entering");
     return MinSize;
 }
Exemplo n.º 21
0
            protected override Order ReadOrder(Tick tick, BarList bl, BoxInfo boxinfo)
            {
                Order o = new Order();

                if (orders == 0)
                {
                    o = new LimitOrder(s, true, 200, 10);
                }
                if (orders==1)
                    CancelOrders(true);
                if (o.isValid)
                    orders++;
                return o;
            }
Exemplo n.º 22
0
 protected override int Read(Tick tick, BarList bl, BoxInfo boxinfo)
 {
     // go short off first trade
     if (tick.isTrade && (PosSize == 0)) return MinSize;
     // cover at the next opportunity
     else if (tick.isTrade && (PosSize != 0))
     {
         Shutdown("All done for today");
         return Flat;
     }
     return 0;
 }