Monday, April 15, 2013

GzipStream To Byte Array

public static string Compress(this string s)
    {
        byte[] bytesToEncode = Encoding.UTF8.GetBytes(s);
        return Convert.ToBase64String(bytesToEncode.Compress());
    }

public static byte[] Compress(this byte[] bytesToEncode)
    {
        using (MemoryStream input = new MemoryStream(bytesToEncode))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
            {
                input.CopyTo(zip);
            }
            return output.ToArray();
        }
    }



public static string Explode(this string s)
    {
        byte[] compressedBytes = Convert.FromBase64String(s);
        return Encoding.UTF8.GetString(compressedBytes.Explode());
    }

public static byte[] Explode(this byte[] compressedBytes)
    {
        using (MemoryStream input = new MemoryStream(compressedBytes))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
            {
                zip.CopyTo(output);
            }
            return output.ToArray();
        }
    }

Tuesday, April 9, 2013

Sql Server ConnectionTimeout vs CommandTimeout


ConnectionTimeout a property of Connection class in ADO.NET, is the time you would wait, for connecting to a given database, before flagging a connection failure. Default value is 30 seconds.
new SqlConnection().ConnectionTimeout = 10;
CommandTimeout a property of the Command class in ADO.NET, is the time you would wait, for a command (query, stored procedure, etc.) to return result set, before flagging an execution failure. Default value for CommandTimeout too is 30 seconds.
new SqlConnection().CreateCommand().CommandTimeout = 10;
Unlike ConnectionTimeout which are part of connection string, command timeouts are defined separately (hardcoded or as appSettings). Setting both of them to 0 (zero) would result in indefinite wait period which is generally considered a bad practice. You ideally want to set both of them to in accordance to your performance SLAs. Though at times, while running data heavy background jobs / workflows you may want to set your CommandTimeout to a larger value.


Friday, March 15, 2013

Dell Windows Vista Recovery from Factory image



  • Start the computer.
  • As the computer starts, press the F8 key until the Advanced Boot Options menu appears on the screen.
  • Press the (Down Arrow) to select Repair Your Computer on the Advanced Boot Options menu, and then press Enter.
  • Specify the language settings that you want, and then click Next.
  • Log in as a user who has administrative credentials, and then click OK.
  • Click Dell Factory Image Restore.
  • In the Dell Factory Image Restore window, click Next.
  • Click to select the check box for Yes, reformat hard drive and restore system software to factory condition.
  • Click Next. The computer is restored to the default factory configuration.
  • When the restore operation is completed, click Finish to restart the computer. 

Reference : http://oem.windowsreinstall.com/dell/dell_vistahdd.htm

Tuesday, January 1, 2013

Check unmanaged dll is 32-bit or 64-bit


Using a Visual Studio command prompt, dumpbin /headers dllname.dll works too. On my machine the beginning of the output stated:
FILE HEADER VALUES
8664 machine (x64)
5 number of sections
47591774 time date stamp Fri Dec 07 03:50:44 2007

Tuesday, December 18, 2012

KD 随机指标


当价格上涨时,收市价倾向于接近当日价格区间的上端;相反,在下降趋势中收市价趋向于接近当日价格区间的下端。随机指标在设计中充分考虑价格波动的随机震幅与中短期波动的测算,使其短期测市功能比移动平均线更加准确有效,在市场短期超买超卖的预测方面又比强弱指标敏感,因此,这一指标被投资者广泛采用。
计算方法
计算随机指标数值时,首先须找出最近N天内曾出现过的最高价、最低价与第N天的收盘价,然后利用这三个数字来计算第N天的未成熟随机值(RSV)。
N:待设定参数,缺省值9日
然后,求RSV的M1日移动平均,得K值;再求K值的M2日移动平均得D值。M1、M2的缺省值都为3。
缺省时,系统在副图上绘制两条线,分别为RSV值的三日平均线K,K值的三日平均线D。
K与D值永远介于0与100之间。
KD线中的RSV,随着9日中高低价、收盘价的变动而有所不同。如果行情是一个明显的涨势,会带动线K(快速平均值)与线D(慢速平均值)向上升。但如涨势开始迟缓,便会慢慢反应到K值与D值,使线K跌破线D,此时中短期跌势确立。由于KD线本质上是一个随机波动的观念,对于掌握中短期的行情走势非常正确。
应用法则
1、D值在70以上时,市场呈现超买现象。D值在30以下时,市场则呈现超卖现象。
2、当随机指数与股价出现背离时,一般为转势的信号。中期或短期的走势有可能已见顶或见底。
3、当K值大于D值,显示目前趋势是向上涨,因此线K向上突破线D时,为买进信号。当D值大于K值,显示趋势是向下跌,因此线K向下跌破线D,为卖出信号。
4、线K与线D的交叉,须在70以上,30以下信号才较为正确。K线与D线在特性上与强弱指标一样,当K值与D值在70以上,已显示超买的现象,30以下出现超卖的现象。KD线不仅能反映市场的超买超卖程度,还能通过交叉突破发出买卖信号。但若这类交叉突破在50左右发生,走势又陷入盘局时,买卖信号可视为无效。
5、当K值和D值上升或下跌的速度减弱,倾斜度趋于平缓是短期转势的预警信号。
6、KD不适用于发行量太小,交易太小的股票;但对指数以及热门大型股有极高的准确性。
[缺点]:
股价短期波动剧烈或者瞬间行情幅度太大时,使用KD值交叉讯号买卖,经常发生买在高点、卖在低点的的窘境。
K值进入超买或超卖区之后,和RSI一样会发生指标徘徊的现象,令股民一时手足无措。

