Пример #1
0
        public override void Evaluate() // TOASYNC
        {
#if !cAlgo
            if (State == ExecutionStateEx.Faulted)
            {
                return;
            }
#endif

            try
            {
#if cAlgo
                DateTime time = Server.Time;
#else
                DateTime time = Account.ExtrapolatedServerTime;
#endif
                if (time == default(DateTime))
                {
                    return;
                }

                if (!StartDate.HasValue)
                {
                    StartDate = ExtrapolatedServerTime;
                }
                EndDate = ExtrapolatedServerTime;

#if !cAlgo
                if (State != ExecutionStateEx.Started)
                {
                    throw new InvalidExecutionStateException(ExecutionStateEx.Started, State);
                }
#endif
#if NULLCHECKS
                if (Indicator == null)
                {
                    throw new ArgumentNullException("Indicator (Evaluate)");
                }
                //if (Market.IsBacktesting && Server == null)
                //{
                //    throw new ArgumentNullException("!IsBacktesting && Server");
                //}
#endif

                try
                {
                    Indicator.CalculateToTime(time);
                }
                catch (Exception ex)
                {
                    throw new Exception("Indicator.CalculateToTime threw " + ex + " stack: " + ex.StackTrace, ex);
                }

#if TRACE_EVALUATE
                var traceThreshold = 0.0;

                Debug.WriteLineIf(double.IsNaN(Indicator.OpenLongPoints.LastValue), "NaN OpenLongPoints: " + this.ToString());

                if (Indicator.OpenLongPoints.LastValue > traceThreshold ||
                    Indicator.CloseLongPoints.LastValue > traceThreshold ||
                    Indicator.OpenShortPoints.LastValue > traceThreshold ||
                    Indicator.CloseShortPoints.LastValue > traceThreshold
                    )
                {
                    logger.LogDebug($"[{this.ToString()} evaluate #{Indicator.OpenLongPoints.Count}] Open long: " + Indicator.OpenLongPoints.LastValue.ToString("N2") + " Close long: " + Indicator.CloseLongPoints.LastValue.ToString("N2") +
                                    " Open short: " + Indicator.OpenShortPoints.LastValue.ToString("N2") + " Close short: " + Indicator.CloseShortPoints.LastValue.ToString("N2")
                                    );
                }
#endif

#if false
                if (Indicator == null)
                {
                    var msg = "No indicator for SignalBot.";
                    l.Error(msg);
                    throw new Exception(msg);
                }
                if (Indicator.OpenLongPoints == null)
                {
                    var msg = "No Indicator.OpenLongPoints.";
                    l.Error(msg);
                    throw new Exception(msg);
                }
#endif

                if (double.IsNaN(Indicator.OpenLongPoints.LastValue))
                {
                    Debug.WriteLine($"{this} Indicator.OpenLongPoints.LastValue is NaN");
                }

                if (Template.AllowLong &&
                    Indicator.OpenLongPoints.LastValue >=
                    1.0 &&
                    Indicator.CloseLongPoints.LastValue <
                    0.9 &&
                    CanOpenLong && CanOpen)
                {
                    _Open(TradeType.Buy, Indicator.LongStopLoss);
                }

                if (Template.AllowShort &&
                    -Indicator.OpenShortPoints.LastValue >=
                    1.0 &&
                    -Indicator.CloseShortPoints.LastValue <
                    0.9 &&
                    CanOpenShort && CanOpen)
                {
                    _Open(TradeType.Sell, Indicator.ShortStopLoss);
                }

                List <PositionDouble> toClose = null;
                if (Indicator.CloseLongPoints.LastValue >= 1.0)
                {
                    foreach (var position in MyPositions.Where(p => p.TradeType == TradeType.Buy))
                    {
#if TRACE_CLOSE
                        string plus = position.NetProfit > 0 ? "+" : "";
                        logger.LogInformation($"{Server.Time.ToDefaultString()} [CLOSE LONG {position.Quantity} x {Symbol.Code} @ {Indicator.Symbol.Ask}] {plus}{position.NetProfit}");
#endif
                        if (toClose == null)
                        {
                            toClose = new List <PositionDouble>();
                        }
                        toClose.Add(position);
                    }
                }
                if (-Indicator.CloseShortPoints.LastValue >= 1.0)
                {
                    foreach (var position in MyPositions.Where(p => p.TradeType == TradeType.Sell))
                    {
#if TRACE_CLOSE
                        string plus = position.NetProfit > 0 ? "+" : "";
                        logger.LogInformation($"{Server.Time.ToDefaultString()} [CLOSE SHORT {position.Quantity} x {Symbol.Code} @ {Indicator.Symbol.Bid}] {plus}{position.NetProfit}");
#endif
                        if (toClose == null)
                        {
                            toClose = new List <PositionDouble>();
                        }
                        toClose.Add(position);
                    }
                }
                if (toClose != null)
                {
                    foreach (var c in toClose)
                    {
                        var result = ClosePosition(c);
#if TRACE_CLOSE
                        logger.LogTrace(result.ToString());
#endif
                    }
                }
                OnEvaluated();
            }
            catch (InvalidExecutionStateException iex)
            {
                Debug.WriteLine($"{this} InvalidExecutionStateException. {iex.ToString()}");
            }
            catch (Exception ex)
            {
                LastException = ex;
                Debug.WriteLine($"{this} faulted with exception: {ex.ToString()}");
#if cAlgo
                Stop();
#else
                SetState(ExecutionStateEx.Faulted);
#endif
            }
        }