Exemplo n.º 1
0
        public static double ExitPriceFromOffset(PositionLongShort positionToClose,
                                                 double priceEntered, double priceOffsetSigned)
        {
            switch (positionToClose)
            {
            case PositionLongShort.Long:
                if (priceOffsetSigned > 0)
                {
                    return(priceEntered + priceOffsetSigned);
                }
                throw new Exception("WRONG! PositionPrototype contained negative offset for Long Take Profit");

            case PositionLongShort.Short:
                if (priceOffsetSigned < 0)
                {
                    return(priceEntered + priceOffsetSigned);
                }
                throw new Exception("WRONG! PositionPrototype contained positive offset for Short Take Profit");

            default:
                string msg = " No PositionLongShort[" + positionToClose + "] handler "
                             + "; must be one of those: Long/Short";
                throw new Exception(msg);
            }
        }
Exemplo n.º 2
0
        public Alert EntryAlertCreate(Bar entryBar, double stopOrLimitPrice, string entrySignalName,
                                      Direction direction, MarketLimitStop entryMarketLimitStop)
        {
            this.checkThrowEntryBarIsValid(entryBar);

            double          priceScriptOrStreaming = stopOrLimitPrice;
            OrderSpreadSide orderSpreadSide        = OrderSpreadSide.Unknown;

            if (entryMarketLimitStop == MarketLimitStop.Market)
            {
                priceScriptOrStreaming = this.getStreamingPriceForMarketOrder(entryMarketLimitStop, direction, out orderSpreadSide);
            }

            PositionLongShort longShortFromDirection = MarketConverter.LongShortFromDirection(direction);
            // ALREADY_ALIGNED_AFTER GetAlignedBidOrAskForTidalOrCrossMarketFromStreaming
            double entryPriceScript = entryBar.ParentBars.SymbolInfo.RoundAlertPriceToPriceLevel(
                priceScriptOrStreaming, true, longShortFromDirection, entryMarketLimitStop);

            double shares = this.executor.PositionSizeCalculate(entryBar, entryPriceScript);

            Alert alert = new Alert(entryBar, shares, entryPriceScript, entrySignalName,
                                    direction, entryMarketLimitStop, orderSpreadSide,
                                    //this.executor.Script,
                                    this.executor.Strategy);

            alert.AbsorbFromExecutor(executor);

            return(alert);
        }
Exemplo n.º 3
0
        public override int GetHashCode()
        {
            unchecked {
                int hash = 17;
                hash = hash * 23 + PositionLongShort.GetHashCode();
                hash = hash * 23 + Symbol.GetHashCode();
                hash = hash * 23 + Shares.GetHashCode();
                if (this.EntryFilledBarIndex == -1)
                {
                    return(hash);
                }

                hash = hash * 23 + EntryMarketLimitStop.GetHashCode();
                hash = hash * 23 + EntryFilledBarIndex.GetHashCode();
                hash = hash * 23 + EntryFilledPrice.GetHashCode();
                hash = hash * 23 + EntrySignal.GetHashCode();
                if (this.ExitFilledBarIndex == -1)
                {
                    return(hash);
                }

                hash = hash * 23 + ExitMarketLimitStop.GetHashCode();
                hash = hash * 23 + ExitFilledBarIndex.GetHashCode();
                hash = hash * 23 + ExitFilledPrice.GetHashCode();
                hash = hash * 23 + ExitSignal.GetHashCode();
                return(hash);
            }
        }
Exemplo n.º 4
0
        public double AlignOrderPriceToPriceLevel(double orderPrice, Direction direction, MarketLimitStop marketLimitStop)
        {
            bool buyOrShort = true;
            PositionLongShort positionLongShort = PositionLongShort.Long;

            switch (direction)
            {
            case Direction.Buy:
                buyOrShort        = true;
                positionLongShort = PositionLongShort.Long;
                break;

            case Direction.Sell:
                buyOrShort        = false;
                positionLongShort = PositionLongShort.Long;
                break;

            case Direction.Short:
                buyOrShort        = true;
                positionLongShort = PositionLongShort.Short;
                break;

            case Direction.Cover:
                buyOrShort        = false;
                positionLongShort = PositionLongShort.Short;
                break;

            default:
                throw new Exception("add new handler for new Direction[" + direction + "] besides {Buy,Sell,Cover,Short}");
            }
            return(this.RoundAlertPriceToPriceLevel(orderPrice, buyOrShort, positionLongShort, marketLimitStop));
        }
