Esempio n. 1
0
        protected internal override void OnEquity(double equity)
        {
            if (this.longAccValue == 0 && this.shortAccValue == 0)
            {
                this.longAccValue = this.shortAccValue = this.portfolio.AccountValue;
            }

            var longValue  = this.longAccValue;
            var shortValue = this.shortAccValue;

            foreach (var position in this.portfolio.Positions)
            {
                var value = position.Value;
                if (position.Side == PositionSide.Long)
                {
                    longValue += value;
                }
                else
                {
                    shortValue -= value;
                }
            }
            this.longEquityMax  = Max(longValue, this.longEquityMax);
            this.shortEquityMax = Max(shortValue, this.shortEquityMax);
            this.totalEquityMax = Max(equity, this.totalEquityMax);
            this.longValue      = longValue - this.longEquityMax;
            this.shortValue     = shortValue - this.shortEquityMax;
            this.totalValue     = equity - this.totalEquityMax;
            LongValues.Add(Clock.DateTime, this.longValue);
            ShortValues.Add(Clock.DateTime, this.shortValue);
            TotalValues.Add(Clock.DateTime, this.totalValue);
            Emit();
        }
 protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
 {
     if (statistics.Type == this.type)
     {
         bool changed = false;
         if (statistics.LongValues.Count > LongValues.Count)
         {
             this.longAvg   = (this.longAvg * LongValues.Count + statistics.LongValue) / (LongValues.Count + 1);
             this.longValue = GetStdDev(statistics.LongValues, this.longAvg);
             LongValues.Add(Clock.DateTime, LongValue);
             changed = true;
         }
         if (statistics.ShortValues.Count > this.shortValues.Count)
         {
             this.shortAvg   = (this.shortAvg * this.shortValues.Count + statistics.ShortValue) / (ShortValues.Count + 1);
             this.shortValue = GetStdDev(statistics.ShortValues, this.shortAvg);
             ShortValues.Add(Clock.DateTime, this.shortValue);
             changed = true;
         }
         if (statistics.TotalValues.Count > this.totalValues.Count)
         {
             this.totalAvg   = (this.totalAvg * this.totalValues.Count + statistics.TotalValue) / (TotalValues.Count + 1);
             this.totalValue = GetStdDev(statistics.TotalValues, this.totalAvg);
             TotalValues.Add(Clock.DateTime, this.totalValue);
             changed = true;
         }
         if (changed)
         {
             Emit();
         }
     }
 }
Esempio n. 3
0
        protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
        {
            var changed = false;

            if (statistics.Type == PortfolioStatisticsType.GrossProfit)
            {
                this.grossProfit = statistics;
                changed          = true;
            }
            if (statistics.Type == PortfolioStatisticsType.GrossLoss)
            {
                this.grossLoss = statistics;
                changed        = true;
            }
            if (changed)
            {
                this.longValue = this.shortValue = this.totalValue = 0;
                if (this.grossProfit != null)
                {
                    this.longValue  += this.grossProfit.LongValue;
                    this.shortValue += this.grossProfit.ShortValue;
                    this.totalValue += this.grossProfit.TotalValue;
                }
                if (this.grossLoss != null)
                {
                    this.longValue  += this.grossLoss.LongValue;
                    this.shortValue += this.grossLoss.ShortValue;
                    this.totalValue += this.grossLoss.TotalValue;
                }
                LongValues.Add(Clock.DateTime, this.longValue);
                ShortValues.Add(Clock.DateTime, this.shortValue);
                TotalValues.Add(Clock.DateTime, this.totalValue);
                Emit();
            }
        }
