int getMaxPeriod(CBreakStrategy bs) { List <int> ps = new List <int>(); ps.Add(bs.mAtrPeriod); ps.Add(bs.mBreakPeriod); ps.Add(bs.mAveFilterSmall); ps.Add(bs.mAveFilterBig); return(ps.Max()); }
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); }
//0:no break 1:buy -1:sell int checkBreak(CBreakStrategy bs) { int re = 0; List <OHLC> breakList = mPriceList.Skip(Math.Max(0, mPriceList.Count() - bs.mBreakPeriod)).ToList(); double highPrice = breakList.Take(bs.mBreakPeriod - 1).Select(i => i.c).Max(); if (breakList.Last().c > highPrice) { re = 1; } double lowPrice = breakList.Take(bs.mBreakPeriod - 1).Select(i => i.c).Min(); if (breakList.Last().c < lowPrice) { re = -1; } return(re); }
bool checkAveFilter(CBreakStrategy bs, int buyOrSell) { if (!bs.mUseAveFilter) { return(true); } bool re = false; double min = ave(bs.mAveFilterSmall); double max = ave(bs.mAveFilterBig); if (buyOrSell == 1 && min >= max) { re = true; } if (buyOrSell == -1 && min <= max) { re = true; } return(re); }
/* * public double calMargin(double vol,double price,double leverage) * { * double re = 0; * re = vol * mPro.mContractSize * price * mPro.mMarginRadio / leverage; * return re; * } */ public double getVol(CBreakStrategy bs, double point, DateTime d) { double loss = bs.mMoney * bs.mRisk / 100; loss = toCuy(loss, d); double v = loss / mPro.mTickVal / (point / mPro.mTickSize); int howV = (int)(v / mPro.mStepVol); v = howV * mPro.mStepVol; if (v < mPro.mMinVol) { v = mPro.mMinVol; } if (v > mPro.mMaxVol) { v = mPro.mMaxVol; } return(v); }
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; } } } }
private void button1_test_Click(object sender, EventArgs e) { button1_test.Enabled = false; CBreakStrategy BS = new CBreakStrategy(); BS.mAtrPeriod = int.Parse(textBox8_atrPeriod.Text.Trim()); BS.mBreakPeriod = int.Parse(textBox1_breakPeriod.Text.Trim()); BS.mCloseStopAtr = double.Parse(textBox2_closeStopAtr.Text.Trim()); BS.mImeStopAtr = double.Parse(textBox3_imeStopAtr.Text.Trim()); BS.mLeverage = double.Parse(textBox1_leverage.Text.Trim()); BS.mMoney = double.Parse(textBox7_money.Text.Trim()); BS.mInitMoney = BS.mMoney; BS.mRisk = double.Parse(textBox6_risk.Text.Trim()); BS.mAveFilterSmall = int.Parse(textBox4_aveSmall.Text.Trim()); BS.mAveFilterBig = int.Parse(textBox5_aveBig.Text.Trim()); BS.mUseAveFilter = checkBox1_averageFilter.Checked; DateTime startTime = dateTimePicker1_start.Value.Date; DateTime endTime = dateTimePicker2_end.Value.Date; BS.mStartTime = startTime; BS.mEndTime = endTime; Dictionary <string, CSymbol> symbols = new Dictionary <string, CSymbol>(); List <CSymbolPro> selSym = getSelSymbol(); foreach (var v in selSym) { CSymbol s = new CSymbol(); s.mPro = v; s.mCandleData = getCandleDataFromSqlite(s.mPro.mSymbolName, comboBox1_PricePeriod.SelectedItem.ToString()); symbols[s.mPro.mSymbolName] = s; } BS.mSymbolCount = symbols.Count; if (int.Parse(label21_taskCount.Text.Trim()) == 0) { progressBar1_test.Value = 0; progressBar1_test.Maximum = (endTime - startTime).Days; } else { progressBar1_test.Maximum += (endTime - startTime).Days; } BS.initHuiChe(startTime); Thread thread = new Thread(new ThreadStart(() => { Invoke(new Action(() => { int taskCount = int.Parse(label21_taskCount.Text.Trim()); taskCount++; label21_taskCount.Text = taskCount.ToString(); })); do { Invoke(new Action(() => { progressBar1_test.Value++; })); foreach (var v in symbols) { v.Value.step(startTime, BS); } BS.addMoneyLine(startTime); startTime = startTime.AddDays(1); }while (startTime.Date != endTime.Date); Invoke(new Action(() => { ChartForm cf = new ChartForm(BS); cf.Show(); int taskCount = int.Parse(label21_taskCount.Text.Trim()); taskCount--; label21_taskCount.Text = taskCount.ToString(); })); Log4netHelper.LogInfo("money:" + BS.mMoney.ToString("F2")); })); thread.IsBackground = true; thread.Start(); Invoke(new Action(() => { button1_test.Enabled = true; })); }
public ChartForm(CBreakStrategy bs) { mBs = bs; InitializeComponent(); }