Exemple #1
0
        /// <summary>Called when new data is received</summary>
        public override void Step()
        {
            base.Step();

            if (Instrument.NewCandle)
            {
                Debugging.LogInstrument();
                Debugging.BreakOnPointOfInterest();
            }

            // Run the position manager
            if (PosMgr != null)
            {
                PosMgr.Step();
            }

            // If there is a pending order, wait for it to trigger or expire
            var pending = PendingOrders.FirstOrDefault();

            if (pending != null)
            {
                // If the price is more than the SL/TP distance from the EP then give up on the order
                var dist = Math.Abs(Instrument.LatestPrice.Mid - pending.TargetPrice);
                if (dist > Math.Abs(pending.TakeProfitRel()) ||
                    dist > Math.Abs(pending.StopLossRel()))
                {
                    Broker.CancelPendingOrder(pending);
                }
                else
                {
                    return;
                }
            }

            // Look for an existing trade for this strategy.
            var position = Positions.FirstOrDefault();

            if (position == null)
            {
                // Look for indicators to say "enter" and which direction.
                QuoteCurrency?ep, tp;
                var           tt = Instrument.FindTradeEntry(out ep, out tp);
                if (tt == null)
                {
                    return;
                }

                // Create a trade in the suggested direction
                var trade = new Trade(Instrument, tt.Value, Label, ep: ep, tp: tp);
                trade.Expiration = (Bot.UtcNow + Instrument.TimeFrame.ToTimeSpan(10)).DateTime;
                if (trade.RtR < 0.2)
                {
                    return;
                }

                // Create a pending order
                Broker.CreatePendingOrder(trade);
                //Broker.CreateOrder(trade);
            }
        }
Exemple #2
0
        protected sealed override void OnTick()
        {
            ++TickNumber;
            Debugging.BreakOnPointOfInterest();

            // Emergency stop on large draw-down
            BalanceMinimum = Math.Max((double)BalanceMinimum, Account.Balance * (1.0 - Settings.MaxDrawDownFrac));
            if (Account.Equity < BalanceMinimum)
            {
                Debugging.Trace("Account equity (${0}) dropped below the balance minimum (${1}). Stopping".Fmt(Account.Equity, BalanceMinimum));
                Print("Account equity (${0}) dropped below the balance minimum (${1}). Stopping".Fmt(Account.Equity, BalanceMinimum));
                CloseAllPositions("Emergency Stop");
                Stop();
                return;
            }

            // Raise the Bot.Tick event before stepping the bot
            // Instruments are signed up to the Tick event so they will be updated first
            base.OnTick();
            Tick.Raise(this);

            try
            {
                // Update the account info
                Broker.Update();

                // Remove position sets that don't have any active positions
                var set_ids = Positions.Select(x => Guid_.Parse(x.Comment)).NotNull().ToHashSet(x => x.Value);
                foreach (var set_id in PositionSets.Keys.ToArray())
                {
                    if (set_ids.Contains(set_id))
                    {
                        continue;
                    }
                    PositionSets.Remove(set_id);
                }

                // Step active position managers
                foreach (var pm in PositionManagers)
                {
                    pm.Step();
                }

                // Entry cool down
                if (EntryCooldown != 0 && Instrument.NewCandle)
                {
                    --EntryCooldown;
                }

                // Step the bot
                Step();
            }
            catch (Exception ex)
            {
                Debugging.Trace(ex.Message);
                Debugging.Trace(ex.StackTrace);
            }
        }
Exemple #3
0
        /// <summary>Called when new data is received</summary>
        public override void Step()
        {
            base.Step();

            Debugging.BreakOnPointOfInterest();
            if (Instrument.NewCandle)
            {
                Debugging.LogInstrument();
            }

            if (PendingOrders.Any())
            {
                return;
            }

            // Look for an existing trade for this strategy.
            var position = Positions.FirstOrDefault();

            if (position == null)
            {
                // 'ep' must be lower than 'tp'
                var ep   = 0.2;
                var tp   = 0.6;
                var sl   = 0.8;
                var exp  = 1;
                var step = Instrument.MCS;                //MedianCandleSize(-5,1);

                var sym    = Instrument.LatestPrice;
                var trade0 = new Trade(Instrument, TradeType.Buy, Label, ep: sym.Ask + ep * step, sl: sym.Ask - sl * step, tp: sym.Ask + tp * step, risk: 0.5f)
                {
                    Expiration = (Bot.UtcNow + Instrument.TimeFrame.ToTimeSpan(exp)).DateTime
                };
                var trade1 = new Trade(Instrument, TradeType.Sell, Label, ep: sym.Bid - ep * step, sl: sym.Bid + sl * step, tp: sym.Bid - tp * step, risk: 0.5f)
                {
                    Expiration = (Bot.UtcNow + Instrument.TimeFrame.ToTimeSpan(exp)).DateTime
                };

                Broker.CreatePendingOrder(trade0);
                Broker.CreatePendingOrder(trade1);
            }
            else
            {
            }
        }