Exemplo n.º 5
0
        public Alert ExitAlertCreate(Bar exitBar, Position position, double stopOrLimitPrice, string signalName,
                                     Direction direction, MarketLimitStop exitMarketLimitStop)
        {
            this.checkThrowEntryBarIsValid(exitBar);
            this.checkThrowPositionToCloseIsValid(position);

            double          priceScriptOrStreaming = stopOrLimitPrice;
            OrderSpreadSide orderSpreadSide        = OrderSpreadSide.Unknown;

            if (exitMarketLimitStop == MarketLimitStop.Market)
            {
                priceScriptOrStreaming = this.getStreamingPriceForMarketOrder(exitMarketLimitStop, direction, out orderSpreadSide);
            }

            PositionLongShort longShortFromDirection = MarketConverter.LongShortFromDirection(direction);
            double            exitPriceScript        = exitBar.ParentBars.SymbolInfo.RoundAlertPriceToPriceLevel(
                priceScriptOrStreaming, true, longShortFromDirection, exitMarketLimitStop);

            Alert alert = new Alert(exitBar, position.Shares, exitPriceScript, signalName,
                                    direction, exitMarketLimitStop, orderSpreadSide,
                                    //this.executor.Script,
                                    this.executor.Strategy);

            alert.AbsorbFromExecutor(executor);
            alert.PositionAffected = position;
            // moved to CallbackAlertFilled - we can exit by TP or SL - and position has no clue which Alert was filled!!!
            //position.ExitCopyFromAlert(alert);
            alert.PositionAffected.ExitAlertAttach(alert);

            return(alert);
        }
Exemplo n.º 6
0
 protected Position(Bars bars, PositionLongShort positionLongShort, string strategyID, double basisPrice,
                    double shares) : this()
 {
     this.Bars = bars;
     this.PositionLongShort = positionLongShort;
     this.StrategyID        = strategyID;
     this.LastQuoteForMarketOrStopLimitImplicitPrice = basisPrice;
     this.Shares = shares;
 }
Exemplo n.º 7
0
 public PositionPrototype(string symbol, PositionLongShort positionLongShort, double priceEntry,
                          double takeProfitPositiveOffset,
                          double stopLossNegativeOffset, double stopLossActivationNegativeOffset = 0)
 {
     this.Symbol     = symbol;
     this.LongShort  = positionLongShort;
     this.PriceEntry = priceEntry;
     this.SetNewTakeProfitOffset(takeProfitPositiveOffset);
     this.SetNewStopLossOffsets(stopLossNegativeOffset, stopLossActivationNegativeOffset);
 }
		public static Direction ExitDirectionFromLongShort(PositionLongShort positionToClose) {
			switch (positionToClose) {
				case PositionLongShort.Long:
					return Direction.Sell;
				case PositionLongShort.Short:
					return Direction.Cover;
				default:
					string msg = " No PositionLongShort[" + positionToClose + "] handler "
						+ "; must be one of those: Long/Short";
					throw new Exception(msg);
			}
		}
		public static Direction EntryDirectionFromLongShort(PositionLongShort positionLongShort) {
			switch (positionLongShort) {
				case PositionLongShort.Long:
					return Direction.Buy;
				case PositionLongShort.Short:
					return Direction.Short;
				default:
					string msg = " No PositionLongShort[" + positionLongShort + "] handler "
						+ "; must be one of those: Long/Short";
					throw new Exception(msg);
			}
		}
Exemplo n.º 10
0
        public double BidOrAskFor(string Symbol, PositionLongShort direction)
        {
            if (direction == PositionLongShort.Unknown)
            {
                string msg = "BidOrAskFor(" + Symbol + ", " + direction + "): Bid and Ask are wrong to return for [" + direction + "]";
                throw new Exception(msg);
            }
            double price = (direction == PositionLongShort.Long)
                                ? this.BestBidGetForMarketOrder(Symbol) : this.BestAskGetForMarketOrder(Symbol);

            return(price);
        }
		public static double ExitPriceFromOffset(PositionLongShort positionToClose,
				double priceEntered, double priceOffsetSigned) {
			switch (positionToClose) {
				case PositionLongShort.Long:
					if (priceOffsetSigned > 0) return priceEntered + priceOffsetSigned;
					throw new Exception("WRONG! PositionPrototype contained negative offset for Long Take Profit");
				case PositionLongShort.Short:
					if (priceOffsetSigned < 0) return priceEntered + priceOffsetSigned;
					throw new Exception("WRONG! PositionPrototype contained positive offset for Short Take Profit");
				default:
					string msg = " No PositionLongShort[" + positionToClose + "] handler "
						+ "; must be one of those: Long/Short";
					throw new Exception(msg);
			}
		}
Exemplo n.º 12
0
        public static Direction ExitDirectionFromLongShort(PositionLongShort positionToClose)
        {
            switch (positionToClose)
            {
            case PositionLongShort.Long:
                return(Direction.Sell);

            case PositionLongShort.Short:
                return(Direction.Cover);

            default:
                string msg = " No PositionLongShort[" + positionToClose + "] handler "
                             + "; must be one of those: Long/Short";
                throw new Exception(msg);
            }
        }
