Please refer original Post here, where I wanted to see if anyone has LONG / SHORT position Tool like you have on TView.
One user did have it. But he was not particularly helpful.
I have no coding knowledge but using AI Coding etc. I could manage below. However, the Drawing is very primitive, dragging it is very jerky, Even resizing the Top (Risk) and Bottom Rectangles (Reward) is very jerky. I wanted circles to appear when I hover mouse near the top and bottom Left of rectangles. Even the circles which appear are not instantaneous.
I wanted functionality of the drawing like how Native drawings of Amibroker are smooth (Drawing Rectangles, Circles etc) on charts and mine is not so smooth.
I also tried increasing timedrefresh but to no avail..
Following is the code,
_SECTION_BEGIN("Smooth Risk-Reward Tool");
// Enable QuickGFX for smooth rendering
GfxSetOverlayMode(1);
// Function to Draw a Rectangle
function DrawRectangle(x1, y1, x2, y2, BackColor)
{
GfxSelectSolidBrush(BackColor);
GfxRectangle(x1, y1, x2, y2);
}
// Function to Draw Circles for Resizing
function DrawCircle(cx, cy, radius, color, opacity)
{
GfxSelectSolidBrush(ColorBlend(color, colorWhite, opacity));
GfxCircle(cx, cy, radius);
}
// Define Rectangle Size
ParamSqrWidth = 150;
ParamSqrHeight = 50;
// Define Colors via Parameters
RiskColor = ParamColor("Risk Rectangle Color", colorRose);
RewardColor = ParamColor("Reward Rectangle Color", colorGreen);
// Reset Position if Required
Reset = ParamTrigger("Reset Coordinates", "RESET");
xOffset = Nz(StaticVarGet("xOffset"), 50);
yOffset = Nz(StaticVarGet("yOffset"), 100);
RiskHeight = Nz(StaticVarGet("RiskHeight"), ParamSqrHeight);
RewardHeight = Nz(StaticVarGet("RewardHeight"), ParamSqrHeight);
if (Reset)
{
StaticVarSet("xOffset", 50);
StaticVarSet("yOffset", 100);
StaticVarSet("RiskHeight", ParamSqrHeight);
StaticVarSet("RewardHeight", ParamSqrHeight);
StaticVarSet("MoveInProgress", False);
StaticVarSet("ResizeInProgress", False);
}
// Constants
circleRadius = 8;
minHeight = 10;
// Define Rectangle Positions
x1 = xOffset;
y2 = yOffset;
y1 = y2 - RiskHeight;
x2 = x1 + ParamSqrWidth;
y3 = y2 + RewardHeight;
// Define Corner Handles
circleTL_x = x1;
circleTL_y = y1;
circleBL_x = x1;
circleBL_y = y3;
// Get Mouse Position & Clicks
MousePx = GetCursorXPosition(1);
MousePy = GetCursorYPosition(1);
LButtonDown = GetCursorMouseButtons() & 1;
LButtonReleased = GetCursorMouseButtons() == 0;
// Check Cursor Proximity
CursorNearTopLeft = (abs(MousePx - circleTL_x) <= circleRadius AND abs(MousePy - circleTL_y) <= circleRadius);
CursorNearBottomLeft = (abs(MousePx - circleBL_x) <= circleRadius AND abs(MousePy - circleBL_y) <= circleRadius);
CursorInField = (MousePx >= x1 AND MousePx <= x2 AND MousePy >= y1 AND MousePy <= y3);
// Get Resize & Move Status
ResizeInProgress = Nz(StaticVarGet("ResizeInProgress"), False);
MoveInProgress = Nz(StaticVarGet("MoveInProgress"), False);
// Start Moving or Resizing
if (!MoveInProgress AND !ResizeInProgress)
{
if (LButtonDown AND CursorInField AND !CursorNearTopLeft AND !CursorNearBottomLeft)
{
StaticVarSet("MoveInProgress", True);
StaticVarSet("DownPx1", MousePx);
StaticVarSet("DownPy1", MousePy);
}
else if (LButtonDown AND CursorNearTopLeft)
{
StaticVarSet("ResizeInProgress", True);
StaticVarSet("ResizeType", 1); // Top resize
StaticVarSet("StartMouseY", MousePy);
}
else if (LButtonDown AND CursorNearBottomLeft)
{
StaticVarSet("ResizeInProgress", True);
StaticVarSet("ResizeType", 2); // Bottom resize
StaticVarSet("StartMouseY", MousePy);
}
}
// Stop Moving or Resizing on Mouse Release
if (LButtonReleased)
{
StaticVarSet("MoveInProgress", False);
StaticVarSet("ResizeInProgress", False);
}
// Moving Logic
if (MoveInProgress AND LButtonDown)
{
DownPx1 = StaticVarGet("DownPx1");
DownPy1 = StaticVarGet("DownPy1");
xMove = MousePx - DownPx1;
yMove = MousePy - DownPy1;
xOffset += xMove;
yOffset += yMove;
// Prevent Moving Out of Bounds
xOffset = Min(Max(xOffset, 10), Status("pxWidth") - ParamSqrWidth - 10);
yOffset = Min(Max(yOffset, 10), Status("pxHeight") - RiskHeight - RewardHeight - 10);
StaticVarSet("xOffset", xOffset);
StaticVarSet("yOffset", yOffset);
StaticVarSet("DownPx1", MousePx);
StaticVarSet("DownPy1", MousePy);
}
// Resizing Logic
if (ResizeInProgress AND LButtonDown)
{
ResizeType = StaticVarGet("ResizeType");
StartMouseY = StaticVarGet("StartMouseY");
if (ResizeType == 1) // Adjust top of red rectangle
{
deltaY = StartMouseY - MousePy;
RiskHeight = Max(minHeight, RiskHeight + deltaY);
StaticVarSet("RiskHeight", RiskHeight);
StaticVarSet("StartMouseY", MousePy);
}
else if (ResizeType == 2) // Adjust bottom of green rectangle
{
deltaY = MousePy - StartMouseY;
RewardHeight = Max(minHeight, RewardHeight + deltaY);
StaticVarSet("RewardHeight", RewardHeight);
StaticVarSet("StartMouseY", MousePy);
}
}
// Update Positions for Drawing
xOffset = Nz(StaticVarGet("xOffset"), 50);
yOffset = Nz(StaticVarGet("yOffset"), 100);
RiskHeight = Nz(StaticVarGet("RiskHeight"), ParamSqrHeight);
RewardHeight = Nz(StaticVarGet("RewardHeight"), ParamSqrHeight);
x1 = xOffset;
y2 = yOffset;
y1 = y2 - RiskHeight;
x2 = x1 + ParamSqrWidth;
y3 = y2 + RewardHeight;
circleTL_x = x1;
circleTL_y = y1;
circleBL_x = x1;
circleBL_y = y3;
// Draw Rectangles with Customizable Colors
DrawRectangle(x1, y1, x2, y2, RiskColor);
DrawRectangle(x1, y2, x2, y3, RewardColor);
// Draw Circles for Resizing (Visual Feedback)
if (CursorNearTopLeft OR (ResizeInProgress AND StaticVarGet("ResizeType") == 1))
DrawCircle(circleTL_x, circleTL_y, circleRadius, colorBlue, 0.3);
if (CursorNearBottomLeft OR (ResizeInProgress AND StaticVarGet("ResizeType") == 2))
DrawCircle(circleBL_x, circleBL_y, circleRadius, colorBlue, 0.3);
// Calculate & Display Risk-Reward Ratio
RiskRewardRatio = IIf(RiskHeight > 0, RewardHeight / RiskHeight, 0);
Title = StrFormat("Smooth Risk-Reward Tool\nRisk-Reward Ratio: %.2f\nClick inside to move\nDrag top circle to adjust risk\nDrag bottom circle to adjust reward\nX1: %g | Y1: %g | Risk H: %g | Reward H: %g",
RiskRewardRatio, x1, y1, RiskHeight, RewardHeight);
_SECTION_END();
Please point me in the right direction of how I should go about it. I referred to KB artices of moving GUI shapes, and the Gif that the user posted on my previous Original Post.
Also, attaching the .gif of my tool. Its too jerky and primitive. Please guide !