Ejemplo n.º 1
0
        /// <summary>Watch for pending order filled</summary>
        protected override void OnPositionOpened(Position position)
        {
            // When a position opens, cancel any pending orders
            Broker.CancelAllPendingOrders(Label);

            // Manage the position
            //PositionManagers.Add(new PositionManagerNervious(this, position));
            PositionManagers.Add(new PositionManagerCandleFollow(this, position, 7));
            //PositionManagers.Add(new PositionManagerPeakPC(this, position));
        }
        /// <summary>Position opened</summary>
        protected override void OnPositionOpened(Position position)
        {
            // Close positions if there is a breakout in the wrong direction
            PositionManagers.Add(new PositionManagerBreakOut(this, position, CloseBreakoutPeriods, only_if_in_profit: true));

            // Close positions when they fail to make new peaks
            PositionManagers.Add(new PositionManagerTopDrop(this, position, CloseTopDropCount, only_if_in_profit: true));

            EntryCooldown = 5;
        }
Ejemplo n.º 3
0
        protected override void OnPositionOpened(Position position)
        {
            base.OnPositionOpened(position);

            // Close pending orders when a position is opened
            foreach (var ord in PendingOrders)
            {
                Broker.CancelPendingOrder(ord);
            }

            // Create a position manager
            PositionManagers.Add(new PositionManagerNervious(this, position));
        }
Ejemplo n.º 4
0
        /// <summary>Position opened</summary>
        protected override void OnPositionOpened(Position position)
        {
            EntryCooldown = 5;

            // Close positions if there is a breakout in the wrong direction
            PositionManagers.Add(new PositionManagerBreakOut(this, position, CloseBreakoutPeriods, only_if_in_profit: true));

            // Close positions when they fail to make new peaks
            PositionManagers.Add(new PositionManagerTopDrop(this, position, CloseTopDropCount, only_if_in_profit: true));

            //// Close positions when there's a steady stream of adverse candles
            //PositionManagers.Add(new PositionManagerAdverseCandles(this, position, CloseAdverseCount));
        }
Ejemplo n.º 5
0
        protected override void OnPositionOpened(Position position)
        {
            // Prevent positions being entered on the same bulge
            EntryCooldown = NonIntersectingCount;

            {            // Adjust the stop loss to allow for slip
                var mcs  = Instrument.MCS;
                var sign = position.Sign();
                var ep   = position.EntryPrice;
                var sl   = ep - sign * mcs * SLFrac;
                Broker.ModifyOrder(Instrument, position, sl: sl);
            }

            // If the main position of a position set opens, create a pending order in the opposite direction.
            // This is designed to catch the case when we choose a break-out trade but it's actually a reversal, or visa-versa.
            var id = Guid_.Parse(position.Comment);

            if (id != null && PositionSets[id.Value].Count == 1)
            {
                var mcs   = Instrument.MCS;
                var sign  = -position.Sign();
                var tt    = CAlgo.SignToTradeType(sign);
                var rel   = Math.Abs(position.EntryPrice - position.StopLoss.Value) * 0.75;
                var ep    = MA0[0] + sign * rel;
                var sl    = ep - sign * mcs * SLFrac;
                var tp    = (QuoteCurrency?)null;
                var vol   = Broker.ChooseVolume(Instrument, Math.Abs(ep - sl), risk: Risk);
                var trade = new Trade(Instrument, tt, Label, ep, sl, tp, vol, comment: Guid.NewGuid().ToString())
                {
                    Expiration = Instrument.ExpirationTime(2)
                };
                Broker.CreatePendingOrder(trade);
            }

            // Close positions if there is a breakout in the wrong direction
            PositionManagers.Add(new PositionManagerBreakOut(this, position, CloseBreakoutPeriods, only_if_in_profit: true));

            // Close positions when they fail to make new peaks
            PositionManagers.Add(new PositionManagerTopDrop(this, position, CloseTopDropCount, only_if_in_profit: true));

            // Close positions when there's a steady stream of adverse candles
            PositionManagers.Add(new PositionManagerAdverseCandles(this, position, CloseAdverseCount));
        }
