Exemplo n.º 1
0
            /// <summary>
            /// Controlla ed effettua la modifica in trailing se le condizioni le permettono
            /// </summary>
            private void _checkTrailing(Position position, TrailingData trailingdata)
            {
                if (trailingdata == null || trailingdata.Activation == 0 || trailingdata.Distance == 0)
                {
                    return;
                }

                double trailing = 0;

                switch (position.TradeType)
                {
                case TradeType.Buy:

                    trailing = Math.Round(Symbol.Bid - Symbol.PipsToDigits(trailingdata.Distance), Symbol.Digits);

                    if ((Symbol.Bid >= (position.EntryPrice + Symbol.PipsToDigits(trailingdata.Activation))) && (position.StopLoss == null || position.StopLoss < trailing))
                    {
                        position.ModifyStopLossPrice(trailing);
                    }

                    break;

                case TradeType.Sell:

                    trailing = Math.Round(Symbol.Ask + Symbol.PipsToDigits(trailingdata.Distance), Symbol.Digits);

                    if ((Symbol.Ask <= (position.EntryPrice - Symbol.PipsToDigits(trailingdata.Activation))) && (position.StopLoss == null || position.StopLoss > trailing))
                    {
                        position.ModifyStopLossPrice(trailing);
                    }

                    break;
                }
            }
Exemplo n.º 2
0
            /// <summary>
            /// Filtra e rende disponibili le informazioni per la strategia monitorata. Eventualmente Chiude e gestisce le operazioni
            /// </summary>
            public Information Update(bool closeall, BreakEvenData breakevendata, TrailingData trailingdata, TradeType?filtertype = null)
            {
                // --> Raccolgo le informazioni che mi servono per avere il polso della strategia
                Positions = _allPositions.FindAll(Label, Symbol.Name);

                // --> Resetto le informazioni
                Info = new Information();

                double tmpVolume = 0;

                foreach (Position position in Positions)
                {
                    // --> Per prima cosa devo controllare se chiudere la posizione
                    if (closeall && (filtertype == null || position.TradeType == filtertype))
                    {
                        position.Close();
                        continue;
                    }

                    // --> Poi tocca al break even
                    _checkBreakEven(position, breakevendata);

                    // --> Poi tocca al trailing
                    _checkTrailing(position, trailingdata);

                    Info.TotalNetProfit += position.NetProfit;
                    tmpVolume           += position.VolumeInUnits;

                    switch (position.TradeType)
                    {
                    case TradeType.Buy:

                        Info.BuyPositions++;
                        break;

                    case TradeType.Sell:

                        Info.SellPositions++;
                        break;
                    }

                    if (Info.FirstPosition == null || position.EntryTime < Info.FirstPosition.EntryTime)
                    {
                        Info.FirstPosition = position;
                    }

                    if (Info.LastPosition == null || position.EntryTime > Info.LastPosition.EntryTime)
                    {
                        Info.LastPosition = position;
                    }

                    if (Info.MinVolumeInUnits == 0 || position.VolumeInUnits < Info.MinVolumeInUnits)
                    {
                        Info.MinVolumeInUnits = position.VolumeInUnits;
                    }

                    if (Info.MaxVolumeInUnits == 0 || position.VolumeInUnits > Info.MaxVolumeInUnits)
                    {
                        Info.MaxVolumeInUnits = position.VolumeInUnits;
                    }
                }

                // --> Restituisce una Exception Overflow di una operazione aritmetica, da approfondire
                //     Info.MidVolumeInUnits = Symbol.NormalizeVolumeInUnits(tmpVolume / Positions.Length,RoundingMode.ToNearest);
                Info.MidVolumeInUnits = Math.Round(tmpVolume / Positions.Length, 0);

                return(Info);
            }