Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Virtual Trade Simulator Module Version 1.0 – Amibroker AFL Code

3 min read

Thought of launching a simple and user-friendly Virtual Trade Simulator to study/test your inner intuition before practicing in the live markets. Virtual Trade Simulator comes with GUI Button controls to place virtual signals and a profit and loss trading dashboard to trade the current running profits/loss.

Persistent static variables are used to capture the state of the signals and will be autosaved every 30 seconds. If the case to clear the virtual signals then a parameter control to clear those static variables are provided.

Supported Amibroker Version

It is highly recommended to use Amibroker 6.21 version or higher

Currently, Signals are fetched at the close of the candle and the same gets tracked in the PNL Dashboard as well. If any consecutive same signals placed will be ignored. Code is designed in such a way that every entry signals has to be followed by exit signals else the signal gets ignored.

//Coded by Rajandran R - Founder (Marketcalls) and Co-Founder (Algomojo)
//Website - www.marketcalls.in / www.algomojo.com
//Coded Date - 15th Mar 2021

_SECTION_BEGIN("Marketcalls - Virtual Trade Simulator Module");

Version(6.25);

SetBarsRequired(-2,-2);
SetOption("StaticVarAutoSave", 30 );
clear = ParamTrigger("Clear Signals","Clear");

bi = BarIndex();
sbi = lastvalue(bi);
buttonclick = "Simulator"+sbi;

function GuiButtonTrigger( ButtonName, x, y, width, Height ) {
	
	global IDset;
	local id, event, clickeven;
	
	if( typeof( IDset ) == "undefined" ) IDset = 0; 

	//_TRACEF( "IDset before: %g", IDset );	
	GuiButton( ButtonName, ++IDset, x, y, width, height, 7 ); 
	//_TRACEF( "IDset after: %g", IDset );
	result = 0;
	id = GuiGetEvent( 0, 0 );// receiving button id
	event = GuiGetEvent( 0, 1 );// receiving notifyflag
	clickevent = event == 1;
	BuyClicked = id == 1 && clickevent;
	SellClicked = id == 2 && clickevent;
	ShortClicked = id == 3 && clickevent;
	CoverClicked = id == 4 && clickevent;
	if( BuyClicked AND StaticVarGet(Name()+GetChartID()+"buyClicked")==0 ) 
	{
		StaticVarSet(buttonclick,1,True);
		Say("Buy Button Clicked");
		result = 1;
		StaticVarSet(Name()+GetChartID()+"buyClicked",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"buyClicked",0);
	}
	if( SellClicked AND StaticVarGet(Name()+GetChartID()+"sellClicked")==0 ) 
	{
		StaticVarSet(buttonclick,-1,True);
		Say("Sell Button Clicked");
		result = -1;
		StaticVarSet(Name()+GetChartID()+"sellClicked",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"sellClicked",0); 
	}
	if( ShortClicked AND StaticVarGet(Name()+GetChartID()+"shortClicked")==0 ) 
	{
		StaticVarSet(buttonclick,-2,True);
		Say("Short Button Clicked");
		result = -2;
		StaticVarSet(Name()+GetChartID()+"shortClicked",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"shortClicked",0); 
	}
	if( coverClicked AND StaticVarGet(Name()+GetChartID()+"coverClicked")==0 ) 
	{
		StaticVarSet(buttonclick,2,True);
		Say("Cover Button Clicked");
		result = 2;
		StaticVarSet(Name()+GetChartID()+"coverClicked",1); 
	}
	else
	{
		StaticVarSet(Name()+GetChartID()+"coverClicked",0); 
	}
	return result;
	
}

_SECTION_END();

_SECTION_BEGIN("Button Plot and Mouse Click Detection Module");

BuyTrigger = GuiButtonTrigger( "Long Entry", 0, 100, 200, 30 );
SellTrigger = GuiButtonTrigger( "Long Exit", 0, 100+50, 200, 30);
ShortTrigger = GuiButtonTrigger( "Short Entry", 200, 100, 200, 30 );
coverTrigger = GuiButtonTrigger( "Short Exit", 200, 100+50, 200, 30 );
GuiSetColors( 1,1, 2, colorgreen, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); 
GuiSetColors( 2,2, 2, colororange, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); 
GuiSetColors( 3,3, 2, colorred, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); 
GuiSetColors( 4,4, 2, colorBlue, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); 

//initializing
Buy = Sell = Short = Cover = 0;

for(i=0; i< BarCount-1;i++)
{
sig = StaticVarGet("Simulator"+i);
if(sig == 1) Buy[i] = true;
if(sig == -1) Sell[i] = true;
if(sig == -2) Short[i] = true;
if(sig == 2) Cover[i] = true;

}

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