Esempio n. 4
0
 protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
 {
     if (statistics.Type == PortfolioStatisticsType.NumOfWinTrades)
     {
         bool changed = false;
         if (statistics.LongValue > this.longWinTrades)
         {
             this.longWinTrades = statistics.LongValue;
             LongValues.Add(Clock.DateTime, this.longValue += 1.0);
             changed = true;
         }
         if (statistics.ShortValue > this.shortWinTrades)
         {
             this.shortWinTrades = statistics.ShortValue;
             ShortValues.Add(Clock.DateTime, this.shortValue += 1.0);
             changed = true;
         }
         if (statistics.TotalValue > this.totalWinTrades)
         {
             this.totalWinTrades = statistics.TotalValue;
             TotalValues.Add(Clock.DateTime, this.totalValue += 1.0);
             changed = true;
         }
         if (changed)
         {
             Emit();
         }
     }
     if (statistics.Type == PortfolioStatisticsType.NumOfLossTrades)
     {
         bool changed = false;
         if (statistics.LongValue > this.longLossTrades)
         {
             this.longLossTrades = statistics.LongValue;
             this.longValue      = 0;
             LongValues.Add(Clock.DateTime, 0);
             changed = true;
         }
         if (statistics.ShortValue > this.shortLossTrades)
         {
             this.shortLossTrades = statistics.ShortValue;
             this.shortValue      = 0;
             ShortValues.Add(Clock.DateTime, 0);
             changed = true;
         }
         if (statistics.TotalValue > this.totalLossTrades)
         {
             this.totalLossTrades = statistics.TotalValue;
             this.totalValue      = 0;
             TotalValues.Add(Clock.DateTime, 0);
             changed = true;
         }
         if (changed)
         {
             Emit();
         }
     }
 }
Esempio n. 5
0
 protected internal override void OnInit()
 {
     this.shortValue = 1;
     this.longValue  = 1;
     this.totalValue = 1;
     LongValues.Add(Clock.DateTime, this.longValue);
     ShortValues.Add(Clock.DateTime, this.shortValue);
     TotalValues.Add(Clock.DateTime, this.totalValue);
     Subscribe(PortfolioStatisticsType.AvgWinTrade);
     Subscribe(PortfolioStatisticsType.AvgLossTrade);
 }
Esempio n. 6
0
 protected internal override void OnInit()
 {
     this.shortValue = 1;
     this.longValue  = 1;
     this.totalValue = 1;
     LongValues.Add(Clock.DateTime, this.longValue);
     ShortValues.Add(Clock.DateTime, this.shortValue);
     TotalValues.Add(Clock.DateTime, this.totalValue);
     base.Subscribe(PortfolioStatisticsType.GrossProfit);
     base.Subscribe(PortfolioStatisticsType.GrossLoss);
 }
Esempio n. 7
0
 protected internal override void OnInit()
 {
     this.shortValue = 1;
     this.longValue  = 1;
     this.totalValue = 1;
     LongValues.Add(Clock.DateTime, this.longValue);
     ShortValues.Add(Clock.DateTime, this.shortValue);
     TotalValues.Add(Clock.DateTime, this.totalValue);
     Subscribe(PortfolioStatisticsType.NetProfit);
     Subscribe(PortfolioStatisticsType.MaxDrawdown);
 }
Esempio n. 8
0
 protected internal override void OnRoundTrip(TradeInfo trade)
 {
     if (trade.IsLong)
     {
         LongValues.Add(Clock.DateTime, this.longValue += 1);
     }
     else
     {
         ShortValues.Add(Clock.DateTime, this.shortValue += 1);
     }
     TotalValues.Add(Clock.DateTime, this.totalValue += 1);
     Emit();
 }
Esempio n. 9
0
 protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
 {
     if (statistics.Type == this.type)
     {
         this.longValue = Max(this.longValue, statistics.LongValue);
         LongValues.Add(Clock.DateTime, this.longValue);
         this.shortValue = Max(this.shortValue, statistics.ShortValue);
         ShortValues.Add(Clock.DateTime, this.shortValue);
         this.totalValue = Max(this.totalValue, statistics.TotalValue);
         TotalValues.Add(Clock.DateTime, this.totalValue);
         Emit();
     }
 }
Esempio n. 10
0
 protected internal override void OnRoundTrip(TradeInfo trade)
 {
     if (trade.IsLong)
     {
         this.longValue = trade.NetPnL;
         LongValues.Add(Clock.DateTime, this.longValue);
     }
     else
     {
         this.shortValue = trade.NetPnL;
         ShortValues.Add(Clock.DateTime, this.shortValue);
     }
     this.totalValue = trade.NetPnL;
     TotalValues.Add(Clock.DateTime, this.totalValue);
     Emit();
 }
