Friday, May 18, 2007

Password Generator

You have probably got in situations when you need a password generator, somethimes for generating keys like for generating ticket for thin client using webservices, or for your personal use when you need to create a key for user account, but want that to be randomly. Here is a good piece of code for generating simple or strong passwords.

public static string GeneratePassword(int passwordLen, bool includeSmallLetters, bool includeBigLetters,
                            bool includeNumbers, bool includeSpecialCharacters)
{
   char[] returnValue = new char[passwordLen];
   char[] Choises;
   string smallLetters = string.Empty;
   string bigLetters = string.Empty;
   string numbers = string.Empty;
   string specialcharacters = string.Empty;

   int choisesLen = 0;

   if (includeSmallLetters)
   {
      smallLetters = "abcdefghijklmnopqrstuvwxyz";
      choisesLen += smallLetters.Length;
   }
   if (includeBigLetters)
   {
      bigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      choisesLen += bigLetters.Length;
   }
   if (includeNumbers)
   {
      numbers = "012345678901234567890123456789";
      choisesLen += numbers.Length;
   }
   if (includeSpecialCharacters)
   {
      specialcharacters = "~`!@#$%^&*()-_+=\|<,>.?/ {[}]";
      choisesLen += specialcharacters.Length;
   }
   if (choisesLen == 0)
      throw new ArgumentOutOfRangeException("includeSmallLetters",
                                  "At least one type of characters must be included!");

   Choises = (smallLetters + bigLetters + numbers + specialcharacters).ToCharArray();
   Random rnd = new Random();

   for (int i = 0; i < passwordLen; i++)
   {
      returnValue[i] = Choises[rnd.Next(choisesLen - 1)];
   }
   return new string(returnValue);
}

Thursday, May 17, 2007

Real-Time Technical Indicator

I develop trading systems. In most of systems, technical indicators are used. What is very important when you run hundreds of trading systems that do very intensive calculations on price change, is that executions must be very efficient and quick. I was using some library, and what was bothering me is that all such indicators library are static. What i mean by static is that you have a function for calculating some indicator and you need to send array of input values, and to receive a value or array as output. But if you receive a new value or a change of last value in the array, you need to send the whole array of values again, and calculations to be done once again for all that data. Not just that array of prices and volumes can be big in number (hundreds) but also the number of times when you need calculation can be a problem. Sometimes you receive very frequent price events and you need all that calculations for all systems.

For that purpose i start developing my library of real-time technical indicators, that will when possible, do just additional calculation for new or changed value and not all that calculation for previous values. So I don't have static methods and instead have class for every indicator.

Here is source for one of the simplest indicators named Accumulation/Distribution indicator or AD.

Formula for this indicator is:.

PreviousValue + ((Close - Low) - (High - Close))/ (High - Low) * Volume

public class AD
{
    private Decimal _Value;
    private readonly String _Name;
    private readonly BarDecimalCollection _PriceBars = new BarDecimalCollection();
    private readonly DecimalCollection _ValueArray = new DecimalCollection();

    /// <summary>
    /// Class for calculating Accumulation/Distribution
    /// </summary>
    public AD()
    {
        _Name = GetType().Name;
    }
    public Decimal Value
    {
        get { return _Value; }
    }

    public String Name
    {
        get { return _Name; }
    }

    public DecimalCollection ValueArray
    {
        get { return _ValueArray; }
    }

    /// <summary>
    /// Method for adding one BarDecimal struct instance
    /// </summary>
    /// <param name="priceBar"></param>
    public void PriceBar(BarDecimal priceBar)
    {
        if (priceBar.AddBar)
            AddPriceBar(priceBar);
        else
            EditPriceBar(priceBar);
    }

    /// <summary>
    /// Method for adding collection of BarDecimal struct instances
    /// </summary>
    /// <param name="priceBars"></param>
    public void PriceBars(BarDecimalCollection priceBars)
    {
        if (priceBars == null || priceBars.Count == 0)
            return;
        for (Int32 i = 0; i < priceBars.Count; i++)
        {
            AddPriceBar(priceBars[i]);
        }
    }

    private void AddPriceBar(BarDecimal priceBar)
    {
        _PriceBars.Add(priceBar);
        Decimal change = priceBar.HighPrice - priceBar.LowPrice;
        if (change != 0)
        {
            _Value = ((priceBar.ClosePrice - priceBar.LowPrice) - (priceBar.HighPrice - priceBar.ClosePrice)) / change * priceBar.Volume + _Value;
        }
        ValueArray.Add(_Value);
    }

    private void EditPriceBar(BarDecimal priceBar)
    {
        _PriceBars[_PriceBars.Count - 1] = priceBar;

        Decimal change = priceBar.HighPrice - priceBar.LowPrice;
        if (change != 0)
        {
            Decimal prevValue = ValueArray.Count > 1 ? ValueArray[ValueArray.Count - 2] : 0;
            _Value = ((priceBar.ClosePrice - priceBar.LowPrice) - (priceBar.HighPrice - priceBar.ClosePrice)) / change + prevValue;
        }
        ValueArray[ValueArray.Count - 1] = _Value;
    }
}

Monday, May 14, 2007

DecimalCollection

This is my DecimalCollection that i use in my Technical Indicators Library. I would like your comments on this source.

public class DecimalCollection : CollectionBase, IEnumerable<Decimal>, IEnumerator
{
   private Int32 _CurrentIndex = 0;

   public virtual void AddRange(Decimal[] items)
   {
      //Rule CA1062
      if (items == null) throw new ArgumentNullException("items");

      foreach (Decimal item in items)
      {
         List.Add(item);
      }
   }

   public virtual void AddRange(DecimalCollection items)
   {
      foreach (Decimal item in items)
      {
         List.Add(item);
      }
   }

   public virtual void Add(Decimal value)
   {
      List.Add(value);
   }

   public virtual Boolean Contains(Decimal value)
   {
      return List.Contains(value);
   }

   public virtual Int32 IndexOf(Decimal value)
   {
      return List.IndexOf(value);
   }

   public virtual void Insert(Int32 index, Decimal value)
   {
      List.Insert(index, value);
   }

   public virtual Decimal this[Int32 index]
   {
      get { return (Decimal) List[index]; }
      set { List[index] = value; }
   }

   public virtual void Remove(Decimal value)
   {
      List.Remove(value);
   }

   private Decimal Sum()
   {
      Decimal sum = 0m;
      for (Int32 i = 0; i < Count; i++)
         sum += this[i];
      return sum;
   }

   public virtual Decimal Average()
   {
      if (Count > 0)
         return Sum()/Count;
      else
         return 0m;
   }


   #region IEnumerable<Double> Members

   IEnumerator<Decimal> IEnumerable<Decimal>.GetEnumerator()
   {
      foreach (Decimal item in InnerList)
      {
         yield return item;
      }
   }

   #endregion

   #region IEnumerable Members

   IEnumerator IEnumerable.GetEnumerator()
   {
      return this;
   }

   #endregion

   #region IEnumerator Members

   public object Current
   {
      get { return this[_CurrentIndex]; }
   }

   public Boolean MoveNext()
   {
      if (_CurrentIndex < Count)
      {
         _CurrentIndex++;
         return true;
      }
      else
         return false;
   }

   public void Reset()
   {
      _CurrentIndex = 0;
   }

   #endregion
}