Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Button trading for Amibroker (AFL)
krrish
over 14 years ago
Amibroker (AFL)

Rating:
5 / 5 (Votes 1)
Tags:
oscillator, macd, trading system, amibroker

This is the MACD with Button trading so you can automatically execute trades via Interactive Brokers. My advice is be careful with it and test it before using it on a real account.

Similar Indicators / Formulas

Rahul Mohindar Oscillator (RMO)
Submitted by kaiji almost 15 years ago
Price Oscillator
Submitted by ariful_islam over 14 years ago
1 hour stoch
Submitted by siddhisagar over 14 years ago
Stochastic-and-linear
Submitted by btdung over 13 years ago
Nj demand and supply
Submitted by njethva about 12 years ago
Trending Wave 2 System
Submitted by Dryden almost 13 years ago

Indicator / Formula

Copy & Paste Friendly
_SECTION_BEGIN("Button trading"); 
EnableTextOutput(False);
SetChartOptions(0, chartShowDates); 
RequestTimedRefresh(1);
Filename = StrLeft(_DEFAULT_NAME(),StrLen(_DEFAULT_NAME())-2);
_N(Title = Filename + StrFormat(" - {{DATE}} \nOpen %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));

VarPfx = "Btn1"; // some var that reflects the trading file

// ####  Parameters   ########################################################################################################
AutoTrade	= ParamToggle("Auto trade", "Off|Running");
Contracts	= Param("Num contracts", 1, 1, 1000, 1);
typeOrder = ParamList("Order type", "MKT,STP");
BuyOrder  	= ParamTrigger("Place Buy order","Buy");
SellOrder = ParamTrigger("Place Sell order","Sell");
CloseAll  = ParamTrigger("Close all positions","Close all");
CancelAll = ParamTrigger("Cancel all orders","Cancel all");
Reset		= ParamTrigger("Reset", "Reset"); 


// ####  Place your timing system here #######################################################################################

// calc MACD
myMACD = MACD(); 		// calculate the macd
mySig = Signal();		// calculate the signal
// macd buy and sell conditions
upMacd = myMACD > mysig; // buy condition is the macd line above the signal line
dnMacd = mySig  > myMACD; // sell condition is the macd line below the signal line

Buy = Sell = Short = Cover = 0; // make sure all arrays are set false
Buy  = Cover = upMACD; 
Sell = Short = dnMACD;  

// ####  Plot indicators and shapes  ##########################################################################################
Plot(myMACD, "\nMACD",colorRed,styleThick|styleLeftAxisScale); // plot the macd line
Plot(mySig,"\nSignal",colorBlack,styleDashed|styleLeftAxisScale); // plot the macd signal line

PlotShapes(Buy * shapeUpArrow, colorGreen,0,Low, 2);
PlotShapes(Sell * shapeDownArrow, colorRed,0,High, 2);
 
// ####  Static vars reset   ##################################################################################################
if( reset OR Nz(StaticVarGet(varPfx  + "Init") == False) )
{
	StaticVarSetText(varPfx  + "orderID", "");
	StaticVarSetText(VarPfx + "lastTrade", ""); 
	StaticVarSet(VarPfx + "numPositions", 0);
	_TRACE("# init, " + NumToStr(StaticVarGet(varPfx  + "num"), 1.0));
}

// ####  functions  ###########################################################################################################

function fSayOnce( text ) 
{ 
   	if( StaticVarGetText(VarPfx + "lastsaidtext") != text ) 
	{ 
      	Say( text ); 
      	StaticVarSetText(VarPfx + "lastsaidtext", text ); 
		if(DebugOn) 	_TRACE("#, SayOnce Text =" + text + "\n");
   } 
} 

// ####  Trading section ########################################################################################################

if(autotrade) 
{
	ibc = GetTradingInterface("IB");
	ConnectedStatus = ibc.IsConnected();	// get the connection status, 2 is OK

	// this is where the trade processing is done
	if( ConnectedStatus == 2 OR ConnectedStatus == 3) // connected to TWS with no error messages
	{
		OrderID = StaticVarGetText(VarPfx + "OrderID");
		OrderStatus = ibc.GetStatus( OrderID, True );
		oldNumPositions = StaticVarGet(VarPfx + "numPositions");
		numPositions  = ibc.GetPositionSize(Name()); 
		StaticVarSet(VarPfx + "numPositions", numPositions  );
		LastTrade = StaticVarGetText(VarPfx + "lastTrade"); 
		_TRACE("# buy, positions = " + NumToStr(numPositions, 1.0) + ", last trade = " + LastTrade + ", OID = " + OrderID);

		if(  ( LastTrade == "Buy" AND numPositions  > oldNumPositions ) 	OR 
			 ( LastTrade == "Sell" AND numPositions  < oldNumPositions ) 	OR 
			 ( LastTrade == "Close" AND numPositions  == 0) 				    )
		{
			fSayOnce("Filled");
			OrderID = StaticVarSetText(VarPfx + OrderID, "");
		}
		else if (OrderStatus == "Cancelled")
			OrderID = StaticVarSetText(VarPfx + OrderID, "");
			
		if( BuyOrder )
	   {
			fSayOnce("buy");
 	      	OrderID = ibc.PlaceOrder( Name(), "Buy", Contracts, typeOrder, 0, LastValue(C), "GTC", True);
 	      	StaticVarSetText(VarPfx + "OrderID", OrderID);
			StaticVarSetText(VarPfx + "lastTrade", "Buy"); 
			_TRACE("# buy, positions = " + NumToStr(numPositions, 1.0));
	   }

		if( SellOrder )
		{
			fSayOnce("sell");
			OrderID = ibc.PlaceOrder( Name(), "Sell", Contracts, typeOrder , 0, LastValue(C), "GTC", True);
			StaticVarSetText(VarPfx + "OrderID", OrderID);
			StaticVarSetText(VarPfx + "lastTrade", "Sell"); 
			_TRACE("# sell, positions = " + NumToStr(numPositions, 1.0));
		}
		else if( CloseAll  )
		{
			fSayOnce("close all");
			ibc.CancelAllPendingOrders( );
			ibc.CloseAllOpenPositions();
			StaticVarSetText(VarPfx + "lastTrade", "Close"); 
			_TRACE("# close, positions = " + NumToStr(numPositions, 1.0));
		}
		else if( CancelAll )
		{
			fSayOnce("cancel all");
			ibc.CancelAllPendingOrders( );
			_TRACE("# cancel, ");
		}
		LastTWSMsg = ibc.getLastError( 0 );

	// the following will display in the interprettion window
		printf("Order type: " + LastTrade +
		"\nOrder Status: " + OrderStatus + 
		"\nOrder ID: " + StaticVarGetText(VarPfx + "OrderID") + 
		"\nNum positions: " + NumToStr(ibc.GetPositionSize( Name() ),1.0,False) +
		"\nLast TWS Msg: " + LastTWSMsg );
	}
	else // ConnectedStatus == 0 OR ConnectedStatus == 1, lost connection
	{
		// handle commection errors 
		if(ConnectedStatus == 0) stat = "Not Connected."; else if(ConnectedStatus == 1) stat = "Lost Connection.";
		SetChartBkColor( colorYellow);		
	// the following will display in the interprettion window
		printf("\nTWS Status: " + stat + "\n"); 
	}
} // end auto trading loop
else
{
	SetChartBkColor( colorTan);
	// the following will display in the interprettion window
	printf("\n1. Autotrading is turned off\n" +
	"2. TWS not started." );
}

_SECTION_END();

2 comments

1. sudhir12192

IF IRUN TRADE THEN IT IS GIVING ERROR AS NOT INITIALISED
ibc = GetTradingInterface(“IB”);
IN THE (“IB”) BEFORE TYPING I
PL RECTIFY

2. administrator

Are you using interactive brokers sudhir12192? Also read this because you need to have the interactive brokers interface installed https://www.amibroker.com/guide/afl/afl_view.php?id=276

Leave Comment

Please login here to leave a comment.

Back