Esempio n. 11
0
        protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
        {
            bool changed = false;

            if (statistics.Type == PortfolioStatisticsType.AvgWinTrade)
            {
                this.avgWinTrades = statistics;
                changed           = true;
            }
            if (statistics.Type == PortfolioStatisticsType.AvgLossTrade)
            {
                this.avgLossTrades = statistics;
                changed            = true;
            }
            if (changed && this.avgWinTrades != null && this.avgLossTrades != null)
            {
                bool updated = false;
                if (this.avgLossTrades.LongValue != 0)
                {
                    this.longValue = Abs(this.avgWinTrades.LongValue / this.avgLossTrades.LongValue);
                    LongValues.Add(Clock.DateTime, this.longValue);
                    updated = true;
                }
                if (this.avgLossTrades.ShortValue != 0.0)
                {
                    this.shortValue = Math.Abs(this.avgWinTrades.ShortValue / this.avgLossTrades.ShortValue);
                    ShortValues.Add(Clock.DateTime, this.shortValue);
                    updated = true;
                }
                if (this.avgLossTrades.TotalValue != 0.0)
                {
                    this.totalValue = Abs(this.avgWinTrades.TotalValue / this.avgLossTrades.TotalValue);
                    TotalValues.Add(Clock.DateTime, this.totalValue);
                    updated = true;
                }
                if (updated)
                {
                    Emit();
                }
            }
        }
Esempio n. 12
0
        protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
        {
            bool changed = false;

            if (statistics.Type == PortfolioStatisticsType.NumOfWinTrades)
            {
                this.numOfWinTrades = statistics;
                changed             = true;
            }
            if (statistics.Type == PortfolioStatisticsType.NumOfTrades)
            {
                this.numOfTrades = statistics;
                changed          = true;
            }
            if (changed && this.numOfWinTrades != null && this.numOfTrades != null)
            {
                bool updated = false;
                if (this.numOfTrades.LongValue != 0)
                {
                    this.longValue = this.numOfWinTrades.LongValue / this.numOfTrades.LongValue;
                    LongValues.Add(Clock.DateTime, this.longValue);
                    updated = true;
                }
                if (this.numOfTrades.ShortValues.Count != 0)
                {
                    this.shortValue = this.numOfWinTrades.ShortValue / this.numOfTrades.ShortValue;
                    ShortValues.Add(Clock.DateTime, this.shortValue);
                    updated = true;
                }
                if (this.numOfTrades.TotalValues.Count != 0)
                {
                    this.totalValue = this.numOfWinTrades.TotalValue / this.numOfTrades.TotalValue;
                    TotalValues.Add(Clock.DateTime, this.totalValue);
                    updated = true;
                }
                if (updated)
                {
                    Emit();
                }
            }
        }
Esempio n. 13
0
        protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
        {
            bool changed = false;

            if (statistics.Type == PortfolioStatisticsType.GrossProfit)
            {
                this.grossProfit = statistics;
                changed          = true;
            }
            if (statistics.Type == PortfolioStatisticsType.GrossLoss)
            {
                this.grossLoss = statistics;
                changed        = true;
            }
            if (changed && this.grossProfit != null && this.grossLoss != null)
            {
                bool updated = false;
                if (this.grossLoss.LongValue != 0)
                {
                    this.longValue = this.grossProfit.LongValue / -this.grossLoss.LongValue;
                    LongValues.Add(base.Clock.DateTime, this.longValue);
                    updated = true;
                }
                if (this.grossLoss.ShortValue != 0)
                {
                    this.shortValue = this.grossProfit.ShortValue / -this.grossLoss.ShortValue;
                    ShortValues.Add(Clock.DateTime, this.shortValue);
                    updated = true;
                }
                if (this.grossLoss.TotalValue != 0.0)
                {
                    this.totalValue = this.grossProfit.TotalValue / -this.grossLoss.TotalValue;
                    TotalValues.Add(Clock.DateTime, this.totalValue);
                    updated = true;
                }
                if (updated)
                {
                    Emit();
                }
            }
        }