Exemplo n.º 13
0
        public static Direction EntryDirectionFromLongShort(PositionLongShort positionLongShort)
        {
            switch (positionLongShort)
            {
            case PositionLongShort.Long:
                return(Direction.Buy);

            case PositionLongShort.Short:
                return(Direction.Short);

            default:
                string msg = " No PositionLongShort[" + positionLongShort + "] handler "
                             + "; must be one of those: Long/Short";
                throw new Exception(msg);
            }
        }
		// http://forum.mql4.com/6611
		public static MarketLimitStop EntryMarketLimitStopFromDirection(double priceCurrent,
				double priceExecutionDesired, PositionLongShort positionDesired) {
			if (priceExecutionDesired == 0) return MarketLimitStop.Market;
			if (priceExecutionDesired == priceCurrent) return MarketLimitStop.Market;
			switch (positionDesired) {
				case PositionLongShort.Long:
					if (priceExecutionDesired > priceCurrent) return MarketLimitStop.Stop;
					return MarketLimitStop.Limit;
				case PositionLongShort.Short:
					if (priceExecutionDesired < priceCurrent) return MarketLimitStop.Stop;
					return MarketLimitStop.Limit;
				default:
					string msg = " No PositionLongShort[" + positionDesired + "] handler "
						+ "; must be one of those: Long/Short";
					throw new Exception(msg);
			}
		}
Exemplo n.º 15
0
 public SystemPerformanceSlice(SystemPerformance performance, PositionLongShort positionLongShortImTracking, string reasonToExist) : this()
 {
     parentPerformance           = performance;
     PositionLongShortImTracking = positionLongShortImTracking;
     ReasonToExist = reasonToExist;
 }
Exemplo n.º 16
0
        // http://forum.mql4.com/6611
        public static MarketLimitStop EntryMarketLimitStopFromDirection(double priceCurrent,
                                                                        double priceExecutionDesired, PositionLongShort positionDesired)
        {
            if (priceExecutionDesired == 0)
            {
                return(MarketLimitStop.Market);
            }
            if (priceExecutionDesired == priceCurrent)
            {
                return(MarketLimitStop.Market);
            }
            switch (positionDesired)
            {
            case PositionLongShort.Long:
                if (priceExecutionDesired > priceCurrent)
                {
                    return(MarketLimitStop.Stop);
                }
                return(MarketLimitStop.Limit);

            case PositionLongShort.Short:
                if (priceExecutionDesired < priceCurrent)
                {
                    return(MarketLimitStop.Stop);
                }
                return(MarketLimitStop.Limit);

            default:
                string msg = " No PositionLongShort[" + positionDesired + "] handler "
                             + "; must be one of those: Long/Short";
                throw new Exception(msg);
            }
        }
Exemplo n.º 17
0
		public double RoundAlertPriceToPriceLevel(double orderPrice, bool buyOrShort, PositionLongShort positionLongShort0, MarketLimitStop marketLimitStop0) {
			double d = orderPrice / this.PriceLevelSizeForBonds;
			string a = d.ToString("N6");
			string b = Math.Truncate(d).ToString("N6");
			if (a == b) return orderPrice;

			PriceLevelRoundingMode rounding;
			switch (marketLimitStop0) {
				case MarketLimitStop.Limit:
					if (positionLongShort0 == PositionLongShort.Long) {
						rounding = (buyOrShort ? PriceLevelRoundingMode.RoundDown : PriceLevelRoundingMode.RoundUp);
					} else {
						rounding = (buyOrShort ? PriceLevelRoundingMode.RoundUp : PriceLevelRoundingMode.RoundDown);
					}
					break;
				case MarketLimitStop.Stop:
				case MarketLimitStop.StopLimit:
					if (positionLongShort0 == PositionLongShort.Long) {
						rounding = (buyOrShort ? PriceLevelRoundingMode.RoundUp : PriceLevelRoundingMode.RoundDown);
					} else {
						rounding = (buyOrShort ? PriceLevelRoundingMode.RoundDown : PriceLevelRoundingMode.RoundUp);
					}
					break;
				default:
					rounding = PriceLevelRoundingMode.DontRound;
					break;
			}
			double lowerLevel = Math.Truncate(orderPrice / this.PriceLevelSizeForBonds);
			lowerLevel *= this.PriceLevelSizeForBonds;
			double upperLevel = lowerLevel + this.PriceLevelSizeForBonds;
			switch (rounding) {
				case PriceLevelRoundingMode.RoundDown:
					if (lowerLevel >= upperLevel) {
						return upperLevel;
					}
					return lowerLevel;
				case PriceLevelRoundingMode.RoundUp:
					if (lowerLevel <= upperLevel) {
						return upperLevel;
					}
					return lowerLevel;
				default:
					double distanceUp = Math.Abs(orderPrice - upperLevel);
					double distanceDown = Math.Abs(orderPrice - lowerLevel);
					if (distanceUp >= distanceDown) {
						return upperLevel;
					}
					return lowerLevel;
			}
		}
		public SystemPerformanceSlice(SystemPerformance performance, PositionLongShort positionLongShortImTracking, string reasonToExist) : this() {
			parentPerformance = performance;
			PositionLongShortImTracking = positionLongShortImTracking;
			ReasonToExist = reasonToExist;
		}
