Friday, May 18, 2007
Password Generator
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
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
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
}
Wednesday, April 4, 2007
Log file lock problem
public class MyLogger
{
private String _LogFilePath;
private String _Name;
private List<String> _Waiting = new List<String>();
public MyLogger(String name)
{
_LogFilePath = Path.GetDirectoryName(
GetType().Assembly.Location) + @"\Log\";
_Name = name;
if (!Directory.Exists(LogFilePath))
Directory.CreateDirectory(LogFilePath);
}
public void WriteToLogFile(String message)
{
_Waiting.Add(DateTime.Now.ToString("HH:mm:ss.fffff ") + message);
String filePath = String.Format("{0}{1}_{2}.log",
LogFilePath,
DateTime.Now.ToString("yyyyMMdd"),
Name);
try
{
if (!File.Exists(filePath))
{
using (StreamWriter sw = File.CreateText(filePath))
{
foreach (String s in _Waiting)
{
sw.WriteLine(s);
}
_Waiting.Clear();
}
}
else
{
using (StreamWriter sw = File.AppendText(filePath))
{
foreach (String s in _Waiting)
{
sw.WriteLine(s);
}
_Waiting.Clear();
}
}
}
catch(Exception ex)
{
Debug.WriteLine(String.Format("Unable to add log in {0} {1}", Name, ex.Message));
}
}
}