Please help with basics of IBC PlaceOrder


#1

Hi team,

I have been trading manually (thru IB) and I do need the auto trading to improve discipline. I have read the AT section in Amibroker but I can’t even send the order to IB thru the IBC. Please help me. Many thanks. Below please see sample of my simple strategy to trade Hong Kong listed futures “HHI”.
I have basic PC skills so please forgive me for my ignorance below… I should have studied computer science…

Q1. May I know what does the bracket { } mean for? Are they essential for Buy, Sell orders?

Q2. I trade Hong Kong HHI futures only. The Symbol used in Amibroker is “HHIM17-HKFE-FUT-HKD”. Is this supposed to be the code used in the below ibc Placeorder condition? or “HHIM17” is good enough?

Q3. I would like to place stoploss(mkt) order right after the initial trade is executed. Stop loss limit will be 0.6% of the executed price. May I know how to input it into the ibc Placeorder condition?

//place mkt order when there is Buy Signal
parentID = ibc.PlaceOrder("HHIM17","BUY",1,"MKT",0,0,"DAY",False);
//stop loss order 0.6% stop loss limit
ibc.PlaceOrder("HHIM17", "SELL", 1, "LMT","STP",[???what shall I input here],0,"DAY", False,1,"", parentID );

Q4. Does string action have “Buy”, “Sell”, “SShort” only? For my strategy below, will IBC execute Buy trade for Buy and Cover and Sell trade for Sell and Short trade?

Is there anything in the library which I can refer to to learn teh basics? Many thanks
SK

SetPositionSize(1,spsShares);
RSIO=Optimize("RSIO",45,15,51,3);
BBWidthNum=Optimize("BBWidthNum",20,10,30,3);

BBTop = BBandTop (Close,RSIO,2);	//Upper Bollinger Band 
BBBottom = BBandBot (Close,RSIO,2);	//Lower Bollinger  Band 
BBWidth = BBTop-BBBottom;  // Bollinger Band Width My Calculation

Firsttrade=133000;
Lasttrade=155800;
ForceExitTime=162500;

Buy = (Ref( Low , -1 ) < Ref( BBandBot( Close, RSIO, 2 ) , -1 )) AND BBWidth>=BBWidthNum AND TimeNum()<Lasttrade AND TimeNum()>Firsttrade;

Sell = High > BBandTop( Close, RSIO, 2 ) OR TimeNum()>Forceexittime;

Short = Ref( High , -1 ) > Ref( BBandTop( Close, RSIO, 2 ) , -1 ) AND BBWidth>=BBWidthNum AND TimeNum()<Lasttrade AND TimeNum()>Firsttrade;

Cover = Low < BBandBot( Close, RSIO, 2 ) OR TimeNum()>Forceexittime;

{

//Long, place order
ibc = GetTradingInterface("IB");
if(ibc.IsConnected())// check if connection to IB was successful
{
//place mkt order when there is Buy Signal
parentID = ibc.PlaceOrder("HHIM17","BUY",1,"MKT",0,0,"DAY",False);
//stop loss order 0.6% stop loss limit
ibc.PlaceOrder("HHIM17", "SELL", 1, "LMT","STP",0[executed price*0.994.. asking helpdesk...],0,"DAY", False,1,"", parentID );

}
//short, place order
ibc = GetTradingInterface("IB");
if(ibc.IsConnected())// check if connection to IB was successful

parentID = ibc.PlaceOrder("HHIM17", "SELL", 1, "MKT", 0, 0, "DAY", False );//place mkt order when there is Short Signal 
ibc.PlaceOrder("HHIM17", "BUY", 1, "STP",0[executed price*1.006.. asking helpdesk...], 0, "DAY", False, 1, "", parentID );//stop loss order 0.6% stop loss limit

}

#2

If you are at this level when you are asking what { } brackets mean I strongly suggest NOT to use your own code for automated trading. This question shows that there is plenty of knowledge to be absorbed prior to trying AT. Running incorrect AT code may and eventually will lead to financial loss.

Brackets delimit code blocks. Code block is used as so called compound statement and is generally used in conjunction with if-else, for, while, do-while and such control flow statements.

You need to use full symbol. Or just Name() function call if you trade current symbol

Again, this shows that you are NOT ready for auto-trading. PlaceOrder syntax is presented in
http://www.amibroker.com/at/ and it goes like this:

PlaceOrder( string Ticker, string Action, number Quantity, string Type, 
number LimitPrice, number StopPrice,
string TimeInForce /* that is your 7th argument */, 
bool Transmit, [optional] number TickSize = 100, 
[optional] string Attributes = "", [optional] string ParentID = "", 
[optional] string OCAGroup, [optional] number OCAType, 
[optional] string FAParams, [optional] string Account)

It is explained in the docs what you should enter in place of 7th argument: TimeInForce. The help includes all examples and precise explanation what “TimeInForce” field should contain. If you can’t follow these instructions automated trading is not for you because you will lose money due to coding mistakes.

Yes, it is as per Interactive Brokers documentation:
http://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#gsc.tab=0

Quote:

Action - Identifies the side. Generally available values are BUY, SELL. Additionally, SSHORT, SLONG are available in some institutional-accounts only. For general account types, a SELL order will be able to enter a short position automatically if the order quantity is larger than your current long position. SSHORT is only supported for institutional account configured with Long/Short account segments or clearing with a separate account. SLONG is available in specially-configured institutional accounts to indicate that long position not yet delivered is being sold.

So if you want to cover short position you just issue “BUY”. And if you want to enter Short position you just do “SELL” (if order quantity is larger than existing long position)

Recommended reading:
If you are interested in auto-trading first thing to do (besides reading the AT help) is to read Users KB posts on automated trading (scroll down to “Real-time applications”)


#3

Thanks Tomasz. Noted your advice. If I want to learn more about the afl programming, what language shall I study? C+?

I have been working in NYSE, JPM, PT72 equities. If by any chance you visit HK, please flag us and I am sure a number of your users would like to meet you.

Regards
SK


#4

Plain old “C” or JavaScript are most similar. JavaScript has similar concept of variable being able to hold any data type.