How to place signals manually?

Hello guys,
I want to be able to place Buy signals on chart manually in real-time:

ManualBuy 	= Nz(StaticVarGet("ManualBuy"));

function StampSignal(SignalToStamp)
{
	if (SignalToStamp == "Buy")
	{
		ManualBuy[ BarCount - 1 ] = 1;
		StaticVarSet("ManualBuy", ManualBuy);
	}
}

PlotShapes	(StaticVarGet("ManualBuys") * shapeUpTriangle,		colorGreen	);

But seems like I am assigning values wrong (most likely here)

ManualBuy[ BarCount - 1 ] = 1;

where I intend to set last bar's value to 1

Is using static var array a way to go in my situation?
Thank you.

It isn't as easy as that and you definitely need a static variable.

I have a similar code, but I use it to bookmark certain events.

Note: Ideally, you should use the GfxTextOut() function, but I didn't have time to align the font so
used the PlotShape() as well. Its a single call so shouldn't matter much.

I use a 1D matrix to store the DateTime of the bars, the first index [0][0] holds the count that I reset to 0.
mxSig = 10; // Maximum allowed Signals This is the max size that you can adjust.

To set a new signal, I use Left-Ctrl+LMB (left click Mouse button) and
Reset, using Left-Shift+LMB

/// Authored by @travick  11415/2
dt = DateTime(); bi = BarIndex();

LBClick = GetCursorMouseButtons() == 9;   // Left Mouse click
kCtl = GetAsyncKeyState( 162 ) < 0;	  // Ctrl key Left
kSft = GetAsyncKeyState( 160 ) < 0;	  // Shift key Left
clkArr = StaticVarGet( "BuyClicks"+Name() );
mxSig = 10;				  // Maximum allowed Signals
if( typeof( clkArr ) != "matrix" ) {
    clkArr = Matrix( 1, mxSig, 0 );	
    StaticVarSet( "BuyClicks"+Name() , clkArr );
}
cbi = clkArr[0][0];
plotArr[0] = 0;

if ( LBClick AND KCtl ) {		    // Store Buy clicks
    MouseX  = Nz( GetCursorXPosition( 0 ));
    for( i = 1; i <= cbi; i++ )    {	    // Skip duplicates
	if( clkArr[0][i] == MouseX ) {
		i = -1;	break;
	}
    }
    if( (i > cbi AND i <= mxSig-1) OR cbi == 0) {  // Assign Signals
	clkArr[0][0] = ++cbi;
	clkArr[0][cbi] = MouseX;
	StaticVarSet( "BuyClicks"+Name() , clkArr );
    }
    else if(i != -1) Error( "Too many signals" );   // harmless, just click chart again
}
else if (LBClick AND kSft ) {	// Reset
    plotArr = 0; cbi = 0;
    StaticVarSet( "BuyClicks"+Name() , 0 );
}

GfxSetCoordsMode(1); GfxSetBkMode(1); GfxSetTextColor( colorYellow );

dtf = FirstVisibleValue( bi );
dtl = LastVisibleValue( bi );
for(  i = 1; i <= cbi; i++  ) {
    for( j = dtf; j <= dtl; j++ ) {
	if ( dt[j] == clkArr[0][i] ) {
	    plotArr[ j ] = 1;
	    GfxTextOut( "^", j, L[ j ] );
	    // You can use GfxSelectFont and use arrows from Windings etc
	}
    }
}
GfxSetCoordsMode(0);	// Restore default

Plot( C, "Close", colorDefault, ParamStyle("Style") | GetPriceStyle() );
PlotShapes( plotArr * shapeUpArrow, colorLightOrange, 0, L, -15 );

Many thanks & credit to @Milosz and others bcos I've learnt ideas used in some of the snippets from his various implementations in the Forum.

image
The small yellow ^ is the GfxText which I use, converted signals to Array and used PlotShapes() for you.

Also note, I used Static Variable + Name() and create/store signals for each symbol separately which I've done but not thoroughly tested bcos I don't use it.
Also, persist is False, you can set to True to preserve state through AB restarts.

5 Likes

If you have an audio device, a little more user-friendly way to handle the "Too many signals" could be:

// ...
else if( i != -1 ) 
{
	Say( "Too many signals" ); 
}
// ...
2 Likes

Thanks beppe, also a good idea.
that line was added today as a quick measure instead of a GfxTextOut().

Thank you so much!
I need some time to dive into your code now.

No problem, it is still in a crude state, a few things were missing but if it gives you a good direction, we can optimize/add the code further.

Just add a break; statement in the Plotting For Loop after the GfxTextOut(). I hurried yest, was short of time.
also, functionality can be added, to remove only certain signals, currently, you can't, a reset will remove all of them etc or use an IF to check DateTime range before running nested For in Plot (more efficient).
and allowed signals are only 9, 10-1 for counter, not 10 as mentioned :stuck_out_tongue:

1 Like