Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: Manual Bracket Order Trader
Author/Uploader: Barry Scarborough - razzbarry [at] imagerview.us
Date/Time added: 2008-11-16 11:30:28
Origin:
Keywords: bracket order barry trader
Level: semi-advanced
Flags: system

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

This formula allows testing bracket orders sent to TWS. This will help users see how bracket orders are handled by TWS and help users design their bracket orders for auto trading.

Formula:

_N(SectionName = "Barrys Manual Bracket Trader");
_N(Ver = "Ver1_0"); //  this is my version number and should not be changed 
_SECTION_BEGIN(SectionName);  
EnableTextOutput(False); // prevents incidental text from being displayed in
the interpretation
_N(Filename = StrLeft(_DEFAULT_NAME(),StrLen(_DEFAULT_NAME())-2) + " " + Ver);
// the name of the program and is displayed on the title line
// CAUTION: VarPfx is used to make the static variables unique tp prevent
bizarre results if you use multiple versions of the trading program
simultaneously. 
VarPfx = Filename + Ver;  

_N(Title = Filename + StrFormat(" - {{DATE}} "));

// _N(Title = Filename + StrFormat(" - {{DATE}} \nOpen %g, Hi %g, Lo %g, Close
%g Vol %g " + " {{VALUES}}", O, H, L, C, V ));

// Instructions:
// Set up a paper trading account at IneractiveBrokers - Dont use a real
account until you know your system works
// You can also use their Deom account but it is not as close to real life as a
paper trading account.
// Download the auto tading interface from AB,
http://www.amibroker.com/download.html#amibroker and put it in the AB folder on
your C or D drive
// Once you get the IB Controller window active by pressing AutoTrade you must
unlock it with AB, releasing them from any liability. 
// They will email you a code that you must put in the IB Controller window to
unlock it.
// You have to be signed onto your IB account and TWS has to be running.
// It is assumed that the symbol in the chart is th eone you are trading
because that is the symbol that will be traded.  
// You can track and manage your trades on TWS as usual but this interface
allows you to place a trade on the 

