Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Integration of libzenfire in ninjascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Integration of libzenfire in ninjascript

    Is there any way to integrate some C# methods/function like that following (a market_data_detailed.cpp) from zenfire library so that i can pass the value collected from API to variables in ninjascript?


    +++++++++++++++++++++++++++++++++++++++++

    using System;
    public static class GlobalMembers
    {

    //################################################## ##########################//
    // F U N C T I O N S

    using zenfire.debug;
    //! read istream into a product_t

    public static std.istream operator >>(ref std.istream @is, ref zenfire.arg.product prod)
    {
    if([email protected]())
    return(@is);

    string s;
    std.getline(@is, s);

    // split on .
    size_t idx = s.IndexOfAny((System.Convert.ToString('.')).ToChar Array());
    prod.symbol = s.Substring(0, idx);
    prod.exchange = zenfire.exchange.from_string(s.Substring(idx+1));

    Console.Write("got ");
    Console.Write(prod.symbol);
    Console.Write(" on ");
    Console.Write(prod.exchange);
    Console.Write("\n");
    return @is;
    }

    //! format timestamp of a tick message

    public static string fmtts(zenfire.tick_t t)
    {
    size_t sz;
    string tsbuf = new string(new char[9]);
    std.stringstream str;
    tm ltm;


    #if LOCALTIME_R_AlternateDefinition1
    localtime_s((ltm), (t.ts));
    #elif LOCALTIME_R_AlternateDefinition2
    localtime_r((t.ts), (ltm));
    #elif LOCALTIME_R_AlternateDefinition3
    localtime_s((ltm), (t.ts));
    #elif LOCALTIME_R_AlternateDefinition4
    localtime_r((t.ts), (ltm));
    #endif
    sz = strftime(tsbuf, sizeof(tsbuf), "%H:%M:%S", ltm);
    str << (string)(tsbuf, sz) << ".";
    str.width(6);
    str.fill('0');
    str << t.usec;
    return(str.str());
    }


    //! display a tick

    public static void print_tick(zenfire.tick_t m, string typ)
    {
    Console.Writefmtts(m);
    Console.Write(" [");
    Console.Write(typ);
    Console.Write("] ");
    Console.Write(m.product);
    Console.Write(" ");
    Console.Write(m.size);
    Console.Write(" @ ");
    Console.Write(m.price);
    Console.Write(" ");
    Console.Write("\n");
    }


    //! print mode

    public static void print_mode(zenfire.tick_t m)
    {
    Console.Writefmtts(m);
    Console.Write(" [MODE] ");
    Console.Write(m.product);
    Console.Write(" ");
    Console.Write(mode_to_string(m.flags));
    Console.Write("\n");
    }

    //! display usage and exit

    public static void die_usage()
    {
    Console.Write("usage : user password [Sim|Live]");
    Console.Write("\n");
    std.exit(0);
    }

    //################################################## ##########################//
    // C A L L B A C K S

    //! display a tick

    public static void recv_alert(zenfire.alert_t m)
    {
    switch(m.type())
    {
    case zenfire.alert.UP:
    Console.Write("Connection [UP]");
    Console.Write("\n");
    break;
    case zenfire.alert.DOWN:
    Console.Write("Connection [DOWN]");
    Console.Write("\n");
    break;
    case zenfire.alert.DISABLED:
    Console.Write("Connection [DISABLED]");
    Console.Write("\n");
    break;
    }
    }

    //################################################## ##########################//
    //! receive ask tick

    public static void recv_ask(zenfire.tick_t m)
    {
    print_tick(m, "ASK");
    }

    //################################################## ##########################//
    // M A I N ################################################## #################//
    //################################################## ##########################//

    internal static int Main(int argc, ref string[] argv)
    {
    if(argc != 4)
    die_usage();

    // get auth info from the command line
    string[] user = new string[1](argv);
    string[] pass = new string[2](argv);
    string[] sys = new string[3](argv);

    try
    {
    // connect to zenfire
    zenfire.client_t zf = *zenfire.client.create("../cert");
    zf.hook_alerts(recv_alert);
    zf.login(user, pass, sys);

    // read a product
    zenfire.arg.product prod;
    Console.Write("Enter symbol.exchange: ");
    prod = SimulateCin.ReadToWhiteSpace(true);

    // look up product
    zenfire.product_t sym = zf.lookup_product(prod);
    Console.Write(sym.symbol);
    Console.Write(" (");
    Console.Write(sym.description);
    Console.Write(" tick ");
    Console.Write(sym.increment);
    Console.Write(" : ");
    Console.Write(sym.point_value);
    Console.Write(sym.currency);
    Console.Write("/pt): OK");
    Console.Write("\n");

    // set up callbacks
    zenfire.tick.callback_t tick_callback;

    // callback functions can be set to existing functions matching the prototype
    tick_callback.ask = recv_ask;

    // callback functions can be set using tr1's functional bind
    tick_callback.bid = std.tr1.bind(print_tick, std.tr1.placeholders._1, "BID");
    tick_callback.low = std.tr1.bind(print_tick, std.tr1.placeholders._1, "LOW");
    tick_callback.high = std.tr1.bind(print_tick, std.tr1.placeholders._1, "HIGH");
    tick_callback.open = std.tr1.bind(print_tick, std.tr1.placeholders._1, "OPEN");
    tick_callback.close = std.tr1.bind(print_tick, std.tr1.placeholders._1, "CLOSE");
    tick_callback.volume = std.tr1.bind(print_tick, std.tr1.placeholders._1, "VOLUME");
    tick_callback.mode = std.tr1.bind(print_mode, std.tr1.placeholders._1);

    // callback functions can be set to use member functions on specific objects
    bbo_history_t bbo_hist = new bbo_history_t();
    tick_callback.best_ask = std.tr1.bind(bbo_history_t.ask_tick, bbo_hist, std.tr1.placeholders._1);
    tick_callback.best_bid = std.tr1.bind(bbo_history_t.bid_tick, bbo_hist, std.tr1.placeholders._1);

    // callback functions can set to objects with operator() set to receive tick
    trade_history_t trade_hist = new trade_history_t();
    tick_callback.trade = trade_hist;

    // set callbacks and subscribe
    zf.hook(tick_callback);
    zf.subscribe(sym);

    // wait for "quit" to exit
    string cmd;
    while(cmd != "quit")
    {
    Console.Write("Type quit to exit");
    Console.Write("\n");
    cmd = SimulateCin.ReadToWhiteSpace(true);
    }

    zf = null;
    }
    catch(std.exception e)
    {
    std.cerr << "exception " << e.what() << ", exiting" << std.endl;
    return(1);
    }

    return(0);
    }
    }

    //************************************************** ***************************
    // ************************************************** ***************************

    //* \file market_data_detailed.cpp
    // * market data example program
    // *
    // * $Id: market_data_detailed.cpp 1273 2009-09-02 23:44:54Z grizz $
    //

    // I N C L U D E S


    #if _MSC_VER
    #define LOCALTIME_R_AlternateDefinition1
    //C++ TO C# CONVERTER NOTE: The following #define macro was replaced in-line:
    //ORIGINAL LINE: #define LOCALTIME_R(tp, tm) localtime_s((tm), (tp))
    #define LOCALTIME_R
    #else
    #define LOCALTIME_R_AlternateDefinition2
    //C++ TO C# CONVERTER NOTE: The following #define macro was replaced in-line:
    //ORIGINAL LINE: #define LOCALTIME_R(tp, tm) localtime_r((tp), (tm))
    #define LOCALTIME_R
    #endif

    //################################################## ##########################//
    //! best bid offer history class

    public class bbo_history_t
    {

    //################################################## ########################//
    //! receive best ask tick

    public void ask_tick(zenfire.tick.best_ask_t t)
    {
    // print tick
    GlobalMembers.print_tick(t, "BEST ASK");

    // save in history
    ask.push_back(t.price);
    }

    //################################################## ########################//
    //! receive best bid tick

    public void bid_tick(zenfire.tick.best_bid_t t)
    {
    // print tick
    GlobalMembers.print_tick(t, "BEST BID");

    // save in history
    bid.push_back(t.price);
    }

    private std.vector<double> ask;
    private std.vector<double> bid;
    }

    //################################################## ##########################//
    //! trade print history class

    public class trade_history_t
    {

    //################################################## ########################//
    //! receive trade tick

    //C++ TO C# CONVERTER TODO TASK: The () operator cannot be overloaded in C#:
    public static void operator ()(zenfire.tick.trade_t t)
    {
    // output tick
    GlobalMembers.print_tick(t, "TRADE");

    // save in history
    hist.push_back(t.price);
    }

    private std.vector<double> hist;
    }

    //************************************************** ***************************
    // ************************************************** ***************************

    //----------------------------------------------------------------------------------------
    internal static class SimulateCin
    {
    private static bool goodlastread = false;
    internal static bool LastReadWasGood
    {
    get
    {
    return goodlastread;
    }
    }

    internal static string ReadToWhiteSpace(bool skipleadingwhitespace)
    {
    string input = "";
    char nextchar;
    if (skipleadingwhitespace)
    {
    while (char.IsWhiteSpace(nextchar = (char)Console.Read()))
    {
    }
    input += nextchar;
    }
    while ( ! char.IsWhiteSpace(nextchar = (char)Console.Read()))
    {
    input += nextchar;
    }
    goodlastread = input.Length > 0;
    return input;
    }
    }

    #2
    paolfili, the code you posted looks like some mix of C# and C++. Unfortunately integrating external libraries is unsupported.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks Austin.
      Can you suggest me any Ninjatrader consultant working in this direction (integrating external libraries)?

      Originally posted by NinjaTrader_Austin View Post
      paolfili, the code you posted looks like some mix of C# and C++. Unfortunately integrating external libraries is unsupported.

      Comment


        #4
        paolfili, unfortunately we can't recommend you one directly - I suggest you contact them individually - http://www.ninjatrader.com/webnew/pa...injaScript.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        571 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        331 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        549 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        550 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X