Ejemplo n.º 1
0
        public Alert CreateTakeProfitFromPositionPrototype(Position position)
        {
            PositionPrototype proto = position.Prototype;

            if (proto.TakeProfitPositiveOffset == 0)
            {
                string msg = "What should Activator do with proto.StopLossNegativeOffset=0?";
                throw new Exception(msg);
            }

            Alert alertTakeProfit = executor.SellOrCoverAlertCreateDontRegister(
                executor.Bars.BarStreaming,
                position, proto.PriceTakeProfit,
                "protoTakeProfitExit:" + proto.TakeProfitPositiveOffset
                + "@" + proto.PriceTakeProfit + " for " + position.EntrySignal,
                MarketConverter.ExitDirectionFromLongShort(position.Prototype.LongShort),
                MarketLimitStop.Limit);

            if (alertTakeProfit == null)
            {
                string msg = "alertTakeProfit should NOT be null";
                throw new Exception(msg);
            }
            proto.TakeProfitAlertForAnnihilation = alertTakeProfit;
            return(alertTakeProfit);
        }
Ejemplo n.º 2
0
        public Alert CreateStopLossFromPositionPrototype(Position position)
        {
            //if (this.executor.checkPrototypeAlreadyPlaced(position)) return;
            PositionPrototype proto = position.Prototype;

            if (proto.StopLossNegativeOffset == 0)
            {
                string msg = "What should Activator do with proto.StopLossNegativeOffset=0?";
                throw new Exception(msg);
            }

            if (proto.StopLossNegativeOffset == 0)
            {
                string msg = this.ReasonWhyNewStopLossOffsetDoesntMakeSense(position, proto.StopLossNegativeOffset);
                if (String.IsNullOrEmpty(msg) == false)
                {
                    string msg2 = "What should Activator do with sense-less proto.StopLossNegativeOffset[" + proto.StopLossNegativeOffset + "], ";
                    throw new Exception(msg2, new Exception(msg));
                }
            }

            MarketLimitStop simpleStopIfActivationZero = (proto.StopLossActivationNegativeOffset == 0) ? MarketLimitStop.Stop : MarketLimitStop.StopLimit;

            Alert alertStopLoss = executor.SellOrCoverAlertCreateDontRegister(
                executor.Bars.BarStreaming,
                position, proto.PriceStopLoss,
                "protoStopLossExit:" + proto.StopLossActivationNegativeOffset
                + "@" + proto.StopLossNegativeOffset + " for " + position.EntrySignal,
                MarketConverter.ExitDirectionFromLongShort(proto.LongShort),
                simpleStopIfActivationZero);

            if (alertStopLoss == null)
            {
                string msg = "alertStopLoss should NOT be null";
                throw new Exception(msg);
            }
            alertStopLoss.PriceStopLimitActivation = 0;
            if (proto.StopLossActivationNegativeOffset < 0)
            {
                alertStopLoss.PriceStopLimitActivation = proto.PriceStopLossActivation;
            }
            if (proto.StopLossAlertForAnnihilation != null && this.executor.Backtester.IsBacktestingNow == false)
            {
                string msg = "CLEANUP: I was trying to catch MoveStopLoss::if(proto.StopLossAlertForAnnihilation==null)"
                             + " so I thought there is a new prototype assigned to a position,"
                             + " since we never put null directly proto.StopLossAlertForAnnihilation";
                throw new Exception(msg);
            }

            proto.StopLossAlertForAnnihilation = alertStopLoss;
            return(alertStopLoss);
        }