public void AddPrice(float open, float high, float low, float close, float volume = 0) { PriceBar priceBar = new PriceBar(); priceBar.Write(_timeStamper.Next, open, high, low, close, volume); _priceBars.Add(priceBar); }
private void OnNewPrice(PriceBar price) { // Ensure only one update processed at a time from multi-threaded timer lock (this) { // Update the last price, or append? var ds0 = (IOhlcDataSeries <DateTime, double>)_seriesViewModels[0].DataSeries; var ds1 = (IXyDataSeries <DateTime, double>)_seriesViewModels[1].DataSeries; if (_lastPrice != null && _lastPrice.DateTime == price.DateTime) { ds0.Update(price.DateTime, price.Open, price.High, price.Low, price.Close); ds1.Update(price.DateTime, _sma50.Update(price.Close).Current); } else { ds0.Append(price.DateTime, price.Open, price.High, price.Low, price.Close); ds1.Append(price.DateTime, _sma50.Push(price.Close).Current); // If the latest appending point is inside the viewport (i.e. not off the edge of the screen) // then scroll the viewport 1 bar, to keep the latest bar at the same place if (XVisibleRange.Max > ds0.Count) { var existingRange = _xVisibleRange; var newRange = new IndexRange(existingRange.Min + 1, existingRange.Max + 1); XVisibleRange = newRange; } } _lastPrice = price; } }
public void PriceBar_Write_Works() { //Arrange var priceBars = new PriceBars(10); var priceBar1 = new PriceBar(DateTime.Now.Ticks, 1, 2, 3, 4); var priceBar2 = new PriceBar(); priceBar2.Write(DateTime.Now.Ticks, 1, 2, 3, 4); //Act priceBars.Write(priceBar1.Timestamp, priceBar1.Open, priceBar1.High, priceBar1.Low, priceBar1.Close); priceBars.Write(priceBar2.Timestamp, priceBar2.Open, priceBar2.High, priceBar2.Low, priceBar2.Close); priceBars.Write(priceBar1.Timestamp, priceBar1.Open, priceBar1.High, priceBar1.Low, priceBar1.Close); //Assert Assert.AreEqual(3, priceBars.Length); Assert.AreEqual(priceBars.Current.Timestamp, priceBar1.Timestamp); Assert.AreEqual(priceBars.Current.Open, priceBar1.Open); Assert.AreEqual(priceBars.Current.High, priceBar1.High); Assert.AreEqual(priceBars.Current.Low, priceBar1.Low); Assert.AreEqual(priceBars.Current.Close, priceBar1.Close); Assert.AreEqual(priceBars.Current.Volume, 0); Assert.AreEqual(priceBars.Previous.Timestamp, priceBar2.Timestamp); Assert.AreEqual(priceBars.Previous.Open, priceBar2.Open); Assert.AreEqual(priceBars.Previous.High, priceBar2.High); Assert.AreEqual(priceBars.Previous.Low, priceBar2.Low); Assert.AreEqual(priceBars.Previous.Close, priceBar2.Close); Assert.AreEqual(priceBars.Previous.Volume, 0); }
private PriceBar GetUpdatedData() { double num = _lastPriceBar.Close + ((_random.NextDouble() - 0.48) * (_lastPriceBar.Close / 100.0)); double high = (num > _lastPriceBar.High) ? num : _lastPriceBar.High; double low = (num < _lastPriceBar.Low) ? num : _lastPriceBar.Low; long volumeInc = (long)((_random.NextDouble() * 30000 + 20000) * 0.05); _lastPriceBar = new PriceBar(_lastPriceBar.DateTime, _lastPriceBar.Open, high, low, num, _lastPriceBar.Volume + volumeInc); return(_lastPriceBar); }
protected override Signal GenerateSignal(Security security, DateTime AsOf) { security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Weekly); // Only generate signals after the last close of the week has finalized this bar if (AsOf != Calendar.LastTradingDayOfWeek(AsOf)) { return(null); } // Today's weekly trend PriceBar weeklyBar = security.GetPriceBar(AsOf, PriceBarSize.Weekly); if (weeklyBar == null) { return(null); } TrendQualification weeklyTrend = weeklyBar.GetTrendType(BarCount); // Get the weekly trend from the prior week PriceBar priorWeek = weeklyBar.PriorBar; TrendQualification priorWeekTrend = priorWeek?.GetTrendType(BarCount) ?? TrendQualification.NotSet; if (weeklyTrend == priorWeekTrend) { return(null); } if (weeklyTrend == TrendQualification.AmbivalentSideways || weeklyTrend == TrendQualification.ConfirmedSideways || weeklyTrend == TrendQualification.SuspectSideways) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.CloseIfOpen, 1)); } if (weeklyTrend == TrendQualification.SuspectBullish || weeklyTrend == TrendQualification.ConfirmedBullish) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, 1)); } if (weeklyTrend == TrendQualification.SuspectBearish || weeklyTrend == TrendQualification.ConfirmedBearish) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, 1)); } return(null); }
private void InitData(IMarketDataService marketDataService) { var prices = marketDataService.GetHistoricalData(DefaultPointCount); _lastPrice = prices.Last(); // Populate data series with some data _ohlcDataSeries.Append(prices.Select(x => x.DateTime), prices.Select(x => x.Open), prices.Select(x => x.High), prices.Select(x => x.Low), prices.Select(x => x.Close)); _xyDataSeries.Append(prices.Select(x => x.DateTime), prices.Select(y => _sma50.Push(y.Close).Current)); }
public RandomPricesDataSource(int candleIntervalMinutes, bool simulateDateGap, int updatesPerPrice, int randomSeed, double startingPrice, DateTime startDate) { _candleIntervalMinutes = candleIntervalMinutes; _simulateDateGap = simulateDateGap; _updatesPerPrice = updatesPerPrice; _initialPriceBar = new PriceBarInfo { Close = startingPrice, DateTime = startDate }; _lastPriceBar = new PriceBar(_initialPriceBar.DateTime, _initialPriceBar.Close, _initialPriceBar.Close, _initialPriceBar.Close, _initialPriceBar.Close, 0L); _random = new Random(randomSeed); }
public PriceSeries GetPriceData(string dataset) { if (_dataSets.ContainsKey(dataset)) { return(_dataSets[dataset]); } // e.g. resource format: Abt.Controls.SciChart.Example.Resources.EURUSD_Daily.csv var csvResource = string.Format("{0}.{1}", ResourceDirectory, Path.ChangeExtension(dataset, "csv")); var priceSeries = new PriceSeries(); priceSeries.Symbol = dataset; var assembly = typeof(DataManager).Assembly; // Debug.WriteLine(string.Join(", ", assembly.GetManifestResourceNames())); using (var stream = assembly.GetManifestResourceStream(csvResource)) using (var streamReader = new StreamReader(stream)) { string line = streamReader.ReadLine(); while (line != null) { var priceBar = new PriceBar(); // Line Format: // Date, Open, High, Low, Close, Volume // 2007.07.02 03:30, 1.35310, 1.35310, 1.35280, 1.35310, 12 var tokens = line.Split(','); priceBar.DateTime = DateTime.Parse(tokens[0], DateTimeFormatInfo.InvariantInfo); priceBar.Open = double.Parse(tokens[1], NumberFormatInfo.InvariantInfo); priceBar.High = double.Parse(tokens[2], NumberFormatInfo.InvariantInfo); priceBar.Low = double.Parse(tokens[3], NumberFormatInfo.InvariantInfo); priceBar.Close = double.Parse(tokens[4], NumberFormatInfo.InvariantInfo); priceBar.Volume = long.Parse(tokens[5], NumberFormatInfo.InvariantInfo); priceSeries.Add(priceBar); line = streamReader.ReadLine(); } } _dataSets.Add(dataset, priceSeries); return(priceSeries); }
private PriceBar GetNextRandomPriceBar() { double close = _lastPriceBar.Close; double num = (_random.NextDouble() - 0.9) * _initialPriceBar.Close / 30.0; double num2 = _random.NextDouble(); double num3 = _initialPriceBar.Close + _initialPriceBar.Close / 2.0 * Math.Sin(7.27220521664304E-06 * _currentTime) + _initialPriceBar.Close / 16.0 * Math.Cos(7.27220521664304E-05 * _currentTime) + _initialPriceBar.Close / 32.0 * Math.Sin(7.27220521664304E-05 * (10.0 + num2) * _currentTime) + _initialPriceBar.Close / 64.0 * Math.Cos(7.27220521664304E-05 * (20.0 + num2) * _currentTime) + num; double num4 = Math.Max(close, num3); double num5 = _random.NextDouble() * _initialPriceBar.Close / 100.0; double high = num4 + num5; double num6 = Math.Min(close, num3); double num7 = _random.NextDouble() * _initialPriceBar.Close / 100.0; double low = num6 - num7; long volume = (long)(_random.NextDouble() * 30000 + 20000); DateTime openTime = _simulateDateGap ? EmulateDateGap(_lastPriceBar.DateTime) : _lastPriceBar.DateTime; DateTime closeTime = openTime.AddMinutes(_candleIntervalMinutes); PriceBar candle = new PriceBar(closeTime, close, high, low, num3, volume); _lastPriceBar = new PriceBar(candle.DateTime, candle.Open, candle.High, candle.Low, candle.Close, volume); _currentTime += _candleIntervalMinutes * 60; return(candle); }
public override void InitExampleForUiTest() { base.InitExampleForUiTest(); _marketDataService.ClearSubscriptions(); _xyDataSeries.Clear(); _ohlcDataSeries.Clear(); _sma50.Clear(); _lastPrice = default(PriceBar); var marketDataService = new MarketDataService(new DateTime(2000, 08, 01, 12, 00, 00), 5, 20); // add initizal data InitData(marketDataService); // perform zoom extents to restore default VisibleRange MainSurface.ZoomExtentsX(); // add one price bar to update location of annotations OnNewPrice(marketDataService.GetNextBar()); }
private void OnNewPrice(PriceBar price) { // Update the last price, or append? double smaLastValue; if (_lastPrice.DateTime == price.DateTime) { _ohlcDataSeries.Update(_ohlcDataSeries.Count - 1, price.Open, price.High, price.Low, price.Close); smaLastValue = _sma50.Update(price.Close).Current; _xyDataSeries.UpdateYAt(_xyDataSeries.Count - 1, smaLastValue); } else { _ohlcDataSeries.Append(price.DateTime, price.Open, price.High, price.Low, price.Close); smaLastValue = _sma50.Push(price.Close).Current; _xyDataSeries.Append(price.DateTime, smaLastValue); // If the latest appending point is inside the viewport (i.e. not off the edge of the screen) // then scroll the viewport 1 bar, to keep the latest bar at the same place var visibleRange = MainSurface.XAxes[0].VisibleRange; if (visibleRange.MaxAsDouble > _ohlcDataSeries.Count) { visibleRange.SetMinMaxDouble(visibleRange.MinAsDouble + 1, visibleRange.MaxAsDouble + 1); } } Activity.RunOnUiThread(() => { _ohlcAxisMarker.SetBackgroundColor((price.Close >= price.Open ? StrokeUpColor : StrokeDownColor).ToColor()); }); _ohlcAxisMarker.Y1Value = price.Close; _smaAxisMarker.Y1Value = smaLastValue; _lastPrice = price; }
private void OnNewPrice(PriceBar price) { InvokeOnMainThread(() => { // Update the last price, or append? double smaLastValue; if (_lastPrice.DateTime == price.DateTime) { _ohlcDataSeries.Update(_ohlcDataSeries.Count - 1, price.Open, price.High, price.Low, price.Close); smaLastValue = _sma50.Update(price.Close).Current; _xyDataSeries.UpdateYAt(_xyDataSeries.Count - 1, smaLastValue); } else { _ohlcDataSeries.Append(price.DateTime, price.Open, price.High, price.Low, price.Close); smaLastValue = _sma50.Push(price.Close).Current; _xyDataSeries.Append(price.DateTime, smaLastValue); // If the latest appending point is inside the viewport (i.e. not off the edge of the screen) // then scroll the viewport 1 bar, to keep the latest bar at the same place var xaxis = Runtime.GetNSObject <SCICategoryDateTimeAxis>(MainSurface.XAxes[0].Handle); var visibleRange = Runtime.GetNSObject <SCIDoubleRange>(xaxis.VisibleRange.Handle); if (visibleRange.Max > _ohlcDataSeries.Count) { MainSurface.XAxes[0].VisibleRange = new SCIDoubleRange(visibleRange.Min + 1, visibleRange.Max + 1); } } _ohlcAxisMarker.Style.BackgroundColor = (price.Close >= price.Open ? StrokeUpColor : StrokeDownColor).ToColor(); _ohlcAxisMarker.Position = price.Close; _smaAxisMarker.Position = smaLastValue; _lastPrice = price; }); }
public void HeikenAshiBar_Write_Works() { //Arrange var haBars = new HeikenAshiBars(10); haBars.BarUpdated += delegate(IHeikenAshiBar current) { }; var priceBar1 = new PriceBar(DateTime.Now.Ticks, 1, 2, 3, 4); var priceBar2 = new PriceBar(DateTime.Now.Ticks, 2, 3, 4, 0); var priceBar3 = new PriceBar(); priceBar3.Write(DateTime.Now.Ticks, 1, 2, 3, 4); //Act haBars.Write(priceBar2); haBars.Write(priceBar1); haBars.Write(priceBar3); haBars.Write(priceBar1.Timestamp, priceBar1.Open, priceBar1.High, priceBar1.Low, priceBar1.Close); //Assert Assert.AreEqual(4, haBars.Length); Assert.AreEqual(haBars.Current.Timestamp, priceBar1.Timestamp); Assert.AreEqual(haBars.Previous.Timestamp, priceBar3.Timestamp); }
public void CandlestickBar_Write_Works() { //Arrange var CandlestickBars = new CandlestickBars(1000); CandlestickBars.BarUpdated += delegate(ICandlestickBar current) { }; var priceBar1 = new PriceBar(DateTime.Now.Ticks, 2, 3, 4, 0); var priceBar2 = new PriceBar(); var priceBar3 = new PriceBar(DateTime.Now.Ticks, 1, 2, 3, 4); priceBar2.Write(DateTime.Now.Ticks, priceBar3.Open, priceBar3.High, priceBar3.Low, priceBar3.Close); //Act CandlestickBars.Write(priceBar1); CandlestickBars.Write(priceBar3); CandlestickBars.Write(priceBar2); CandlestickBars.Write(priceBar3.Timestamp, priceBar3.Open, priceBar3.High, priceBar3.Low, priceBar3.Close); //Assert Assert.AreEqual(4, CandlestickBars.Length); Assert.AreEqual(CandlestickBars.Current.Timestamp, priceBar3.Timestamp); Assert.AreEqual(CandlestickBars.Previous.Timestamp, priceBar2.Timestamp); }
public async Task <List <PriceBar> > GetPriceBars(List <Tuple <string, EGranularity> > barSpecsList, int count, ECandleFormat priceFormat = ECandleFormat.midpoint) { List <PriceBar> bars = new List <PriceBar>(); foreach (var spec in barSpecsList) { Func <CandlesRequest> request = () => new CandlesRequest { instrument = spec.Item1, granularity = spec.Item2, candleFormat = priceFormat, count = count }; List <Candle> pollCandles = null; pollCandles = await Rest.GetCandlesAsync(request()); if (pollCandles != null && pollCandles.Count > 0) { pollCandles.OrderBy(c => Convert.ToDateTime(c.time).ToUniversalTime()); foreach (var candle in pollCandles) { var bar = new PriceBar() { instrument = spec.Item1, granularity = spec.Item2.ToString() }; bar.InjectWith(candle, false); bars.Add(bar); } } } return(bars); }
public override Trade NewStoploss(Position position, DateTime AsOf) { position.Security.SetSwingPointsAndTrends(BarCount, BarSize); if (position.ExecutedTrades.Count > 1) { throw new UnknownErrorException(); } // First prior swingpoint is first prior that is REALIZED at the time of signal, so we have to look at least BarCount bars back PriceBar firstPriorSwingPoint = null; switch (BarSize) { case PriceBarSize.Daily: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingDay(AsOf, BarCount), BarSize); break; case PriceBarSize.Weekly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingWeekStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Monthly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Quarterly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; default: break; } decimal stopPrice = -1m; decimal expExecutionPx = position.Security.GetPriceBar(AsOf, BarSize).Close; switch (position.PositionDirection) { case PositionDirection.ShortPosition: // First prior swingpoint high while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointHigh) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.High + .01m; break; case PositionDirection.LongPosition: // First prior swingpoint low while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointLow) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.Low - .01m; break; } // If the last valid swingpoint is on the wrong side of our trade (above exp trade price for a long, below a short), use an ATR atop switch (position.PositionDirection) { case PositionDirection.LongPosition: if (stopPrice >= expExecutionPx) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.NewStoploss(position, AsOf)); } break; case PositionDirection.ShortPosition: if (stopPrice <= expExecutionPx) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.NewStoploss(position, AsOf)); } break; } if (stopPrice <= 0) { stopPrice = 0.01m; } var stop = new Trade( position.Security, (TradeActionBuySell)(-position.PositionDirection.ToInt()), Math.Abs(position.Size(AsOf)), TradeType.Stop, 0, stopPrice) { TradeDate = AsOf }; return(stop); }
private decimal TrailingStop(Position position, Trade currentStop, DateTime AsOf) { // // Get the last price bar of the position // PriceBar bar = position.Security.GetPriceBarOrLastPrior(AsOf, PriceBarSize.Daily, 1); decimal lastClose = bar.Close; // // Get the last ATR, which is the AsOf date // var securityLastAtr = bar.AverageTrueRange(Stoploss_ATR_Period); // // Calculate the stop price baseline // decimal riskDollarsPerShare = (securityLastAtr * Stoploss_ATR_Multiple); decimal stopPrice = (lastClose - (position.PositionDirection.ToInt() * riskDollarsPerShare)); if (stopPrice <= 0) { stopPrice = 0.01m; } // // If a creeping stop is enabled, calculate the new stop price based on change from the existing stop // decimal adjustedBaseStop = currentStop.StopPrice; if (Stoploss_Creep_Percent > 0) { switch (position.PositionDirection) { case PositionDirection.LongPosition: // Increase base stop adjustedBaseStop *= (1 + Stoploss_Creep_Percent); break; case PositionDirection.ShortPosition: // Decrease base stop adjustedBaseStop *= (1 - Stoploss_Creep_Percent); break; } } // // Compare the new baseline stop vs the adjusted prior stop, and select whichever is more conservative // switch (position.PositionDirection) { case PositionDirection.NotSet: throw new UnknownErrorException(); case PositionDirection.LongPosition: return(Math.Max(stopPrice, adjustedBaseStop)); case PositionDirection.ShortPosition: return(Math.Min(stopPrice, adjustedBaseStop)); default: throw new UnknownErrorException(); } }
public static PriceSeries GetPriceDataByRange(List <Tick> ticks, double rangeCount, IDiscontinuousDateTimeCalendar calendar, double barTimeFrame, out Tuple <DateTime, double> min, out Tuple <DateTime, double> max) { var priceSeries = new PriceSeries(); var dateTime = new DateTime(); min = null; max = null; double open = 0; double high = 0; double low = 0; double close = 0; long volume = 0; double range = 0; bool hasTicks = false; for (int i = 0; i < ticks.Count; i++) { if (Math.Abs(range) < rangeCount) { if (!calendar.IsValueInGap(ticks[i].DateTime)) { if (!hasTicks) { high = ticks[i].High; low = ticks[i].Low; open = ticks[i].Open; if (priceSeries.Count == 0) { dateTime = ticks[i].DateTime; } hasTicks = true; } else { high = high < ticks[i].High ? ticks[i].High : high; low = low > ticks[i].Low ? ticks[i].Low : low; } volume += ticks[i].Volume; range = high - low; close = ticks[i].Close > open ? low + rangeCount : high - rangeCount; } } else if (hasTicks) { if (range / rangeCount > 2) { for (int j = 0; j < range / rangeCount; j++) { var priceBar = new PriceBar { DateTime = calendar.GetValueByOffset(dateTime, priceSeries.Count * barTimeFrame), Open = ticks[i].Close > open ? open + j * rangeCount : open - j * rangeCount, High = ticks[i].Close > open ? open + j * rangeCount : open - j * rangeCount, Low = ticks[i].Close > open ? open + j * rangeCount : open - (j + 1) * rangeCount, Close = ticks[i].Close > open ? open + j * rangeCount : open - (j + 1) * rangeCount, Volume = volume }; priceSeries.Add(priceBar); if (priceSeries.Count == 1) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } else { if (priceBar.High > max.Item2) { max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } if (priceBar.Low < min.Item2) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); } } } } else { var priceBar = new PriceBar { DateTime = calendar.GetValueByOffset(dateTime, priceSeries.Count * barTimeFrame), Open = open, High = high, Low = low, Close = close, Volume = volume }; priceSeries.Add(priceBar); if (priceSeries.Count == 1) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } else { if (priceBar.High > max.Item2) { max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } if (priceBar.Low < min.Item2) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); } } } range = 0; hasTicks = false; } } return(priceSeries); }
public override decimal UpdatePositionStoplossPrice(Position position, Trade currentStop, DateTime AsOf) { position.Security.SetSwingPointsAndTrends(BarCount, BarSize); // First prior swingpoint is first prior that is REALIZED at the time of signal, so we have to look at least BarCount bars back PriceBar firstPriorSwingPoint = null; switch (BarSize) { case PriceBarSize.Daily: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingDay(AsOf, BarCount), BarSize); break; case PriceBarSize.Weekly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingWeekStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Monthly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Quarterly: firstPriorSwingPoint = position.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; default: break; } decimal stopPrice = -1m; decimal lastPx = position.Security.GetPriceBar(AsOf, PriceBarSize.Daily).Close; switch (position.PositionDirection) { case PositionDirection.ShortPosition: // First prior swingpoint high while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointHigh) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.High + .01m; break; case PositionDirection.LongPosition: // First prior swingpoint low while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointLow) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.Low - .01m; break; } // If the last valid swingpoint is on the wrong side of our trade (above exp trade price for a long, below a short), use an ATR atop switch (position.PositionDirection) { case PositionDirection.LongPosition: if (stopPrice >= position.Security.GetPriceBar(AsOf, PriceBarSize.Daily).Close) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.UpdatePositionStoplossPrice(position, currentStop, AsOf)); } break; case PositionDirection.ShortPosition: if (stopPrice >= position.Security.GetPriceBar(AsOf, PriceBarSize.Daily).Close) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.UpdatePositionStoplossPrice(position, currentStop, AsOf)); } break; } if (stopPrice <= 0) { stopPrice = 0.01m; } // Calculate the adjusted base stop if we are using a creeping stop decimal adjustedBaseStop = currentStop.StopPrice; if (Stoploss_Creep_Percent > 0) { switch (position.PositionDirection) { case PositionDirection.LongPosition: // Increase base stop adjustedBaseStop *= (1 + Stoploss_Creep_Percent); break; case PositionDirection.ShortPosition: // Decrease base stop adjustedBaseStop *= (1 - Stoploss_Creep_Percent); break; } } switch (position.PositionDirection) { case PositionDirection.LongPosition: return(Math.Max(stopPrice, adjustedBaseStop)); case PositionDirection.ShortPosition: return(Math.Min(stopPrice, adjustedBaseStop)); default: throw new UnknownErrorException(); } }
public static PriceSeries GetPriceDataByTimeFrame(List <Tick> ticks, int timeFrame, IDiscontinuousDateTimeCalendar calendar, out Tuple <DateTime, double> min, out Tuple <DateTime, double> max) { var priceSeries = new PriceSeries(); var dateTime = ticks[0].DateTime; min = null; max = null; double open = 0; double high = 0; double low = 0; double close = 0; long volume = 0; bool hasTicks = false; for (int i = 0; i < ticks.Count; i++) { if (ticks[i].DateTime < dateTime + TimeSpan.FromMinutes(timeFrame)) { if (!calendar.IsValueInGap(ticks[i].DateTime)) { if (!hasTicks) { high = ticks[i].High; low = ticks[i].Low; open = ticks[i].Open; hasTicks = true; } else { high = high < ticks[i].High ? ticks[i].High : high; low = low > ticks[i].Low ? ticks[i].Low : low; } close = ticks[i].Close; volume += ticks[i].Volume; } } else { if (hasTicks) { var priceBar = new PriceBar { DateTime = dateTime, Open = open, High = high, Low = low, Close = close, Volume = volume }; priceSeries.Add(priceBar); if (priceSeries.Count == 1) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } else { if (priceBar.High > max.Item2) { max = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.High); } if (priceBar.Low < min.Item2) { min = new Tuple <DateTime, double>(priceBar.DateTime, priceBar.Low); } } hasTicks = false; } else { dateTime = dateTime + TimeSpan.FromMinutes(timeFrame); } } } return(priceSeries); }
private void Push(PriceBar priceBar) { PushFeed(priceBar.TimeStamp, priceBar.Open, priceBar.High, priceBar.Low, priceBar.Close, priceBar.Volume); _position++; }
/// <summary> /// Handler for OANDA rate data received from data stream. /// </summary> /// <param name="data">The price tick data received.</param> protected virtual void OnRateReceived(RateStreamResponse data) { if (!data.IsHeartbeat()) { if (_chartsInitialized) { string instrument = data.tick.instrument; Chart instrumentChart = _charts.FirstOrDefault(c => c.Value.Instrument == instrument).Value; IPriceBar lastFrameBar = instrumentChart.Frames.Last().Bar; double bidPrice = data.tick.bid; double askPrice = data.tick.ask; double midPrice = (data.tick.ask - data.tick.bid) / 2; if (Convert.ToDateTime(data.tick.time).ToUniversalTime() < instrumentChart.CreateNewFrameTime) { lastFrameBar.volume++; lastFrameBar.closeBid = bidPrice; lastFrameBar.closeAsk = askPrice; lastFrameBar.closeMid = midPrice; if (lastFrameBar.closeBid <= lastFrameBar.lowBid) { lastFrameBar.lowBid = lastFrameBar.closeBid; } if (lastFrameBar.closeAsk <= lastFrameBar.lowAsk) { lastFrameBar.lowAsk = lastFrameBar.closeAsk; } if (lastFrameBar.closeMid <= lastFrameBar.lowMid) { lastFrameBar.lowMid = lastFrameBar.closeMid; } if (lastFrameBar.closeBid >= lastFrameBar.highBid) { lastFrameBar.highBid = lastFrameBar.closeBid; } if (lastFrameBar.closeAsk >= lastFrameBar.highAsk) { lastFrameBar.highAsk = lastFrameBar.closeAsk; } if (lastFrameBar.closeMid >= lastFrameBar.highMid) { lastFrameBar.highMid = lastFrameBar.closeMid; } } else { lastFrameBar.complete = true; PriceBar newBar = new PriceBar() { time = MAOE.Utilities.GetTimeAsXmlSerializedUtc(instrumentChart.CreateNewFrameTime.Value), openBid = bidPrice, highBid = bidPrice, lowBid = bidPrice, closeBid = bidPrice, openAsk = askPrice, highAsk = askPrice, lowAsk = askPrice, closeAsk = askPrice, openMid = midPrice, highMid = midPrice, lowMid = midPrice, closeMid = midPrice, volume = 1 }; instrumentChart.Frames.Add(CreateChartFrame(newBar, instrumentChart)); } } MAOE.Utilities.ReleaseTick(); } }
public override int NewPositionSize(Portfolio portfolio, Signal IndicatedTrade, DateTime AsOf, decimal initialRiskPercent) { IndicatedTrade.Security.SetSwingPointsAndTrends(BarCount, BarSize); decimal riskDollarsTotal = portfolio.EquityWithLoanValue(AsOf, TimeOfDay.MarketEndOfDay) * initialRiskPercent; // First prior swingpoint is first prior that is REALIZED at the time of signal, so we have to look at least BarCount bars back PriceBar firstPriorSwingPoint = null; switch (BarSize) { case PriceBarSize.Daily: firstPriorSwingPoint = IndicatedTrade.Security.GetPriceBar(Calendar.PriorTradingDay(AsOf, BarCount), BarSize); break; case PriceBarSize.Weekly: firstPriorSwingPoint = IndicatedTrade.Security.GetPriceBar(Calendar.PriorTradingWeekStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Monthly: firstPriorSwingPoint = IndicatedTrade.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; case PriceBarSize.Quarterly: firstPriorSwingPoint = IndicatedTrade.Security.GetPriceBar(Calendar.PriorTradingMonthStart(AsOf, BarCount), BarSize); break; default: break; } decimal stopPrice = -1m; decimal expExecutionPx = IndicatedTrade.Security.GetPriceBar(AsOf, BarSize).Close; switch (IndicatedTrade.SignalAction) { case SignalAction.Sell: // First prior swingpoint high while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointHigh) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.High + .01m; break; case SignalAction.Buy: // First prior swingpoint low while ((firstPriorSwingPoint.GetSwingPointType(BarCount) & SwingPointType.SwingPointLow) == 0) { firstPriorSwingPoint = firstPriorSwingPoint.PriorBar; } stopPrice = firstPriorSwingPoint.Low - .01m; break; } // If the last valid swingpoint is on the wrong side of our trade (above exp trade price for a long, below a short), use an ATR atop switch (IndicatedTrade.SignalAction) { case SignalAction.Sell: if (stopPrice >= expExecutionPx) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.NewPositionSize(portfolio, IndicatedTrade, AsOf, initialRiskPercent)); } break; case SignalAction.Buy: if (stopPrice <= expExecutionPx) { BackupStoploss.Stoploss_ATR_Multiple = this.Stoploss_ATR_Multiple; BackupStoploss.Stoploss_ATR_Period = this.Stoploss_ATR_Period; return(BackupStoploss.NewPositionSize(portfolio, IndicatedTrade, AsOf, initialRiskPercent)); } break; } decimal initialBuffer = expExecutionPx * initialRiskPercent; decimal riskDollarsPerShare = Math.Abs(IndicatedTrade.Security.GetPriceBar(AsOf, BarSize).Close - stopPrice); if (riskDollarsPerShare < initialBuffer) { riskDollarsPerShare = initialBuffer; } int positionSize = Convert.ToInt32(Math.Round((riskDollarsTotal / riskDollarsPerShare), 0)); return(positionSize); }
protected override Signal GenerateSignal(Security security, DateTime AsOf) { security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Daily); security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Weekly); // Today's daily trend PriceBar dailyBar = security.GetPriceBar(AsOf, PriceBarSize.Daily); TrendQualification dailyTrend = dailyBar.GetTrendType(BarCount); // Today's weekly trend // We use last week's value until this week is completed on Friday, then we can start using that bar PriceBar weeklyBar = security.GetPriceBar(AsOf, PriceBarSize.Weekly); if (AsOf != Calendar.LastTradingDayOfWeek(AsOf)) { weeklyBar = weeklyBar?.PriorBar; } if (weeklyBar == null) { return(null); } TrendQualification weeklyTrend = weeklyBar.GetTrendType(BarCount); TrendAlignment currentTrendAlignment = GetTrendAlignment(dailyTrend, weeklyTrend); if (currentTrendAlignment == TrendAlignment.Sideways || currentTrendAlignment == TrendAlignment.Opposing) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.CloseIfOpen, 1)); } // Get the trend from the last period if (dailyBar.PriorBar == null) { return(null); } TrendQualification priorDayTrend = dailyBar.PriorBar.GetTrendType(BarCount); // Get the weekly trend from the prior day, which will align with yesterday's daily trend PriceBar priorWeek = security.GetPriceBar(Calendar.PriorTradingDay(AsOf), PriceBarSize.Weekly); if (priorWeek == null) { return(null); } TrendQualification priorWeekTrend = priorWeek.GetTrendType(BarCount); TrendAlignment priorTrendAlignment = GetTrendAlignment(priorDayTrend, priorWeekTrend); if (currentTrendAlignment == priorTrendAlignment) { return(null); } if (currentTrendAlignment == TrendAlignment.Bullish) { if (dailyTrend == TrendQualification.ConfirmedBullish && weeklyTrend == TrendQualification.ConfirmedBullish) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, 1.0)); } //if (dailyTrend == TrendQualification.SuspectBullish && weeklyTrend == TrendQualification.ConfirmedBullish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .75); //if (dailyTrend == TrendQualification.ConfirmedBullish && weeklyTrend == TrendQualification.SuspectBullish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .50); //if (dailyTrend == TrendQualification.SuspectBullish && weeklyTrend == TrendQualification.SuspectBullish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .25); } if (currentTrendAlignment == TrendAlignment.Bearish) { if (dailyTrend == TrendQualification.ConfirmedBearish && weeklyTrend == TrendQualification.ConfirmedBearish) { return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, 1.0)); } //if (dailyTrend == TrendQualification.SuspectBearish && weeklyTrend == TrendQualification.ConfirmedBearish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .75); //if (dailyTrend == TrendQualification.ConfirmedBearish && weeklyTrend == TrendQualification.SuspectBearish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .50); //if (dailyTrend == TrendQualification.SuspectBearish && weeklyTrend == TrendQualification.SuspectBearish) // return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .25); } return(null); }