What am I doing wrong here?

IBPlugin is further development of TWSLink.It is focused for the useage with MetaTrader. But as it is a DLL, you can use it everywhere on Windows which supports DLL, e.g. in Trade Station or C++ applications or make wrapper for Perl, Python etc.

Users who just registered, please write an email to
forum@trade-commander.de
with your username mentioned.
So we can distinguish you from bots.
Thank you
Post Reply
chancee90046
Posts: 5
Joined: Fri Oct 22, 2021 3:35 am

What am I doing wrong here?

Post by chancee90046 »

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:

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

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: What am I doing wrong here?

Post by board_admin »

Where does it crash ?
What TradeStation version. Is it 64 bit TS or 32 bit TS ?
Thank you

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: What am I doing wrong here?

Post by board_admin »

I checked out your code.
I am not so familar with EasyLanguage, but it seems that the CONNECT part has called with each tick,
which should not be a problem but causes unnecessary load.

Just call Connect and the conid mapping once. The Plugin will keep connection.
Find attached corrected script.

Make sure, the symbol ES is mapped to a valid not expired contract.
Since you did not INIT with a specific configuration, use the default database in Admin Tool for mapping.
Attachments
ts_signal.zip
(2.2 KiB) Downloaded 648 times

chancee90046
Posts: 5
Joined: Fri Oct 22, 2021 3:35 am

Re: What am I doing wrong here?

Post by chancee90046 »

Thanks for responding.... so I'm using Tradestation 10. I used your corrected code but it's still doing the same thing. As soon as I run the strategy I see an order flash in the API tab in TWS for just a millisecond and then it disappears. It keeps doing that about every few seconds. During which the cursor is just spinning over tradestation and I can't click on anything. It's frozen. I have to basically just shutdown computer and restart.

chancee90046
Posts: 5
Joined: Fri Oct 22, 2021 3:35 am

Re: What am I doing wrong here?

Post by chancee90046 »

So I've been experimenting around with it and I've found that when I change the symbol to EURUSD, with the CONID you use in the demo, the orders go through, but they fire off repeated orders every second. Within a minute I had about thirty buy orders filled. Obviously it's a Demo account so no big deal.... but your IBPlugin Demo strategy with Eurusd works properly.... to all I can think is something is wrong with the code or symbol, not sure? Any help will be appreciated. Thanks.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: What am I doing wrong here?

Post by board_admin »

Hello Sir,
when you call a Plugin function, it is executed once.
So, if a function is fired multiple times, check your script code.

When the EUR.USD symbol works, but yours not, then your symbol is not mapped.
In this case, use the Admin Panel to map the symbol for your configuration (which is most likely "default").

Or get the conid of your ES contract and use this directly.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: What am I doing wrong here?

Post by board_admin »

chancee90046 wrote:
Tue Oct 26, 2021 7:50 pm
So I've been experimenting around with it and I've found that when I change the symbol to EURUSD, with the CONID you use in the demo, the orders go through, but they fire off repeated orders every second. Within a minute I had about thirty buy orders filled. Obviously it's a Demo account so no big deal.... but your IBPlugin Demo strategy with Eurusd works properly.... to all I can think is something is wrong with the code or symbol, not sure? Any help will be appreciated. Thanks.
The example of IBPlugin executes all once, because it is within the "Initialized " condition.
Your code is outside the "Initialized" condition and get's executed which each tick.

So the fault is not with IBPlugin or the sample, it is your code.

This forum does not discusses EasyLanguage or any Script Language, just the IBPlugin functions.
You need of course to be familar with the script language in use.

Post Reply