public List<dateData> GetHistory_Web(Quote q)
        {
            long lastDate = q.LastDate;
            if (lastDate == 0) {
                return pWSD.GetHistory(q);
                //return pWSD.GetHistory(q);
            }

            DateTime DTTo = DateTime.Now.ToLocalTime();
            DTTo = DTTo.AddDays(-1);//as today will not be the histroy
            long stampTo = Util.GetUnixTimeStamp(DTTo);

            date dtFrom = Util.GetDate(lastDate);
            DateTime DTFrom = new DateTime(dtFrom.year, dtFrom.month, dtFrom.day);
            DTFrom = DTFrom.AddDays(1); //the bland data only begins from the next day (of the latest data we have)
            long stampFrom = Util.GetUnixTimeStamp(DTFrom);

            if (stampTo > stampFrom) {
                date dFrom = Util.GetDate(stampFrom);
                date dTo = Util.GetDate(stampTo);
                return pWSD.GetHistory(q, dFrom, dTo);
                //return pWSD_G.GetHistory(q, dFrom, dTo);
            } else {
                Output.Log(q.Describe + " already updated to latest : " + dtFrom);
            }
            return new List<dateData>();
        }
        public List<dateData> LoadHistory_DB(Quote q)
        {
            List<dateData> dataList = new List<dateData>();
            string tblNm = GetTableNameHistory_DB(q);
            if (!TableExist(tblNm))
                return dataList;

            SqliteConnection conn = null;
            string sql = "";

            try {
                conn = new SqliteConnection(souce);
                conn.Open();
                SqliteCommand cmd = conn.CreateCommand();
                sql = "select date, volume, open, high, low, close from " + tblNm + " order by date desc;";
                cmd.CommandText = sql;
                SqliteDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read()) {
                    dateData dd = new dateData();
                    dd._date = rdr.GetInt64(0);
                    dd._indic._volume = rdr.GetInt64(1);
                    dd._price._open = rdr.GetDouble(2);
                    dd._price._high = rdr.GetDouble(3);
                    dd._price._low = rdr.GetDouble(4);
                    dd._price._close = rdr.GetDouble(5);
                    dataList.Add(dd);
                }
            } catch (Exception e) {
                Output.LogException(" sql (" + sql + ") error : " + e.Message);
            }
            if (conn != null)
                conn.Close();
            return dataList;
        }
        /// <summary>
        /// Loads all empty quotes.
        /// </summary>
        /// <returns>
        /// The all quotes only have basic information like code.
        /// </returns>
        public List<Quote> LoadQuotesInfo_DB()
        {
            List<Quote> allQ = new List<Quote>();
            SqliteConnection conn = null;
            string sql = "";
            try {
                conn = new SqliteConnection(souce);
                conn.Open();
                SqliteCommand cmd = conn.CreateCommand();
                sql = "select si_market, si_code, si_name from " + TableNameInfo + ";";
                cmd.CommandText = sql;
                SqliteDataReader rdr = cmd.ExecuteReader();
                information info = information.EMPTY;
                int count = 0;
                while (rdr.Read()) {
                    info._market._value = (market.type)rdr.GetInt32(0);
                    info.CodeInt = rdr.GetInt32(1);
                    info._name = rdr.GetString(2);
                    Quote q = new Quote(info);
                    allQ.Add(q);
                    Output.Log("" + ++count + " quotes loaded - " + q.Describe);
                }
            } catch (Exception e) {
                Output.LogException("sql(" + sql + ") error : " + e.Message);
            }

            if (conn != null)
                conn.Close();
            return allQ;
        }
Exemple #4
0
 public override string GetCode(Quote q)
 {
     switch(q.market._value)
     {
         case market.type.ShangHai: return q.CodeStr + ".ss";
         case market.type.ShenZhen: return q.CodeStr + ".sz";
     }
     return "";
 }
Exemple #5
0
 public void Calc_MA(Quote q)
 {
     Output.Log("calculate MA trend of " + q.Name);
     if (q.History.Count == 0){
         Output.Log(q.Describe + " has no history");
         return;
     }
     DoCalc_MA(q);
 }
Exemple #6
0
 public List<dateData> GetHistory(Quote q)
 {
     date dt;
     dt.year = 0;
     dt.month = 0;
     dt.day = 0;
     string code = _src.GetCode(q);
     Output.Log("update quote " + q.Describe + " (desn't have all datas data)");
     List<dateData> dataList = DoGetHistory(code, dt, dt);
     return dataList;
 }
