Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

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 Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 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