Requesting a simple Easy Language/Strategy example using IBPlugin

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

Requesting a simple Easy Language/Strategy example using IBPlugin

Post by chancee90046 »

I am fluent in Easy Language but I cannot get the IBPlugin code to work in a strategy in any way, shape or form. I am requesting for anyone to post a strategy in Easy Language that uses the IBPlugin and performs successfully. I would like to see the exact implementation of how the IBPlugin is arranged within the easy language code - primarily the buy and sell statements.

You may block out proprietary components of your code if you wish. I'm just trying to get an idea of how to make this work.

I have installed the IBPlugin Demo strategy and it works. But I cannot figure out how to use the code when actual Buy and Sell orders are used. Any help would be much appreciated. Thanks, James

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

Re: Requesting a simple Easy Language/Strategy example using IBPlugin

Post by board_admin »

The sample that is installed along with IBPlugin and be tested on MultiCharts should run
perfectly without any problems also on TS.
This sample is discussed in this video:

https://youtu.be/lH3GE_pDm48?list=PLYD3 ... dcECuxa954


The example demonstrates the most important features.
Please mind: Other than in your code, all code is within the "initialized" condition.
So it is exectued once.

You wondered about the code multiple times executed but you definitely made the mistake to place
it outside the initialized flag. Then of course, it is executed with each call of the script and this is a matter
of EasyLanguage and not of IBPlugin.

Find attached the plugin sample and a small script just placing a MKT order ONCE each time you attach
the script to the chart.

Unfortunately, when i sent you a small code sample, i did the same mistake and leave some code placing orders
outside the condition flag.

So the new small example has corrected this. Please check out.
Attachments
ts_signal.zip
(2.2 KiB) Downloaded 4 times
ibplugin_demo.zip
(3.42 KiB) Downloaded 5 times

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

Re: Requesting a simple Easy Language/Strategy example using IBPlugin

Post by chancee90046 »

First of all, there is no BUY statement in your small ts_signal strategy. This is what you have:

Code: Select all

inputs:  
	Length( 9 ); 
	
	
variables:  
	Avg( 0 ) ;
	
Avg = AverageFC( Close, Length ) ;

if Initialized = 0  then begin
	
	once clearprintlog;	 
	

etc., etc., etc.,


If I attach that strategy/script to a chart it will just buy a market order the second I run it and do nothing else. But why would I want it to do that? I want it to buy when this is true:

Code: Select all

If close < Avg then begin
Or any IF/THEN statement for that matter.... that is what a strategy is. When I move that line of code below the:

Code: Select all

if Initialized = 0 then begin
then nothing happens. When I move it above it the strategy will immediately send a market order and then do nothing else.

I will post the entire version of the code I'm experimenting below again. And if you could please tell me why it's not working. I have even simplified it to do only one thing. If the close is below the AVG, then send a sell market order. Right now it does nothing.

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(12087792),     		{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

If close < Avg 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("EURUSD");
	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);
	
	
	
	
		
	

// place MKT SELL Order	
	Print("--- place MKT order");
	order_attribute_str="#1=150000#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;

		
Initialized=1;
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	
	

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

Re: Requesting a simple Easy Language/Strategy example using IBPlugin

Post by board_admin »

First of all, there is no BUY statement...
:
There is, please look a bit closer: order_attribute_str="#1=1#53=BUY#6=MKT#49=";
The order is defined by an attribute string. The possible attributes are listed in the help.
If I attach that strategy/script to a chart it will just buy a market order
Yes. This is what it should do. The intialized flag is intended to execute the BUY MKT order just once. When this code is called, initialized is set to 1 and so the code within
condition is not executed again.

I remember that you first copied the code, changed it and placed your orders outside the "initialized" condition and you wondered why
it placed so many orders. Well, this condition in the sample is intended to avoid exactly such situations.

if Initialized = 0 then begin

If close < Avg then begin
When the script is called first time, it executes the code below Initialized. If the average condition is given, you are lucky, if not,
nothing will happen, because this code section never called again, because initialized is now 1.

When you move this code outside the initialize section, the code is executed each time close < Avg. And i guess, this can be the case very often.
And so no wonder all code below this condition is executed multiple times.


But i don't want to discuss EL code logic here. Please have understanding for this. It is focused on IBPlugin functions.

Again: The sample script places all IBPlugin functions within the initialized condition to execute them only once for demonstrating purpose.
And this is exactly what happens.


I suggest: Outcomment all IBPlugin function calls. Make console Printouts instead. Run the script. Once you think the console printouts occur like
intended, enable IBPlugin function calls and all should be fine.

IBPlugin code that just need to be called once, keep it inside initialization flag. This applies at least for connect the symbol mapping function.

Post Reply