private double coreLogic()//獲利動態加碼 { int stopPeriod = 0;//獲利反轉的間隔 int stopPrice = 0;//獲利反轉的目標點位 totalPureProfit = 0; lotIndex = Array_Begin_Index; //while (sourceFile.hasNext()) for (int z = 0; z < recordList.Count; z++) { try { if (recordList[z].TradePrice == 0) { recordList[z].TradePrice = recordList[z - 1].TradePrice; } if (recordList[z].TradePrice == 99999) { recordList[z].TradePrice = recordList[z - 1].TradePrice; } //nowLine = sourceFile.getLine(); //record = OriginalRecordConverter.getOriginalRecord(nowLine); //recordList.Add(record); record = recordList[z]; if (isStartOrder == false) { //maxTradePoint.TradePrice = 0; //minTradePoint.TradePrice = 99999; if (isPrevLose == true || isPrevWin == true || Dice.run(Random_Seed)) { tradeTime = record.TradeTime; tradeDateTime = record.TradeMoment; nowTradeType = prevTradeType = TradeType.SELL.GetHashCode(); //if (isPrevLose == true) //{ // if (prevTradeType == TradeType.BUY.GetHashCode()) // { // if (reverseEnabled) // { // nowTradeType = prevTradeType = TradeType.SELL.GetHashCode(); // } // else // { // nowTradeType = prevTradeType = TradeType.BUY.GetHashCode(); // } // } // else // { // if (reverseEnabled) // { // nowTradeType = prevTradeType = TradeType.BUY.GetHashCode(); // } // else // { // nowTradeType = prevTradeType = TradeType.SELL.GetHashCode(); // } // } //} //else // if (isPrevWin == true) // { // if (prevTradeType == TradeType.BUY.GetHashCode()) // { // if (reverseEnabled) // { // nowTradeType = prevTradeType = TradeType.SELL.GetHashCode(); // } // else // { // nowTradeType = prevTradeType = TradeType.BUY.GetHashCode(); // } // } // else // { // if (reverseEnabled) // { // nowTradeType = prevTradeType = TradeType.BUY.GetHashCode(); // } // else // { // nowTradeType = prevTradeType = TradeType.SELL.GetHashCode(); // } // } // } // else // if (!dealSellOrBuy(record, tradeDateTime)) // { // continue; // } //secondAfterTradeToActiveCheck = tradeDateTime.AddSeconds(ActiveProfitStartPeriod);//5秒內利潤擴大50點 //minuteAfterStartActiveProfit = tradeDateTime.AddMinutes(1);//開始動態停利檢查,每一分鐘一次 orderPrice = record.TradePrice; stopPrice = orderPrice; orderPriceList.Add(orderPrice); orderPointList.Add(record); if (nowTradeType == TradeType.BUY.GetHashCode()) { debugMsg("交易方式---->" + TradeType.BUY); } else if (nowTradeType == TradeType.SELL.GetHashCode()) { debugMsg("交易方式---->" + TradeType.SELL); } debugMsg("交易金額---->" + orderPrice); debugMsg("交易時間---->" + tradeDateTime); isStartOrder = true;//下單啦 addList.Clear(); } } else if (isStartOrder == true)//已經開始下單 { record.TradePrice = recordList[z].TradePrice; //if (record.TradePrice >= 99999) //{ // record.TradePrice = recordList[z - 1].TradePrice; //} //if (record.TradePrice < minTradePoint.TradePrice) //{ // minTradePoint = record; //} //if (record.TradePrice > maxTradePoint.TradePrice) //{ // maxTradePoint = record; //} //if (maxTradePoint.TradePrice > 9999) //{ // debugMsg("9999---->" + maxTradePoint.TradeMoment); //} if (nowTradeType == TradeType.BUY.GetHashCode()) { evenPrice = record.TradePrice; oneProfit = 0; if ( (orderPriceList.Count > 1 && record.TradePrice <= stopPrice - reverseLine[orderPriceList.Count]) || (orderPriceList.Count == 1 && record.TradePrice <= stopPrice - loseLine[orderPriceList.Count]) ) //反轉 { stopPrice = record.TradePrice; if (stopPrice > prevStopPrice)//繼續向上加碼 { continueLoseTime = 0; } prevStopPrice = stopPrice; for (int i = 0; i < orderPriceList.Count; i++) { debugMsg("加碼價位:" + orderPriceList[i]); debugMsg("加碼時間:" + orderPointList[i].TradeMoment); } debugMsg("認賠殺出"); debugMsg(" addTimes---->" + addList.Count); debugMsg(" 平倉口數---->" + lotArray[lotIndex]); debugMsg("平倉點數001---->" + evenPrice); debugMsg("平倉時間---->" + record.TradeMoment); if (continueLoseTime >= 2) { for (int i = 0; i < orderPriceList.Count; i++) { oneProfit += evenPrice - orderPriceList[i]; } orderPriceList.Clear(); continueLoseTime = 0; } else { for (int i = 0; i < Convert.ToInt16(lotArray[lotIndex]); i++) { oneProfit += evenPrice - orderPriceList[0]; orderPriceList.RemoveAt(0); orderPointList.RemoveAt(0); } lotIndex--; continueLoseTime++; if (lotIndex < Array_Begin_Index) { lotIndex = Array_Begin_Index; } } pureProfit = oneProfit * valuePerPoint - cost; if (oneProfit < 0 - loseLine[1]) { totalLoseProfit += pureProfit; } totalPureProfit += pureProfit; if (orderPriceList.Count > maxLot) //最大交易口數 { maxLot = orderPriceList.Count; } totalProfit += oneProfit; if (pureProfit > 0) { continueWinMoney += pureProfit; continueLossMoney = 0; if (maxContinueWinMoney < continueWinMoney) { maxContinueWinMoney = continueWinMoney; maxContinueWinMoneyTime = record.TradeMoment; } } else { continueWinMoney = 0; continueLossMoney += pureProfit; if (maxContinueLossMoney > continueLossMoney) { maxContinueLossMoney = continueLossMoney; maxContinueLossMoneyTime = record.TradeMoment; } } if (oneProfit > 0) { winVolume += orderPriceList.Count; winCount++; } else { loseVolume += orderPriceList.Count; if (orderPriceList.Count == 1) { loseCount++; } } //debugMsg("平倉前最低價:" + minTradePoint.TradePrice); //debugMsg("平倉前最低價的時間:" + minTradePoint.TradeMoment); //debugMsg("平倉前最高價:" + maxTradePoint.TradePrice); //debugMsg("平倉前最高價的時間:" + maxTradePoint.TradeMoment); if (hasAdd)//有加碼過,又回到原點 { reverseEnabled = false;//同方向 continueLossMoney = 0; } else { reverseEnabled = false;//一開始就賠錢,同方向 } debugMsg("純利:" + pureProfit); debugMsg("總純利:" + totalPureProfit); //debugMsg("停損策略:" + loseLine[nowLoseLineIndex]); //if (addList.Count >= 1) //{ // debugMsg("停利策略:" + winLine[addList.Count]); // debugMsg("反轉策略:" + reverseLine[addList.Count]); //} debugMsg("停損次數:" + loseCount); debugMsg("----------------------------------------------------------------------------------------------"); prevTradeType = TradeType.BUY.GetHashCode(); loseOut(); } else if ((record.TradePrice - orderPriceList[orderPriceList.Count - 1]) >= winLine[orderPriceList.Count]) //加碼 { //賺了XX點,加碼 //-------------------------------------------------------------------------------------------------------------------------------- // 加碼部分 //-------------------------------------------------------------------------------------------------------------------------------- //if (Convert.ToInt16(record.TradePrice - orderPriceList[orderPriceList.Count - 1]) >= winLine[orderPriceList.Count]) { if (orderPriceList.Count < lotLimit) { hasAdd = true; lotIndex++; addList.Add(1);//實際加碼的次數 for (int i = 0; i < Convert.ToInt16(lotArray[lotIndex]); i++) { orderPriceList.Add(record.TradePrice); orderPointList.Add(record); } stopPrice = record.TradePrice; continue; } } } } else if (nowTradeType == TradeType.SELL.GetHashCode()) { evenPrice = record.TradePrice; oneProfit = 0; if ( (orderPriceList.Count > 1 && record.TradePrice >= stopPrice + reverseLine[orderPriceList.Count]) || (orderPriceList.Count == 1 && record.TradePrice >= stopPrice + loseLine[orderPriceList.Count]) ) //反轉 { stopPrice = record.TradePrice; if (stopPrice < prevStopPrice)//繼續向下加碼 { continueLoseTime = 0; } prevStopPrice = stopPrice; for (int i = 0; i < orderPriceList.Count; i++) { debugMsg("加碼價位:" + orderPriceList[i]); debugMsg("加碼時間:" + orderPointList[i].TradeMoment); } debugMsg("認賠殺出"); debugMsg(" addTimes---->" + addList.Count); debugMsg(" 平倉口數---->" + lotArray[lotIndex]); debugMsg("平倉點數002---->" + evenPrice); debugMsg("平倉時間---->" + record.TradeMoment); if (continueLoseTime >= 2) { for (int i = 0; i < orderPriceList.Count; i++) { oneProfit += orderPriceList[i] - evenPrice; } orderPriceList.Clear(); continueLoseTime = 0; } else { for (int i = 0; i < Convert.ToInt16(lotArray[lotIndex]); i++) { oneProfit += orderPriceList[0] - evenPrice; orderPriceList.RemoveAt(0); orderPointList.RemoveAt(0); } lotIndex--; continueLoseTime++; if (lotIndex < Array_Begin_Index) { lotIndex = Array_Begin_Index; } } pureProfit = oneProfit * valuePerPoint - cost; if (oneProfit < 0 - loseLine[1]) { totalLoseProfit += pureProfit; } totalPureProfit += pureProfit; if (orderPriceList.Count > maxLot) //最大交易口數 { maxLot = orderPriceList.Count; } totalProfit += oneProfit; if (pureProfit > 0) { continueWinMoney += pureProfit; continueLossMoney = 0; if (maxContinueWinMoney < continueWinMoney) { maxContinueWinMoney = continueWinMoney; maxContinueWinMoneyTime = record.TradeMoment; } } else { continueWinMoney = 0; continueLossMoney += pureProfit; if (maxContinueLossMoney > continueLossMoney) { maxContinueLossMoney = continueLossMoney; maxContinueLossMoneyTime = record.TradeMoment; } } if (oneProfit > 0) { winVolume += orderPriceList.Count; winCount++; } else { loseVolume += orderPriceList.Count; if (orderPriceList.Count == 1) { loseCount++; } } //debugMsg("平倉前最低價:" + minTradePoint.TradePrice); //debugMsg("平倉前最低價的時間:" + minTradePoint.TradeMoment); //debugMsg("平倉前最高價:" + maxTradePoint.TradePrice); //debugMsg("平倉前最高價的時間:" + maxTradePoint.TradeMoment); if (hasAdd)//有加碼過 { reverseEnabled = false;//獲利後,同方向 } else { reverseEnabled = false;//一開始就賠錢,同方向 } debugMsg("純利:" + pureProfit); debugMsg("總純利:" + totalPureProfit); // debugMsg("停損策略:" + loseLine[nowLoseLineIndex]); //if (addList.Count >= 1) //{ // debugMsg("停利策略:" + winLine[addList.Count]); // debugMsg("反轉策略:" + reverseLine[addList.Count]); //} debugMsg("停損次數:" + loseCount); debugMsg("----------------------------------------------------------------------------------------------"); prevTradeType = TradeType.SELL.GetHashCode(); loseOut(); } else if ((orderPriceList[orderPriceList.Count - 1] - record.TradePrice) >= winLine[orderPriceList.Count]) //加碼 { //賺了XX點,加碼 //-------------------------------------------------------------------------------------------------------------------------------- // 加碼部分 //-------------------------------------------------------------------------------------------------------------------------------- { if (orderPriceList.Count < lotLimit) { hasAdd = true; lotIndex++; addList.Add(1);//實際加碼的次數 for (int i = 0; i < Convert.ToInt16(lotArray[lotIndex]); i++) { orderPriceList.Add(record.TradePrice); orderPointList.Add(record); } stopPrice = record.TradePrice; continue; } } } } }//下單結束 if (totalPureProfit < maxProfitLoss) //已達最大虧損水平線 { return totalProfit; } //if (totalLoseProfit < maxProfitLoss) //{ // return totalProfit; //} //if (record.TradeHour >= 13 && record.TradeMinute >= 30)//交易時間截止 //{ // if (isStartOrder == true) // { // oneProfit = 0; // evenPrice = record.TradePrice; // if (nowTradeType == TradeType.BUY.GetHashCode()) // { // for (int i = 0; i < orderPriceList.Count; i++) // { // oneProfit += evenPrice - orderPriceList[i]; // } // } // else if (nowTradeType == TradeType.SELL.GetHashCode()) // { // for (int i = 0; i < orderPriceList.Count; i++) // { // oneProfit += orderPriceList[i] - evenPrice; // } // } // totalProfit += oneProfit; // isStartOrder = false; // tradeCount++; // if (oneProfit > 0) // { // winVolume += orderPriceList.Count; // winCount++; // } // else // { // loseVolume += orderPriceList.Count; // loseCount++; // } // if (nowTradeType == TradeType.BUY.GetHashCode()) // { // debugMsg("時間到買入平倉"); // } // else if (nowTradeType == TradeType.SELL.GetHashCode()) // { // debugMsg("時間到賣出平倉"); // } // debugMsg("平倉點數---->" + evenPrice); // debugMsg("平倉時間---->" + record.TradeMoment); // debugMsg(" addTimes---->" + addList.Count); // debugMsg(" 平倉口數---->" + orderPriceList.Count); // for (int i = 0; i < orderPriceList.Count; i++) // { // debugMsg("加碼價位:" + orderPriceList[i]); // debugMsg("加碼時間:" + orderPointList[i].TradeMoment); // } // //debugMsg("平倉前最低價:" + minTradePoint.TradePrice); // //debugMsg("平倉前最低價的時間:" + minTradePoint.TradeMoment); // //debugMsg("平倉前最高價:" + maxTradePoint.TradePrice); // //debugMsg("平倉前最高價的時間:" + maxTradePoint.TradeMoment); // pureProfit = oneProfit * valuePerPoint - (orderPriceList.Count) * cost; // totalPureProfit += pureProfit; // debugMsg("純利:" + pureProfit); // debugMsg("總純利:" + totalPureProfit); // //debugMsg("停損策略:" + loseLine[nowLoseLineIndex]); // //if (addList.Count >= 1) // //{ // // debugMsg("停利策略:" + winLine[addList.Count]); // // debugMsg("反轉策略:" + reverseLine[addList.Count]); // //} // debugMsg("停利次數:" + winCount); // debugMsg("----------------------------------------------------------------------------------------------"); // return totalProfit; // } // return totalProfit; //}//end 交易時間截止 } catch (Exception ex) { Console.WriteLine(ex.Source); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }//end of while return totalProfit; }
private Boolean dealSellOrBuy(OriginalRecord record, DateTime tradeDateTime)//決定買或賣的方向,回傳値代表計算成功或是失敗 { try { sellOrBuyCheckPeriod = new int[checkCount]; int baseTimePeriod = 5; int[] basePrice = new int[checkCount]; minuteBeforeTradeTime = new DateTime[checkCount]; int[] direction = new int[checkCount]; for (int i = 0; i < checkCount; i++) { direction[i] = -1; sellOrBuyCheckPeriod[i] = baseTimePeriod; baseTimePeriod += 5; minuteBeforeTradeTime[i] = tradeDateTime.AddSeconds(0 - sellOrBuyCheckPeriod[i]); debugMsg("minuteBeforeTradeTime[" + i + "]:" + minuteBeforeTradeTime[i]); } for (int i = 0; i < checkCount; i++) { try { befofeRecord = RecordScanner.getRecordMinuteBeforeOrAfter(minuteBeforeTradeTime[i]);//XX鐘前的交易紀錄 if (befofeRecord == null) { return false; } basePrice[i] = befofeRecord.TradePrice;//交易基準 debugMsg("basePrice:" + i + ":" + basePrice[i]); } catch (Exception e) { Console.WriteLine("掃描失敗:" + e.Message + "---" + e.StackTrace + "---" + e.Source); } }//end for for (int i = 0; i < checkCount; i++) { if (record.TradePrice > basePrice[i])//目前交易金額大於XX分鐘前的交易金額 { direction[i] = TradeType.BUY.GetHashCode(); } else if (record.TradePrice < basePrice[i])//目前交易金額小於XX分鐘前的交易金額 { direction[i] = TradeType.SELL.GetHashCode(); } //if (i == 0) //{ // if (record.TradePrice > basePrice[i])//目前交易金額大於XX分鐘前的交易金額 // { // direction[i] = TradeType.BUY.GetHashCode(); // } // else if (record.TradePrice < basePrice[i])//目前交易金額小於XX分鐘前的交易金額 // { // direction[i] = TradeType.SELL.GetHashCode(); // } //} //else //{ // if (basePrice[i - 1] > basePrice[i])//目前交易金額大於XX分鐘前的交易金額 // { // direction[i] = TradeType.BUY.GetHashCode(); // } // else if (basePrice[i - 1] < basePrice[i])//目前交易金額小於XX分鐘前的交易金額 // { // direction[i] = TradeType.SELL.GetHashCode(); // } //} } for (int i = checkCount - 1; i > 0; i--) { if (direction[i] != direction[i - 1]) { return false; } } if (direction[0] == -1)//五個時間的價位都一樣 { return false; } nowTradeType = direction[0]; for (int i = 0; i < checkCount; i++) { debugMsg("direction[" + i + "]:" + direction[i]); } debugMsg("nowTradeType : " + nowTradeType); return true; } catch (Exception ex) { Console.WriteLine(ex.Source); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } return false; }
public List<OriginalRecord> prepareRecordList() { recordList = new List<OriginalRecord>(); try { for (int i = 0; i < sourceFileList.Count; i++) { sourceFile = sourceFileList[i]; sourceFile.prepareReader(); while (sourceFile.hasNext()) { nowLine = sourceFile.getLine(); record = OriginalRecordConverter.getOriginalRecord(nowLine); recordList.Add(record); }//end while sourceFile.close(); }//end for return recordList; } catch (Exception e) { Console.WriteLine(e.StackTrace); return null; } }
public ProcessedRecord(OriginalRecord originalRecord) { EventDateTime = originalRecord.EventDateTime; Town = originalRecord.Town; Country = originalRecord.Country; }
public static OriginalRecord getOriginalRecord(String currentLine) { try { OriginalRecord record = new OriginalRecord(); String currentPrice = ""; int tradeCurrentVolumn = 0; String[] tmpData = new String[10]; tmpData = currentLine.Split(','); if (tmpData == null || tmpData[1] == null) { return null; } record.TradeDate = tmpData[0].Trim(); record.TradeTime = tmpData[3].Trim(); record.TradeYearMonth = tmpData[2].Trim(); ; if (record.TradeYearMonth.Contains('/')) { currentPrice = tmpData[6].Trim(); } else { currentPrice = tmpData[4].Trim(); } tradeCurrentVolumn = int.Parse(tmpData[5].Trim()); record.TradePrice =Convert.ToInt32(currentPrice); record.TradeVolumn = tradeCurrentVolumn / 2; record.TradeYear = int.Parse(record.TradeDate.Substring(0, 4)); record.TradeMonth = int.Parse(record.TradeDate.Substring(4, 2)); record.TradeDay = int.Parse(record.TradeDate.Substring(6, 2)); record.TradeHour = int.Parse(record.TradeTime.Substring(0, 2)); record.TradeMinute = int.Parse(record.TradeTime.Substring(2, 2)); record.TradeSecond = int.Parse(record.TradeTime.Substring(4, 2)); record.TradeMoment = new DateTime(record.TradeYear, record.TradeMonth, record.TradeDay, record.TradeHour, record.TradeMinute, record.TradeSecond); return record; } catch (Exception ex) { Console.WriteLine(ex.Source); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return null ; } }
private static void Analyze(OriginalRecord originalRecord1, OriginalRecord originalRecord2, List <ResultRecord> resultRecords) { if (!CanDo(originalRecord1.route, originalRecord2.route)) { AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(originalRecord1.route.Count - 1)); AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(originalRecord2.route.Count - 1)); return; } IEnumerable <BusStop> intersection = originalRecord1.route.Intersect <BusStop>(originalRecord2.route); int numOfIntersection = intersection.Count(); int index1 = 0, index2 = 0, count1 = 0, count2 = 0; index1 = originalRecord1.route.FindIndex(item => item == intersection.ElementAt(0)); index2 = originalRecord2.route.FindIndex(item => item == intersection.ElementAt(0)); count1 = originalRecord1.route.Count(); count2 = originalRecord2.route.Count(); //if first trip's head is longer than second and tail is the same long if (index1 > index2 && count1 - index1 == count2 - index2) { AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(originalRecord1.route.Count - 1)); } //if second trip's head is longer than first and tail is the same long else if (index2 > index1 && count1 - index1 == count2 - index2) { AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(originalRecord2.route.Count - 1)); } //if first trip's tail is longer than second and head is the same long else if (index1 == index2 && count1 - index1 > count2 - index2) { AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(originalRecord1.route.Count - 1)); } //if second trip's tail is longer than second and head is the same long else if (index1 == index2 && count2 - index2 > count1 - index1) { AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(originalRecord2.route.Count - 1)); } //if first trip's head is longer and second's tail is longer else if (index1 > index2 && count2 - index2 > count1 - index1) { AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(index1)); AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(originalRecord2.route.Count - 1)); } //if second trip's head is longer and first's tail is longer else if (index2 > index1 && count1 - index1 > count2 - index2) { AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(index2)); AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(originalRecord1.route.Count - 1)); } //if first trip's head and tail are all longer else if (index1 > index2 && count1 - index1 > count2 - index2) { AddTrip(resultRecords, originalRecord1.busNo, originalRecord1.route.ElementAt(0), originalRecord1.route.ElementAt(originalRecord1.route.Count - 1)); } //if second trip's head and tail are all longer else if (index2 > index1 && count2 - index2 > count1 - index1) { AddTrip(resultRecords, originalRecord2.busNo, originalRecord2.route.ElementAt(0), originalRecord2.route.ElementAt(originalRecord2.route.Count - 1)); } else { } }