AmiBroker Simulator

amitrandive

Well-Known Member
#1
Got this from the net , not used .But am sure may help many.

Plotting real trades in Amibroker
There are two ways to communicate with Amibroker to do such a simulation. One is to use the feature of external studies, or trend lines which can be drawn and programatically using the intersection of the price line with the study to generate a buy or sell signal. Its best to use the vertical study line as shown in the next paragraph. This was the approach followed in the previous version of the simulator.

In the new version of the simulator, you simply select the vertical bar location on the chart where you want to put a Buy, Sell, Short or Cover signal, and use the ParamTrigger function to communicate with Amibroker.

See the following simple code which helps you to generate these simulation lines. The best part of this code is that it can be used in any time frame.
Code:
SetBarsRequired(10000, 0 );//Makes sure that your signals do not move - enough bars are loaded.
avp=(O+C)/2; // generating an average price line. You could use just close as well.
setbuy = ParamTrigger("Buy", "Buy" ); // ParamTrigger function used to generate a Buy Signal
setsell = ParamTrigger("Sell", "Sell" ); // ParamTrigger function used to generate a Sell Signal 
setshort = ParamTrigger("Short", "Short" ); // ParamTrigger function used to generate a Short Signal 
setcover = ParamTrigger("Cover", "Cover" ); // ParamTrigger function used to generate a Cover Signal 
clear = ParamTrigger("Clear", "Clear" ); // ParamTrigger function used to Clear the current signal (selected)
clearall = ParamTrigger("Clear All", "Clear All" ); // ParamTrigger function used to clear all signals.

bi = BarIndex(); //Current bar index
sbi = SelectedValue( bi );//Selected chart position - vertical line
qty = LastValue( bi );// last value of bar index
Varname = Name() + sbi;//Static variable being set
if( setbuy )
{
StaticVarSet( Varname, 1 );//Static variable stores the appropriate signal, Buy here.
} 
if( setsell )
{
StaticVarSet( Varname, -1 ); //Static variable stores the appropriate signal, Sell here. 
} 
if( setshort )
{
StaticVarSet( Varname, -2 ); //Static variable stores the appropriate signal, Short here. 
} 
if( setcover )
{
StaticVarSet( Varname, 2 ); //Static variable stores the appropriate signal, Cover here. 
} 
if( clear )
{
StaticVarRemove( Varname );//Removes currently selected signal
} 
if( clearall )
{
for( i = 0; i < qty; i++ ) StaticVarRemove( Name() + i );//clears all signals
} 
Buy = Sell = Short = Cover = 0;
for( i = 0; i < qty; i++ )
{
sig = StaticVarGet( Name() + i );
  if( sig == 1 ) Buy[ i ] = True;//assign the signals to the right arrays.
if( sig == -1 ) Sell[ i ] = True;
if( sig == -2 ) Short[ i ] = True;
if( sig == 2 ) Cover[ i ] = True;
} 
Plot(avp, "Mid", IIf(avp>Ref(avp,-1),colorGreen,colorRed), styleNoTitle  ); 
shape = Buy * shapeUpArrow + Sell * shapeHollowDownArrow;PlotShapes( shape, IIf( Buy, colorLime, colorRed ), 0, IIf( Buy, Low, High));


Also attached the complete AFL

Code:
// Trade simulation. Helps you to record your real trades and test new trading approaches without any programming skills.
// Upto 9 trades can be plotted in any time frame.
// Once installed on Amibroker insert the vertical Study lines with labels b1, b2 etc for Buy signals AND s1, s2 etc for Short signals AND 
// e1, e2 etc for the exit signals. The AFL automatically recognises the context of the exit for a Buy OR Sell Signal. 
// Remember that the Buy AND Sell signals are generated one bar after the vertical position of the Study line. 
// This has no practical impact on entering your trade signals.
// Next you can modify the trade entry AND exit values, if required in the parameter windows AND you have your trade journal ready!
// To re-use the AFL for another trading session, please remember to delete all the existing studies, 
// by right clicking in the display area AND selecting delete al studies AND resetting the parameter window.

