2 registered members (AndrewAMD, rki),
390
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Broker API - Options Questions
#466972
07/11/17 02:11
07/11/17 02:11
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Hello, I'm writing options features into the Ally Invest plugin, and I have three questions: #1 - What is the correct procedure for writing the BrokerCommand GET_OPTIONS function? Am I supposed to build up an array of CONTRACT's and then copy it to the DWORD like so...
BrokerCommand (int nCommand, DWORD dwParameter)
{
// the below code is for nCommand=="GET_OPTIONS"
std::vector <CONTRACT> contracts;
// send request to server,
// get response from server
// parse data into contracts vector
// here's the part I'm not sure about:
dwParameter = (DWORD)contracts.data();
return contracts.size();
}
If not, please point me in the right direction. #2 - I see that I have two symbol definitions... One is in SET_SYMBOL, and one is char* Asset. If a user wants to buy/sell an option, would the option symbol be defined in SET_SYMBOL or in char* Asset? Will there be cases where I would have to use one and override the other? Is SET_SYMBOL strictly for usage with BrokerCommand? #3 - Will a user need the options symbols all individually in the AssetFix.csv file? I appreciate any clarifications. Thanks. Andrew EDIT - I did eventually fix the above snippet. I'll document it here.
BrokerCommand (int nCommand, DWORD dwParameter)
{
// the below code is for nCommand=="GET_OPTIONS"
//this next line is a pointer to an array of 10,000 elements.
CONTRACT* contracts_ptr = (CONTRACT*)dwParameter;
std::vector <CONTRACT> contracts;
// send request to server,
// get response from server
// parse data into contracts vector
// I copied vector elements to the pointer, not to exceed 10,000 elements.
int i;
for (i = 0; i < Contracts.size() && i < 10000; i++)
{
contracts_ptr[i] = Contracts[i];
}
return i;
}
Last edited by AndrewAMD; 07/15/17 12:51.
|
|
|
Re: Broker API - Options Questions
[Re: AndrewAMD]
#467006
07/12/17 03:17
07/12/17 03:17
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Question #4: Below is the CONTRACT definition:
typedef struct CONTRACT
{
DATE time; // or trading class
float fAsk, fBid; // premium without multiplicator
float fVal; // open interest or multiplier
float fVol;
float fUnl; // underlying price
float fStrike;
long Expiry; // YYYYMMDD
long Type; // PUT, CALL, FUTURE, EUROPEAN, BINARY - functions.h
} CONTRACT; // for options, futures, FOPs
4.a) DATE time: Is this supposed to be the expiration date? Also, what does "or trading class" mean, and how would it apply to options contracts? Is this just an IB thing? 4.b) float fVal: Am I supposed to use the SET_MULTIPLIER value or the actual multiplier for the asset?
Last edited by AndrewAMD; 07/12/17 23:30.
|
|
|
Re: Broker API - Options Questions
[Re: AndrewAMD]
#467051
07/13/17 16:56
07/13/17 16:56
|
Joined: Jul 2000
Posts: 27,982 Frankfurt
jcl
Chief Engineer
|
Chief Engineer
Joined: Jul 2000
Posts: 27,982
Frankfurt
|
The multiplier is asset specific, so you must set it after selecting the asset, like the other asset parameters. The time is a union. In historical data it's the quote time, in option chains it's the first 8 bytes of the trading class. Here's a section of the IB plugin code that loads the option chain into the CONTRACT structs:
void contractDetails(int id, const ContractDetails& CD )
{
if(id != g_Id || !g_Contracts) return;
Contract C = CD.summary;
memset(g_Contracts,0,sizeof(CONTRACT));
int Multiplier = atoi(C.multiplier.c_str());
if(g_Multiplier > 0
&& g_Multiplier != Multiplier) return;
if(*g_TradingClass
&& 0 != strcmpi(g_TradingClass,C.tradingClass.c_str())) return;
if(C.secType == *SecType::FOP) {
g_Contracts->Type = FUTURE | ((C.right == "C")? CALL : PUT);
g_Contracts->fStrike = C.strike;
} else if(C.secType == *SecType::OPT) {
g_Contracts->Type = (C.right == "C"? CALL : PUT);
g_Contracts->fStrike = C.strike;
} else if(C.secType == *SecType::FUT) {
g_Contracts->Type = FUTURE;
g_Contracts->fStrike = 0;
}
strcpy_s((char*)g_Contracts,8,C.tradingClass.c_str());
g_Contracts->Expiry = atoi(C.expiry.c_str());
g_Contracts->fVal = Multiplier;
g_Contracts++;
if(++g_nContracts >= NUM_CONTRACTS) {
g_End = TRUE;
g_Contracts = NULL;
}
}
|
|
|
Re: Broker API - Options Questions
[Re: jcl]
#467238
07/21/17 15:47
07/21/17 15:47
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Question #5:
What's the difference between GET_MARGININIT and GET_MARGINREQUIRED? Do either apply to options? to stocks?
Also, I understand that the margin required to sell an option to open is higher than to buy an option to open. Is there some way that I can/should differentiate between the two cases using these brokercommand functions? Does Zorro automatically calculate this?
Last edited by AndrewAMD; 07/21/17 15:49.
|
|
|
Re: Broker API - Options Questions
[Re: AndrewAMD]
#467324
07/26/17 11:23
07/26/17 11:23
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Question #6: This is more of an engineering question. I'm looking to place "combo" option orders, or "multi-leg" orders. Basically, you pay less commission for up to four simultaneous option orders. Below is an example of a two-leg order:
<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
<NewOrdMleg TmInForce="0" Px="-3.10" OrdTyp="2" Acct="12345678">
<Ord OrdQty="4" PosEfct="O">
<Leg Side="1" Strk="190" Mat="2014-01-18T00:00:00.000-05:00" MMY="201401" SecTyp="OPT" CFI="OC" Sym="IBM"/>
</Ord>
<Ord OrdQty="4" PosEfct="O">
<Leg Side="2" Strk="200" Mat="2014-01-18T00:00:00.000-05:00" MMY="201401" SecTyp="OPT" CFI="OC" Sym="IBM"/>
</Ord>
</NewOrdMleg>
</FIXML>
Here's an example of how I can currently perform a four-leg order from a zorro script:
brokerCommand(SET_COMBO_LEGS,4);
// buy leg 1 <-- plugin saves leg, BrokerBuy returns ID from counter
// buy leg 2 <-- plugin saves leg, BrokerBuy returns ID from counter
// buy leg 3 <-- plugin saves leg, BrokerBuy returns ID from counter
// buy leg 4 <-- plugin executes order, BrokerBuy returns ID from counter or 0 if failure
SET_COMBO_LEGS would be a new BrokerCommand that sets the number of legs in a trade. If it's 2, BC will return 1 and execute on the second trade. 3, return 1, third trade, etc. The problem with this approach is that I have three legs returning a successful trade, with the last one returning the actual status. If the last one returns a trade failure, then Zorro will think the first three trades were fine. Is there some way that I can delay the return of the first 1-3 legs until I receive all of the legs and get a response from the server?
Last edited by AndrewAMD; 07/26/17 11:33.
|
|
|
Re: Broker API - Options Questions
[Re: jcl]
#469689
12/02/17 16:34
12/02/17 16:34
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
For buying and selling an option, the symbol is passed in char* Asset, coded in the way as described on the "IB" page of the manual. jcl, Is there some way that my plugin can define a futures/options/futures-options contract symbol so that Zorro's use of char* Asset always matches that of my broker? I mean like this: * Zorro calls GET_OPTIONS or GET_FUTURES. * Plugin copies the CONTRACT structs and then return a **negative** number of contracts to indicate that the plugin has symbols to supply. Plugin saves the symbols. * Zorro calls a new function GET_OPTIONS_SYMBOLS or GET_FUTURES_SYMBOLS. Zorro provides a pointer to an array of strings, and the plugin copies the symbols to the array of strings. * The number of CONTRACT structs and symbols would therefore be identical. * Any BrokerAsset, BrokerHistory2, and BrokerBuy call using one of these contracts would therefore use the respective symbol for char* Asset. This would be especially helpful to me, since my SC plugin will be multi-broker.
|
|
|
Re: Broker API - Options Questions
[Re: jcl]
#469774
12/07/17 17:26
12/07/17 17:26
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
The symbols are fact already loaded, only it's an undocumented function because it's not used yet. The Symbol string is stored at the begin of any CONTRACT* struct in the Contracts list, instead of the date that is not needed in the options chain.
So, string CSymbol = Contracts + ContractRow should get the symbol of the current contract. Earlier in the thread, you gave me this line of code:
strcpy_s((char*)g_Contracts,8,C.tradingClass.c_str());
... and you said it was reserved for the trading class. Are you saying that this has been changed to the actual symbol? Back to the struct...
typedef struct CONTRACT
{
DATE time; // or trading class
float fAsk, fBid; // premium without multiplier (f1,f2)
float fVal; // open interest or multiplier (f3)
float fVol; // volume (f4)
float fUnl; // unadjusted underlying price (f5)
float fStrike; // (f6)
long Expiry; // YYYYMMDD (i7)
long Type; // PUT, CALL, FUTURE, EUROPEAN, BINARY (s8)
} CONTRACT; // for options, futures, FOPs
"time" only has room for 8 characters... I don't think that's enough room for an options / futures / FOP symbol. Did you mean that I can overwrite fAsk and fBid as well, giving me 16 characters?
|
|
|
Re: Broker API - Options Questions
[Re: jcl]
#469989
12/18/17 13:15
12/18/17 13:15
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Do you mean a code like "AAPL-OPT-150-20180303-USD"? That's not supplied by the plugin. It is an internal code used only by Zorro for sending strike, expiry and other parameters to the IB plugin. The API does not know this code. If broker APIs have similar codes in internal lists, they are then broker specific, and not used by Zorro.
Yes, that's it! Did you know that Zorro has been sending IB codes to my Ally plugin for options, and that my plugin had to parse the IB codes and translate them to Ally codes? (Look at my source. I distinguish between "Zorro Assets" and "Ally Assets".) This approach is not feasible for my new plugin, since it supports many brokers, and therefore many broker codes. I have a new idea: 1) Zorro calls GET_OPTIONS or GET_FUTURES. 2) Plugin fills CONTRACT structs. BUT... instead of filling in the exchange info for the time variable, the plugin sends a uint32_t value instead. This will act as an Asset ID number. Before: strcpy to CONTRACT.time the exchange name, eight bytes max. New approach: memcpy eight bytes to CONTRACT.time: "ID#\0xxxx", where "ID#\0" is a header, and "xxxx" is the uint32_t Asset ID number which the plugin has memorized. Plugin will be responsible for memorizing the Asset broker code associated with each Asset ID number. 3) Plugin returns number of structs. 4) NEW FEATURE: Zorro will see that the plugin has filled out the CONTRACT time value in a certain way (such as with a header) and instead of sending an IB code upon BrokerAsset, will send a string indicating the ID number, so that my plugin can retrieve the broker code. For example: "ID#123456789"; 5) My plugin parses this string and retrieves the ID number. Now it knows what broker code to use. Can this be implemented?
Last edited by AndrewAMD; 12/18/17 14:59. Reason: struct handling clarification
|
|
|
|