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); } }
public int HeartBeat() { return 1; } //TLSend(TL2.HEARTBEAT); } // server to clients public void newTick(Tick t) { if (t.sym == "") return; // can't process symbol-less ticks for (int i = 0; i < client.Count; i++) // send tick to each client that has subscribed to tick's stock if ((client[i] != null) && (stocks[i].Contains(t.sym))) TLSend(client[i], new LinkMessage(TL2.TICKNOTIFY, t)); }
public bool newTick ( Tick t ) { if (isbarcons) throw new Exception("You can't call newTick method without using the right constructor"); if (t.isTrade) { count++; l.Add(t.trade); 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; }
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(); }
// server to clients /// <summary> /// Notifies subscribed clients of a new tick. /// </summary> /// <param name="tick">The tick to include in the notification.</param> public void newTick(Tick tick) { if (this.InvokeRequired) this.Invoke(new TickDelegate(newTick), new object[] { tick }); else { if (!tick.isValid) return; // need a valid tick for (int i = 0; i < client.Count; i++) // send tick to each client that has subscribed to tick's stock if ((client[i] != null) && (stocks[i].Contains(tick.sym))) WMUtil.SendMsg(tick.Serialize(), TL2.TICKNOTIFY, Handle, client[i]); } }
/// <summary> /// Accepts the specified tick. /// </summary> /// <param name="t">The tick you want to add to the bar.</param> /// <returns>true if the tick is accepted, false if it belongs to another bar.</returns> public bool newTick(Tick t) { if (bartime == 0) { bartime = BarTime(t.time); bardate = t.date;} if (bardate != t.date) DAYEND = true; else DAYEND = false; if ((BarTime(t.time) != bartime) || (bardate!=t.date)) return false; // not our tick if (!t.isTrade) return true; // if it's not a trade, accept tick but don't update anything tradesinbar++; // count it v += t.size; // add trade size to bar volume if (o == 0) o = t.trade; if (t.trade > h) h = t.trade; if (t.trade < l) l = t.trade; c = t.trade; return true; }
public Tick(Tick c) { if (c.sym!="") sym = c.sym; time = c.time; date = c.date; sec = c.sec; size = c.size; trade = c.trade; bid = c.bid; ask = c.ask; bs = c.bs; os = c.os; be = c.be; oe = c.oe; ex = c.ex; }
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; }
public bool Watch(Tick tick, int NoTicksAlertWait) { long last = Util.ToDateTime(tick.date, tick.time, tick.sec).Ticks; if (!_last.ContainsKey(tick.sym)) { _last.Add(tick.sym, last); if (_alertonfirst) // if we're notifying when first tick arrives, do it. if (FirstTick != null) FirstTick(new Stock(tick.sym, Util.ToTLDate(_last[tick.sym]))); return true; } TimeSpan span = new TimeSpan(Util.ToDateTime(tick.date, tick.time, tick.sec).Ticks - _last[tick.sym]); bool alert = span.TotalSeconds>NoTicksAlertWait; _last[tick.sym] = last; if (alert && (Alerted!=null)) Alerted(new Stock(tick.sym,tick.date)); return !alert; }
public void Basics() { const string sym = "TST"; const int d = 20080509; const int t = 935; const string x = "NYSE"; Tick[] ticklist = new Tick[] { Tick.NewTrade(sym,d,t,0,10,100,x), Tick.NewTrade(sym,d,t+1,0,10,100,x), Tick.NewTrade(sym,d,t+2,0,10,100,x), Tick.NewTrade(sym,d,t+3,0,10,100,x), Tick.NewTrade(sym,d,t+4,0,15,100,x), // blade up Tick.NewTrade(sym,d,t+5,0,16,100,x), // new bar (blades reset) Tick.NewTrade(sym,d,t+6,0,16,100,x), Tick.NewTrade(sym,d,t+7,0,10,100,x), // blade down Tick.NewTrade(sym,d,t+7,10,10,100,x), // still a blade down (same bar) Tick.NewTrade(sym,d,t+8,0,15,100,x), Tick.NewTrade(sym,d,t+15,0,15,800,x), // volume spike Tick.NewTrade(sym,d,t+20,0,15,100,x), Tick.NewTrade(sym,d,t+25,0,15,100,x), }; BarList bl = new BarList(BarInterval.FiveMin,sym); Blade b = new Blade(); Assert.That(b.BladePercentage != 0); b = new Blade(.2m); // 20 percent move is a blade int up=0,down=0,newbar=0,bigvol=0; foreach (Tick k in ticklist) { bl.AddTick(k); b.newBar(bl); if (bl.NewBar) newbar++; if (b.isBladeUP) up++; if (b.isBladeDOWN) down++; if (b.isBigVolume) bigvol++; } Assert.That(up == 1,up.ToString()); Assert.That(down == 2,down.ToString()); Assert.That(newbar == 5,newbar.ToString()); Assert.That(bigvol == 1,bigvol.ToString()); }
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; }
public void newTick(Tick tick) { if (this.InvokeRequired) this.Invoke(new TickDelegate(newTick), new object[] { tick }); else { if ((tick == null) || (tick.sym != work.symbol)) return; if (touched) return; decimal changedval = obuybut.Checked ? (limitbut.Checked ? tick.ask : tick.bid) : (limitbut.Checked ? tick.bid : tick.ask); if (changedval != 0) { if (this.oprice.InvokeRequired) { SetTickCallBack d = new SetTickCallBack(newTick); this.Invoke(d, new object[] { tick }); } else oprice.Value = (decimal)changedval; } } }
public void QuoteOnlyTest() { Tick[] timesales = new Tick[] { Tick.NewBid("TST",100m,100), Tick.NewAsk("TST",100.1m,200), }; Blade b = new Blade(); BarList bl = new BarList(BarInterval.FiveMin,"TST"); foreach (Tick k in timesales) { bl.newTick(k); b.newBar(bl); } // average volume should be zero bc // with only quotes we should have no bars to process Assert.That(b.AvgVol(bl) == 0, b.AvgVol(bl).ToString()); Assert.That(!bl.Has(1), bl.ToString()); }
public void Basics() { Tick t = new Tick(); Assert.That(!t.isValid); t.sym = "IBM"; t.size = 100; t.trade = 1; Assert.That(t.isValid); Assert.That(t.isTrade); Assert.That(!t.isQuote); t = new Tick("TST"); t.TradeSize = 100; Assert.That(t.TradeSize == t.ts * 100, t.TradeSize.ToString()); t = new Tick("TST"); t.BidSize = 200; Assert.That(t.BidSize == t.bs * 100, t.BidSize.ToString()); t = new Tick("TST"); t.AskSize = 300; Assert.That(t.AskSize == t.os*100, t.AskSize.ToString()); }
public void WatcherTest() { // create a new tick watcher TickWatcher tw = new TickWatcher(); tw.Alerted += new StockDelegate(test_alert); const string sym = "TST"; const int y = 2008; const int m = 1; const int d = 1; int date = Util.ToTLDate(new DateTime(y, m, d)); Tick t = Tick.NewTrade(sym,date,1300,0,100m,100,""); // watch this stock and supply a watch time // we have no previous updates to no whether to alert on, // so it returns true Assert.That(tw.Watch(t, 300)); tw.DefaultWait = 300; Assert.That(tw.DefaultWait == 300); Tick t2 = new Tick(t); t2.time = 1304; t2.sec = 58; // this should succeed bc it's within the window Assert.That(tw.Watch(t2)); // this time check should send no alerts bc it's w/in window tw.SendAlerts(new DateTime(y,m,d, 13, 7, 0)); Tick t3 = new Tick(t2); t3.time = 1310; // this should return false and send an alert Assert.That(!tw.Watch(t3)); // this timecheck is outside the window, should alert DateTime iswaylate = new DateTime(y,m,d, 14, 0, 0); tw.SendAlerts(iswaylate); Assert.That(alertssent == 2); // here's our alert check }
bool SaveTick(Tick t) { if ((t.sym==null) || (t.sym=="")) return false; if (filedict.ContainsKey(t.sym)) { try { filedict[t.sym].WriteLine(eSigTick.ToEPF(t)); } catch (IOException) { return false; } } else { string fn = _path + @"/" + t.sym + t.date + ".EPF"; bool hasheader = false; try { if (File.Exists(fn)) { StreamReader sr = new StreamReader(fn); Stock s = eSigTick.InitEpf(sr); if (s.isValid) hasheader = true; sr.Close(); } filedict.Add(t.sym, new StreamWriter(fn, true)); if (!hasheader) filedict[t.sym].Write(eSigTick.EPFheader(t.sym, t.date)); filedict[t.sym].WriteLine(eSigTick.ToEPF(t)); } catch (IOException) { return false; } catch (Exception) { return false; } } return true; }
/// <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); }
void gotMessage(object sender, ReceiveCompletedEventArgs e) { TL2 mt = TL2.INFO; string from = ""; LinkMessage gotmess = null; object m = null; gotmess = (LinkMessage)e.Message.Body; mt = gotmess.Type; m = gotmess.Body; from = gotmess.From; int cid = -1; Tick t = new Tick(); try { switch (mt) { // CLIENT MESSAGES case TL2.TICKNOTIFY: t = (Tick)m; gotTick(t); break; case TL2.EXECUTENOTIFY: Trade trade; trade = (Trade)m; gotFill(trade); break; // SERVER MESSAGES case TL2.REGISTERCLIENT: if (client.IndexOf(from) != -1) break; // we already had your client, ignore client.Add(from); stocks.Add(""); heart.Add(new DateTime()); break; case TL2.REGISTERSTOCK: cid = client.IndexOf(from); if (cid == -1) break; // client not found MarketBasket mb = (MarketBasket)m; stocks[cid] = mb.ToString(); BeatHeart(from); break; case TL2.HEARTBEAT: BeatHeart(from); break; case TL2.CLEARSTOCKS: cid = client.IndexOf(from); if (cid == -1) break; stocks[cid] = null; BeatHeart(from); break; case TL2.CLEARCLIENT: cid = client.IndexOf(from); if (cid == -1) break; // we don't have the client, nothing to do client[cid] = null; stocks[cid] = null; heart[cid] = new DateTime(); break; } } catch (Exception ex) { string s = ex.Message; } if (this.GotMessage != null) GotMessage(mt, from); // send GotMessage event to any subscribers rec.BeginReceive(); // prepare to receive new messages }
public void Fill(Tick t) { this.xprice = t.trade; this.xsize = this.size; this.xtime = t.time; this.xdate = t.date; }
protected override void WndProc(ref System.Windows.Forms.Message m) { long result = 0; TradeLinkMessage tlm = WMUtil.ToTradeLinkMessage(ref m); if (tlm == null) // if it's not a WM_COPYDATA message { base.WndProc(ref m); // let form process it return; // we're done } string msg = tlm.body; string[] r = msg.Split(','); switch (tlm.type) { case TL2.ORDERCANCELRESPONSE: if (gotOrderCancel != null) { gotOrderCancel(Convert.ToUInt32(msg)); } break; case TL2.TICKNOTIFY: if (Index.isIdx(r[(int)TickField.symbol])) { // we got an index update Index i = Index.Deserialize(msg); if (gotIndexTick != null) { gotIndexTick(i); } break; } Tick t = Tick.Deserialize(msg); if (t.isTrade) { try { if (t.trade > chighs[t.sym]) { chighs[t.sym] = t.trade; } if (t.trade < clows[t.sym]) { clows[t.sym] = t.trade; } } catch (KeyNotFoundException) { decimal high = DayHigh(t.sym); decimal low = DayLow(t.sym); chighs.Add(t.sym, high); if (low == 0) { low = 640000; } clows.Add(t.sym, low); } } if (gotTick != null) { gotTick(t); } break; case TL2.EXECUTENOTIFY: // date,time,symbol,side,size,price,comment Trade tr = Trade.Deserialize(msg); try { cpos[tr.symbol] = new Position(tr.symbol, AvgPrice(tr.symbol), PosSize(tr.symbol)); } catch (KeyNotFoundException) { cpos.Add(tr.symbol, new Position(tr.symbol, AvgPrice(tr.symbol), PosSize(tr.symbol))); } if (gotFill != null) { gotFill(tr); } break; case TL2.ORDERNOTIFY: Order o = Order.Deserialize(msg); if (gotOrder != null) { gotOrder(o); } break; case TL2.ACCOUNTRESPONSE: if (gotAccounts != null) { gotAccounts(msg); } break; case TL2.FEATURERESPONSE: string[] p = msg.Split(','); List <TL2> f = new List <TL2>(); foreach (string s in p) { try { f.Add((TL2)Convert.ToInt32(s)); } catch (Exception) { } } if (gotSupportedFeatures != null) { gotSupportedFeatures(f.ToArray()); } break; } result = 0; m.Result = (IntPtr)result; }
void tlclient_gotTick(Tick t) { ticks++; }
void tl_gotTick(Tick t) { ta.Save(t); }
/// <summary> /// Watches the specified tick. /// </summary> /// <param name="tick">The tick.</param> /// <returns></returns> public bool Watch(Tick tick) { return(Watch(tick, _defaultwait)); }
public void AddTick(Tick t) { newTick(t); }
public bool Save(Tick t) { return SaveTick(t); }
/// <summary> /// Converts the tick to a string-equivalent that can be written to an EPF file. /// </summary> /// <param name="t">The t.</param> /// <returns></returns> public static string ToEPF(Tick t) { string s = ""; if (!t.isTrade) s = "Q," + epfdate(t.date) + "," + epftime(t.time, t.sec) + "," + t.bid + "," + t.ask + "," + t.bs + "," + t.os + "," + t.be + "," + t.oe; else s = "T," + epfdate(t.date) + "," + epftime(t.time, t.sec) + "," + t.trade + "," + (t.size) + "," + t.ex; return s; }
void gotMessage(object sender, ReceiveCompletedEventArgs e) { TL2 mt = TL2.INFO; string from = ""; LinkMessage gotmess = null; object m = null; gotmess = (LinkMessage)e.Message.Body; mt = gotmess.Type; m = gotmess.Body; from = gotmess.From; int cid = -1; Tick t = new Tick(); try { switch (mt) { // CLIENT MESSAGES case TL2.TICKNOTIFY: t = (Tick)m; gotTick(t); break; case TL2.EXECUTENOTIFY: Trade trade; trade = (Trade)m; gotFill(trade); break; // SERVER MESSAGES case TL2.REGISTERCLIENT: if (client.IndexOf(from) != -1) { break; // we already had your client, ignore } client.Add(from); stocks.Add(""); heart.Add(new DateTime()); break; case TL2.REGISTERSTOCK: cid = client.IndexOf(from); if (cid == -1) { break; // client not found } MarketBasket mb = (MarketBasket)m; stocks[cid] = mb.ToString(); BeatHeart(from); break; case TL2.HEARTBEAT: BeatHeart(from); break; case TL2.CLEARSTOCKS: cid = client.IndexOf(from); if (cid == -1) { break; } stocks[cid] = null; BeatHeart(from); break; case TL2.CLEARCLIENT: cid = client.IndexOf(from); if (cid == -1) { break; // we don't have the client, nothing to do } client[cid] = null; stocks[cid] = null; heart[cid] = new DateTime(); break; } } catch (Exception ex) { string s = ex.Message; } if (this.GotMessage != null) { GotMessage(mt, from); // send GotMessage event to any subscribers } rec.BeginReceive(); // prepare to receive new messages }
public static Tick Deserialize(string msg) { string [] r = msg.Split(','); Tick t = new Tick(); t.sym = r[(int)TickField.symbol]; t.trade = Convert.ToDecimal(r[(int)TickField.trade]); t.size = Convert.ToInt32(r[(int)TickField.tsize]); t.bid = Convert.ToDecimal(r[(int)TickField.bid]); t.ask = Convert.ToDecimal(r[(int)TickField.ask]); t.os = Convert.ToInt32(r[(int)TickField.asksize]); t.bs = Convert.ToInt32(r[(int)TickField.bidsize]); t.ex = r[(int)TickField.tex]; t.be = r[(int)TickField.bidex]; t.oe = r[(int)TickField.askex]; t.time = Convert.ToInt32(r[(int)TickField.time]); t.sec = Convert.ToInt32(r[(int)TickField.sec]); t.date = Convert.ToInt32(r[(int)TickField.date]); return t; }
public static Tick NewQuote(string sym, int date, int time, int sec, decimal bid, decimal ask, int bidsize, int asksize, string be, string oe) { Tick q = new Tick(sym); q.date = date; q.time = time; q.sec = sec; q.bid = bid; q.ask = ask; q.be = be.Trim(); q.oe = oe.Trim(); q.AskSize = asksize; q.BidSize= bidsize; q.trade = 0; q.size = 0; return q; }
public bool Save(Tick t) { return(SaveTick(t)); }
public static Tick NewTrade(string sym, int date, int time, int sec, decimal trade, int size, string ex) { Tick t = new Tick(sym); t.date = date; t.time = time; t.sec = sec; t.trade = trade; t.size = size; t.ex = ex.Trim(); t.bid = 0; return t; }
/// <summary> /// Watches the specified tick. /// </summary> /// <param name="tick">The tick.</param> /// <returns></returns> public bool Watch(Tick tick) { return Watch(tick, _defaultwait); }
/// <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()); }
void kadinamain_KadTick(Tick t) { if ((t.sym == "") || (t.sym!=stock.Symbol)) return; // get stats prior to execution decimal cpl = broker.GetClosedPL(t.sym); decimal cpt = broker.GetClosedPT(t.sym); int x = broker.Execute(t); if (x != 0) // mark tick/row if an execution happened xrows.Add(dt.Rows.Count - 1); nowtime = t.time.ToString() + ":" + t.sec.ToString(); if (bl == null) bl = new BarList(BarInterval.FiveMin, t.sym); bl.AddTick(t); Order o = new Order(); Position mypos = broker.GetOpenPosition(t.sym); if (mybox != null) { o = mybox.Trade(t, bl, mypos, BoxInfo.FromBarList(bl)); mybox_IndicatorUpdate(mybox.Indicators); } if (o.isValid) // mark tick/row if an order happened orows.Add(dt.Rows.Count - 1); broker.sendOrder(o); string flags = ""; flags += o.isValid ? "O" : ""; flags += x > 0 ? "X" : ""; // tick grid NewTRow(new object[] { nowtime, t.trade, t.size, t.bid, t.ask, t.bs, t.os, flags }); // position grid if (x != 0) { // get difference between stats pre and stats post cpl = broker.GetClosedPL(t.sym) - cpl; cpt = broker.GetClosedPT(t.sym) - cpt; ptab.Rows.Add(nowtime, (mypos.Flat ? "FLAT" : (mypos.Side ? "LONG" : "SHORT")), mypos.Size, mypos.AvgPrice, cpl.ToString("C2"),cpt.ToString("N1")); } }
public Tick(Tick a, Tick b) { // this constructor creates a new tick by combining two ticks // this is to handle tick updates that only provide bid/ask changes (like anvil) // a = old tick, b = new tick or tick update if (b.sym != a.sym) return; // don't combine different symbols if (b.time < a.time) return; // don't process old updates time = b.time; date = b.date; sec = b.sec; sym = b.sym; if (b.isTrade) { trade = b.trade; size = b.size; ex = b.ex; // bid = a.bid; ask = a.ask; os = a.os; bs = a.bs; be = a.be; oe = a.oe; } else if (b.hasAsk && b.hasBid) { bid = b.bid; ask = b.ask; bs = b.bs; os = b.os; be = b.be; oe = b.oe; // trade = a.trade; size = a.size; ex = a.ex; } else if (b.hasAsk) { ask = b.ask; os = b.os; oe = b.oe; // bid = a.bid; bs = a.bs; be = a.be; trade = a.trade; size = a.size; ex = a.ex; } else if (b.hasBid) { bid = b.bid; bs = b.bs; be = b.be; // ask = a.ask; os = a.os; oe = a.oe; trade = a.trade; size = a.size; ex = a.ex; } }
/// <summary> /// Adds the tick to the barlist, creates other bars if needed. /// </summary> /// <param name="t">The tick to add.</param> public bool newTick(Tick t) { if ((t.sym != Symbol) && (Symbol=="")) this.sym = t.sym; // if we have no symbol, take ticks symbol else if ((t.sym!=Symbol) && (Symbol!="")) return NewBar; //don't process ticks for other stocks if (!t.isTrade) return NewBar; // don't process quotes // if we have no bars, add bar with a tick if (Count == 0) { minlist.Add(new Bar(BarInterval.Minute)); fivelist.Add(new Bar(BarInterval.FiveMin)); fifteenlist.Add(new Bar(BarInterval.FifteenMin)); hourlist.Add(new Bar(BarInterval.Hour)); daylist.Add(new Bar(BarInterval.Day)); minlist[minlist.Count - 1].newTick(t); fivelist[fivelist.Count - 1].newTick(t); fifteenlist[fifteenlist.Count - 1].newTick(t); hourlist[hourlist.Count - 1].newTick(t); daylist[daylist.Count - 1].newTick(t); } else { // if we have at least a bar, get most current bar BarInterval saveint = Int; foreach (BarInterval inv in Enum.GetValues(typeof(BarInterval))) { Int = inv; Bar cbar = RecentBar; // if tick fits in current bar, then we're done for this interval if (cbar.newTick(t)) continue; else { DefaultBar.Add(new Bar(Int)); // otherwise we need another bar in this interval DefaultBar[DefaultBar.Count - 1].newTick(t); } } Int = saveint; } return NewBar; }
/// <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 }