using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TradeIdeas.TIProGUI;
using TradeIdeas.MiscSupport;
namespace CustomClassLibrary
{
public partial class RBIGBIConfig : Form
{
//this window is currently just a shell...
//Start location of Config window when first brought up...
private static int _configLocation_X = 50;
private static int _configLocation_Y = 50;
//Start size of Config window when *first* brought up (default size)
private static int _configWindowHeight = -1;
private static int _configWindowWidth = -1;
private FilterValues _fValues = new FilterValues();
///
/// Small form to set filters for the RBI/GBI windows.
///
/// Object containg filter and window name settings
/// Flag to hide Position in Range filters. Set to True for preview candles
public RBIGBIConfig(FilterValues vals, bool hidePosRng = false)
{
InitializeComponent();
MinimizeBox = false;
if (hidePosRng)
{
// We nead to hide the Position in Range filter for the preview category.
label12.Visible = false;
label13.Visible = false;
label14.Visible = false;
label15.Visible = false;
minPositionInRange.Visible = false;
maxPositionInRange.Visible = false;
}
StartPosition = FormStartPosition.CenterParent;
Icon = GuiEnvironment.Icon;
if ((_configWindowHeight == -1 && _configWindowWidth == -1))
{
_configWindowWidth = Width;
_configWindowHeight = Height;
}
minPriceTextBox.Text = populateTextBox(vals.getMinPrice());
maxPriceTextBox.Text = populateTextBox(vals.getMaxPrice());
minVolumeTodayTextBox.Text = populateTextBox(vals.getMinVolToday());
minAverageDailyVolume.Text = populateTextBox(vals.getMinAvDailyVolume());
minVolumePercent.Text = populateTextBox(vals.getMinVolumePercent());
minPositionInRange.Text = populateTextBox(vals.getMinPosInRange());
maxPositionInRange.Text = populateTextBox(vals.getMaxPosInRange());
txtWindowName.Text = vals.getWindowName();
Font = GuiEnvironment.FontSettings;
this.Refresh();
}
private string populateTextBox(Double value)
{
if (Double.IsNaN(value))
return "";
else return value.ToString();
}
private void RBIGBIConfig_VisibleChanged(object sender, EventArgs e)
{
//Here we retain the location whin this window is created again
if (!this.Visible)
{
_configLocation_X = Location.X;
_configLocation_Y = Location.Y;
_configWindowHeight = Height;
_configWindowWidth = Width;
}
else
{
if (GuiEnvironment.ConfigWindowRemembersLocation)
Location = new Point(_configLocation_X, _configLocation_Y);
Width = _configWindowWidth;
Height = _configWindowHeight;
}
}
public FilterValues getFilterValuesObject()
{
return _fValues;
}
private void btnOK_Click(object sender, EventArgs e)
{
FilterValues fValues = new FilterValues();
fValues.setMinPrice(minPriceTextBox.Text.Trim());
fValues.setMaxPrice(maxPriceTextBox.Text.Trim());
fValues.setMinVolToday(minVolumeTodayTextBox.Text.Trim());
fValues.setMinAvDailyVolume(minAverageDailyVolume.Text.Trim());
fValues.setMinVolumePercent(minVolumePercent.Text.Trim());
fValues.setMinPosInRange(minPositionInRange.Text.Trim());
fValues.setMaxPosInRange(maxPositionInRange.Text.Trim());
fValues.setWindowName(txtWindowName.Text.Trim());
_fValues = fValues;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class FilterValues
{
private Double _minPrice = Double.NaN;
private Double _maxPrice = Double.NaN;
private Double _minVolToday = Double.NaN;
private Double _minAvDailyVol = Double.NaN;
private Double _minVolumePercent = Double.NaN;
private Double _minPositionInRange = Double.NaN;
private Double _maxPositionInRange = Double.NaN;
private string _windowName = "";
private enum Compare { Min, Max };
private List _truthValues = new List();
public FilterValues()
{ }
private Double? getTextBoxValue(String text)
{
if ((text == "") || (null == TryParse(text)))
{
return Double.NaN;
}
else
{
return TryParse(text);
}
}
private Double? TryParse(String asText)
{
double value;
// This automatically allows commas. It should be specific to the user's settings,
// i.e. commas vs. periods, and therefor consistent with the real config window.
if (Double.TryParse(asText, out value))
return value;
else
return null;
}
public void setWindowName(string text)
{
_windowName = text;
}
public void setMinPrice(String text)
{
_minPrice = (getTextBoxValue(text)).Value;
}
public void setMaxPrice(String text)
{
_maxPrice = (getTextBoxValue(text)).Value;
}
public void setMinVolToday(String text)
{
_minVolToday = (getTextBoxValue(text)).Value;
}
public void setMinAvDailyVolume(String text)
{
_minAvDailyVol = (getTextBoxValue(text)).Value;
}
public void setMinVolumePercent(String text)
{
_minVolumePercent = (getTextBoxValue(text)).Value;
}
public void setMinPosInRange(String text)
{
_minPositionInRange = (getTextBoxValue(text)).Value;
}
public void setMaxPosInRange(String text)
{
_maxPositionInRange = (getTextBoxValue(text)).Value;
}
public string getWindowName()
{
return _windowName;
}
public Double getMinPrice()
{
return _minPrice;
}
public Double getMaxPrice()
{
return _maxPrice;
}
public Double getMinVolToday()
{
return _minVolToday;
}
public Double getMinAvDailyVolume()
{
return _minAvDailyVol;
}
public Double getMinVolumePercent()
{
return _minVolumePercent;
}
public Double getMinPosInRange()
{
return _minPositionInRange;
}
public Double getMaxPosInRange()
{
return _maxPositionInRange;
}
public FilterValues getFilterValuesObject()
{
return this;
}
public bool willRowBeDisplayed(RowData data)
{
_truthValues.Clear();
//here, we'll get the appropriate values from the RowDataObject
Double volume = data.GetAsDouble("tvol", Double.NaN);
Double avVol = data.GetAsDouble("advol", Double.NaN);
Double volumePercent = data.GetAsDouble("volPercent", Double.NaN);
Double positionInRange = data.GetAsDouble("RD", Double.NaN);
Double price = data.GetAsDouble("price", Double.NaN);
compareRowDataValueWithFilterValueMember(volume, _minVolToday, Compare.Min);
compareRowDataValueWithFilterValueMember(avVol, _minAvDailyVol, Compare.Min);
compareRowDataValueWithFilterValueMember(price, _minPrice, Compare.Min);
compareRowDataValueWithFilterValueMember(price, _maxPrice, Compare.Max);
compareRowDataValueWithFilterValueMember(volumePercent, _minVolumePercent, Compare.Min);
compareRowDataValueWithFilterValueMember(positionInRange, _minPositionInRange, Compare.Min);
compareRowDataValueWithFilterValueMember(positionInRange, _maxPositionInRange, Compare.Max);
if (_truthValues.Count == 0)
{
return true;
}
else if (_truthValues.Contains(false))
{
return false;
}
return true;
}
private void compareRowDataValueWithFilterValueMember(Double rowDataValue, Double filterValuesValue, Compare compare)
{
if (Double.IsNaN(filterValuesValue))
{
return;
}
else if (Double.IsNaN(rowDataValue))
{
_truthValues.Add(false);
return;
}
if (compare == Compare.Max)
{
if (rowDataValue > filterValuesValue)
_truthValues.Add(false);
else _truthValues.Add(true);
return;
}
if (compare == Compare.Min)
{
if (rowDataValue > filterValuesValue)
_truthValues.Add(true);
else _truthValues.Add(false);
return;
}
}
}
}