Trying to create a risk calculator but keeps getting an error
Trying to create a risk calculator but keeps getting an error
13 Oct 2024, 13:36
Hello im trying to create an risk calculator and a button to buy and sell but keeps getting error on “Chart.GetMousePosition”. im not an expert in scripting or anything. here is the script and hope someone can help: :)
using cAlgo.API; // Ensure we include the cAlgo API
using static System.Math; // Using static for Math methods
using Color = cAlgo.API.Color; // Alias Color to avoid confusion
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class AdjustableStopLossTool : Robot
{
[Parameter("Risk Percentage", DefaultValue = 1.0, MinValue = 0.1)]
public double RiskPercentage { get; set; }
[Parameter("Risk-to-Reward Ratio", DefaultValue = 2.0, MinValue = 1.0)]
public double RiskRewardRatio { get; set; }
[Parameter("Use Account Balance (true) or Equity (false)", DefaultValue = true)]
public bool UseAccountBalance { get; set; }
private ChartHorizontalLine _stopLossLine;
protected override void OnStart()
{
// Create a draggable stop-loss line on the chart
_stopLossLine = Chart.DrawHorizontalLine("StopLossLine", Symbol.Bid - 20 * Symbol.PipSize, Color.Red, 2, LineStyle.Dots);
_stopLossLine.IsInteractive = true; // Make the line draggable
// Draw buttons
DrawButtons();
}
private void DrawButtons()
{
// Draw clickable text for Buy
Chart.DrawText("BuyText", "Buy", 10, 20, Color.Green);
// Draw clickable text for Sell
Chart.DrawText("SellText", "Sell", 10, 50, Color.Red);
}
protected override void OnTick()
{
// Check mouse clicks to execute trades
CheckForButtonClicks();
if (_stopLossLine != null)
{
double stopLossPrice = _stopLossLine.Y; // Get the price from the stop-loss line
double stopLossPips = Abs(Symbol.Bid - stopLossPrice) / Symbol.PipSize; // Use Abs for absolute value
// Display risk and reward information
DisplayRiskRewardInfo(stopLossPrice, stopLossPips);
}
}
private void CheckForButtonClicks()
{
var mousePosition = Chart.GetMousePosition();
// Check Buy button area
if (mousePosition.Y >= 20 && mousePosition.Y <= 40 && mousePosition.X >= 10 && mousePosition.X <= 50)
{
ExecuteTrade(TradeType.Buy);
}
// Check Sell button area
if (mousePosition.Y >= 50 && mousePosition.Y <= 70 && mousePosition.X >= 10 && mousePosition.X <= 50)
{
ExecuteTrade(TradeType.Sell);
}
}
private void DisplayRiskRewardInfo(double stopLossPrice, double stopLossPips)
{
double riskAmount = CalculateRiskAmount();
double lotSize = CalculateLotSize(riskAmount, stopLossPips);
double takeProfitPips = stopLossPips * RiskRewardRatio;
// Construct the text to display
string text = $"Risk: {riskAmount:C}, Lot Size: {lotSize:F2}, TP: {takeProfitPips:F2} pips";
// Draw the text on the chart
Chart.RemoveObject("RiskInfo"); // Remove old risk info
Chart.DrawText("RiskInfo", text, 0, stopLossPrice + 10 * Symbol.PipSize, Color.White);
}
private double CalculateRiskAmount()
{
return UseAccountBalance ? Account.Balance * (RiskPercentage / 100) : Account.Equity * (RiskPercentage / 100);
}
private double CalculateLotSize(double riskAmount, double stopLossPips)
{
double pipValue = Symbol.PipValue;
return riskAmount / (pipValue * stopLossPips);
}
public void ExecuteTrade(TradeType tradeType)
{
if (_stopLossLine == null)
{
Print("Please set a valid stop-loss line on the chart.");
return;
}
double stopLossPrice = _stopLossLine.Y; // Get stop-loss price
double takeProfitPrice = stopLossPrice + (Abs(Symbol.Bid - stopLossPrice) / Symbol.PipSize * RiskRewardRatio * Symbol.PipSize);
double riskAmount = CalculateRiskAmount();
double lotSize = CalculateLotSize(riskAmount, Abs(Symbol.Bid - stopLossPrice) / Symbol.PipSize); // Use Abs for absolute value
// Place the trade with calculated parameters
ExecuteMarketOrder(tradeType, SymbolName, Symbol.NormalizeVolumeInUnits(lotSize), "AdjustableSLTrade", stopLossPrice, takeProfitPrice);
}
protected override void OnStop()
{
// Remove the stop-loss line and risk info if they exist
if (_stopLossLine != null)
{
Chart.RemoveObject(_stopLossLine.Name);
}
Chart.RemoveObject("RiskInfo");
Chart.RemoveObject("BuyText");
Chart.RemoveObject("SellText");
}
}
}
PanagiotisCharalampous
14 Oct 2024, 05:18
Hi there,
Where did you find this code? You are using a method that does not exists.
Best regards,
Panagiotis
@PanagiotisCharalampous