private List <netvalueDaily> getNetValueDaily(List <StockTransaction> etf, double[] netvalue) { List <netvalueDaily> nv = new List <netvalueDaily>(); for (int i = 0; i < netvalue.Length; i++) { if (etf[i].DateTime.TimeOfDay == new TimeSpan(14, 59, 00)) { var nvtoday = new netvalueDaily(); nvtoday.date = etf[i].DateTime.Date; nvtoday.netvalue = netvalue[i]; nv.Add(nvtoday); } } return(nv); }
private List <double> getNetValueDaily(List <StockTransaction> etf, double[] netvalue) { List <double> nv = new List <double>(); List <netvalueDaily> nv2 = new List <netvalueDaily>(); for (int i = 0; i < netvalue.Length; i++) { if (etf[i].DateTime.TimeOfDay == new TimeSpan(14, 59, 00)) { nv.Add(netvalue[i]); var nvtoday = new netvalueDaily(); nvtoday.date = etf[i].DateTime.Date; nvtoday.netvalue = netvalue[i]; nv2.Add(nvtoday); } } DataTableExtension.SaveCSV(DataTableExtension.ToDataTable <netvalueDaily>(nv2), "E:\\result\\td\\netvalue.csv"); return(nv); }
//第二类布林带的使用,当价格上穿上轨1的时候买入,当下轨下穿下轨1的时候卖出 private bool bollingerBrand2(List <StockTransaction> underlying1, List <StockTransaction> underlying2, double[] signal, int duration, List <BollingerBandwithPrice> myboll, ref List <OneByOneTransaction> data, ref List <netvalueDaily> netvalueList) { netvalueList = new List <netvalueDaily>(); data = new List <OneByOneTransaction>(); OneByOneTransaction transaction = new OneByOneTransaction(); double position1 = 0; double position2 = 0; double cash = 1; double slipRatio = 0.003; double nv = 1; double trailingProfit = 0; bool trailingStop = false; double trailingParameter = 0.02; netvalueDaily nvToday = new netvalueDaily(); nvToday.netvalue = nv; nvToday.date = underlying1[0].DateTime; bool trade = true; int length = signal.Length; netvalueList.Add(nvToday); for (int i = 1; i < length; i++) { trade = true; if (underlying1[i].TradeStatus != "交易" || underlying2[i].TradeStatus != "交易") { trade = false; } double underlying1AvgPrice = underlying1[i].AdjFactor * underlying1[i].Amount / underlying1[i].Volume; double underlying2AvgPrice = underlying2[i].AdjFactor * underlying2[i].Amount / underlying2[i].Volume; double underlying1ClosePrice = underlying1[i].AdjFactor * underlying1[i].Close; double underlying2ClosePrice = underlying2[i].AdjFactor * underlying2[i].Close; double pairPriceYesterday = Math.Log(underlying1[i - 1].AdjFactor * underlying1[i - 1].Close / (underlying2[i - 1].AdjFactor * underlying2[i - 1].Close)); var today = underlying1[i].DateTime; //空仓情况,看情况开仓 if (position1 == 0 && position2 == 0 && trade == true) { //上穿上轨1 if (signal[i - 1] == 3) { cash = cash * (1 - slipRatio); position1 = cash / 2 / underlying1AvgPrice; position2 = -cash / 2 / underlying2AvgPrice; transaction.openTime = today; transaction.position = 1; transaction.openPrice = Math.Log(underlying1AvgPrice / underlying2AvgPrice); trailingProfit = 0; trailingStop = false; } //下穿上轨1 else if (signal[i - 1] == -1) { cash = cash * (1 - slipRatio); position1 = -cash / 2 / underlying1AvgPrice; position2 = cash / 2 / underlying2AvgPrice; transaction.openTime = today; transaction.position = -1; transaction.openPrice = Math.Log(underlying1AvgPrice / underlying2AvgPrice); trailingProfit = 0; trailingStop = false; } } //多头情况 else if (position1 > 0 && position2 < 0 && trade == true) { //追踪止损 if (trailingStop == true) { cash += position1 * underlying1AvgPrice * (1 - slipRatio) + position2 * underlying2AvgPrice * (1 + slipRatio); position1 = 0; position2 = 0; transaction.closeTime = today; transaction.closePrice = Math.Log(underlying1AvgPrice / underlying2AvgPrice); transaction.closeStatus = "追踪止损"; data.Add(transaction); transaction = new OneByOneTransaction(); trailingStop = false; trailingProfit = 0; } } //空头情况 else if (position1 < 0 && position2 > 0 && trade == true) { //追踪止损 if (trailingStop == true) { cash += position1 * underlying1AvgPrice * (1 + slipRatio) + position2 * underlying2AvgPrice * (1 - slipRatio); position1 = 0; position2 = 0; transaction.closeTime = today; transaction.closePrice = Math.Log(underlying1AvgPrice / underlying2AvgPrice); transaction.closeStatus = "追踪止损"; data.Add(transaction); transaction = new OneByOneTransaction(); trailingStop = false; trailingProfit = 0; } } //计算追踪止损点位 double closePrice = Math.Log(underlying1ClosePrice / underlying2ClosePrice); if (position1 > 0 && position2 < 0) //多头 { if (closePrice > trailingProfit + transaction.openPrice) { trailingProfit = closePrice - transaction.openPrice; } else if (trailingProfit - (closePrice - transaction.openPrice) > trailingParameter) { trailingStop = true; } } else if (position1 < 0 && position2 > 0) //空头 { if (closePrice < -trailingProfit + transaction.openPrice) { trailingProfit = -closePrice + transaction.openPrice; } else if (trailingProfit - (-closePrice + transaction.openPrice) > trailingParameter) { trailingStop = true; } } nv = cash + position1 * underlying1ClosePrice + position2 * underlying2ClosePrice; nvToday = new netvalueDaily(); nvToday.netvalue = nv; nvToday.date = today; netvalueList.Add(nvToday); } return(true); }
private bool ComputeDualTrust(List <DateTime> tradedays, List <StockTransaction> underlying, double[] signal, double[] range, double trailingParameter, ref List <OneByOneTransaction> data, ref List <netvalueDaily> netvalueList) { int lengthOfDays = tradedays.Count(); double originalCash = 10000; double cash = originalCash; double position = 0; double maxProfit = 0; bool tradable = false; OneByOneTransaction transaction = new OneByOneTransaction(); for (int i = 0; i < lengthOfDays; i++) { if (range[i] < 10) { continue; } int j = 0; int index = 0; StockTransaction dataNow = new StockTransaction(); double avgPrice = 0; for (j = 0; j < minutes - 5; j++) { index = i * minutes + j; dataNow = underlying[index]; if (dataNow.Volume > 0) { tradable = true; } else { tradable = false; continue; } avgPrice = dataNow.Amount / dataNow.Volume / multiplicator; if (position == 0) //开仓 { if (signal[index] == 1) //开多头 { position = 1; cash = cash - avgPrice - avgPrice * slipRatio; transaction = new OneByOneTransaction(); transaction.position = 1; transaction.openTime = dataNow.DateTime; transaction.openPrice = avgPrice; maxProfit = 0; } if (signal[index] == -1) //开空头 { position = -1; cash = cash + avgPrice - avgPrice * slipRatio; transaction = new OneByOneTransaction(); transaction.position = -1; transaction.openTime = dataNow.DateTime; transaction.openPrice = avgPrice; maxProfit = 0; } } else { if (position == 1) { if ((dataNow.Open / transaction.openPrice - 1) < maxProfit - trailingParameter) { cash = cash + avgPrice - avgPrice * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "追踪止损"; data.Add(transaction); transaction = new OneByOneTransaction(); } } if (position == -1) { if ((transaction.openPrice / dataNow.Open - 1) < maxProfit - trailingParameter) { cash = cash - avgPrice - avgPrice * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "追踪止损"; data.Add(transaction); transaction = new OneByOneTransaction(); } } } if (position == 1) { if ((dataNow.Close / transaction.openPrice - 1) > maxProfit) { maxProfit = (dataNow.Close / transaction.openPrice - 1); } } if (position == -1) { if ((transaction.openPrice / dataNow.Close - 1) > maxProfit) { maxProfit = (transaction.openPrice / dataNow.Close - 1); } } } //日内最后3分钟平仓 j = minutes - 3; index = i * minutes + j; dataNow = underlying[index]; if (dataNow.Volume > 0) { tradable = true; } else { tradable = false; } avgPrice = dataNow.Amount / dataNow.Volume / multiplicator; if (position == 1 && tradable) { cash = cash + avgPrice - avgPrice * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "收盘强平"; data.Add(transaction); transaction = new OneByOneTransaction(); } if (position == -1 && tradable) { cash = cash - avgPrice - avgPrice * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "收盘强平"; data.Add(transaction); transaction = new OneByOneTransaction(); } //计算每日收盘时候的净值 index = i * minutes + minutes - 1; dataNow = underlying[index]; netvalueDaily nvToday = new netvalueDaily(); nvToday.date = dataNow.DateTime.Date; nvToday.netvalue = cash + position * dataNow.Close; netvalueList.Add(nvToday); } return(true); }
private bool dualTrust(stockInfo stock, Dictionary <DateTime, parameterPair> parameters, ref List <netvalueDaily> nv, ref List <OneByOneTransaction> transactionData) { if (allStocks.ContainsKey(stock.code) == false) { return(false); } var data = allStocks[stock.code]; double cash = 10000; double position = 0; var myTradeDays = dateRepo.GetStockTransactionDate(stock.startDate, stock.endDate); OneByOneTransaction transaction = new OneByOneTransaction(); //观察期限30分钟 int duration = 30; int minutes = 240; foreach (var date in myTradeDays) { if (data.ContainsKey(date) == false || parameters.ContainsKey(date) == false) { return(false); } var stockToday = data[date]; var para = parameters[date]; double k1 = para.parameter1; double k2 = para.parameter2; double trailing = para.parameter3; double maxProfit = 0; bool tradable = true; if (para.existGoodParameter == false) { tradable = false; } //通过每日前30分钟计算指标 double HH = 0; //最高价的最高价 double HC = 0; //收盘价的最高价 double LC = 99999; //收盘价的最低价 double LL = 99999; //最低价的最低价 double Range = 0; double longPoint = -1; double shortPoint = -1; for (int j = 0; j < duration; j++) { var dataNow = stockToday[j]; if (dataNow.High > HH) { HH = dataNow.High; } if (dataNow.Close > HC) { HC = dataNow.Close; } if (dataNow.Close < LC) { LC = dataNow.Close; } if (dataNow.Low < LL) { LL = dataNow.Low; } } Range = Math.Max(HH - LC, HC - LL); longPoint = stockToday[duration].Open + k1 * Range; shortPoint = stockToday[duration].Open - k2 * Range; for (int j = duration; j < minutes; j++) { var dataNow = stockToday[j]; var dataLast = stockToday[j - 1]; double avgPrice = dataNow.Close; if (dataNow.Volume == 0) { tradable = false; } else { avgPrice = dataNow.Amount / dataNow.Volume / multiple; if (para.existGoodParameter == true) { tradable = true; } } if (position == 0 && j <= minutes - 10 && tradable) { //多头信号 if (dataNow.Open > longPoint && dataLast.Open < longPoint) { position = 0.9 * Math.Floor(cash * 1000 / avgPrice) / 1000; cash = cash - avgPrice * position - avgPrice * position * slipRatio; transaction = new OneByOneTransaction(); transaction.position = position; transaction.openTime = dataNow.DateTime; transaction.openPrice = avgPrice; maxProfit = 0; } //空头信号 else if (dataNow.Open < shortPoint && dataLast.Open > shortPoint) { position = -0.9 * Math.Floor(cash * 1000 / avgPrice) / 1000; cash = cash - avgPrice * position + avgPrice * position * slipRatio; transaction = new OneByOneTransaction(); transaction.position = position; transaction.openTime = dataNow.DateTime; transaction.openPrice = avgPrice; maxProfit = 0; } } else if (position != 0 && j <= minutes - 5 && tradable) //非收盘前5分钟,按追踪止损平仓 { if (position > 0) { if ((dataNow.Open / transaction.openPrice - 1) < maxProfit - trailing) { cash = cash + avgPrice * position - avgPrice * position * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "追踪止损"; transactionData.Add(transaction); transaction = new OneByOneTransaction(); } } if (position < 0) { if ((transaction.openPrice / dataNow.Open - 1) < maxProfit - trailing) { cash = cash + avgPrice * position + avgPrice * position * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "追踪止损"; transactionData.Add(transaction); transaction = new OneByOneTransaction(); } } } else if (position != 0 && j > minutes - 5 && tradable)//收盘前5分钟强制平仓 { if (position > 0) { cash = cash + avgPrice * position - avgPrice * position * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "收盘强平"; transactionData.Add(transaction); transaction = new OneByOneTransaction(); } if (position < 0) { cash = cash + avgPrice * position + avgPrice * position * slipRatio; position = 0; transaction.closePrice = avgPrice; transaction.closeTime = dataNow.DateTime; transaction.closeStatus = "收盘强平"; transactionData.Add(transaction); transaction = new OneByOneTransaction(); } } //计算追踪止损的参数 if (position > 0) { if ((dataNow.Close / transaction.openPrice - 1) > maxProfit) { maxProfit = (dataNow.Close / transaction.openPrice - 1); } } if (position < 0) { if ((transaction.openPrice / dataNow.Close - 1) > maxProfit) { maxProfit = (transaction.openPrice / dataNow.Close - 1); } } if (j == minutes - 1) { dataNow = stockToday[j]; netvalueDaily nvToday = new netvalueDaily(); nvToday.date = dataNow.DateTime.Date; nvToday.netvalue = cash + position * dataNow.Close; nv.Add(nvToday); } } } return(true); }