Exemple #7
0
        private void DoCalc_MA(Quote q )
        {
            List<dateData> history = q.History;
            ac_ma = Market.Static.ConnectAccount();
            ac_ma.found = 100000; //starting found : 10W

            int[] MADayUnit = new int[]{5, 10, 20, 60}; //a week
            Dictionary<int, double> MAValue = new Dictionary<int, double>();

            foreach (int ma in MADayUnit){
                MAValue [ma] = 0;
            }

            for (int index = 0; index < history.Count; index++){
                int count = index + 1;

                dateData ddM = history [index];
                double priceM = ddM._price._close;
                Console.Write(priceM);

                foreach (int ma in MADayUnit){
                    double maVal = MAValue [ma];

                    Output.LogNR(",");
                    if (count < ma){
                        maVal += priceM; //add only.
                    }
                    else{
                        if (count == ma){//calc 1st average
                            maVal += priceM;
                            maVal /= ma;
                        }
                        else{ //moving average
                            dateData ddM_n = history [index - ma];
                            double priceM_n = ddM_n._price._close;
                            maVal = maVal - (priceM_n / ma) + (priceM / ma);
                        }

                        maVal = Math.Round(maVal, 2);
                        Console.Write(maVal);
                    }

                    MAValue [ma] = maVal;
                }//for
                Output.Log("");

                MA_FindSellBuyPoint(q, MAValue, priceM);
            }

            Output.Log("MA calc Finished. Found : " + ac_ma.found);
        }
        /// <summary>
        /// Will replace the old entry.
        /// </summary>
        /// <param name='c'>
        /// quote to save into DB.
        /// </param>
        public void SaveQuoteInfo_DB(Quote c)
        {
            CreateDBInformation();
            SqliteConnection conn = null;
            string sql = "";

            try {
                conn = new SqliteConnection(souce);
                conn.Open();
                SqliteCommand cmd = conn.CreateCommand();
                sql = "insert or replace into " + TableNameInfo + " values(" + c.market + ", " + c.CodeInt + ", \"" + c.Name + "\");";
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            } catch (Exception e) {
                Output.LogException(" Save Quote Information error (Quest:" + c.Describe + "): " + e.Message);
            }
            if(conn != null ) conn.Close();
            Output.Log(sql + "\n");
        }
Exemple #9
0
 public virtual string GetCode(Quote q)
 {
     return q.CodeStr;
 }
Exemple #10
0
 public List<dateData> GetHistory(Quote q, date date_from, date date_to)
 {
     string code = _src.GetCode(q);
     Output.Log("update quote " + q.Describe + " (desn't have data " + date_from + " to " + date_to + ")");
     return DoGetHistory(code, date_from, date_to);
 }
Exemple #11
0
        private void MA_FindSellBuyPoint(Quote q, Dictionary<int, double> MAValue, double price)
        {
            double ma5 = MAValue[5];
            double ma10 = MAValue[10];
            double ma20 = MAValue[20];
            double ma60 = MAValue[60];

            if(ma5 < ma10 && ma10 < ma20 && ma20 < ma60){
                if(pt == pipeType.PT_NONE){
                    pt = pipeType.PT_DOWN;
                }
                else if(pt != pipeType.PT_UP){
                    //sell
                    long sell_vol = ac_ma.CanSell(q);
                    ac_ma.Sell(q, price, sell_vol);
                    //ac_ma.Sell();
                }
            }
            else if(ma5 > ma10 && ma10 > ma20 && ma20 > ma60){
                if(pt == pipeType.PT_NONE){
                    pt = pipeType.PT_UP;
                }
                else if(pt != pipeType.PT_UP){
                    //buy
                    long buy
                }
            }
        }
Exemple #12
0
 protected void SaveInfomation(information info)
 {
     Quote q = new Quote(info);
     q.SaveInformation();
     _callback(q);
 }
Exemple #13
0
 public void SaveHistory_DB(Quote q)
 {
     CreateDBHistory(q);
     SqliteConnection conn = new SqliteConnection(souce);
     conn.Open();
     SqliteCommand cmd = conn.CreateCommand();
     int entries = 0;
     foreach (dateData dt in q.History) {
         string sql = "insert or replace into " + GetTableNameHistory_DB(q) +
             " values(" + dt._date +
             ", " + dt._indic._volume +
             ", " + dt._price._open +
             ", " + dt._price._high +
             ", " + dt._price._low +
             ", " + dt._price._close +
             ");";
         cmd.CommandText = sql;
         cmd.ExecuteNonQuery();
         entries++;
     }
     conn.Close();
     Output.Log(q.Describe + " history save -> DB " + entries + " entries");
 }
Exemple #14
0
 //private WebStockDetail pWSD_G = new WebStockDetail_GOOG();
 private string GetTableNameHistory_DB(Quote q)
 {
     return "history_" + q.CodeStr + "_" + q.market;
 }
Exemple #15
0
        private void DoCreateDBHistory(Quote q)
        {
            string tblName = GetTableNameHistory_DB(q);
            SqliteConnection conn = null;
            const int tryTimes = 1;
            for (int i = 0; i <= tryTimes; i ++) {
                try {
                    if(conn != null){
                        conn.Close();
                    }
                    conn = new SqliteConnection(souce);
                    _DoCreateDBHistroy(conn, tblName);
                    break;
                } catch (Exception e) {
                    string msg = "create table[" + tblName + "] failed.";
                    if(i < tryTimes){
                        msg += "Will try again " + ( i + 1) + "/" + tryTimes + ".";
                    }
                    msg += "msg : " + e.Message;
                    Output.Log( msg );
                }
            }

            conn.Close();
        }
Exemple #16
0
 /// <summary>
 /// Creates the information DB.
 /// </summary>
 private void CreateDBHistory(Quote q)
 {
     if (!File.Exists(dbfile) || !TableExist(GetTableNameHistory_DB(q))) {
             DoCreateDBHistory(q);
     }
 }