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; })); }