SetBarsRequired(10000, 0 );  
setbuy = ParamTrigger("Buy", "Buy" );
setsell = ParamTrigger("Sell", "Sell" );
setshort = ParamTrigger("Short", "Short" );
setcover = ParamTrigger("Cover", "Cover" );
clear = ParamTrigger("Clear", "Clear" );
 
clearall = ParamTrigger("Clear All", "Clear All" );

showcandlechart=ParamList("Show Candle chart","YES|NO");

stylecndl=ParamList("Bar/Candle chart","BAR|CANDLE");
BarLum1 		= Param("Bar Color Intensity", 8, 0, 10,01);
LineColor 		= ColorBlend(ColorRGB(50,60,60), ColorRGB(50,60,60), BarLum1);
UpBarColor		= ColorBlend(ColorRGB(5,36,5), ColorRGB(10,75,10), BarLum1);
DnBarColor		= ColorBlend(ColorRGB(36,5,5), ColorRGB(75,10,10), BarLum1);



bi = BarIndex();
qty = LastValue( bi );

y = Year(); 
m = Month(); 
d = Day(); 
r = Hour();
e = Minute();

sby = SelectedValue( y );
sbm = SelectedValue( m );
sbd = SelectedValue( d );
sbr = SelectedValue( r );
sbe = SelectedValue( e );


 qty = LastValue( bi );
 
Varname = Name()+ sby + sbm + sbd + sbr + sbe;

 
if( setbuy )
 {
   StaticVarSet( Varname, 1 );
 } 
 
if( setsell )
 {
   StaticVarSet( Varname, -1 );
 } 
 
if( setshort )
 {
   StaticVarSet( Varname, -2 );
 } 
 
if( setcover )
 {
   StaticVarSet( Varname, 2 );
 } 
 
if( clear )
 {
   StaticVarRemove( Varname );
 } 
 
if( clearall )
 {
   for( i = 0; i < qty; i++ ) StaticVarRemove( Name()+y[i] + m[i] + d[i] + r[i] + e[i]);
 
 } 
 
Buy = Sell = Short = Cover = exit =0;



tp1=Param("Trade 1 price ",0,0,10000);
ep1=Param("Exit 1 price ",0,0,10000);
tp2=Param("Trade 2 price ",0,0,10000);
ep2=Param("Exit 2 price ",0,0,10000);
tp3=Param("Trade 3 price ",0,0,10000);
ep3=Param("Exit 3 price ",0,0,10000);
tp4=Param("Trade 4 price ",0,0,10000);
ep4=Param("Exit 4 price ",0,0,10000);
tp5=Param("Trade 5 price ",0,0,10000);
ep5=Param("Exit 5 price ",0,0,10000);
tp6=Param("Trade 6 price ",0,0,10000);
ep6=Param("Exit 6 price ",0,0,10000);
tp7=Param("Trade 7 price ",0,0,10000);
ep7=Param("Exit 7 price ",0,0,10000);
tp8=Param("Trade 8 price ",0,0,10000);
ep8=Param("Exit 8 price ",0,0,10000);
tp9=Param("Trade 9 price ",0,0,10000);
ep9=Param("Exit 9 price ",0,0,10000);



profit=sessprofit=priceatbuy=priceatshort=priceatexit=tradecount=lastsig=0;
dmy=ATR(10);
shape=xp=ep=cp=0;
tradeline=Null;
BarColor		= IIf(Close > Open, UpBarColor, DnBarColor);
SetBarFillColor(BarColor);
avp=(O+C)/2;//you can choose to have avp=c instead of average
if (stylecndl=="BAR")stylec=styleBar;
else stylec=styleCandle;
if (showcandlechart=="YES")
{
	
	Plot(C, "Close", colorWhite, styleNoTitle | stylec); 
}
avp=(O+C)/2;


