Program to alert and check IB account value?

Discussion in 'Automated Trading' started by sheepsucker, Dec 20, 2011.

  1. Hi,

    Does anyone know of a program that can be used to check IB account value and alert if above or below a certain treshold?

    Situation is that I trade automated using IB TWS and Multicharts version 6. The less I check in on the trading during the day the better, I only check up on it to see that everything is running as it should.

    I am looking for a way to be alerted in case something has gone wrong, like MC crashed, stop not executed etc. etc. and best way to cover all events is to check account value.

    Unfortunately IB's margin alerts do not work in this case because the used margin depends so much on wether the strategies are flat or not.

    Very thankful for ANY tips!
  2. you can do this with tradelink, eg :

    public class MyAlertResponse : ResponseTemplate
       decimal OPENPLALERTLEVEL = -500;
       Results rt = new Results();
       PositionTracker pt = new PositionTracker();
       TickTracker kt = new TickTracker();
       bool alerted = false;
       void GotTick(Tick k)
           // get open pl for portfolio
           decimal portfoliolevel = Calc.AbsoluteReturn(pt,kt);
           // send an alert if it's below our warning point
           if ((portfoliolevel < OPENPLALERTLEVEL) && !alert)
                 sendticket("PL ALERT","pl "+portfoliolevel+" triggered alert at: "+k.time);
                 // mark alert as sent so we don't do it continually
                 alert = true;
                 // optionally flat all existing positions
                 foreach (Position p in pt)
                     sendorder(new MarketOrderFlat(p));
           // optionally send day-end-alert with trading stats
       void report(string repdata)
             sendticket(Util.ToTLDate()+" closing report",repdata);
       public MyAlertResponse()
             rt.SendReportEvent+=new DebugDelegate(report);
       void GotFill(Fill f) { pt.Adjust(f); rt.GotFill(f); }
       void GotPosition(Positon p) { pt.Adjust(p); rt.GotPosition(p);}
    tradelink is 100% open source, supports 17+ brokers. google tradelink project or to get involved.
  3. Mr_You


    I suspect you could do this with Java or C#. Access the IB TWS API to check account status and access the local machine task/process list to check for the MultiCharts application.

    You could possibly even try AutoHotKey if Java or C# is too difficult.
  4. Thanks! Gonna check up on both tradelink and the IB API.
  5. great.

    note that tradelink works with IB out of the box, so the code above will work with IB or any supported broker (without having to learn a specific brokers api).
  6. byteme


    I think you mean...

    bool alert = false;
    How intesive is:

    Calc.AbsoluteReturn(x, y);
    ...because that will get run on every tick.

    Also, isn't there a way to wire up the position tracker and results automatically? Instead, it looks like you have to use boiler-plate code in GotFill() and GotPosition() which doesn't smell right to me.
  7. calc times are measured in nano seconds, iow you can get millions of ops per second. most brokers deliver tick with millisecond latencies (unless you are colo-d and then you may get in tenths of milliseconds). IB limits you to 5 ticks per second.

    google tradelink speed if you have more questions about this.

    if you desire to have certain things turned on by default, you can use any existing class as a template.

    public class MyNewAlert : MyAlertReponse
         override void GotTick(Tick k)
              // new code with previous alerts still in place
  8. downloaded and installed tradelink. lol @requestcapital button

    Thank you for this sample piece of code you posted. Would it check the pnl from IB as IB calculates it, or calculate it on running strategies in tradelink?
  9. calculation code above for PL is done based on fills/positions (as they occur/exist), as well as market data from whatever source of data you're using (eg IB or one of other feeds in tradelink).
  10. hmm.. I see, I guess if there is some problem with the feed the alarm would malfunction.
    Next gonna look up the api because I am trying to make it as simple as possible.
    #10     Dec 24, 2011