Exemplo n.º 19
0
        public double RoundAlertPriceToPriceLevel(double orderPrice, bool buyOrShort, PositionLongShort positionLongShort0, MarketLimitStop marketLimitStop0)
        {
            double d = orderPrice / this.PriceLevelSizeForBonds;
            string a = d.ToString("N6");
            string b = Math.Truncate(d).ToString("N6");

            if (a == b)
            {
                return(orderPrice);
            }

            PriceLevelRoundingMode rounding;

            switch (marketLimitStop0)
            {
            case MarketLimitStop.Limit:
                if (positionLongShort0 == PositionLongShort.Long)
                {
                    rounding = (buyOrShort ? PriceLevelRoundingMode.RoundDown : PriceLevelRoundingMode.RoundUp);
                }
                else
                {
                    rounding = (buyOrShort ? PriceLevelRoundingMode.RoundUp : PriceLevelRoundingMode.RoundDown);
                }
                break;

            case MarketLimitStop.Stop:
            case MarketLimitStop.StopLimit:
                if (positionLongShort0 == PositionLongShort.Long)
                {
                    rounding = (buyOrShort ? PriceLevelRoundingMode.RoundUp : PriceLevelRoundingMode.RoundDown);
                }
                else
                {
                    rounding = (buyOrShort ? PriceLevelRoundingMode.RoundDown : PriceLevelRoundingMode.RoundUp);
                }
                break;

            default:
                rounding = PriceLevelRoundingMode.DontRound;
                break;
            }
            double lowerLevel = Math.Truncate(orderPrice / this.PriceLevelSizeForBonds);

            lowerLevel *= this.PriceLevelSizeForBonds;
            double upperLevel = lowerLevel + this.PriceLevelSizeForBonds;

            switch (rounding)
            {
            case PriceLevelRoundingMode.RoundDown:
                if (lowerLevel >= upperLevel)
                {
                    return(upperLevel);
                }
                return(lowerLevel);

            case PriceLevelRoundingMode.RoundUp:
                if (lowerLevel <= upperLevel)
                {
                    return(upperLevel);
                }
                return(lowerLevel);

            default:
                double distanceUp   = Math.Abs(orderPrice - upperLevel);
                double distanceDown = Math.Abs(orderPrice - lowerLevel);
                if (distanceUp >= distanceDown)
                {
                    return(upperLevel);
                }
                return(lowerLevel);
            }
        }
		public double AlignAlertPriceToPriceLevel(Bars bars, double orderPrice, bool buyOrShort, PositionLongShort positionLongShort0, MarketLimitStop marketLimitStop0) {
			if (this.Strategy.ScriptContextCurrent.NoDecimalRoundingForLimitStopPrice) return orderPrice;
			if (bars == null) bars = this.Bars;
			if (bars.SymbolInfo.PriceLevelSizeForBonds == 0.0) {
				string text = "1";
				if (this.Strategy.ScriptContextCurrent.PriceLevelSizeForBonds > 0) {
					text = text.PadRight(this.Strategy.ScriptContextCurrent.PriceLevelSizeForBonds + 1, '0');
					bars.SymbolInfo.PriceLevelSizeForBonds = 1.0 / (double)Convert.ToInt32(text);
				}
				return orderPrice;
			}
			orderPrice = bars.SymbolInfo.RoundAlertPriceToPriceLevel(orderPrice, buyOrShort, positionLongShort0, marketLimitStop0);
			return orderPrice;
		}
		public double BidOrAskFor(string Symbol, PositionLongShort direction) {
			if (direction == PositionLongShort.Unknown) {
				string msg = "BidOrAskFor(" + Symbol + ", " + direction + "): Bid and Ask are wrong to return for [" + direction + "]";
				throw new Exception(msg);
			}
			double price = (direction == PositionLongShort.Long)
				? this.BestBidGetForMarketOrder(Symbol) : this.BestAskGetForMarketOrder(Symbol);
			return price;
		}
Exemplo n.º 22
0
		protected Position(Bars bars, PositionLongShort positionLongShort, string strategyID, double basisPrice,
				double shares) : this() {
			this.Bars = bars;
			this.PositionLongShort = positionLongShort;
			this.StrategyID = strategyID;
			this.LastQuoteForMarketOrStopLimitImplicitPrice = basisPrice;
			this.Shares = shares;
		}