注意!如果波动的幅度够大,买卖之间扣除手续费仍有利润的话,此时将画面转变成五分钟或十五分钟图形,再以KD指标的交叉信号买卖还可以斩获一点利润。
极强或极弱的行情,会造成指标在超买或超卖区内上下徘徊,K值也会发生这种情形,应该参考VR、ROC指标,观察股价是否超出常态分布的范围,一旦确定为极强的走势,则K值的超买超卖的功能将失去作用。
注意!
以D值来代替K值,将可使超买超卖的功能更具效果,一般常态行情,D值大于80时,股价经常向下回跌;D值低于20时,股价容易向上回升。在极端行情中,D值大于90时,股价容易产生瞬间回档;D值低于15时,股价容易产生瞬间反弹。
[诀窍]
(1)K、D值交叉讯号的可靠度如何?要怎样辨认是极端行情还是常态行情?经常困扰着股民,致使这个指标看似有用,实际上又存在着许多无法理清的盲点。此时,你可以试着找两线交叉时所产生的“风洞”。
所谓的风洞就是K、D两线第一次相交时两线所夹成的一个空间,但都是已下跌一段了。[风洞的具体用法]
(2)J值>100时,股价会形成头部。
(3)J值<0时,股价会形成底部。
注意!
J值的讯号不会经常出现,一旦出现,则可信度相当高,很多有心的投机客专门寻找J值的讯号,而且,运用人工智慧选股扫描系统,很轻易的找到最佳买卖点的个股这个讯号可以说是随机指标的精华。
因此,倒过来使用JDK是使用短线指标KDJ的要诀。J值钝化向下发散的一刹那是决定出货日的时机,然而打开5分钟或60分钟KDJ,5分钟K线跌20日均线(系指分时图内)是当日较佳出货点,60分钟K线20日均线一个时段(比如一周内)当然也可以注视5分钟J线,钝化向下发散的瞬间,或60分钟J线钝化向下发散的时点。随机指标KD的标准参数为9,5,3,1;修正参数为27,5,3,1。

Thursday, November 22, 2012

C# Get IPV4 ipaddress


Option 1 :  

public static string GetIP4Address()
{
    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
        if (IPA.AddressFamily == AddressFamily.InterNetwork)
        {
            IP4Address = IPA.ToString();
            break;
        }
    }

    return IP4Address;
}


 Option 2 :  
 /// <summary> 
/// This utility function displays all the IP (v4, not v6) addresses of the local computer. 
/// </summary> 
public static void DisplayIPAddresses() 

    StringBuilder sb = new StringBuilder(); 

    // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection) 
    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); 

    foreach (NetworkInterface network in networkInterfaces) 
    { 
        // Read the IP configuration for each network 
        IPInterfaceProperties properties = network.GetIPProperties(); 

        // Each network interface may have multiple IP addresses 
        foreach (IPAddressInformation address in properties.UnicastAddresses) 
        { 
            // We're only interested in IPv4 addresses for now 
            if (address.Address.AddressFamily != AddressFamily.InterNetwork) 
                continue; 

            // Ignore loopback addresses (e.g., 127.0.0.1) 
            if (IPAddress.IsLoopback(address.Address)) 
                continue; 

            sb.AppendLine(address.Address.ToString() + " (" + network.Name + ")"); 
        } 
    } 

    MessageBox.Show(sb.ToString()); 
}

Tuesday, November 20, 2012

Windows Domain Controller NTP 設定



DC NTP對時的方式需透過GPO來設定, 修改Registry是無效的

開啟GPO->電腦設定->系統管理範本->系統->Windows時間服務->時間提供者

要去向別人校時就必須啟用『Windows NTP用戶端』

要給別人校時就必須啟用『Windows NTP伺服器』

在『設定Windows NTP用戶端』內設定NTP Server

NoSync: The client does not synchronize time.
NTP: The client synchronizes time from an external time source. Review the values in the NtpServer line in the output to see the name of the server or servers that the client uses fortime synchronization.(其他外部NTP Server)
NT5DS: The client is configured to use the domain hierarchy for its time synchronization.(當電腦在Domain內時使用)
AllSync: The client synchronizes time from any available time source, including domainhierarchy and external time sources.(任一種皆可)
所以DC角色就是全開, 因為他要當Domain User的NTP Server, 又要當NTP Client去跟中華電信校時

另外, 有遇到網路設備無法跟Windows NTP Server校時的問題
因為w32tm使用的是sntp與一般unix, linux及網路設備懂的ntp不同, 所以無法校時
解法就是別用w32tm......