public static void setHLOC(MarketDataElement data, double high, double low, double open, double close) { data.high = high; data.low = low; data.open = open; data.close = close; }
private static Series <DateTime, MarketDataElement> addMarketDataElementWithTimeOnlyToSeries(DateTime dateTime, Series <DateTime, MarketDataElement> series) { MarketDataElement currentElement = MarketDataUtil.createHLOC(dateTime, "HLOC", "HSI", "FUT"); currentElement.volume = 900; MarketDataUtil.setHLOC(currentElement, 20200, 20000, 20050, 20150); if (series == null) { series = new SeriesBuilder <DateTime, MarketDataElement>() { { dateTime, currentElement } }.Series; } else { if (!series.ContainsKey(dateTime)) { series = series.Merge(new SeriesBuilder <DateTime, MarketDataElement>() { { dateTime, currentElement } }.Series); } } return(series); }
/*static private double getMaxLevel(double max, String direction) * { * double residure = max % CELL_UNIT; * * if (RISE.Equals(direction)) * return max - residure; * * if (DROP.Equals(direction)) * return max + (CELL_UNIT - residure); * * return INVALID_NUM; * }*/ static private Boolean isOverLastMax(String lastDir, MarketDataElement item, double lastMax) { if (RISE.Equals(lastDir)) { if (item.high > lastMax) { return(true); } else { return(false); } } else { if (item.low < lastMax) { return(true); } else { return(false); } } }
static private Boolean isReverseThreshold(String lastDir, double maxLevel, MarketDataElement item, double threshold) { double rThreshold; if (RISE.Equals(lastDir)) { rThreshold = maxLevel - threshold; if (item.low <= rThreshold) { return(true); } else { return(false); } } else { rThreshold = maxLevel + threshold; if (item.high >= rThreshold) { return(true); } else { return(false); } } }
protected void dayEndCloseTrade(Series <DateTime, MarketDataElement> series) { if (dayClosed) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); DateTime current = lastItem.time; String strCurrent = String.Format("{0:yyyyMMdd}", current); DateTime dayEnd = new DateTime(current.Year, current.Month, current.Day, 16, 13, 0); if (current < dayEnd) { return; } dayClosed = true; double tickClose = calculateMid(); if (tickClose.Equals(double.NaN)) { return; } stgHelper.cancelPendingTrades(); if (execution.isPositionSignalBothEmpty()) { return; } double enterPrice = lastExecutePrice; if (execution.CurrentMarketPosition > 0) { String reason = "[Strategy] : Dayend Exit Sell Generated !!! | MarketDataElement=" + lastItem.ToString(); log.Info("[Strategy] : Cut Loss SELL Exit Generated !!!"); log.Info(reason); if (sentOrder) { stgHelper.placeMarketTrade(AppConstant.SELL_SIGNAL, "Day Exit Rule", tickClose, reason, 1, 1); clearCache(); } } if (execution.CurrentMarketPosition < 0) { String reason = "[Strategy] : Dayend Exit BUY Generated !!! | MarketDataElement=" + lastItem.ToString(); log.Info("[Strategy] : Cut Loss BUY Exit Generated !!!"); log.Info(reason); if (sentOrder) { stgHelper.placeMarketTrade(AppConstant.BUY_SIGNAL, "Day Exit Rule", tickClose, reason, 1, 1); clearCache(); } } }
public static MarketDataElement createHLOC(DateTime time, String elementType, String symbol, String contractType) { MarketDataElement tempObj = new MarketDataElement(); tempObj.time = time; tempObj.elementType = elementType; tempObj.symbol = symbol; tempObj.contractType = contractType; return(tempObj); }
static public List <SAPaFRow> addNewRow(List <SAPaFRow> rows, MarketDataElement item, double pullBackThreshold) { SAPaFRow lastRow = rows[rows.Count - 1]; String lastDir = lastRow.direction; double lastStart = lastRow.startPt; double lastMax = lastRow.maxPt; //double lastMaxLevel = lastRow.maxLevel; Boolean lbOverLastMaxLevel = isOverLastMax(lastDir, item, lastMax); Boolean lbReverseThreshold = isReverseThreshold(lastDir, lastMax, item, pullBackThreshold); String newDir = ""; double newStart = 0.0; double newMax = 0.0; //double newMaxLevel = 0.0; if (!lbOverLastMaxLevel && lbReverseThreshold) { newDir = getOppDirection(lastDir); newStart = lastMax; newMax = getNewReverseMaxValue(lastDir, item); SAPaFRow row = new SAPaFRow(); //newMaxLevel = getMaxLevel(newMax, newDir); row.direction = newDir; row.startPt = newStart; row.maxPt = newMax; //row.maxLevel = newMaxLevel; row.startTime = item.time; rows.Add(row); } else { newDir = lastDir; newStart = lastStart; if (lbOverLastMaxLevel) { newMax = getUpdateMaxValue(lastDir, item); } else { newMax = lastMax; } SAPaFRow row = rows[rows.Count - 1]; //newMaxLevel = getMaxLevel(newMax, newDir); row.direction = newDir; row.startPt = newStart; row.maxPt = newMax; //row.maxLevel = newMaxLevel; //row.startTime = item.time; } return(rows); }
static private double getUpdateMaxValue(String lastDir, MarketDataElement item) { if (RISE.Equals(lastDir)) { return(item.high); } else { return(item.low); } }
public void test_updateRTBarSeriesActions_init() { //[Test: Init Case, TimeBarSeries is null and currentCompleteRTB is not null] DateTime time1 = new DateTime(2015, 10, 21, 10, 0, 0, DateTimeKind.Local); AppMDManager appMDManager = createManager_updateRTBarSeriesActions_init(time1); appMDManager.updateRTBarSeriesActions(); MarketDataElement element = appMDManager.getTimeBarSeries().Get(time1); Assert.AreEqual(time1, element.time); Assert.AreEqual(1, appMDManager.getTimeBarSeries().KeyCount); }
protected void calculateCurrentOpen(Series <DateTime, MarketDataElement> series) { if (currentOpenPrice != 0) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); MarketDataElement openItem = MarketDateElementHelper.getFirstAvaMarketData(series, MarketDataUtil.getStartTime(lastItem.time)); currentOpenPrice = openItem.open; log.Info("[calculateCurrentOpen] : currentOpen = " + currentOpenPrice); }
public void test_updateRTBarSeriesActions_cont_negative() { //[Test: Continuou Case, TimeBarSeries is not null and currentCompleteRTB is not null] DateTime time1 = new DateTime(2015, 10, 21, 10, 0, 0, DateTimeKind.Local); DateTime time2 = new DateTime(2015, 10, 21, 10, 1, 0, DateTimeKind.Local); AppMDManager appMDManager = createManager_updateRTBarSeriesActions_cont_negative(time1, time2); appMDManager.updateRTBarSeriesActions(); MarketDataElement element1 = appMDManager.getTimeBarSeries().Get(time1); Assert.AreEqual(time1, element1.time); Assert.AreEqual(1, appMDManager.getTimeBarSeries().KeyCount); }
public static MarketDataElement convertBarToMarketDataElement(RTDataBar bar) { double h = bar.high; double l = bar.low; double o = bar.open; double c = bar.close; double vol = bar.volume; DateTime time = bar.time; MarketDataElement currentHsiData = createHLOC(time, "HLOC", "HSI", "FUT"); currentHsiData.volume = vol; currentHsiData.time = time; MarketDataUtil.setHLOC(currentHsiData, h, l, o, c); return(currentHsiData); }
public static List <SARRow> addFirstRow(Series <DateTime, MarketDataElement> series, int period, DateTime startTime, double af, double max_af) { List <SARRow> resultRow = new List <SARRow>(); MarketDataElement lastData = series.GetAt(series.KeyCount - 1); Series <DateTime, MarketDataElement> lastNData = MarketDateElementHelper.getLastNMarketData(series, period); List <MarketDataElement> lstData = lastNData.Values.ToList(); SARRow row = new SARRow(); foreach (MarketDataElement data in lstData) { if (row.sarValue == 0) { row.sarValue = Math.Min(data.open, data.close); } else { row.sarValue = Math.Min(row.sarValue, Math.Min(data.open, data.close)); } if (row.extremePoint == 0) { row.extremePoint = Math.Max(data.open, data.close); } else { row.extremePoint = Math.Max(row.extremePoint, Math.Max(data.open, data.close)); } } if (Math.Min(lastData.open, lastData.close) > row.sarValue) { row.direction = 1; } else { row.direction = -1; } row.delta_EP_SAR = row.extremePoint - row.sarValue; row.accFactor = af; row.product_af_delta = row.accFactor * row.delta_EP_SAR; row.time = lastData.time; resultRow.Add(row); return(resultRow); }
protected double getValidCutLossPt(double exitPrice, MarketDataElement data, String buySell) { if (AppConstant.BUY_SIGNAL.Equals(buySell)) { if (exitPrice <= data.low) { return(data.low); } } else { if (exitPrice >= data.high) { return(data.high); } } return(exitPrice); }
public static double findEnterPtTriggerMomentum(List <CMRow> rows, Series <DateTime, MarketDataElement> series, int period, DateTime startTime, double delta, String buySell) { Series <DateTime, MarketDataElement> lastNData = MarketDateElementHelper.getLastNMarketData(series, period - 1); MarketDataElement firstData = MarketDateElementHelper.getFirstAvaMarketData(lastNData, startTime); MarketDataElement lastData = series.GetAt(series.KeyCount - 1); //log.Info("firstData.open : " + firstData.open); double open = firstData.open; if (AppConstant.BUY_SIGNAL.Equals(buySell)) { return(open + delta); } else { return(open - delta); } }
private void cacluateRanges() { if (!rangesCalculated) { MarketDataElement preData = getPreTradeDayMData(); log.Info("[cacluateRanges]: preData=" + preData.ToString()); lastClose = preData.close; lastHigh = preData.high; lastLow = preData.low; sellCheck = lastHigh + f1 * (lastClose - lastLow); buyCheck = lastLow - f1 * (lastHigh - lastClose); revSell = ((1 + f2) / 2) * (lastHigh + lastClose) - f2 * lastLow; revBuy = ((1 + f2) / 2) * (lastLow + lastClose) - f2 * lastHigh; trendSell = buyCheck - f3 * (sellCheck - buyCheck); trendBuy = sellCheck + f3 * (sellCheck - buyCheck); sellCheck = Math.Floor(sellCheck); buyCheck = Math.Floor(buyCheck); revSell = Math.Floor(revSell); revBuy = Math.Floor(revBuy); trendSell = Math.Floor(trendSell); trendBuy = Math.Floor(trendBuy); rangesCalculated = true; /* * double anchor = 22290; * trendBuy = anchor + 25; * sellCheck = anchor + 15; * revSell = anchor + 5; * * revBuy = anchor - 5; * buyCheck = anchor - 15; * trendSell = anchor - 25; */ log.Info("-----------------------------------------------"); log.Info("[cacluateRanges]: trendBuy=" + trendBuy.ToString()); log.Info("[cacluateRanges]: sellCheck=" + sellCheck.ToString()); log.Info("[cacluateRanges]: revSell=" + revSell.ToString()); log.Info("-----------------------------------------------"); log.Info("[cacluateRanges]: revBuy=" + revBuy.ToString()); log.Info("[cacluateRanges]: buyCheck=" + buyCheck.ToString()); log.Info("[cacluateRanges]: trendSell=" + trendSell.ToString()); log.Info("-----------------------------------------------"); } }
public static List <CMRow> calculate(List <CMRow> rows, Series <DateTime, MarketDataElement> series, int period, DateTime startTime) { Series <DateTime, MarketDataElement> lastNData = MarketDateElementHelper.getLastNMarketData(series, period); MarketDataElement firstData = MarketDateElementHelper.getFirstAvaMarketData(lastNData, startTime); MarketDataElement lastData = series.GetAt(series.KeyCount - 1); double open = firstData.open; double close = lastData.close; double delta = Math.Abs(close - open); CMRow row = new CMRow(); row.startTime = firstData.time; row.startPt = firstData.open; row.endTime = lastData.time; row.endPt = lastData.close; row.deltaPt = delta; rows.Add(row); return(rows); }
public void test_convertBarToMarketDataElement() { RTDataBar RTBar = new RTDataBar(); RTBar.high = 20200; RTBar.open = 20100; RTBar.low = 20000; RTBar.close = 20150; RTBar.volume = 99999; RTBar.time = new DateTime(2015, 10, 21, 9, 49, 0); MarketDataElement element = MarketDataUtil.convertBarToMarketDataElement(RTBar); Assert.AreEqual(RTBar.high, element.high); Assert.AreEqual(RTBar.low, element.low); Assert.AreEqual(RTBar.open, element.open); Assert.AreEqual(RTBar.close, element.close); Assert.AreEqual(RTBar.volume, element.volume); Assert.AreEqual(RTBar.time, element.time); }
private void exitTradeStrategy(Series <DateTime, MarketDataElement> series) { if (!execution.isPositionOnSignalEmpty()) { return; } double tickClose = calculateMid(); if (tickClose.Equals(double.NaN)) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); double enterPrice = lastExecutePrice; if (execution.CurrentMarketPosition > 0) { String reason = "Cut Loss {0} trade| tickClose = {1} | cutLossPrice = {2} "; reason = reason.Replace("{0}", "SELL").Replace("{1}", tickClose.ToString()).Replace("{2}", buyCheck.ToString()); stgHelper.placeStopTrade(AppConstant.SELL_SIGNAL, "Cutloss Loss Rule", buyCheck, "", 1, 1); reason = "Take Profit {0} trade| tickClose = {1} | profitPrice = {2} "; reason = reason.Replace("{0}", "SELL").Replace("{1}", tickClose.ToString()).Replace("{2}", revSell.ToString()); stgHelper.placeLimitTrade(AppConstant.SELL_SIGNAL, "Profit Target Rule", revSell, "", 1, 2); } if (execution.CurrentMarketPosition < 0) { String reason = "Cut Loss {0} trade| tickClose = {1} | cutLossPrice = {2} "; reason = reason.Replace("{0}", "BUY").Replace("{1}", tickClose.ToString()).Replace("{2}", sellCheck.ToString()); stgHelper.placeStopTrade(AppConstant.BUY_SIGNAL, "Cutloss Loss Rule", sellCheck, "", 1, 1); reason = "Take Profit {0} trade| tickClose = {1} | profitPrice = {2} "; reason = reason.Replace("{0}", "BUY").Replace("{1}", tickClose.ToString()).Replace("{2}", revBuy.ToString()); stgHelper.placeLimitTrade(AppConstant.BUY_SIGNAL, "Profit Target Rule", revBuy, "", 1, 2); } }
private MarketDataElement getPreTradeDayMData() { MarketDataElement data = new MarketDataElement(); /* * data.open = 23415; * data.close = 23046; * data.high = 23419; * data.low = 23036; */ int reqID = MDManager.reqHistDataAdHoc(DateTime.Now); while (true) { if (MDManager.getHistDataAdHoc(reqID) != null) { data = MDManager.getHistDataAdHoc(reqID); break; } } return(data); }
protected void calculateCurrentMax(Series <DateTime, MarketDataElement> series) { if (series == null) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); if (lastItem.high > currentDayHigh) { currentDayHigh = lastItem.high; } if (double.MinValue.Equals(currentDayLow)) { currentDayLow = lastItem.low; } if (lastItem.low < currentDayLow) { currentDayLow = lastItem.low; } }
protected double getValidProfit(double exitPrice, MarketDataElement data, String buySell) { if (exitPrice >= data.low && exitPrice <= data.high) { return(exitPrice); } if (AppConstant.SELL_SIGNAL.Equals(buySell)) { if (exitPrice <= data.low && exitPrice <= data.high) { return(data.open); } } else { if (exitPrice >= data.low && exitPrice >= data.high) { return(data.open); } } return(exitPrice); }
static public List <SAPaFRow> addFirstNode(MarketDataElement item) { double open = item.open; double close = item.close; String direction = ""; if (open > close) { direction = DROP; } else { direction = RISE; } List <SAPaFRow> rows = new List <SAPaFRow>(); SAPaFRow row = new SAPaFRow(); row.direction = direction; if (DROP.Equals(direction)) { row.startPt = item.high; row.maxPt = item.low; } else { row.startPt = item.low; row.maxPt = item.high; } //row.maxLevel = getMaxLevel(row.maxPt, direction); row.startTime = item.time; rows.Add(row); return(rows); }
public static List <SARRow> addNewRow(List <SARRow> rows, Series <DateTime, MarketDataElement> series, DateTime startTime, double af, double max_af) { MarketDataElement currentData = series.GetAt(series.KeyCount - 1); MarketDataElement lastData = series.Get(currentData.time, Lookup.Smaller); MarketDataElement last2ndData = series.Get(lastData.time, Lookup.Smaller); SARRow lastRow = rows[rows.Count - 1]; //SARRow last2ndRow = rows[rows.Count - 2]; int last2ndDirection = 0; if (rows.Count > 1) { last2ndDirection = rows[rows.Count - 2].direction; } else { last2ndDirection = 1; } SARRow currentRow = new SARRow(); double currentLow = Math.Min(currentData.open, currentData.close); double lastTLow = Math.Min(lastData.open, lastData.close); double last2ndTLow = Math.Min(last2ndData.open, last2ndData.close); double currentHigh = Math.Max(currentData.open, currentData.close); double lastTHigh = Math.Max(lastData.open, lastData.close); double last2ndTHigh = Math.Max(last2ndData.open, last2ndData.close); // sarValue if (lastRow.direction == 1 && last2ndDirection == 1) { double newSAR = lastRow.sarValue + lastRow.product_af_delta; currentRow.sarValue = Math.Min(newSAR, Math.Min(lastTLow, last2ndTLow)); } if (lastRow.direction == -1 && last2ndDirection == -1) { double newSAR = lastRow.sarValue + lastRow.product_af_delta; currentRow.sarValue = Math.Max(newSAR, Math.Max(lastTHigh, last2ndTHigh)); } if (lastRow.direction * last2ndDirection == -1) { currentRow.sarValue = lastRow.extremePoint; /* * if (lastRow.direction == 1) * { * currentRow.sarValue = lastRow.extremePoint + Math.Abs(currentData.low - lastRow.extremePoint) / 75; * } * else * { * currentRow.sarValue = lastRow.extremePoint - Math.Abs(lastRow.extremePoint - currentData.high) / 75; * }*/ } //ep, delta_EP_SAR if (lastRow.direction == 1) { currentRow.extremePoint = Math.Max(currentHigh, lastRow.extremePoint); } else if (lastRow.direction == -1) { currentRow.extremePoint = Math.Min(currentLow, lastRow.extremePoint); } currentRow.delta_EP_SAR = currentRow.extremePoint - currentRow.sarValue; //direction if (lastRow.direction == 1) { if (currentLow > currentRow.sarValue) { currentRow.direction = 1; } else { currentRow.direction = -1; } } if (lastRow.direction == -1) { if (currentHigh < currentRow.sarValue) { currentRow.direction = -1; } else { currentRow.direction = 1; } } //af if (currentRow.direction == 1 && lastRow.direction == 1) { if (currentRow.extremePoint > lastRow.extremePoint) { if (lastRow.accFactor >= max_af) { currentRow.accFactor = max_af; } else { currentRow.accFactor = lastRow.accFactor + af; } } else { currentRow.accFactor = lastRow.accFactor; } } if (currentRow.direction == -1 && lastRow.direction == -1) { if (currentRow.extremePoint < lastRow.extremePoint) { if (lastRow.accFactor >= max_af) { currentRow.accFactor = max_af; } else { currentRow.accFactor = lastRow.accFactor + af; } } else { currentRow.accFactor = lastRow.accFactor; } } if (currentRow.direction * lastRow.direction == -1) { currentRow.accFactor = af; } currentRow.accFactor = Math.Round(currentRow.accFactor, 3); //product_af_delta currentRow.product_af_delta = currentRow.accFactor * currentRow.delta_EP_SAR; currentRow.time = currentData.time; rows.Add(currentRow); return(rows); }
private void enterTradeStrategy(Series <DateTime, MarketDataElement> series) { if (!execution.isPositionSignalBothEmpty()) { return; } if (dayClosed) { return; } if (series == null) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); DateTime current = lastItem.time; String strCurrent = String.Format("{0:yyyyMMdd}", current); double tickClose = calculateMid(); if (tickClose.Equals(double.NaN)) { return; } Boolean enterSignal = false; String signalDir = ""; /******************************/ if (tickClose < trendBuy && tickClose > revSell) { enterSignal = true; signalDir = "R"; } if (tickClose > trendSell && tickClose < revBuy) { enterSignal = true; signalDir = "D"; } /******************************/ if (enterSignal && lossFilter) { if ("R".Equals(signalDir)) { String reason = "Place the {0} trade because TrendBuyLine={1};"; reason = reason.Replace("{0}", "BUY").Replace("{1}", trendBuy.ToString()); log.Info("[Strategy] : BUY Order Generated !!! | MarketDataElement=" + lastItem.toString()); stgHelper.placeStopTrade(AppConstant.BUY_SIGNAL, "Trend Buy Rule", trendBuy, reason, 1, 1); } if ("D".Equals(signalDir)) { String reason = "Place the {0} trade because TrendSellLine={1};"; reason = reason.Replace("{0}", "Sell").Replace("{1}", trendSell.ToString()); log.Info("[Strategy] : SELL Order Generated !!! | MarketDataElement=" + lastItem.toString()); stgHelper.placeStopTrade(AppConstant.SELL_SIGNAL, "Rev Sell Rule", trendSell, reason, 1, 1); } } }
private void enterTradeStrategy(Series <DateTime, MarketDataElement> series) { if (!execution.isPositionSignalBothEmpty()) { return; } if (dayClosed) { return; } if (series == null) { return; } MarketDataElement lastItem = (MarketDataElement)series.GetAt(series.KeyCount - 1); double tickClose = calculateMid(); if (tickClose.Equals(double.NaN)) { return; } if (lastItem.high > currentDayHigh) { currentDayHigh = lastItem.high; } if (double.MinValue.Equals(currentDayLow)) { currentDayLow = lastItem.low; } if (lastItem.low < currentDayLow) { currentDayLow = lastItem.low; } /******************************/ Boolean enterSignal = false; String signalDir = ""; if (tickClose < trendBuy && tickClose > revSell && currentDayHigh >= sellCheck) { enterSignal = true; signalDir = "D"; } if (tickClose > trendSell && tickClose < revBuy && currentDayLow <= buyCheck) { enterSignal = true; signalDir = "R"; } /******************************/ if (enterSignal && lossFilter) { if ("R".Equals(signalDir)) { String reason = "Place the {0} trade because RevBuyLine={1};"; reason = reason.Replace("{0}", "BUY").Replace("{1}", revBuy.ToString()); log.Info("[Strategy] : Enter BUY Generated !!! | MarketDataElement=" + lastItem.toString()); stgHelper.placeStopTrade(AppConstant.BUY_SIGNAL, "Rev Buy Rule", revBuy, reason, 1, 1); } if ("D".Equals(signalDir)) { String reason = "Place the {0} trade because RevSellLine={1};"; reason = reason.Replace("{0}", "SELL").Replace("{1}", revSell.ToString()); log.Info("[Strategy] : Enter SELL Generated !!! | MarketDataElement=" + lastItem.toString()); stgHelper.placeStopTrade(AppConstant.SELL_SIGNAL, "Rev Sell Rule", revSell, reason, 1, 1); } } }