Persistent AFL drawn chart markings based on mouse clicks

All.

I have modified an existing afl (sorry not sure from where i downloaded to give credit to the author, it was a while ago) to draw levels in charts based on mouse clicks.It works as expected but with this code only one level can be drawn. I wanted to draw multiple levels in one chart and have these levels persist for future reference. I am struggling to make any progress, really not sure how to approach this problem, would using static variable with barindex prefixed help in this situation ? I read through other afl's using static variables along with matrix but can't see to get my head around in this.

Here is the code

bi = BarIndex();
LastVisibleBar = Status("firstvisiblebarindex");
FirstVisibleBar= Status("lastvisiblebarindex");
d=FirstVisibleBar-LastVisibleBar;

TradeType = ParamToggle("LONG/SHORT","SHORT|LONG",0); // 0 means SHORT & 1 means LONG
RequestTimedRefresh( 1 );
SetChartOptions( 2, chartHideQuoteMarker );
LButtonDown = GetCursorMouseButtons() & 8;
MousePrice = GetCursorYPosition();
MouseBar = GetCursorXPosition();
a = Nz(StaticVarGet("counter")); 

MouseBarOpen  = Lookup(O,MouseBar);
MouseBarHigh  = Lookup(H,MouseBar);
MouseBarLow   = Lookup(L,MouseBar);
MouseBarClose = Lookup(C,MouseBar);
MouseBarIndex = Lookup(bi,MouseBar);

ChartID = GetChartID();

StaticVarGet( "Change" )==0;
if ( MousePrice )
{
    StaticVarSet( "MousePrice", MousePrice );
    if ( LButtonDown)
		{
        StaticVarSet("counter", a+1);
		if(StaticVarGet("counter")==1)
		{		
			StaticVarSet( "C1", MousePrice );
			//StaticVarSet( ChartID + NumToStr( MouseBarIndex) + "_CB1", MouseBar );
			StaticVarSet( "CB1", MouseBar );
			StaticVarSet( "CBO1", MouseBarOpen );
			StaticVarSet( "CBH1", MouseBarHigh );
			StaticVarSet( "CBL1", MouseBarLow );
			StaticVarSet( "CBC1", MouseBarClose );
			StaticVarSet( "CBbi1", MouseBarIndex);
			
		}
		else if(StaticVarGet("counter")==2)
		{
			StaticVarSet( "C2", MousePrice );
			StaticVarSet( "CB2", MouseBar );
			StaticVarSet( "CBO2", Lookup(O,MouseBar) );
			StaticVarSet( "CBH2", Lookup(H,MouseBar) );
			StaticVarSet( "CBL2", Lookup(L,MouseBar) );
			StaticVarSet( "CBC2", Lookup(C,MouseBar) );
			StaticVarSet( "CBbi2", Lookup(bi,MouseBar));

		}
	}
}

MousePrice = Nz( StaticVarGet( "Mouseprice" ), Null );
C1 = StaticVarGet( "C1" );
CB1 = StaticVarGet( "CB1" );
CBO1 = StaticVarGet( "CBO1" );
CBH1 = StaticVarGet( "CBH1" );
CBL1 = StaticVarGet( "CBL1" );
CBC1 = StaticVarGet( "CBC1" );
CBbi1 = StaticVarGet( "CBbi1" );


C2 = StaticVarGet( "C2" );
CB2 = StaticVarGet( "CB2" );
CBO2 = StaticVarGet( "CBO2" );
CBH2 = StaticVarGet( "CBH2" );
CBL2 = StaticVarGet( "CBL2" );
CBC2 = StaticVarGet( "CBC2" );
CBbi2 = StaticVarGet( "CBbi2" );

CBbiDistance = CBbi1 - CBbi2;

if (NOT TradeType)  // SHORT
{
	CBPrice1 = IIf(CBC1 < CBO1,CBC1,CBO1); // DOWN BAR take close price otherwise take open price
	
	CBPrice2 = CBH2; // DOWN or UP bar take high price
	
	
}
else // LONG
{
	CBPrice1 = IIf(CBC1 < CBO1,CBO1,CBC1); // DOWN BAR take open price otherwise take close price
	CBPrice2 = CBL2; // DOWN or UP bar take low price
}



CBPrice1 = ValueWhen(bi>=(CBbi1-CBbiDistance),CBPrice1);
CBPrice2 = ValueWhen(bi>=(CBbi1-CBbiDistance),CBPrice2);


Change = StaticVarGet( "Change" );

Title = Title + "\n C1 : " + C1;
Title = Title + "\n CB1 : " + CB1;
Title = Title + "\n CBH1 : " + CBH1;
Title = Title + "\n CBL1 : " + CBL1;
Title = Title + "\n CBC1 : " + CBC1;
Title = Title + "\n CBPrice1 : " + CBPrice1;

Title = Title + "\n C2 : " + C2;
Title = Title + "\n CB2 : " + CB2;
Title = Title + "\n CBH2 : " + CBH2;
Title = Title + "\n CBL2 : " + CBL2;
Title = Title + "\n CBC2 : " + CBC2;
Title = Title + "\n CBPrice2 : " + CBPrice2;
Title = Title + "\n TradeType : " + TradeType;
Title = Title + "\n CBbi1 : " + CBbi1;
Title = Title + "\n CBbi2 : " + CBbi2;
Title = Title + "\n CBbiDistance : " + CBbiDistance;

Title = Title + "\n MouseBarOpen : " + MouseBarOpen;

Title = Title + "\n mouseButtons returns: " + (GetCursorMouseButtons()) + "\nclick counter: " + StaticVarGet("counter"); 

if ( GetCursorMouseButtons() == 12 )
{
   StaticVarRemove("counter");
	StaticVarRemove("C1");
	StaticVarRemove("CB1");
	StaticVarRemove("CBH1");
	StaticVarRemove("CBL1");
	StaticVarRemove("CBC1");
	StaticVarRemove("C2");
	StaticVarRemove("CB2");
	StaticVarRemove("CBH2");
	StaticVarRemove("CBL2");
	StaticVarRemove("CBC2");
	
	StaticVarRemove("Change");
}


if (NOT TradeType)  // SHORT
{
	PlotOHLC( CBPrice1,  CBPrice1,  CBPrice2, CBPrice2, "", ColorRGB(250,220,220),styleCloud|styleNoRescale,Null,Null,0 );

}
else // LONG
{
	PlotOHLC( CBPrice1,  CBPrice1,  CBPrice2, CBPrice2, "", ColorRGB(220,250,250),styleCloud|styleNoRescale,Null,Null,0 );
}

Any points on how to approach this please?

Thanks
Deepak

@jvdeepak I haven't tested "your" solution, but you can borrow some ideas from my code (with further Anderson Wilson's modifications):

Using mouse clicks, you can easily draw or remove multiple horizontal lines and these lines can be persistent.

2 Likes

Thank you @Milosz, really appreciate it. There is a lot more for me to learn here, will continue learning and hopefully write efficient code.

1 Like