/// <summary> /// /// </summary> /// <param name="side"></param> /// <param name="retracement"></param> /// <returns></returns> protected virtual Tuple <double, double, double> GetOrderPrices(double spread, string side, Thrust thrust) { double entryPrice, stopLossPrice; FibonacciRetracement retracement = (FibonacciRetracement)thrust.Study; if (side == MACC.Constants.SignalSide.Buy) { entryPrice = retracement.LevelPrice(FibonacciLevel.R382) + (0.75 * spread); stopLossPrice = retracement.LevelPrice(FibonacciLevel.R618) - (0.75 * spread); if (thrust.FillZoneReached() && thrust.TakeProfitZoneReached()) { stopLossPrice = retracement.LevelPrice(FibonacciLevel.R500) - (0.75 * spread); } } else { entryPrice = retracement.LevelPrice(FibonacciLevel.R382) - (0.75 * spread); stopLossPrice = retracement.LevelPrice(FibonacciLevel.R618) + (0.75 * spread); if (thrust.FillZoneReached() && thrust.TakeProfitZoneReached()) { stopLossPrice = retracement.LevelPrice(FibonacciLevel.R500) + (0.75 * spread); } } int multiplier = side == MACC.Constants.SignalSide.Buy ? 1 : -1; double r382Price = retracement.LevelPrice(FibonacciLevel.R382); double takeProfitPrice = r382Price + (0.618 * Math.Abs(retracement.FocusPrice - r382Price) * multiplier); var orderPrices = Tuple.Create <double, double, double>(entryPrice, stopLossPrice, takeProfitPrice); return(orderPrices); }
/// <summary> /// /// </summary> /// <param name="chart"></param> /// <param name="side"></param> /// <param name="state"></param> /// <returns></returns> public virtual double?GetAdjustedStopLossPrice(Chart chart, IParameters parameters, IDictionary <string, object> state) { string side; Thrust thrust; bool hasProfit; #region state validation string stateItem = ""; try { stateItem = "side"; side = (string)state[stateItem]; stateItem = "thrust"; thrust = (Thrust)state[stateItem]; stateItem = "hasProfit"; hasProfit = Convert.ToBoolean(state[stateItem]); } catch (Exception ex) { throw new ArgumentException(string.Format("State argument is missing or invalid: {0}.", stateItem), ex); } #endregion #region logic // if a buy ... // move the stop to the lower of the .500 fib price or [patern lowBid price set by the post-fill price action] // if a sell ... // move the stop to the higher of the .500 fib price or [patern highBid price set by the post-fill price action] #endregion FibonacciRetracement retracement = (FibonacciRetracement)thrust.Study; if (!retracement.ExtremaPrice.HasValue) { return(null); } double?stopLossPrice = null; double r500Price = retracement.LevelPrice(FibonacciLevel.R500); double lastPrice = chart.Frames.Last().Bar.closeMid; double extrema500Delta = 0; double extremaPrice = retracement.ExtremaPrice.Value; double extremaTakeProfitDelta = Math.Abs(thrust.TakeProfitPrice.Value - extremaPrice); double takeProfitZoneReachedExtremaCoefficient = parameters.GetDouble("takeProfitZoneReachedExtremaCoefficient") ?? 0.5; if (side == MACC.Constants.SignalSide.Buy) { extrema500Delta = extremaPrice - r500Price; if (hasProfit) { if (thrust.TakeProfitZoneReached()) { stopLossPrice = extremaPrice + (takeProfitZoneReachedExtremaCoefficient * extremaTakeProfitDelta); } else if (extrema500Delta >= 0 && !thrust.ProfitWindowClosed) { stopLossPrice = null; } else if (extrema500Delta >= 0 && thrust.ProfitWindowClosed) // set to extrema or 500? { stopLossPrice = r500Price; } else if (extrema500Delta < 0 && !thrust.ProfitWindowClosed) { stopLossPrice = extremaPrice; } else // extrema500Delta < 0 && profitWindowClosed .. set to r500 { stopLossPrice = r500Price; } } else { if (thrust.TakeProfitZoneReached()) { stopLossPrice = -1; } if (thrust.ProfitWindowClosed && lastPrice > r500Price) { stopLossPrice = r500Price; } else if (thrust.ProfitWindowClosed && lastPrice <= r500Price) { stopLossPrice = -1; // kill trade } else { stopLossPrice = null; } } if (stopLossPrice.HasValue) { // stopLoss should never move down if (stopLossPrice <= thrust.StopLossPrice) { stopLossPrice = null; } else { stopLossPrice -= chart.HistoricBidAskSpread; } } } else { extrema500Delta = r500Price - extremaPrice; if (hasProfit) { if (thrust.TakeProfitZoneReached()) { stopLossPrice = extremaPrice - (takeProfitZoneReachedExtremaCoefficient * extremaTakeProfitDelta); } else if (extrema500Delta >= 0 && !thrust.ProfitWindowClosed) { stopLossPrice = null; } else if (extrema500Delta >= 0 && thrust.ProfitWindowClosed) { stopLossPrice = extremaPrice; } else if (extrema500Delta < 0 && !thrust.ProfitWindowClosed) { stopLossPrice = extremaPrice; } else // extrema500Delta < 0 && profitWindowClosed { stopLossPrice = r500Price; } } else { if (thrust.TakeProfitZoneReached()) { stopLossPrice = -1; } else if (lastPrice < r500Price && thrust.ProfitWindowClosed) { stopLossPrice = r500Price; } else if (lastPrice >= r500Price && thrust.ProfitWindowClosed) { stopLossPrice = -1; // kill trade .. if not already stopped out } else { stopLossPrice = null; } } if (stopLossPrice.HasValue) { // stopLoss should never move up if (stopLossPrice >= thrust.StopLossPrice) { stopLossPrice = null; } else { stopLossPrice += chart.HistoricBidAskSpread; } } } if (stopLossPrice.HasValue) { stopLossPrice = Math.Round(stopLossPrice.Value, retracement.LevelPlaces()); } return(stopLossPrice); }
protected Thrust SetThrustActive(Chart chart, Thrust thrust, bool isNewThrust) { thrust.Active = true; List <Frame> afterFocusFrames = chart.Frames.Skip(GetFocusIndex(chart, thrust) + 1).ToList(); if (isNewThrust) { // should an order be placed? // thrust range must be >= _percentRange // should some consideration of the time frame happen? // .006 is for hourly // use a lower value for lower time frame scalping? if (Math.Abs(thrust.FocusPrice - thrust.SignalPrice) / thrust.SignalPrice < _minThrustPercentRange) { thrust.Active = false; return(thrust); } if (thrust.ReactionToFocusSpan < _minReactionToFocusSpan) { thrust.Active = false; return(thrust); } if (afterFocusFrames.Count() == 0) { return(thrust); } else { FibonacciRetracement retracement = thrust.Study as FibonacciRetracement; Frame r382ReachedOrExceededFrame = afterFocusFrames.FirstOrDefault(frame => { if (thrust.Direction == EPatternDirection.Up) { if (frame.Bar.lowMid <= retracement.LevelPrice(FibonacciLevel.R382)) { return(true); } } else { if (frame.Bar.highMid >= retracement.LevelPrice(FibonacciLevel.R382)) { return(true); } } return(false); }); thrust.Active = r382ReachedOrExceededFrame == null; return(thrust); } } else { // should the order be cancelled if not yet filled? // if filled, a 'false' from this doesn't matter #region afterFocusAcross3x3Count function Func <EPatternDirection, short> afterFocusAcross3x3Count = (direction) => { short across3x3Count = 0; Frame firstConfirmedAcross3x3 = null; foreach (Frame frame in afterFocusFrames) { IIndicator displaced3x3 = frame.Indicators.First(k => k.Type == IndicatorType.DisplacedMovingAverage && k.ParentOrdinal == 0); if (direction == EPatternDirection.Up) { // grab the first bar below the 3x3 on close if (frame.Bar.closeMid < displaced3x3.Value && firstConfirmedAcross3x3 == null) { firstConfirmedAcross3x3 = frame; continue; } if (firstConfirmedAcross3x3 != null && frame.Bar.lowMid < displaced3x3.Value) { across3x3Count++; } } else { // grab the first bar above the 3x3 on close if (frame.Bar.closeMid > displaced3x3.Value && firstConfirmedAcross3x3 == null) { firstConfirmedAcross3x3 = frame; continue; } if (firstConfirmedAcross3x3 != null && frame.Bar.highMid > displaced3x3.Value) { across3x3Count++; } } } return(across3x3Count); }; #endregion // enforce a reactionToFocusSpan of 9 bars to filter out 'turn' thrusts // turn thrusts happen detection triggers on bars at turns in price // these are new thrusts but need 'seasoning' (ie. >= 9 bars) to be trade worthy // enforce Dinapoli rule on no more than 3 bars across 3x3 after initial closing bar across 3x3 thrust.Active = afterFocusAcross3x3Count(thrust.Direction) <= _maxAfterFocusAcross3x3Count; return(thrust); } }
protected virtual Thrust SetThrustTakeProfitZoneReached(Chart chart, Thrust thrust) { FibonacciRetracement retracement = thrust.Study as FibonacciRetracement; #region did price reach the takeProfit zone? if (thrust.FillZoneReached()) { double? searchTakeProfitPrice = null; List <Frame> searchFrames = null; if (!thrust.TakeProfitZoneReached()) { searchTakeProfitPrice = thrust.TakeProfitPrice.Value; searchFrames = chart.Frames.Skip(thrust.FillZoneReachedIndex.Value + 1).ToList(); } else { if (retracement.ExtremaChanged()) { int multiplier = thrust.Side == MACC.Constants.SignalSide.Buy ? 1 : -1; double r382Price = retracement.LevelPrice(FibonacciLevel.R382); searchTakeProfitPrice = r382Price + (0.618 * Math.Abs(retracement.FocusPrice - r382Price) * multiplier); searchFrames = chart.Frames.Skip(retracement.ExtremaIndex.Value + 1).ToList(); } } Frame takeProfitZoneReachedFrame = searchFrames.FirstOrDefault(frame => { double extremaToTakeProfitZoneDelta = _takeProfitZoneReachedCoefficient * Math.Abs(searchTakeProfitPrice.Value - retracement.ExtremaPrice.Value); bool takeProfitZoneReached = false; if (thrust.Direction == EPatternDirection.Up) { takeProfitZoneReached = (frame.Bar.highMid >= retracement.ExtremaPrice.Value + extremaToTakeProfitZoneDelta); } else { takeProfitZoneReached = (frame.Bar.lowMid <= retracement.ExtremaPrice.Value - extremaToTakeProfitZoneDelta); } if (takeProfitZoneReached) { thrust.TakeProfitZoneReachedIndex = chart.Frames.IndexOf(frame); thrust.TakeProfitZoneReachedFocusPrice = thrust.FocusPrice; return(true); } return(false); }); } #endregion // is it a breakout? if (thrust.TakeProfitZoneReached()) { double breakoutDelta = thrust.SignalPrice * _focusBreakoutCoefficient * _minThrustPercentRange; if (thrust.Direction == EPatternDirection.Up) { thrust.Breakout = thrust.FocusPrice >= thrust.TakeProfitZoneReachedFocusPrice.Value + breakoutDelta; } else { thrust.Breakout = thrust.FocusPrice <= thrust.TakeProfitZoneReachedFocusPrice.Value - breakoutDelta; } if (thrust.Breakout) { thrust.FillZoneReachedIndex = null; thrust.TakeProfitZoneReachedIndex = null; thrust.TakeProfitZoneReachedFocusPrice = null; } } return(thrust); }