Announcement

Collapse
No announcement yet.

Integrate IB Into Backtest Formula

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

  • Integrate IB Into Backtest Formula

    I have a quick question regarding integrating live orders via IB into my current backtest strategy.

    For example, if i have the following if statement:
    if((Strategy.isLong()==true))
    Strategy.doSell("End", Strategy.Market, Strategy.THISBAR);

    Can i modify it to the following in order to invoke live trading:

    if((Strategy.isLong()==true))
    {
    Strategy.doSell("End",Strategy.Market, Strategy.THISBAR);
    IBBroker.Sell(getSymbol(), Quantity, IBBroker.Limit, close()-0.02);
    }

    Then, will everything automatically work by enabling the formula on the current chart I am looking at, and execute a trade via IB when the next buy/sell signal is hit?

    Thank you,
    Anthony
    --------------------
    http://groupshares.com

  • #2
    Hello Anthony,

    The Strategy functions are intended for back testing only. They are designed to pass simulated trade data to the Strategy Analyzer. It is not recommended to use the Strategy object for processing real time data for trading. You'll need to track your position status with some global variables to replace the Strategy.isLong(), .isShort() calls and then use the Generic Broker Functions to send the trades to IB.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      anthony....

      If you need help with automating a strategy.. let me know.. I've done alot of this and know lots of tricks..

      Jason is correct, it is best not to use the "Strategy" functions while trading in real-time. You need to create logical "control" variables to track everything.

      Also, try to avoid using MARKET and THISBAR in your backtest code unless you are confident the logic is correct within your EFS.. It is like trying to BACKUP TIME and BUY/SELL at the open of the current bar instead of the close... Unless your logic handles this correctly, you could have dramatically skewed your backtest results.. (in your favor or not).

      let me know if I can help..

      B
      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        Strategy

        Doji,
        I really appreciate the help, and may seek your consultation in the future. I am highly proficient in Java as I spent 4 years at Oracle developing on 11i platforms, but anyway, if you can help me build a simple strategy with IB, I should be able to modify it to match my strategy.

        Basically, I am looking to accomplish the following:
        A) Track how many shares of a stock i have (why i considered using market order to insure i always have the default as I did not see an IB function for returning the currently owned shares) - Also, i will be trading ETF/E-Minis so I am not worried about slippage as much.
        B) Enter a position +0.05 breaking above a 40 tick MA, or Short a position -0.05 below the 40 day MA.
        C) Max loss of .5%
        D) Trailing gain that is triggered at 1% in .1% intervals. ie. if the stock hits 1% my gain is locked in at .9%, or if it goes to 1.5% my gain is now locked in at 1.4%. Similar work for short positions.
        E) Close all trades at 3:59 and only allow trading during 9:30-4:00.

        -well that's about as simple as you can make it to get something that may function well on ETFs.

        Can you briefly explain how THISBAR/NEXTBAR are different? If my "trigger" is hit on the current bar using THISBAR, it will take the close of that bar to execute the trade, correct? Or, using Strategy.MARKET will it take the next trade on the current bar after the trigger is used?

        I spent some time working on a backtesting strategy that makes about 2 trades per day and has about 96% return on the SPY using 1000 contracts. Also, and most important, time in market is about 9% and the equity curve is very close to being linearly increasing.

        I sincerely appreciate the help in getting started with EFS,
        Anthony
        --------------------
        http://groupshares.com

        Comment


        • #5
          THISBAR/NEXTBAR...

          ok - here you go...

          there are a couple of parameters used in creating a back-test study (efs).. They are as follows..

          Strategy.MARKET - executes an order at the OPEN price of the specified bar.
          Strategy.LIMIT - executes an order at the specified price of the specified bar.

          Strategy.THISBAR - executes the order on THIS (current) bar.
          Strategy.NEXTBAR - executes the order on the NEXT (future) bar.

          Strategy.DEFAULT - the default # of shares/contract entered into the back-test window.
          Strategy.ALL - used for exit and stop order to exit ALL open shares/contracts.

          You would use ...

          Strategy.doLong(...) - to enter a long trade
          Strategy.doSell(...) - to exit a long trade

          Strategy.doShort(...) - to enter a short trade
          Strategy.doCover(...) - to exit a short trade

          so, if I created an efs that compared the close of the current bar against a MA value, then entered an order. I would want to use the following..

          if (close() > vMA) {
          Strategy.doLong(Strategy.MARKET, Strategy.NEXTBAR);
          }

          This way, I'm comparing the closing price of a bar (after the bar has finished forming), then entering an order for the open of the next bar.

          If I wanted the same order to be filled at the close of the bar that generated the trigger, I would use..

          if (close() > vMA) {
          Strategy.doLong(Strategy.LIMIT, Strategy.NEXTBAR, Strategy.DEFAULT, close());
          }

          NOTICE... The only way I have found available to use MARKET/THISBAR is if I'm comparing the values of the previous bar to generate triggers... For example, if the previous bar's close was greater than the previous MA value, then enter at the open of today. But doing it this way is the same as my fist example (MARKET/NEXTBAR). The difference is we are comparing the current bars data, then entering at the open of the NEXTBAR in the first example...

          or ...

          we compare the previous bars data and enter at the open of the current bar.

          I hope this makes sense.. It can be tricky at times..

          B
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #6
            Still Questioning..

            It is a shame that the software cant use the next tick for the entry point after the target was hit, but anyway, I am still curious on how to build the code for IB as I outlined in the five steps below.
            I appreciate your help.
            --------------------
            http://groupshares.com

            Comment


            • #7
              Doji, to fill on the trigger bar, did you mean to say THISBAR?:
              If I wanted the same order to be filled at the close of the bar that generated the trigger, I would use..
              if (close() > vMA) {
              Strategy.doLong(Strategy.LIMIT, Strategy.NEXTBAR, Strategy.DEFAULT, close());
              }

              Comment

              Working...
              X