PowerLanguage, EasyLanguage Beispiel über das Plazieren einer Vwap Order für ein InteractiveBrokers Konto mit IBPlugin

//-------------------------------------------------------------------------
// This signal demonstrates the use of IBPlugin to trade an IBKR account
// from within MultiCharts or TradeStation.
// The signal just connects to TWS/Gateway (watch port,IP)
// places a Vwap orders, waits 5 seconds and cancels this order
//-------------------------------------------------------------------------
// --- declare dll functions ---
external: "ibplugin.dll", int, "mtibTS_ON_CREATE", IEasyLanguageObject;

external: "ibplugin.dll", int, "mtibTS_ON_DESTROY", IEasyLanguageObject;


DefineDLLFunc: "ibplugin.DLL",int, "mtibINIT_SCRIPT_A",
                  LPSTR,{metatrader_identifier}
                  LPSTR,{expert_name}
                  int;{loglevel}


DefineDLLFunc: "ibplugin.DLL",int, "mtibDEINIT_SCRIPT";

DefineDLLFunc: "ibplugin.DLL",int, "mtibCONNECT_A",
                  int,{connid}
                  LPSTR,{ip}
                  int,{port}
                  int,{clientid}
                  int,{nbr_channels}
                  int;{timeout}
                  
DefineDLLFunc: "ibplugin.DLL",int, "mtibCONNECTION_STATUS",
                  int;{connid}
                  
                  
DefineDLLFunc: "ibplugin.DLL",int64, "mtibORDER_SEND_STR_A",
                  int,{connid}
                  int,{uidc}
                  int64,{uid}
                  LPSTR,{order_attrib_list}
                  int,{id_allocation}
                  int;{sync}

DefineDLLFunc: "ibplugin.DLL",int, "mtibORDER_DELETE",
                  int,{connid}
                  int64,{uid}
                  int;{sync}

DefineDLLFunc: "ibplugin.DLL",int, "mtibSLEEP",
                  int;{milliseconds}
                                    
Variables:
     conid_intel(270639),         {IBKR unique id for intel}
     port(7497),                 {API port of TWS or Gateway. Make sure API is on. Active X enabled, port same as here..}
     connection_id(1),             {ID of connection}
     order_attribute_str(""),    {list of attrib=value pairs as order attributes}
     algo_param_str(""),        {list of attrib=value pairs as algo param (Vwap is algo order)}
     uid_order(0),            {unique id of order}

     int_ret(0),                {int return value}
     Initialized(0);            {init flag flag}

{ DLL initialisation}

    {init the DLL }
if Initialized = 0  then begin

    //int_ret=mtibINIT_SCRIPT_A("multicharts","ibplugin_demo",3);
    Print("init=",int_ret);
    
    {connect to TWS / Gateway at standard port. make sure TWS or Gateway are setup to operate with API clients such as ibplugin  (http://www.youtube.com/watch?v=53tmypRq5wI)}
    mtibCONNECT_A(connection_id,"",port,1345,1,3000);
    
    // algo param pair sep char "|"    
    // algo param value assign char "~"
    // So an algo argument is defined by a string concatenation of id1~value1|id2~value2|id3~value3... and so on;how many attributes your algo requires


    // create the Vwap algo param string. The challenge: User need to figure out what parameters required for his algo order (https://interactivebrokers.github.io/tws-api/algos.html)
    algo_param_str="noTakeLiq~1"; // human say: noTakeLiq=1 (the ~ is used as assginment charactar within algo param definition)
    algo_param_str+="|speedUp~1";
    algo_param_str+="|allowPastEndTime~1";

    Print(    algo_param_str);    
    // order attrib pair sep char "#"
    // order attrib value assign char "="
    // So an order is defined by a string concatenation of id1=value1#id2=value2#id3=value3... and so on;how many attributes your order requires
    
    // create order attributes. The <algo_param_str> is such an attribute
    // the algo name (the 69 is the order attribute id.     
    order_attribute_str = "69=Vwap"; // algo name    
    // attribute separator and algo param id
    order_attribute_str += "#70=";
    order_attribute_str += algo_param_str;

    // action attribute    
    order_attribute_str += "#53=BUY";

    // type attribute    
    order_attribute_str += "#6=LMT";

    // exchange attribute    
    order_attribute_str += "#1007=SMART";

    // limit price attribute
    order_attribute_str += "#2=44.84";

    // outside RTH atribute (only false)
    order_attribute_str += "#14=0";

    // time in force attribute (only day accepted)
    order_attribute_str += "#5=DAY";

    // ordersize
    order_attribute_str += "#1=100";
    
    Print("connection status",mtibCONNECTION_STATUS(connection_id));
    
    Print(    order_attribute_str);
    { Finally place order }
    uid_order=mtibORDER_SEND_STR_A(connection_id,conid_intel, 0,     order_attribute_str,0,0);
    Print("uid=",    uid_order);

    // wait 5 seconds
    mtibSLEEP(5000);
    
    // cancel (delete) order async
    int_ret=mtibORDER_DELETE(connection_id,uid_order,0);
    Print("cancel ret=",    int_ret);
    
    Initialized=1;    
end;

#Events
    OnCreate =     mtibTS_ON_CREATE;
    OnDestroy = mtibTS_ON_DESTROY;
#end