// parameters
AutoTrade		= ParamToggle("AutoTrade", "Off|Running");
tBuyA     		= ParamTrigger("Place Buy A order","Buy A");
tBuyB     		= ParamTrigger("Place Buy B order","Buy B");
tSellA			= ParamTrigger("Place Sell A order","Sell A");
tSellB			= ParamTrigger("Place Sell B order","Sell B");
tShort    		= ParamTrigger("Place Short order","Short");
tCoverA		= ParamTrigger("Place Cover A order","Cover A");
tCoverB		= ParamTrigger("Place Cover B order","Cover B");
tCancelOrder  = ParamTrigger("Cancel order","Cancel order");
tCancelBkt 	= ParamTrigger("Cancel bracket","Cancel bracket");
tCloseAll  	= ParamTrigger("Close all positions","Close all positions");
tCancelAll  	= ParamTrigger("Cancel all open orders","Cancel all open
orders");

StatusData = "";
tick = TPL = TPS = SLL = SLS = inb = 0; // reset vars
sTicker = Name();
if (sTicker == "NQZ8-GLOBEX-FUT") 	{sz = 1; tick = 0.25; tickvalue = 5.0;    
TPL = 10; TPS = 10; SLL = 20; SLS = 20; tn = 70;inb = 20; }
if (sTicker == "FDAX   DEC 08-DBT-FUT")  {sz = 1; tick = 0.50; tickvalue =
12.50; TPL = 16; TPS = 12; SLL = 28; SLS = 24; tn = 80;inb = 20 ;}

// process trades 
if(AutoTrade) 
{
	ibc = GetTradingInterface("IB");
	OrderID = StaticVarGetText(VarPfx + "OrderID");
	BracketA = StaticVarGetText(VarPfx + "BracketA");
   	BracketB = StaticVarGetText(VarPfx + "BracketB");
	OrderType = StaticVarGetText(VarPfx + "OrderType");
	OrderStatus = ibc.GetStatus( OrderID, True );
	numPositions  = ibc.GetPositionSize(sTicker); 
	lastC = LastValue(C);
	Result = StaticVarGet(VarPfx + "Result");


	if( tBuyA )
   {
		StpPrice 	= LastC - SLL;
		LmtPrice 	= LastC + TPL;
		orderID 	= ibc.PlaceOrder(sTicker, "BUY",  1, "MKT", 0, 0, "GTC", False, 10,
"outsideRTH"); // trade entry order
		BracketA	= ibc.PlaceOrder(sTicker, "SELL",  1, "STP", 0, StpPrice, "GTC",
False, 10, "", OrderID, "MVGROUP");
		BracketB	= ibc.PlaceOrder(sTicker, "SELL",  1, "LMT", LmtPrice, 0, "GTC",
True, 10, "", OrderID, "MVGROUP");  
       StaticVarSetText(VarPfx + "OrderID", OrderID);
       StaticVarSetText(VarPfx + "BracketA", BracketA);
       StaticVarSetText(VarPfx + "BracketB", BracketB);
   }

	if( tBuyB )
   {
		EntryPrice = LastValue(Ref(H, -1)) + tick;
		StpPrice 	= LastC - SLL;
		LmtPrice 	= LastC + TPL;
		orderID 	= ibc.PlaceOrder(sTicker, "BUY",  1,  "STP", 0, EntryPrice, "GTC",
False, 10, "outsideRTH");  // trade entry order
		BracketA	= ibc.PlaceOrder(sTicker, "SELL",  1, "STP", 0, StpPrice, "GTC",
False, 10, "", OrderID, "MVGROUP");  
		BracketB	= ibc.PlaceOrder(sTicker, "SELL",  1, "LMT", LmtPrice, 0, "GTC",
True, 10, "", OrderID, "MVGROUP");
       StaticVarSetText(VarPfx + "OrderID", OrderID);
       StaticVarSetText(VarPfx + "BracketA", BracketA);
       StaticVarSetText(VarPfx + "BracketB", BracketB);
   }

	if( tShort )
	{
       StaticVarSetText(VarPfx + "BracketA", "");
       StaticVarSetText(VarPfx + "BracketB", "");
		StpPrice = LastC + SLS;
		LmtPrice = LastC - TPS;
		orderID = ibc.PlaceOrder(sTicker, "SELL", 1, "MKT", 0, 0, "GTC", False, 10,
"outsideRTH");
		BracketA = ibc.PlaceOrder(sTicker, "BUY",  1, "STP", 0 , StpPrice , "GTC",
False, 10, "", OrderID, "MVGROUP");
		BracketB = ibc.PlaceOrder(sTicker, "BUY",  1, "LMT", LmtPrice, 0, "GTC",
True, 10, "", OrderID, "MVGROUP");
		StaticVarSetText(VarPfx + "OrderID", OrderID);
       StaticVarSetText(VarPfx + "BracketA", BracketA);
       StaticVarSetText(VarPfx + "BracketB", BracketB);
       StaticVarSetText(VarPfx + "OrderType","Short");
	}

	if( tSellA AND numPositions > 0)
   {
		ibc.CancelOrder(OrderID); // cancel the open bracket 
		ibc.CancelOrder(BracketA); // cancel the open bracket 
		ibc.CancelOrder(BracketB); // cancel the open bracket 
		orderID = ibc.PlaceOrder(sTicker, "SELL",  1, "MKT", 0, 0, "GTC", True, 10,
"outsideRTH");
       StaticVarSetText(VarPfx + "OrderID",OrderID);
       StaticVarSetText(VarPfx + "OrderType","Sell");
  	}

	if( tSellB AND numPositions > 0)
   {
		ibc.CancelOrder(OrderID); // cancel the open bracket 
		ibc.CancelOrder(BracketA); // cancel the open bracket 
		ibc.CancelOrder(BracketB); // cancel the open bracket 
		StpPrice = LastValue(Ref(Low, -1)) - inb;
		orderID = ibc.PlaceOrder(sTicker, "BUY",  1, "STP", 0, StpPrice, "GTC", True,
10, "outsideRTH");
       StaticVarSetText(VarPfx + "OrderID",OrderID);
       StaticVarSetText(VarPfx + "OrderType","Sell");
  	}

	if( tCoverA AND numPositions < 0)
	{
		ibc.CancelOrder(OrderID); // cancel the open bracket 
		ibc.CancelOrder(BracketA); // cancel the open bracket 
		ibc.CancelOrder(BracketB); // cancel the open bracket 
		OrderID = ibc.PlaceOrder(sTicker, "BUY", 1, "MKT", 0, 0, "GTC", True, 10,
"outsideRTH");
		StaticVarSetText(VarPfx + "OrderID",OrderID);
       StaticVarSetText(VarPfx + "OrderType","Cover");
	}

	if( tCoverB AND numPositions < 0)
	{
		ibc.CancelOrder(OrderID); // cancel the open bracket 
		ibc.CancelOrder(BracketA); // cancel the open bracket 
		ibc.CancelOrder(BracketB); // cancel the open bracket 
		StpPrice = LastValue(Ref(High, -1)) + inb;
		orderID = ibc.PlaceOrder(sTicker, "BUY",  1, "STP", 0, StpPrice, "GTC", True,
10, "outsideRTH");
		StaticVarSetText(VarPfx + "OrderID",OrderID);
       StaticVarSetText(VarPfx + "OrderType","Cover");
	}

	if( tCancelOrder )
   {
       Result = ibc.CancelOrder(OrderID);
 		StaticVarSet(VarPfx + "Result", Result);
       StaticVarSetText(VarPfx + "OrderID", "");
        StaticVarSetText(VarPfx + "OrderType","None");
   }

	if( tCancelBkt )
   {
       Result = ibc.CancelOrder(BracketA);
  		StaticVarSet(VarPfx + "Result", Result);
       StaticVarSetText(VarPfx + "BracketA", "");
       StaticVarSetText(VarPfx + "OrderType","None");
   }

	if( tCloseAll )
	{
		ibc.CancelAllPendingOrders( );
		ibc.CloseAllOpenPositions();
       StaticVarSetText(VarPfx + "OrderType","CloseAll");
	}

	if( tCancelAll )
	{
		ibc.CancelAllPendingOrders( );
       StaticVarSetText(VarPfx + "OrderType","CancelAll");
	}
	errMsg = ibc.getLastError( 0 );	
	OrderID = StaticVarGetText(VarPfx + "OrderID");
	OrderType = StaticVarGetText(VarPfx + "OrderType");


	StatusData = "\n" +
	"Error Message: " + errMsg + "\n" +
	"Order ID: " + OrderID + "\n" + "Order Type: " + OrderType + "\n" + 
	"Bracket A: " + BracketA + "\n" + 
	"Bracket B: " + BracketB + "\n" + 
	"Order Status: " + OrderStatus + "\n" +
	"Positions: " + NumToStr(numPositions, 1.0, False) + "\n" +
	"Cancel result: " + NumToStr(result, 1.0);
}
else
	StatusData = "\nPress parameter AutoTrade to connect to TWS";
printf(StatusData);	
Title = Title + StatusData; 

_SECTION_END();

Comments:


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM