public void Indicators_Make_ATR_for_one_bar_test() { List <Bar> bars = new List <Bar>(); bars.Add(new Bar(new DateTime(2013, 10, 1), 50, 55, 49, 51, 35)); Assert.AreEqual(6, ATR.Value(bars)); }
public void Indicators_Make_TrueRange_for_two_bars_test() { List <Bar> bars = new List <Bar>(); bars.Add(new Bar(new DateTime(2013, 10, 1), 50, 55, 49, 51, 35)); bars.Add(new Bar(new DateTime(2013, 10, 2), 51, 56, 48, 52, 38)); Assert.AreEqual(7, ATR.Value(bars)); }
public void Indicators_Make_ATR_for_collection() { List <Bar> bars = new List <Bar>(); bars.Add(new Bar(new DateTime(2013, 10, 1), 50, 55, 49, 51, 35)); bars.Add(new Bar(new DateTime(2013, 10, 2), 51, 56, 48, 52, 38)); bars.Add(new Bar(new DateTime(2013, 10, 3), 49, 53, 44, 50, 41)); Assert.AreEqual(7.6667, ATR.Value(bars)); }
public override void OnInit() { if (HasPosition) { //60天日线 DateTime curDate = Clock.Now.Date; ISeriesObject[] dailyBars = Util.GetNDailiesBeforeDate(this.instrument, curDate, 60); Util.AdjustDailys(dailyBars); //向前复权 foreach (Daily dBar in dailyBars) { this.Bar.Add(dBar); } if (this.DailyBar.Count > this.lengthATR) { this.moveStopATR = ATR.Value(this.DailyBar, this.DailyBar.LastIndex, lengthATR, EIndicatorStyle.MetaStock); //昨日ATR } int n = this.DailyBar.Count; int i; for (i = n - 1; i >= 0; i--) { if (this.DailyBar[i].DateTime <= Position.EntryDate.Date) { break; } } if (this.DailyBar[i].DateTime < Position.EntryDate.Date) { i++; } if (i < 0) { i = 0; } this.positionCycle = n - i + 1; for (int j = i; j < n; j++) { if (this.DailyBar[j].High > this.positionMaxPrice) { this.positionMaxPrice = this.DailyBar[j].High; //入场后的最高价格,不包括入场那一天 } } this.calcMoveStopPrice(); int length = i > this.lengthATR?this.lengthATR:i - 1; if (length > 0) { this.stopLossATR = ATR.Value(this.DailyBar, i - 1, length, EIndicatorStyle.MetaStock); //入场前一天的ATR } this.calcStopLossPrice(); //this.judgeStagnant(); Console.WriteLine("投资组合中的证券 {0} :", Position.Instrument.Symbol); Console.WriteLine("持仓周期是 {0},绝对止损价格是 {1},持仓后最大价格是 {2}", this.positionCycle, this.stopLossPrice, this.positionMaxPrice); } }
public void Indicators_Make_ATR_value() { List <Bar> bars = new List <Bar>(); bars.Add(new Bar(new DateTime(2013, 10, 11, 0, 0, 0), 148630, 149220, 148600, 149140, 35)); bars.Add(new Bar(new DateTime(2013, 10, 11, 11, 0, 0), 149170, 149310, 148380, 148800, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 12, 0, 0), 148800, 148810, 147470, 147900, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 13, 0, 0), 147910, 148370, 147800, 148250, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 14, 0, 0), 148240, 148340, 147920, 148130, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 15, 0, 0), 148110, 148290, 148000, 148260, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 16, 0, 0), 148260, 148260, 147940, 147980, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 17, 0, 0), 147990, 148110, 147670, 147680, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 18, 0, 0), 147680, 147940, 147110, 147920, 38)); bars.Add(new Bar(new DateTime(2013, 10, 11, 19, 0, 0), 147920, 148030, 147550, 147810, 38)); Assert.AreEqual(624, ATR.Value(bars)); }
public override void OnInit() { DateTime curDate = Clock.Now.Date; if (!HasPosition) { int length = this.DailyBar.Count > this.lengthATR?this.lengthATR:this.DailyBar.Count - 1; if (length > 0) { this.lastATR = ATR.Value(this.DailyBar, this.DailyBar.LastIndex, length, EIndicatorStyle.MetaStock); } else { this.lastATR = 1; } this.canOrder = true; } this.openTime = curDate.Add(new TimeSpan(9, 30, 0)); this.portfolioRisk = this.portfolioRisk * this.strategy.PositionLevel; }
protected SingleOrder AddPosition(Position position) { if (position != null) { string text = "买进最佳持仓"; Instrument instrument = position.Instrument; BarSeries dailyBars = this.strategy.Bars[instrument, BarType.Time, 86400]; double lastATR = 1.0; if (dailyBars.Count > 5) { lastATR = ATR.Value(dailyBars, dailyBars.LastIndex, 5, EIndicatorStyle.MetaStock); //昨日ATR } int expectQty = (int)(this.strategy.Portfolio.GetTotalEquity() * this.strategy.PositionLevel * 0.01 / lastATR); expectQty = expectQty / 100 * 100; Signal signal; switch (position.Side) { case PositionSide.Long: signal = new Signal(Clock.Now, SignalType.Market, SignalSide.Buy, expectQty, 0, instrument, text); signal.TimeInForce = TimeInForce.Day; return(this.strategy.EmitSignal(signal)); case PositionSide.Short: signal = new Signal(Clock.Now, SignalType.Market, SignalSide.SellShort, expectQty, 0, instrument, text); signal.TimeInForce = TimeInForce.Day; return(this.strategy.EmitSignal(signal)); default: return(null); } } else { return(null); } }