Ejemplo n.º 6
0
        private void HandlePositionClosed(PositionClosedEventArgs args)
        {
            // Only watch positions created by this strategy
            var position = args.Position;

            if (position.Label != Label)
            {
                return;
            }

            // Close all positions in the same set
            var id = Guid_.Parse(position.Comment);

            if (id != null && PositionSets.ContainsKey(id.Value))
            {
                var set = PositionSets[id.Value];
                Broker.ClosePositions(Positions.Where(x => set.Contains(x.Id)), "Closing Set");
                PositionSets.Remove(id.Value);
            }

            // Remove any position managers that are managing 'position'
            PositionManagers.RemoveIf(x => x.Position.Id == position.Id);

            try
            {
                // Notify position closed
                if (PositionClosed != null)
                {
                    PositionClosed(this, args);
                }
                OnPositionClosed(position);
            }
            catch (Exception ex)
            {
                Debugging.Trace(ex.Message);
                Debugging.Trace(ex.StackTrace);
            }
        }
 /// <summary>Watch for pending order filled</summary>
 protected override void OnPositionOpened(Position position)
 {
     PositionManagers.Add(new PositionManagerPriceDistribution(this, position));
 }
Ejemplo n.º 8
0
 protected override void OnPositionOpened(Position position)
 {
     base.OnPositionOpened(position);
     PositionManagers.Add(new PositionManagerBreakOut(this, position, System1_Exit, only_if_in_profit: false));
 }
Ejemplo n.º 9
0
        /// <summary>Called when new data is received</summary>
        public override void Step()
        {
            base.Step();
            if (Instrument.NewCandle)
            {
                Dump();
            }

            // Don't open new positions while there are pending orders or existing positions
            if (Positions.Any() || PendingOrders.Any())
            {
                return;
            }

            // Look for peak patterns
            TradeType     tt;
            QuoteCurrency ep;
            var           pat = Instrument.IsPeakPattern(Instrument.IdxNow, out tt, out ep);

            if (pat != null)
            {
                var pattern = pat.Value;

                Dump();
                Debugging.Dump(new PricePeaks(Instrument, 0));
                Debugging.Dump(new SnR(Instrument));
                Debugging.Trace("Peak pattern: {0}".Fmt(pattern));

                var sign = tt.Sign();
                var mcs  = Instrument.MCS;

                // Convert the patterns to trades
                switch (pattern)
                {
                case EPeakPattern.BreakOutHigh:
                case EPeakPattern.BreakOutLow:
                {
                    // For break outs, enter immediately and use a candle
                    // follow position manager because they tend to run.
                    var price_range = Instrument.PriceRange(-10, 1);
                    var sl          = price_range.Mid - sign * price_range.Size * 0.6;
                    var tp          = (QuoteCurrency?)null;
                    var vol         = Broker.ChooseVolume(Instrument, Math.Abs(ep - sl), risk: Risk);
                    var trade       = new Trade(Instrument, tt, Label, ep, sl, tp, vol);
                    var pos         = Broker.CreateOrder(trade);
                    if (pos != null)
                    {
                        PositionManagers.Add(new PositionManagerCandleFollow(this, pos, 5));
                    }

                    break;
                }

                case EPeakPattern.HighReversal:
                case EPeakPattern.LowReversal:
                {
                    // For reversals, enter when the price is near the trend line.
                    // Use a fixed SL/TP
                    var price_range = Instrument.PriceRange(-10, 1);
                    var sl          = price_range.Mid - sign * price_range.Size * 0.6;
                    var tp          = price_range.Mid + sign * price_range.Size * 0.4;
                    var vol         = Broker.ChooseVolume(Instrument, Math.Abs(ep - sl), risk: Risk);

                    // If the current price is better than the entry price, enter immediately
                    if (sign * (ep - Instrument.CurrentPrice(sign)) > 0)
                    {
                        var trade = new Trade(Instrument, tt, Label, ep, sl, tp, vol);
                        Broker.CreateOrder(trade);
                    }
                    else
                    {
                        var order = new Trade(Instrument, tt, Label, ep, sl, tp, vol)
                        {
                            Expiration = Instrument.ExpirationTime(1)
                        };
                        Broker.CreatePendingOrder(order);
                    }
                    break;
                }
                }
            }
        }