for (i=1;i<BarCount-1;i++)
{
	sig = StaticVarGet( Name() +y[i] + m[i] + d[i] + r[i] + e[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;
	if (sig==-1 OR sig==2)exit[i]=True;
// } 


	if (Lastsig!=0 AND !Buy[i] AND !Short[i])tradeline[i]=tradeline[i-1];
	if (Buy[i] OR Short[i])
	{
		tradecount=tradecount+1;
		if (tradecount==1){xp=tp1;ep=ep1;}
		else if (tradecount==2){xp=tp2;ep=ep2;}
		else if (tradecount==3){xp=tp3;ep=ep3;}
		else if (tradecount==4){xp=tp4;ep=ep4;}
		else if (tradecount==5){xp=tp5;ep=ep5;}
		else if (tradecount==6){xp=tp6;ep=ep6;}
		else {xp=0;ep=0;}
	}
	if (Buy[i]) 
	{
		if (xp>0)priceatbuy=tradeline[i]=xp;
		else priceatbuy=tradeline[i]=avp[i];//defaulting price to average price.
		if (Lastsig==-1)
		{
			profit=priceatshort-priceatbuy;
			sessprofit=sessprofit+profit;
			PlotText(WriteVal(profit,1.0),i,H[i]+2*dmy[i],colorYellow);
		}
		Lastsig=1;
	}
	if (Short[i])
	{
//		PlotText("1",i,H[i]+10,colorWhite);
		if (xp>0)priceatshort=tradeline[i]=xp;
		else priceatshort=tradeline[i]=avp[i];//defaulting price to average price.
		if (Lastsig==1)
		{
			profit=priceatshort-priceatbuy;
			sessprofit=sessprofit+profit;
			PlotText(WriteVal(profit,1.0),i,H[i]+2*dmy[i],colorYellow);
		}
		Lastsig=-1;
	}
	if (Lastsig==0)tradeline[i]=Null;
	if (exit[i])
	{
		if (ep>0)priceatexit=ep;
		else priceatexit=avp[i];
		if (Lastsig==1)
		{
			profit= priceatexit-priceatbuy;
			Sell[i]=1;
		}
		else if (Lastsig==-1)
		{
			profit= priceatshort-priceatexit;
			Cover[i]=1;
		}
		sessprofit=sessprofit+profit;
		PlotText(WriteVal(profit,1.0),i,H[i]+dmy[i],colorYellow);
		Lastsig=profit=0;
	}
	if (Lastsig==1)Cp[i]=avp[i]-priceatbuy;
	else if (Lastsig==-1)Cp[i]=priceatshort-avp[i];

}


if (Lastsig!=0)sessprofit=sessprofit+Cp[i-1];

Plot(avp, "Mid", ColorRGB(120,120,120), styleNoTitle  ); 
Plot(Tradeline, "Trade price", colorBlue ); 

shapebuy = IIf(Buy,shapeUpArrow,Null);
Shapesell= IIf(Sell,shapeHollowDownArrow,Null);
shapeshort = IIf(Short,shapeDownArrow,Null);
Shapecover= IIf(Cover,shapeHollowUpArrow,Null);

shape2= IIf(exit,shapeDownArrow,Null);
PlotShapes( shapeshort, colorRed, 0, High);
PlotShapes( shapebuy, colorGreen, 0, Low);
PlotShapes( shapesell, colorBlue, 0, High);
PlotShapes( shapecover, colorBlue, 0, Low);
PlotText(WriteVal(sessprofit,1.0)+"\n"+WriteVal(tradecount,1.0),i,H[i]+dmy[BarCount-1],colorYellow);

Title = Date()+" Trade Plotter "+ Interval(format=2)+"  "+Name()+" O "+WriteVal(O,1.2)+" H "+WriteVal(H,1.2)+"  L "+WriteVal(L,1.2)+" C "+WriteVal(C,1.2)+" Mid price " +WriteVal(avp,1.2)+EncodeColor(colorYellow)+" Trade Price " +WriteVal(tradeline,1.2);
 

Similar threads