Workaround: The Event Handling is used in so called passive Eventhandling. The other options is the active event handling, you fetch
the events ocassionally or frequently. Watch the sample please:
Code: Select all
//+------------------------------------------------------------------+
//| twslink2demo.mq5 |
//| Copyright 2012, trade-commander.org |
//| http://www.trade-commander.com |
//+------------------------------------------------------------------+
// function U2A is used to convert unicode strings as used in MT5 to ansi string used in TWSLink
#property copyright "Copyright 2012, trade-commander.org"
#property link "http://www.trade-commander.com"
#property version "1.00"
#include <twslink2.mqh>
int uidMSFT = 0; // unique id for MSFT contract
int uidOrderMSFT = 0; // unique id for MSFT order
//-----------------------------------
//--type of callback function
//-----------------------------------
typedef void (*twsl_fp)(
int ,
int ,
int ,
int ,
double ,
double ,
double ,
double ,
string ,
string ,
string ,
string ,
double ,
double ,
int ,
int );
void ibEventHandler(
int integer1, // carries always main category of event
int integer2, // can have specific meaning, in particular when integer1=2
int integer3, // carries any value
int integer4, // carries any value
double double1, // carries any value
double double2, // carries any value
double double3, // carries any value
double double4, // carries any value
string string1, // carries any value
string string2, // carries any value
string string3, // carries any value
string string4, // carries any value
double double5, // carries any value
double double6, // carries any value
int integer5, // carries any value
int integer6 // carries any value
){
PrintFormat("-%d -%d -%d -%d -%.8f -%.8f -%.8f -%.8f -%s -%s -%s -%s -%.8f -%.8f -%d -%d"
,integer1,integer2,integer3,integer4
,double1,double2,double3,double4
,string1,string2,string3,string4
,double5,double6
,integer5,integer6);
return;
}
twsl_fp cb=ibEventHandler;
void fetch_twslink_events(int timeout_milliseconds=3)
{
int uidev=0;
while( (uidev=WAIT_FOR_EVENT(timeout_milliseconds)) > 0)
{
ibEventHandler(GET_EVENT_VAL_I(uidev,1)
,GET_EVENT_VAL_I(uidev,2)
,GET_EVENT_VAL_I(uidev,3)
,GET_EVENT_VAL_I(uidev,4)
,GET_EVENT_VAL_D(uidev,5)
,GET_EVENT_VAL_D(uidev,6)
,GET_EVENT_VAL_D(uidev,7)
,GET_EVENT_VAL_D(uidev,8)
,GET_EVENT_VAL(uidev,9)
,GET_EVENT_VAL(uidev,10)
,GET_EVENT_VAL(uidev,11)
,GET_EVENT_VAL(uidev,12)
,GET_EVENT_VAL_D(uidev,13)
,GET_EVENT_VAL_D(uidev,14)
,GET_EVENT_VAL_I(uidev,15)
,GET_EVENT_VAL_I(uidev,16));
}
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// connect to TWS / Gateway at standard port. make sure TWS or Gateway are setup to operate with API clients such as TWSLink2 (http://www.youtube.com/watch?v=53tmypRq5wI)
CONNECT(U2A("127.0.0.1"),7497,1,1000);
// register MSFT
uidMSFT=REGISTER_CONTRACT(U2A("MSFT"),U2A("STK"), U2A("USD"), U2A("SMART"), U2A(""), U2A(""),U2A(""), 0.0, U2A(""),0,0.0);
// place buy 100 shares at market for MSFT
uidOrderMSFT=PLACE_ORDER(uidMSFT,0, U2A("BUY"), U2A("MKT"),100,0.0,0.0,U2A("GTC"), 1,0);
/*
void* cbl=(void*) cb;
int ret=SET_EVENT_HANDLER((long) cb,0,0);
*/
// set a timer called each 10 milliseconds
bool bret=EventSetMillisecondTimer(10);
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
fetch_twslink_events();
}
//+------------------------------------------------------------------+
void OnTimer()
{
fetch_twslink_events();
}