0% found this document useful (0 votes)
26 views4 pages

Price Alert Robot for cAlgo

The document is a C# code for a trading robot named 'PriceAlertWithHotkeys' that triggers price alerts based on user-defined parameters. It allows users to set an alert point using hotkeys and mouse clicks, and plays a sound notification when the price crosses the defined alert point. The robot also features a timer to clear the alert message after two minutes and includes graphical elements to display alert information on the trading chart.

Uploaded by

venusiyanvenus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Price Alert Robot for cAlgo

The document is a C# code for a trading robot named 'PriceAlertWithHotkeys' that triggers price alerts based on user-defined parameters. It allows users to set an alert point using hotkeys and mouse clicks, and plays a sound notification when the price crosses the defined alert point. The robot also features a timer to clear the alert message after two minutes and includes graphical elements to display alert information on the trading chart.

Uploaded by

venusiyanvenus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];
using [Link];

namespace [Link]
{
[Robot(TimeZone = [Link], AccessRights = [Link])]
public class PriceAlertWithHotkeys : Robot
{
[Parameter("Alert Point Enabled", DefaultValue = true)]
public bool AlertPointEnabled { get; set; }

[Parameter("Alert Point", DefaultValue = 1.2000)]


public double AlertPoint { get; set; }

[Parameter("Sound File Path", DefaultValue = "C:\\Path\\To\\Your\\


SoundFile\\[Link]")]
public string SoundFilePath { get; set; }

[Parameter("Alert Text Size", DefaultValue = 20)]


public int TextSize { get; set; }

[Parameter("Alert Text Color", DefaultValue = "Red")]


public string TextColor { get; set; }

[Parameter("Price Offset", DefaultValue = 0.001)]


public double PriceOffset { get; set; }

private [Link] _alertTimer;


private Grid _grid;
private TextBlock _alertTextBlock;
private TextBlock _modeTextBlock;
private bool _isSettingAlertPoint;

protected override void OnStart()


{
_isSettingAlertPoint = false;

// Initialize the grid for displaying data


InitializeGrid();

// Draw the horizontal line and price label for the alert point
if (AlertPointEnabled)
{
DrawAlertLineWithLabel(AlertPoint);
}

// Add hotkey for setting alert point


[Link](Chart_KeyDown, Key.A, [Link]);
// Subscribe to mouse down event for setting alert point
[Link] += Chart_MouseDown;
}

protected override void OnTick()


{
double currentPrice = [Link];

if (AlertPointEnabled && currentPrice >= (AlertPoint - PriceOffset) &&


currentPrice <= (AlertPoint + PriceOffset))
{
TriggerAlert();
}
}

private void TriggerAlert()


{
// Play the alert sound
[Link](SoundFilePath);

// Update the alert message on the grid


_alertTextBlock.Text = "its now cross MR VIPER !";

// Start a timer to remove the alert after 2 minutes


_alertTimer = new [Link](2 * 60 * 1000);
_alertTimer.Elapsed += RemoveAlert;
_alertTimer.AutoReset = false;
_alertTimer.Start();
}

private void RemoveAlert(object sender, ElapsedEventArgs e)


{
// Clear the alert message from the grid
_alertTextBlock.Text = [Link];
_alertTimer?.Stop(); // Stop the alert timer
}

protected override void OnStop()


{
// Cleanup resources if necessary
_alertTextBlock.Text = [Link];
_alertTimer?.Stop();
[Link](_grid);
[Link]("alertLine");
[Link]("alertLabel");

// Unsubscribe from mouse down event


[Link] -= Chart_MouseDown;
}

private void InitializeGrid()


{
// Create a grid with two rows and one column
_grid = new Grid(2, 1)
{
HorizontalAlignment = [Link],
VerticalAlignment = [Link],
ShowGridLines = true,
Height = 100,
Width = 300,
};

// Add a text block for the alert message


_alertTextBlock = new TextBlock
{
Text = [Link],
ForegroundColor = [Link](TextColor),
FontSize = TextSize,
HorizontalAlignment = [Link],
VerticalAlignment = [Link]
};
_grid.AddChild(_alertTextBlock, 0, 0);

// Add a text block for the mode message


_modeTextBlock = new TextBlock
{
Text = [Link],
ForegroundColor = [Link],
FontSize = TextSize,
HorizontalAlignment = [Link],
VerticalAlignment = [Link]
};
_grid.AddChild(_modeTextBlock, 1, 0);

// Attach the grid to the chart


[Link](_grid);
}

private void Chart_KeyDown(ChartKeyboardEventArgs obj)


{
// Activate alert point setting mode when the "A" key is pressed
_isSettingAlertPoint = true;
_modeTextBlock.Text = "Click on the chart to set the alert point";
}

private void Chart_MouseDown(ChartMouseEventArgs obj)


{
// If in alert point setting mode, set the alert point to the mouse
click price
if (_isSettingAlertPoint)
{
double clickedPrice = [Link]([Link]);

[Link]("alertLine");
[Link]("alertLabel");
AlertPoint = clickedPrice;
DrawAlertLineWithLabel(AlertPoint);

_isSettingAlertPoint = false;
_modeTextBlock.Text = [Link];
}
}

private void DrawAlertLineWithLabel(double price)


{
// Draw the horizontal line at the alert point
[Link]("alertLine", price, [Link], 2,
[Link]);

// Draw the text label with the price


[Link](
name: "alertLabel",
text: [Link]("F4"),
barIndex: [Link] - 1, // Using the last bar index
y: price, // Y-axis value for the price
color: [Link](TextColor)
);
}
}
}

Common questions

Powered by AI

The PriceAlertWithHotkeys robot allows users to interact with alerts through a user interface by enabling hotkey functionality and mouse interaction. Users can press the 'A' key to activate the alert point setting mode, indicated by a mode message on the chart. Once the mode is activated, users can click on the chart to set a new alert point based on the mouse click price. The robot updates the alert point by removing previous alert lines and labels and drawing a new horizontal line and text label at the clicked price position. This interaction is handled by the Chart_KeyDown and Chart_MouseDown event methods .

The PriceAlertWithHotkeys robot's primary functionality is to alert the user when the current price of a financial instrument crosses a specified alert point. It achieves this by comparing the current price, represented by Symbol.Bid, with the alert point adjusted for a price offset. When the current price is within the specified range, the robot triggers an alert by playing a sound from a specified file path and displaying a message "its now cross MR VIPER !" on a chart grid. This alert mechanism is activated or deactivated automatically in the OnTick method, which checks the price on each tick .

Including both price offset and alert point parameters in the PriceAlertWithHotkeys robot is significant for user flexibility and robustness in alert precision. The alert point acts as a primary threshold for triggering alerts, while the price offset allows for a range of sensitivity around this threshold. This dual-parameter setup provides users with control over how narrowly or broadly they want the alert conditions to be defined, accommodating various trading strategies and market conditions. It enhances the robot's adaptability and usability in different trading scenarios .

Upon stopping, the PriceAlertWithHotkeys robot manages resources by cleaning up UI elements and event subscriptions. The alert message in _alertTextBlock is cleared, and the alert timer is stopped inside the OnStop method. Additionally, the robot removes the grid and graphic elements like "alertLine" and "alertLabel" from the chart and unsubscribes from the Chart.MouseDown event. These steps ensure proper resource management and prevent potential memory leaks by removing unused references and subscriptions .

The hotkey feature contributes to user flexibility by allowing users to quickly enter alert point setting mode without navigating through multiple menu layers. Pressing the 'A' key provides immediate feedback by displaying a mode message, guiding users to click on the chart to set a new alert point. This direct interaction with the chart makes the system intuitive and user-friendly. The hotkey significantly enhances the efficiency of the robot, especially in fast-paced trading scenarios where timely response to price movements is crucial .

The robot uses mouse interactions and hotkeys to dynamically update the alert point through user input. Once the user presses the 'A' key, the chart enters an alert point setting mode, indicated by a message on the mode message TextBlock. Users can then click on the chart, and the Chart_MouseDown event method updates the alert point to the clicked price by removing any existing alert line and label, and drawing a new line and label at the clicked price. This interaction supports dynamic updates without needing to alter hardcoded values or use a traditional UI input form .

The Grid UI component in the PriceAlertWithHotkeys robot serves to display information and status messages to the user. It is structured as a grid with two rows and one column, positioned at the top right of the chart area. The first row contains a TextBlock for displaying alert messages, while the second row holds a TextBlock for mode messages. This grid setup ensures that important alert information is prominently displayed, facilitating user interactions and alert recognition. The grid is initialized and integrated into the chart through the InitializeGrid method .

The robot ensures alert messages do not persist longer than necessary using a timer mechanism. When an alert is triggered, a timer is initialized in the TriggerAlert method to last for two minutes. The timer setup involves associating its Elapsed event with the RemoveAlert method, which clears the alert message and stops the timer once the time elapses. This automated mechanism prevents alert messages from cluttering the interface beyond the necessary duration, promoting a cleaner and less distracting user interface .

The robot handles visual representation of alert conditions by drawing graphical elements on the chart. When an alert is set, a horizontal line is drawn at the alert point using Chart.DrawHorizontalLine, with parameters for color, thickness, and line style. Additionally, a text label showing the alert price is added with Chart.DrawText, positioned at the alert line's Y-coordinate on the chart. This visual setup ensures that users can immediately see where the alert conditions apply in relation to the current market price, enhancing situational awareness and decision-making .

The alert initiation process begins in the OnTick method, which runs on every tick update. It checks if AlertPointEnabled is true and whether the current price is within a specified range relative to the alert point, adjusted by the PriceOffset. If these conditions are met, TriggerAlert is called, which plays a sound file using Notifications.PlaySound and updates a grid TextBlock with the alert message. Additionally, TriggerAlert sets up a timer via System.Timers.Timer to remove the alert message after two minutes. This cleanup process is completed in the RemoveAlert method, which clears the message and stops the timer, maintaining a clean user interface .

You might also like