public DataGridViewImageButtonColumn() { CellTemplate = new DataGridViewImageButtonCell(); ReadOnly = true; }
private void LoadScanner() { string scanName = cboScanners.Text.Trim(); if (string.IsNullOrEmpty(scanName)) { return; } m_loading = true; cmdSave.Enabled = false; cmdDelete.Enabled = false; cboScanners.Enabled = false; txtScannerName.Text = scanName; string data; try { data = svc.GetUserData(frmMain.ClientId, frmMain.ClientPassword, frmMain.LicenseKey, "Scanner Settings: " + scanName); } catch (Exception) { MessageBox.Show("Failed to load scanner.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); cmdSave.Enabled = true; cmdDelete.Enabled = true; cboScanners.Enabled = true; return; } string[] text = data.Split(Utils.Chr(134)); if (text.Length < 4) { return; } List <string> strRows = new List <string>(text[0].Split(Utils.Chr(145))); char d = Utils.Chr(182); //Clear everything m_stop = false; m_TSAlertDictionary.Clear(); m_DGVRowDictionary.Clear(); grdResults.Rows.Clear(); DataGridViewBarGraphColumn volumeCol = (DataGridViewBarGraphColumn)grdResults.Columns["Volume"]; if (volumeCol != null) { volumeCol.MaxValue = 0; } //Load all symbols Symbols.Clear(); string[] strRow; for (int n = 0; n < strRows.Count - 1; n++) { strRow = strRows[n].Split(d); Symbols.Add(strRow[0]); } //Global settings switch (text[1]) { case "Minutely": Periodicity = Periodicity.Minutely; break; case "Hourly": Periodicity = Periodicity.Hourly; break; case "Daily": Periodicity = Periodicity.Daily; break; case "Weekly": Periodicity = Periodicity.Weekly; break; } Interval = Convert.ToInt32(text[2]); Bars = Convert.ToInt32(text[3]); Script = text[4]; //Prime data LoadAllSymbolsIntoMemory(); //Go back through for settings for (int n = 0; n < strRows.Count - 2; n++) { strRow = strRows[n].Split(new[] { d }); if (strRow.Length < 3) { continue; } for (int j = 0; j < grdResults.Rows.Count; j++) { if (string.Compare(grdResults.Rows[j].Cells["Symbol"].Value.ToString(), strRow[0], true) == 0) { DataGridViewImageButtonCell start = (DataGridViewImageButtonCell)grdResults.Rows[j].Cells["Start"]; start.Checked = string.Compare(strRow[3], "true", true) == 0; Alert oAlert = m_TSAlertDictionary[strRow[0]]; if (oAlert != null) { if (!start.Checked) { oAlert.AlertScript = strRow[1]; } else { oAlert.AlertScript = strRow[1]; } }//oAlert != null DataGridViewImageButtonCell @lock = (DataGridViewImageButtonCell)grdResults.Rows[j].Cells["Locked"]; @lock.Checked = string.Compare(strRow[2], "true", true) == 0; break; } //if grdResults } //for j } //for n m_loading = false; m_changed = false; UpdateName(txtScannerName.Text); cmdSave.Enabled = true; cmdDelete.Enabled = true; cboScanners.Enabled = true; cmdDelete.Enabled = cboScanners.SelectedIndex != -1; }
public void SaveScanner(bool prompt) { string scanName = txtScannerName.Text; if (prompt) //Ask the user first (may be called by frmMain) { if (string.IsNullOrEmpty(scanName)) { scanName = "Untitled"; } if (MessageBox.Show("Save scanner '" + scanName + "'?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } cmdSave.Enabled = false; cmdDelete.Enabled = false; cboScanners.Enabled = false; char d = Utils.Chr(134); char d2 = Utils.Chr(182); StringBuilder strRows = new StringBuilder(); Alert oAlert; string symbol, script = string.Empty; bool unlocked, paused; for (int n = 0; n < grdResults.Rows.Count; n++) { //Symbol, Script, Locked, Paused symbol = grdResults.Rows[n].Cells["Symbol"].Value.ToString(); oAlert = m_TSAlertDictionary[symbol]; if (oAlert == null) { continue; } script = oAlert.AlertScript; if (string.IsNullOrEmpty(script)) { script = oAlert.AlertName; } DataGridViewImageButtonCell cell = (DataGridViewImageButtonCell)grdResults.Rows[n].Cells["Locked"]; unlocked = !cell.Checked; cell = (DataGridViewImageButtonCell)grdResults.Rows[n].Cells["Start"]; paused = false; if (cell != null) { paused = cell.Checked; } strRows.Append(symbol) .Append(d2).Append(script) .Append(d2).Append(unlocked) .Append(d2).Append(paused) .Append(Utils.Chr(145)); } StringBuilder data = new StringBuilder(); data.Append(strRows.ToString()) .Append(d).Append(Periodicity) .Append(d).Append(Interval) .Append(d).Append(Bars) .Append(d).Append(Script); try { svc.SetUserData(frmMain.ClientId, frmMain.ClientPassword, frmMain.LicenseKey, "Scanner Settings: " + scanName, data.ToString()); m_changed = false; } catch (Exception) { MessageBox.Show("Failed to save scanner.", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } bool found = cboScanners.FindStringExact(txtScannerName.Text) != -1; if (!found) { LoadScanners(); int idx = cboScanners.FindStringExact(scanName); if (idx != -1) { cboScanners.SelectedIndex = idx; } } UpdateName(txtScannerName.Text); cmdSave.Enabled = true; if (cboScanners.SelectedIndex != -1) { cmdDelete.Enabled = true; } cboScanners.Enabled = true; }
private void oScript_ScriptError(string symbol, string alertName, string description) { if (m_loading || m_dialogShown) { return; } m_dialogShown = true; //Prevent the error from occuring over and over Alert oAlert = m_TSAlertDictionary[symbol]; if (oAlert == null) { return; } //Find and highlight the record int row = -1; for (int n = 0; n < grdResults.Rows.Count; n++) { if (string.Compare(grdResults.Rows[n].Cells["Symbol"].Value.ToString(), symbol, true) == 0) { row = n; break; } } DataGridViewImageButtonCell cell = (DataGridViewImageButtonCell)grdResults.Rows[row].Cells["Start"]; if (cell == null) { return; } TempScript = oAlert.AlertName; if (string.IsNullOrEmpty(oAlert.AlertName)) //if not already paused { TempScript = oAlert.AlertScript; oAlert.AlertName = oAlert.AlertScript; //Using AlertName as a Tag oAlert.AlertScript = string.Empty; //Pause cell.Checked = true; } // Display the error message if (!string.IsNullOrEmpty(oAlert.ScriptHelp)) { if (MessageBox.Show("Your script generated an error: " + Environment.NewLine + description + Environment.NewLine + "Would you like to view help regarding this error?", "Error:", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { MessageBox.Show(oScript.ScriptHelp, symbol, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Your script generated an error:" + Environment.NewLine + description, symbol, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Show edit dialog TempScript = string.IsNullOrEmpty(oAlert.AlertScript) ? oAlert.AlertName : oAlert.AlertScript; frmScannerScript settings = new frmScannerScript(this) { HeaderText = symbol + " Script" }; DialogResult result = settings.ShowDialog(); m_dialogShown = false; if (result == DialogResult.Abort) { m_frmMain.OpenURL("http://www.modulusfe.com/TradeScript/TradeScript.pdf", "TradeScript Help"); return; } if (result == DialogResult.OK) { oAlert.AlertScript = TempScript; oAlert.AlertName = string.Empty; cell.Checked = false; //Script is runing now } }
//Loads all required data into memory. This function must be called first. public bool LoadAllSymbolsIntoMemory() { //Verify the input and check script for errors if (m_ctlData == null || !TestScripts()) { return(false); } EnableControls(false); //Clear everything m_stop = false; m_TSAlertDictionary.Clear(); m_DGVRowDictionary.Clear(); grdResults.Rows.Clear(); DataGridViewBarGraphColumn volumeCol = (DataGridViewBarGraphColumn)grdResults.Columns["Volume"]; if (volumeCol != null) { volumeCol.MaxValue = 0; } //Load history for all symbols into memory pnlProgress.Cursor = Cursors.WaitCursor; pnlProgress.Visible = true; if (Symbols.Count > 1) { ProgressBar1.Properties.Maximum = Symbols.Count - 1; } ChartSelection selection = new ChartSelection { Periodicity = Periodicity, Interval = Interval, Bars = Bars * 3 }; if (selection.Bars < 50) { selection.Bars = 50; } lblInfo.Text = "Priming scanner, please wait..."; for (int n = 0; n < Symbols.Count; n++) { string symbol = Symbols[n]; if (m_stop) { break; } if (m_TSAlertDictionary.ContainsKey(symbol)) { continue; } selection.Symbol = symbol; ProgressBar1.Properties.Value = n; lblSymbol.Text = symbol; Application.DoEvents(); //Get historic data var bars = m_ctlData.GetHistory(symbol, this, selection.Periodicity, selection.Interval, selection.Bars); if (bars == null || bars.Count < 3) { continue; } //Insert the data into TradeScript Alert oAlert = new Alert { License = "XRT93NQR79ABTW788XR48" }; foreach (M4.DataServer.Interface.BarData t in bars) { double jDate = oAlert.ToJulianDate(t.TradeDate.Year, t.TradeDate.Month, t.TradeDate.Day, t.TradeDate.Hour, t.TradeDate.Minute, t.TradeDate.Second, t.TradeDate.Millisecond); oAlert.AppendHistoryRecord(jDate, t.OpenPrice, t.HighPrice, t.LowPrice, t.ClosePrice, (int)t.VolumeF); } //Start the alert object by adding the script oAlert.AlertScript = Script; oAlert.Symbol = symbol; oAlert.ScriptError += oScript_ScriptError; oAlert.Alert += OnAlert; m_TSAlertDictionary.Add(symbol, oAlert); //Add symbol to datagridview int row; try { row = grdResults.Rows.Add(new DataGridViewRow()); } catch (Exception) { return(false); } m_DGVRowDictionary.Add(symbol, grdResults.Rows[row]); grdResults.Rows[row].Height = 25; M4.DataServer.Interface.BarData lastBar = bars.Last(); DataGridViewTextBoxScannerColorCell alertDateTime = (DataGridViewTextBoxScannerColorCell)grdResults.Rows[row].Cells["Alert Time"]; alertDateTime.HighlightOnly = true; alertDateTime.Value = lastBar.TradeDate; alertDateTime.Interval = 5000; grdResults.Rows[row].Cells["Trade Time"].Value = lastBar.TradeDate; grdResults.Rows[row].Cells["Symbol"].Value = symbol; grdResults.Rows[row].Cells["Last"].Value = Format.ToUsCurrency(lastBar.ClosePrice); grdResults.Rows[row].Cells["Volume"].Value = Format.ToLocalInteger((int)lastBar.VolumeF); DataGridViewButtonCell button = (DataGridViewButtonCell)grdResults.Rows[row].Cells["Trade"]; button.Value = "Trade"; button.FlatStyle = FlatStyle.Flat; button = (DataGridViewButtonCell)grdResults.Rows[row].Cells["Chart"]; button.Value = "Chart"; button.FlatStyle = FlatStyle.Flat; button = (DataGridViewButtonCell)grdResults.Rows[row].Cells["Settings"]; button.Value = "Settings"; button.FlatStyle = FlatStyle.Flat; button = (DataGridViewButtonCell)grdResults.Rows[row].Cells["Start"]; button.Value = "Start"; button.FlatStyle = FlatStyle.Flat; DataGridViewImageButtonCell start = (DataGridViewImageButtonCell)grdResults.Rows[row].Cells["Start"]; start.ImageOn = Resources.Play; start.ImageOff = Resources.Pause; start.OffsetY = 4; DataGridViewImageButtonCell @lock = (DataGridViewImageButtonCell)grdResults.Rows[row].Cells["Locked"]; @lock.ImageOn = Resources.Lock; @lock.ImageOff = Resources.Unlock; @lock.OffsetY = 2; } lblInfo.Text = string.Empty; pnlProgress.Visible = false; pnlProgress.Cursor = Cursors.Arrow; EnableControls(true); return(true); }
//Handles grid button clicks private void grdResults_CellClick(object sender, DataGridViewCellEventArgs e) { // this method handles only cell click // if e.RowIndex == -1 it means user clicked on header, so ignore this if (e.RowIndex == -1) { return; } try { string symbol = grdResults.SelectedRows[0].Cells["Symbol"].Value.ToString(); Alert oAlert = m_TSAlertDictionary[symbol]; if (oAlert == null) { return; } if (e.ColumnIndex == 5) //Trade { grdResults.Rows[e.RowIndex].Selected = true; m_frmMain.EnterOrder(symbol); } else if (e.ColumnIndex == 6) //Chart { EnableControls(true, false); grdResults.Cursor = Cursors.WaitCursor; grdResults.Rows[e.RowIndex].Selected = true; ChartSelection selection = GetSelection(symbol); m_frmMain.CreateNewChart(selection); grdResults.Cursor = Cursors.Arrow; EnableControls(true, true); } else if (e.ColumnIndex == 7) //Change the script for this individual alert { TempScript = string.IsNullOrEmpty(oAlert.AlertScript) ? oAlert.AlertName : oAlert.AlertScript; frmScannerScript settings = new frmScannerScript(this) { HeaderText = symbol + " Script" }; var result = settings.ShowDialog(); if (result == DialogResult.Abort) { m_frmMain.OpenURL("http://www.modulusfe.com/TradeScript/TradeScript.pdf", "TradeScript Help"); return; } if (result == DialogResult.OK) { oAlert.AlertScript = TempScript; oAlert.AlertName = string.Empty; DataGridViewImageButtonCell cell = (DataGridViewImageButtonCell)grdResults.Rows[e.RowIndex].Cells["Start"]; if (cell == null) { return; } cell.Checked = false; //Script is running now } }//else if Column Index 7 else if (e.ColumnIndex == 8) //Lock/unlock script to control effects of the "Edit Script" button { } else if (e.ColumnIndex == 9) //Play/pause { DataGridViewImageButtonCell cell = (DataGridViewImageButtonCell)grdResults.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (cell == null) { return; } bool pause = cell.Checked; if (pause) { oAlert.AlertName = oAlert.AlertScript; //Using AlertName as a Tag oAlert.AlertScript = string.Empty; } else { if (!string.IsNullOrEmpty(oAlert.AlertName)) //Play { oAlert.AlertScript = oAlert.AlertName; oAlert.AlertName = string.Empty; } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } }