What am I doing wrong here?
Posted: Mon Oct 25, 2021 11:56 pm
Hello, I'm trying to get the IBPlugin working with just a basic strategy I'm testing it out with in Tradestation but I'm obviously doing something wrong. I can tell an order is being sent but it seems to be instantly cancelled and then Tradestation crashes and I have to restart. It seems to be getting caught in some kind of loop.
I have copied most of the code from the demo example and replaced some of my own code to test out a market BUY and SELL. Any help here would be much appreciated. I would classify myself as a beginner to intermediate in easy language. Thank you -James
Here is the code:
I have copied most of the code from the demo example and replaced some of my own code to test out a market BUY and SELL. Any help here would be much appreciated. I would classify myself as a beginner to intermediate in easy language. Thank you -James
Here is the code:
Code: Select all
external: "ibplugina.DLL", int, "ibgTS_ON_CREATE", IEasyLanguageObject;
external: "ibplugina.DLL", int, "ibgTS_ON_DESTROY", IEasyLanguageObject;
DefineDLLFunc: "ibplugina.DLL",int, "ibgCONNECT",
int,{connid}
LPSTR,{ip}
int,{port}
int,{clientid}
int,{nbr_channels}
int;{timeout}
DefineDLLFunc: "ibplugina.DLL",int, "ibgNAME_TO_UID",
LPSTR;{symbol}
DefineDLLFunc: "ibplugina.DLL",int64, "ibgORDER_SEND_STR",
int,{connid}
int,{uidc}
int64,{uid}
LPSTR,{order_attrib_list}
int;{id_allocation}
DefineDLLFunc: "ibplugina.DLL",int, "ibgORDER_DELETE",
int,{connid}
int64,{uid}
int;{sync}
DefineDLLFunc: "ibplugina.DLL",int64, "ibgORDER_CLOSE",
int,{connid}
int64,{uid}
int;{sync}
DefineDLLFunc: "ibplugina.DLL",LPSTR, "ibgORDER_ATTRIBUTE",
int,{connid}
int64,{uid}
int;{idattrib}
DefineDLLFunc: "ibplugina.DLL",int, "ibgCREATE_PENDING_ORDER_SNAPSHOT",
int,{connid}
int,{conid}
int;{istrict}
DefineDLLFunc: "ibplugina.DLL",double, "ibgORDER_INFO_DBL",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",int, "ibgORDER_INFO_INT",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",int64, "ibgORDER_INFO_INT64",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",LPSTR, "ibgORDER_INFO_STR",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",int, "ibgORDER_STATUS",
int64;{uid}
DefineDLLFunc: "ibplugina.DLL",int, "ibgWAIT_SUBMITTED",
int64,{uid}
int;{timeout}
DefineDLLFunc: "ibplugina.DLL",int, "ibgWAIT_FILLED",
int64,{uid}
int;{timeout}
DefineDLLFunc: "ibplugina.DLL",int, "ibgPOSITION_CLOSE",
int,{connid}
int,{uidc}
LPSTR,{account_list}
int;{sync}
DefineDLLFunc: "ibplugina.DLL",double, "ibgACCOUNT_INFO",
int,{connid}
int,{idinfo}
LPSTR;{account_list}
DefineDLLFunc: "ibplugina.DLL",LPSTR, "ibgACCOUNT_INFO_STR",
int,{connid}
int,{idinfo}
LPSTR;{account_list}
DefineDLLFunc: "ibplugina.DLL",int, "ibgCREATE_POSITION_SNAPSHOT",
int;{connid}
DefineDLLFunc: "ibplugina.DLL",double, "ibgPOSITION_INFO_DBL",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",int, "ibgPOSITION_INFO_INT",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",int64, "ibgPOSITION_INFO_INT64",
int,{idx}
int;{tag}
DefineDLLFunc: "ibplugina.DLL",LPSTR, "ibgPOSITION_INFO_STR",
int,{idx}
int;{tag}
Variables:
int conid(446091461), {IBKR unique id for EUR.USD}
int port(7496), {API port of TWS or Gateway. Make sure API is on. Active X enabled, port same as here..}
int connection_id(1), {ID of connection}
string order_attribute_str(""), {list of attrib=value pairs as order attributes}
string strategy_name("my_strat1"), {Name of this strategy}
int total_objects(0), {Total number of non ZERO positions or pending orders at snapshot time}
double uid_order(0), {unique id of order. We need double here as it has 8 bytes. MC/TC has no 64 bit integer which is the native type of uid}
int uid_parent_order(0), {unique id of parent order}
string fingerprint(""), {fingerprint of position or order}
int order_status(0), {status of order in focus: PendingSubmit=5, PreSubmitted=7,Submitted=8,Filled=9,Cancelled=10,FilledPartially=11}
string order_type(""), {type of order in focus: MKT,LMT,STP}
double lmt_price(0.0), { limit price for an order we like to place}
int ret_int(0), {buffer for int return values}
double ret_double(0.0), {buffer for double returnvalues}
string ret_str(""), {buffer for string return values}
int idx(0), {Loop index}
int Initialized(0); {init flag flag}
inputs:
Length( 9 );
variables:
Avg( 0 ) ;
Avg = AverageFC( Close, Length ) ;
if Initialized = 0 then begin
once clearprintlog;
// Strategy name is taken as comment tag in order reference (to have a hint where this order comes from)
Print("ES IBPlugin");
// if you mapped the symbol of this chart to an IBKR contract using the IBPlugin admin tool, you could call
// the mapping function in order to obtain IBKR contract id.
conid=ibgNAME_TO_UID("ES");
Print("conid=",conid);
// --------------------------------------------
// connect to TWS / Gateway at standard port. make sure TWS or Gateway are setup to operate with API clients such as ibplugin.
// Video API client setup: http://www.youtube.com/watch?v=53tmypRq5wI
// The client id is set to auto.It is created by hash value of configuration.
// The conifguration name for TS and MC is always: TSTN_MUCT
// NOTE: call this function only ONCE. The IBPlugin will keep connection to TWS, even, when TWS restarts.
ret_int=ibgCONNECT(connection_id,"",port,-1,1,20000);
// conn state = 1: connected to TWS; =2: plugin connected to TWS and TWS connected to IBServer;3=all initial requests done
Print("connection status=", ret_int);
If Close crosses over Avg and marketposition = 0 then begin
// place MKT BUY Order
Print("--- place MKT order");
order_attribute_str="#1=1#53=BUY#6=MKT#49=";
// wwrite strategy name into comment of order
order_attribute_str += strategy_name;
uid_order=ibgORDER_SEND_STR(connection_id,conid,0, order_attribute_str,0);
Print("uid MKT=", uid_order);
if uid_order > 0 then
begin
// wait order for been filled
// out: 1=order filled
// 0=timeout
// -1=Order can't be filled (e.g. invalid for what reason ever)
ret_int=ibgWAIT_FILLED(uid_order,5000);
end;
end;
If barssinceentry =2 and marketposition = 1 then begin
// place MKT SELL Order
Print("--- place MKT order");
order_attribute_str="#1=1#53=SELL#6=MKT#49=";
// wwrite strategy name into comment of order
order_attribute_str += strategy_name;
uid_order=ibgORDER_SEND_STR(connection_id,conid,0, order_attribute_str,0);
Print("uid MKT=", uid_order);
if uid_order > 0 then
begin
// wait order for been filled
// out: 1=order filled
// 0=timeout
// -1=Order can't be filled (e.g. invalid for what reason ever)
ret_int=ibgWAIT_FILLED(uid_order,5000);
end;
end;
#Events
// create a configuration of name TSTN_MUCT
// The configuration files - so also the symbol mapping database: contract.db
// can be found in folder:
// c:\trade-commander.com\ibplugin\configurations\TSTN_MUCT
OnCreate = ibgTS_ON_CREATE;
OnDestroy = ibgTS_ON_DESTROY;
#end
end