/***************************************************************************** * FUNCTION: Parse * Description: Replaces a primitive expression with corresponding numeric values * to enable evaluation. * * Ex. (P < AVG[P][-5..0]) * Parameters: * *****************************************************************************/ private String Parse(String pExpression, Equity pEqIn, int pIndex, List <Fn> pFNs) { string parameter, str_expression, fn_str; double parameter_value = 0.0; string[] split_str; int index1, index2; Variable in_param; str_expression = pExpression; foreach (Fn func in pFNs) { fn_str = ""; if (pExpression.Contains(StringEnum.GetStringValue(func))) { index1 = pExpression.IndexOf(StringEnum.GetStringValue(func)); index2 = pExpression.IndexOf("]", index1); if (pExpression.Length > index2 && pExpression.ToCharArray()[index2 + 1] == '[') { index2 = pExpression.IndexOf("]", index2 + 1); } fn_str = pExpression.Substring(index1, index2 - index1 + 1); str_expression = str_expression.Replace(fn_str.Trim(), GetFunction(func, pEqIn, fn_str, pIndex).ToString()); } } split_str = str_expression.Split(RuleParserInputs.Comparators, StringSplitOptions.RemoveEmptyEntries); foreach (string exp_part in split_str) { if (Helpers.ValidateNumeric(exp_part) == false) { parameter = CleanExpression(exp_part.Trim()); in_param = GetParameterType(parameter); //If the data to be analyzed is one of the MACD signals, ensure the passed index is valid if (in_param == Variable.MACD_DIFF || in_param == Variable.MACD_SIG) { if (pIndex > (pEqIn.HistoricalPrice.Count() - pEqIn.MACD_C.Count())) { parameter_value = GetParameter(pEqIn, parameter, pIndex); str_expression = str_expression.Replace(parameter, parameter_value.ToString()); } } else { parameter_value = GetParameter(pEqIn, parameter, pIndex); str_expression = str_expression.Replace(parameter, parameter_value.ToString()); } } } return(str_expression); }
public static double CustomParseDouble(String dblStr) { int strLen = 0; //Replace any spaces or comma place separators dblStr = dblStr.Replace(" ", ""); dblStr = dblStr.Replace(",", ""); //Replace formatted strings with equivalent value (ex. 4K => 4000) if (!Helpers.ValidateNumeric(dblStr)) { try { strLen = dblStr.Length; strLen = strLen - Regex.Replace(dblStr, "[.].*", "").Length - 2; dblStr = dblStr.Replace(".", ""); if (strLen > 0 && dblStr.Contains("K")) { dblStr += String.Join("", Enumerable.Repeat("0", 3 - strLen)); } else if (strLen > 0 && dblStr.Contains("M")) { dblStr += String.Join("", Enumerable.Repeat("0", 6 - strLen)); } dblStr = Regex.Replace(dblStr, "[A-Z]", ""); //Ensure the resulting value can be parsed (otherwise the exception handler will replace it with '0') double tempDbl = double.Parse(dblStr); } catch (Exception) { dblStr = "0"; } } return(double.Parse(dblStr)); }
/***************************************************************************** * FUNCTION: UpdateLiveData * Description: * Parameters: * pDataLabels - * pDataValues - *****************************************************************************/ public Boolean UpdateLiveData(List <String> pDataLabels, List <String> pDataValues, String pLiveUrl = "") { Boolean success = false; int item_index = 0; string item_value = ""; DateTime last_time; //Ensure lists are initialized if (live_price == null) { live_price = new List <double>(); } if (live_timestamps == null) { live_timestamps = new List <DateTime>(); } //Set the live data source address if (pLiveUrl != "") { LiveDataAddress = pLiveUrl; } foreach (String label in pDataLabels) { item_value = pDataValues[item_index]; //ToDo: optimize switch (label) { case "Last": if (Helpers.ValidateNumeric(pDataValues[item_index])) { this.live_price.Add(Double.Parse(pDataValues[item_index])); if (this.live_price.Count > LIVE_DATA_BUFFER_SIZE) { this.live_price.RemoveAt(0); } } break; case "High": if (Helpers.ValidateNumeric(pDataValues[item_index])) { this.live_high = Double.Parse(pDataValues[item_index]); } break; case "Low": if (Helpers.ValidateNumeric(pDataValues[item_index])) { this.live_low = Double.Parse(pDataValues[item_index]); } break; case "Chg.": if (Helpers.ValidateNumeric(pDataValues[item_index])) { this.live_chg = Double.Parse(pDataValues[item_index]); } break; case "Chg. %": if (pDataValues[item_index].Contains("%")) { this.live_chg_pct = Double.Parse(pDataValues[item_index].Replace("%", "")); } break; case "Vol.": this.live_volume = Helpers.ConvertVolumeString(pDataValues[item_index]); break; case "Time": last_time = DateTime.Parse(pDataValues[item_index]); if (this.live_timestamps.Count > 0 && this.live_timestamps[this.live_timestamps.Count - 1] == last_time) { if (this.live_price.Count > this.live_timestamps.Count) { this.live_price.RemoveAt(this.live_price.Count - 1); } item_index = pDataLabels.Count; } else { this.live_timestamps.Add(last_time); if (this.live_timestamps.Count > LIVE_DATA_BUFFER_SIZE) { this.live_timestamps.RemoveAt(0); } } break; default: break; } item_index++; if (item_index > pDataLabels.Count) { break; } } if (ValidateLiveData()) { this.ContainsLiveData = true; } return(success); }
private void dataTransformationThread_DoWork(object sender, DoWorkEventArgs e) { int i, r; String StrParam1, StrParam2, StrParam3; int recursion = 1; Dictionary <String, String> tParams = new Dictionary <string, string>(); List <Equity> DataToTransform; SET_DATA_INVALID(); //Point to the data set to be transformed if (AllTransformApplied == true) { DataToTransform = statsMarketData.Constituents; } else { DataToTransform = dataSet; } //Validate the required control parameters if (trfWorkerParamText.Length != 4) { trfWorkerSelectedIndex = -1; } //Validate the selected transformation combobox index if (trfWorkerSelectedIndex >= 0) { switch (TransformTList[trfWorkerSelectedIndex]) { case TransformType.TRF_GAUSS: StrParam1 = trfWorkerParamText[1]; StrParam2 = trfWorkerParamText[2]; StrParam3 = trfWorkerParamText[3]; //If input parameter is valid, perform the transformation if (Helpers.ValidateNumeric(StrParam1) && Helpers.ValidateNumeric(StrParam2)) { recursion = int.Parse(StrParam2); for (r = 0; r < recursion; r++) { tParams.Add("pSigma", StrParam1); for (i = 0; i < DataToTransform.Count(); i++) { //dataSet[i].TransformData(Transformation.GAUSS, tParams); DataToTransform[i].TransformData(Transformation.GAUSS, tParams); } tParams.Clear(); } } break; case TransformType.TRF_MEAN: StrParam1 = trfWorkerParamText[1]; StrParam2 = trfWorkerParamText[2]; StrParam3 = trfWorkerParamText[3]; //If input parameter is valid, perform the transformation if (Helpers.ValidateNumeric(StrParam1) && Helpers.ValidateNumeric(StrParam2)) { recursion = int.Parse(StrParam2); for (r = 0; r < recursion; r++) { tParams.Add("Window", StrParam1); for (i = 0; i < DataToTransform.Count(); i++) { DataToTransform[i].TransformData(Transformation.MEAN, tParams); } tParams.Clear(); } } break; case TransformType.TRF_NORM: tParams.Clear(); for (i = 0; i < DataToTransform.Count(); i++) { DataToTransform[i].TransformData(Transformation.NORMALIZE, tParams); } break; case TransformType.TRF_SHIFT_PPC: StrParam1 = trfWorkerParamText[1]; if (lastSelectedTab >= 0 && lastSelectedTab < DataToTransform.Count() && Helpers.ValidateNumeric(StrParam1)) { for (i = 0; i < statsMarketData.Constituents.Count; i++) { if (statsMarketData.Constituents[i].Name == DataToTransform[lastSelectedTab].Name) { statsMarketData.Constituents[i].TrimDataLeft(int.Parse(StrParam1)); } else { statsMarketData.Constituents[i].TrimDataRight(int.Parse(StrParam1)); } } } break; default: break; } } SET_DATA_VALID(); }
/***************************************************************************** * FUNCTION: Parse * Description: Replaces a primitive expression with corresponding numeric values * to enable evaluation. * * Ex. (P < AVG[P][-5..0]) * Parameters: * *****************************************************************************/ private String Parse(String pExpression, Equity pEqIn, int pIndex, List <Fn> pFNs) { string parameter, str_expression, fn_str; double parameter_value = 0.0; string[] split_str; int index1, index2; Variable in_param; str_expression = pExpression; //For each function in the expression being parsed foreach (Fn func in pFNs) { fn_str = ""; if (pExpression.Contains(StringEnum.GetStringValue(func))) { //Get the substring containing only the logical expression for this function index1 = pExpression.IndexOf(StringEnum.GetStringValue(func)); index2 = pExpression.IndexOf("]", index1); if (pExpression.Length > index2 && pExpression.ToCharArray()[index2 + 1] == '[') { index2 = pExpression.IndexOf("]", index2 + 1); } fn_str = pExpression.Substring(index1, index2 - index1 + 1); try { //Evaluate the function and replace the substring in the original parent expression with the value str_expression = str_expression.Replace(fn_str.Trim(), GetFunction(func, pEqIn, fn_str, pIndex).ToString()); } catch (IndexOutOfRangeException) { //The index will be out of range even for a valid command if the function operates on past data that // isn't available at the start of a series. // // Ex. AVG[P][-5..0] --> at the start of the price data series, there aren't 5 previous data points available // to calculate an average on. // // In this case, just return false to allow the rest of the command to be processed. return("false"); } catch (Exception) { errorIndex[0] = index1; errorIndex[1] = index2; return("ERROR"); } } } split_str = str_expression.Split(RuleParserInputs.Comparators, StringSplitOptions.RemoveEmptyEntries); foreach (string exp_part in split_str) { parameter = CleanExpression(exp_part.Trim()).Replace("NOT", ""); if (Helpers.ValidateNumeric(parameter) == false) { in_param = GetParameterType(parameter); try { //If the data to be analyzed is one of the MACD signals, ensure the passed index is valid if (in_param == Variable.MACD_DIFF || in_param == Variable.MACD_SIG) { if (pIndex > (pEqIn.HistoricalPrice.Count() - pEqIn.MACD_C.Count())) { parameter_value = GetParameter(pEqIn, parameter, pIndex); str_expression = str_expression.ReplaceFirstOccurrence(parameter, parameter_value.ToString()); } } else { parameter_value = GetParameter(pEqIn, parameter, pIndex); str_expression = str_expression.ReplaceFirstOccurrence(parameter, parameter_value.ToString()); } } catch (IndexOutOfRangeException) { return("false"); } catch (Exception) { errorIndex[0] = 0; errorIndex[1] = 0; return("ERROR"); } } } return(str_expression); }