Esempio n. 14
0
        protected internal override void OnStatistics(PortfolioStatisticsItem statistics)
        {
            bool changed = false;

            if (statistics.Type == PortfolioStatisticsType.NetProfit)
            {
                this.netProfit = statistics;
                changed        = true;
            }
            if (statistics.Type == PortfolioStatisticsType.MaxDrawdown)
            {
                this.maxDrawdown = statistics;
                changed          = true;
            }
            if (changed && this.netProfit != null && this.maxDrawdown != null)
            {
                bool updated = false;
                if (this.maxDrawdown.LongValue != 0)
                {
                    this.longValue = Abs(this.netProfit.LongValue / this.maxDrawdown.LongValue);
                    LongValues.Add(Clock.DateTime, this.longValue);
                    updated = true;
                }
                if (this.maxDrawdown.ShortValue != 0)
                {
                    this.shortValue = Abs(this.netProfit.ShortValue / this.maxDrawdown.ShortValue);
                    ShortValues.Add(Clock.DateTime, this.shortValue);
                    updated = true;
                }
                if (this.maxDrawdown.TotalValue != 0)
                {
                    this.totalValue = Math.Abs(this.netProfit.TotalValue / this.maxDrawdown.TotalValue);
                    TotalValues.Add(Clock.DateTime, this.totalValue);
                    updated = true;
                }
                if (updated)
                {
                    Emit();
                }
            }
        }
Esempio n. 15
0
        public ExecutionState LoadData(ByteReader br)
        {
            while (br.Avaliable > 0)
            {
                var Name = br.ReadString();
                if (!Name)
                {
                    return(ExecutionState.Failed());
                }
                var Type = br.ReadByte();
                if (!Type)
                {
                    return(ExecutionState.Failed());
                }
                if (!DataTypeHelper.TryParse(Type.Result, out DataType TypeResult))
                {
                    return(ExecutionState.Failed());
                }
                switch (TypeResult)
                {
                case DataType.type_byte:
                    var ValueByte = br.ReadByte();
                    if (!ValueByte)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (ByteValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    ByteValues.Add(Name.Result, ValueByte.Result);
                    break;

                case DataType.type_sbyte:
                    var ValueSByte = br.ReadSByte();
                    if (!ValueSByte)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (SByteValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    SByteValues.Add(Name.Result, ValueSByte.Result);
                    break;

                case DataType.type_short:
                    var ValueShort = br.ReadShort();
                    if (!ValueShort)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (ShortValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    ShortValues.Add(Name.Result, ValueShort.Result);
                    break;

                case DataType.type_ushort:
                    var ValueUShort = br.ReadUShort();
                    if (!ValueUShort)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (UShortValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    UShortValues.Add(Name.Result, ValueUShort.Result);
                    break;

                case DataType.type_int:
                    var ValueInt = br.ReadInt();
                    if (!ValueInt)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (IntValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    IntValues.Add(Name.Result, ValueInt.Result);
                    break;

                case DataType.type_uint:
                    var ValueUInt = br.ReadUInt();
                    if (!ValueUInt)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (UIntValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    UIntValues.Add(Name.Result, ValueUInt.Result);
                    break;

                case DataType.type_long:
                    var ValueLong = br.ReadLong();
                    if (!ValueLong)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (LongValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    LongValues.Add(Name.Result, ValueLong.Result);
                    break;

                case DataType.type_ulong:
                    var ValueULong = br.ReadULong();
                    if (!ValueULong)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (ULongValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    ULongValues.Add(Name.Result, ValueULong.Result);
                    break;

                case DataType.type_single:
                    var ValueSingle = br.ReadSingle();
                    if (!ValueSingle)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (SingleValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    SingleValues.Add(Name.Result, ValueSingle.Result);
                    break;

                case DataType.type_double:
                    var ValueDobule = br.ReadDouble();
                    if (!ValueDobule)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (DoubleValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    DoubleValues.Add(Name.Result, ValueDobule.Result);
                    break;

                case DataType.type_string:
                    var ValueString = br.ReadString();
                    if (!ValueString)
                    {
                        return(ExecutionState.Failed());
                    }
                    if (StringValues.ContainsKey(Name.Result))
                    {
                        return(ExecutionState.Failed());
                    }
                    StringValues.Add(Name.Result, ValueString.Result);
                    break;
                }
            }

            return(ExecutionState.Succeeded());
        }