public bool collectPrice(CBreakStrategy bs, OHLC prePrice) { if (mPriceList.Count < bs.mBreakPeriod - 1 || mPriceList.Count < bs.mAtrPeriod - 1 || mPriceList.Count < bs.mAveFilterSmall - 1 || mPriceList.Count < bs.mAveFilterBig - 1) { mPriceList.Add(prePrice); return(true); } return(false); }
double calAtr(CBreakStrategy bs) { double re = 0; List <OHLC> atrList = mPriceList.Skip(Math.Max(0, mPriceList.Count() - bs.mAtrPeriod)).ToList(); List <double> mtr = new List <double>(); for (int i = atrList.Count - 1; i > 0; i--) { OHLC pre = atrList[i - 1]; OHLC cur = atrList[i]; double[] m = { Math.Abs(cur.h - cur.l), Math.Abs(cur.h - pre.c), Math.Abs(cur.l - pre.c) }; mtr.Add(m.Max()); } re = mtr.Average(); return(re); }
public static bool getRecentPrice(string symbol, DateTime d, out OHLC re, string gran = "D") { bool r = true; re = new OHLC(); try { string requestString = CConfig.mServer + "/v3/instruments/" + symbol + "/candles"; requestString = requestString + "?" + "price=ABM&from=" + date2Str(d) + "&count=1&granularity=" + gran; HttpWebRequest request = WebRequest.CreateHttp(requestString); request.Headers[HttpRequestHeader.Authorization] = "Bearer " + CConfig.mToken; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { throw new WebException("Web request error."); } Stream s = response.GetResponseStream(); StreamReader sr = new StreamReader(s); var jr = new JsonTextReader(sr); var serializer = new JsonSerializer(); Instrument im = serializer.Deserialize <Instrument>(jr); foreach (var v in im.candles) { re = v.mid; } } } catch (Exception) { r = false; } return(r); }
public void step(DateTime curTime, CBreakStrategy bs) { if (!mCandleData.Keys.Contains(curTime.Date)) { return; } OHLC prePrice = mCandleData[curTime].mPrice; if (!collectPrice(bs, prePrice)) { mPriceList.Add(prePrice); int bigPeriod = getMaxPeriod(bs); while (mPriceList.Count > bigPeriod) { mPriceList.RemoveAt(0); } double atr = calAtr(bs); if (!mHasVol) { int breakRe = checkBreak(bs); if (breakRe == 1 && checkAveFilter(bs, breakRe)) { //break open vol buy mBuyOrSell = 1; mTradePrice = prePrice.c; mCloseStopPrice = prePrice.c - atr * bs.mCloseStopAtr; mImeStopPrice = prePrice.c - atr * bs.mImeStopAtr; mVol = getVol(bs, atr * (bs.mCloseStopAtr > bs.mImeStopAtr ? bs.mCloseStopAtr : bs.mImeStopAtr), curTime); mHasVol = true; Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " buy:" + mVol.ToString("F2") + " price:" + mTradePrice.ToString("F2") + " time:" + curTime.ToShortDateString()); } if (breakRe == -1 && checkAveFilter(bs, breakRe)) { //break open vol sell mBuyOrSell = -1; mTradePrice = prePrice.c; mCloseStopPrice = prePrice.c + atr * bs.mCloseStopAtr; mImeStopPrice = prePrice.c + atr * bs.mImeStopAtr; mVol = getVol(bs, atr * (bs.mCloseStopAtr > bs.mImeStopAtr ? bs.mCloseStopAtr : bs.mImeStopAtr), curTime); mHasVol = true; Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " sell:" + mVol.ToString("F2") + " price:" + mTradePrice.ToString("F2") + " time:" + curTime.ToShortDateString()); } } else { bool stop = false; //check stop if (mBuyOrSell == 1) { if (!stop && prePrice.l < mImeStopPrice) { stop = true; double profit = calLoss(mVol, mImeStopPrice - mTradePrice, curTime); bs.changeMoney(curTime, mPro.mSymbolTypeName, mPro.mSymbolName, profit); Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " buy stop at ime price:" + mImeStopPrice.ToString("F2") + " profit:" + profit.ToString("F2") + " all_money:" + bs.mMoney.ToString("F2") + " time:" + curTime.ToShortDateString()); } if (!stop && prePrice.c < mCloseStopPrice) { stop = true; double profit = calLoss(mVol, prePrice.c - mTradePrice, curTime); bs.changeMoney(curTime, mPro.mSymbolTypeName, mPro.mSymbolName, profit); Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " buy stop at close price:" + prePrice.c.ToString("F2") + " profit:" + profit.ToString("F2") + " all_money:" + bs.mMoney.ToString("F2") + " time:" + curTime.ToShortDateString()); } if (!stop) { double newCloseStopPrice = prePrice.c - bs.mCloseStopAtr * atr; double newImeStopPrice = prePrice.c - bs.mImeStopAtr * atr; if (newCloseStopPrice > mCloseStopPrice) { mCloseStopPrice = newCloseStopPrice; } if (newImeStopPrice > mImeStopPrice) { mImeStopPrice = newImeStopPrice; } //Log4netHelper.LogInfo(string.Format("buy update price:close price-{0} ime price-{1}", mCloseStopPrice.ToString("F2"), mImeStopPrice.ToString("F2"))); } } if (mBuyOrSell == -1) { if (!stop && prePrice.h > mImeStopPrice) { stop = true; double profit = calLoss(mVol, mTradePrice - mImeStopPrice, curTime); bs.changeMoney(curTime, mPro.mSymbolTypeName, mPro.mSymbolName, profit); Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " sell stop at ime price:" + mImeStopPrice.ToString("F2") + " profit:" + profit.ToString("F2") + " all_money:" + bs.mMoney.ToString("F2") + " time:" + curTime.ToShortDateString()); } if (!stop && prePrice.c > mCloseStopPrice) { stop = true; double profit = calLoss(mVol, mTradePrice - prePrice.c, curTime); bs.changeMoney(curTime, mPro.mSymbolTypeName, mPro.mSymbolName, profit); Log4netHelper.LogInfo("symbol:" + mPro.mSymbolName + " sell stop at close price:" + prePrice.c.ToString("F2") + " profit:" + profit.ToString("F2") + " all_money:" + bs.mMoney.ToString("F2") + " time:" + curTime.ToShortDateString()); } if (!stop) { double newCloseStopPrice = prePrice.c + bs.mCloseStopAtr * atr; double newImeStopPrice = prePrice.c + bs.mImeStopAtr * atr; if (newCloseStopPrice < mCloseStopPrice) { mCloseStopPrice = newCloseStopPrice; } if (newImeStopPrice < mImeStopPrice) { mImeStopPrice = newImeStopPrice; } //Log4netHelper.LogInfo(string.Format("sell update price:close price-{0} ime price-{1}", mCloseStopPrice.ToString("F2"), mImeStopPrice.ToString("F2"))); } } if (stop) { mHasVol = false; } } } }
public CCandleData(OHLC ol, DateTime dt) { mPrice = ol; mCandleTime = dt.Date; }
public CCandleData(OHLC ol, string dateStr) { mPrice = ol; mCandleTime = CHelp.str2Date(dateStr).Date; }
public static double toUSD(string currency, DateTime d) { double re = 1.0; if (currency == "USD") { re = 1.0; } else { string symbol = ""; if (currency == "JPY") { symbol = "USD_JPY"; } if (currency == "GBP") { symbol = "GBP_USD"; } if (currency == "EUR") { symbol = "EUR_USD"; } if (currency == "CHF") { symbol = "USD_CHF"; } if (currency == "CAD") { symbol = "USD_CAD"; } if (currency == "AUD") { symbol = "AUD_USD"; } if (currency == "NZD") { symbol = "NZD_USD"; } if (currency == "HKD") { symbol = "USD_HKD"; } OHLC p = new OHLC(); getRecentPrice(symbol, d, out p); if (symbol.StartsWith("USD")) { return(1.0 / p.c); } else { return(p.c); } /* * OHLC p = new OHLC(); * string symbol = currency + "_USD"; * bool hasP = getRecentPrice(symbol, d, out p); * if(hasP) * { * re = p.c; * } * if(!hasP) * { * symbol = "USD_" + currency; * hasP = getRecentPrice(symbol, d, out p); * re = 1/p.c; * } */ } return(re); }