//Buy at Next Bar Open, Trade Delay is already given at 
BuyPrice=ValueWhen(Buy,close);
SellPrice=ValueWhen(Sell,Close);
ShortPrice=ValueWhen(Short,Close);
CoverPrice=ValueWhen(Cover,Close);

buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);


if(clear)
{


StaticVarRemove("Simulator*");


}


_SECTION_END();


_SECTION_BEGIN("Plot Buy and Sell Signals");

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);

_SECTION_END();


_SECTION_BEGIN("Price");

SetChartOptions(0, chartShowArrows | chartShowDates); //x-Axis will be plottted
//plot the candles
Plot(Close,"Close",colorDefault,GetPriceStyle() | styleNoTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();


_SECTION_BEGIN("Signal Dashboard");

showMsgBoard = ParamToggle("Message Board", "Hide|Show", 1);

if ( showMsgBoard == 1 )
{
Long=Flip(Buy,Sell); 
Shrt=Flip(Short,Cover); 
Relax = NOT Long AND NOT Buy AND NOT shrt AND NOT Short AND NOT Sell AND NOT Cover;
BarsSincebuy = BarsSince( Buy ); 
BarsSinceshort = BarsSince( Short ); 
LastSignal = IIf( BarsSincebuy < BarsSinceshort, 1, -1 ); 

GfxSelectFont( "BOOK ANTIQUA", 12, 100 );
GfxSetBkMode( 1 ); 
GfxSetTextColor( colorWhite ); 

//if ( SelectedValue( LastSignal ) == 1 )
if ( SelectedValue( Long ) == 1 ) 
{ 
	GfxSelectSolidBrush( colorDarkGreen ); 
} 
else if ( SelectedValue( Shrt ) == 1 )
{ 
	GfxSelectSolidBrush( colorDarkRed ); 
} 
else
{ 
	GfxSelectSolidBrush( colorDarkGrey ); 
} 

pxHeight = Status( "pxchartheight" ) ; 
xx = Status( "pxchartwidth"); 
Left = 1100; 
width = 310; 
x = 5; 
x2 = 300; 
y = pxHeight; 
y2 = y;

GfxSelectPen( colorWhite, 1); // border color 
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ; 
GfxTextOut( ( "Virtual Simulator V1.0"),13,y-93);

y2 = y2-70;
GfxTextOut( ("" + WriteIf(Buy, "Buy @ "+ BuyPrice, "")), 13, y2); 
GfxTextOut( ("" + WriteIf(Short, "Short @ "+ ShortPrice,"")), 13, y2); 
GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Long @ "+(BuyPrice),"")), 13, y2); 
GfxTextOut( ("" + WriteIf (shrt AND NOT Short, "Short @ "+(ShortPrice),"")), 13, y2); 
GfxTextOut( ("" + WriteIf (Relax, "No Trade Zone - Wait!","")), 13, y2); 


y2 = y2+20;
GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Current P/L: "+NumToStr((C-BuyPrice),1.2)+" Points","")), 13, y2); 
GfxTextOut( ("" + WriteIf (shrt AND NOT Short, "Current P/L: "+NumToStr((ShortPrice-C),1.2)+" Points","")), 13, y2); 

y2 = y2-20;
GfxTextOut( ("" + WriteIf (Sell AND NOT Short, "Exit Long @ "+ SellPrice ,"")), 13, y2);
GfxTextOut( ("" + WriteIf (Cover AND NOT Buy, "Exit Short @ "+ CoverPrice,"")), 13, y2);

y2 = y2+20;
GfxTextOut( ("" + WriteIf (Sell AND NOT Short, "P/L : "+NumToStr((SellPrice-BuyPrice),1.2)+" Points","")), 13, y2); 
GfxTextOut( ("" + WriteIf (Cover AND NOT Buy, "P/L : "+NumToStr((ShortPrice-CoverPrice),1.2)+" Points","")), 13, y2); 
}
_SECTION_END();

Let me know your feedback,enhancements and comments down below.

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

[Free Course] Mini Certification on Amibroker AFL Programming

Interested in getting to know the basics of Amibroker AFL Programming then here is a free live training course to access hands-on training on...
Rajandran R
53 sec read

Smart Button Trading Module – Trade Automation – Amibroker…

This Algomojo Button trading module provides 4 modes of the button to place orders with a single click directly from Amibroker using simple smart...
Rajandran R
4 min read

AlphaTrend – Amibroker AFL Code to Manage Sideways Markets

Very simple and interesting trend following strategy which could manage sideways markets to a greater extent while following the trend. Recently I got inspired...
Rajandran R
3 min read

2 Replies to “Virtual Trade Simulator Module Version 1.0 – Amibroker AFL…”

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More