Announcement

Collapse
No announcement yet.

Interactive Brokers EFS

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

  • Interactive Brokers EFS

    Hi

    I would like to get a script that does automated trading with IBor esignal papertrading without the use of Dynaorder. Does anyone out there have a script that i could use, I can fill in the strategy part myself, and if possible the script has some sort of trade managment. Like checking if order placed and is there, stops not neccasary as i can code that.


    Thanks in advance
    Last edited by biswar; 05-13-2004, 05:49 AM.

  • #2
    How about these..

    Here is the IB Code for esignal (without DO)

    PHP Code:


    var IBtrade = new IBBroker;
    var 
    DefEntryContracts 300;

    // system variables - do not change !!
    var currentTrendDirection 0;
    var 
    nLastRawTime  0;
    var 
    BLBarOffset  0;
    var 
    OpenContracts 0;
    var 
    vHour;
    var 
    vMin;
    var 
    vSec;
    var 
    vDay;
    var 
    vMonth;


    function 
    preMain() {
        
    setStudyTitle("PT Example");
        
    setCursorLabelName(false);
        
    setPriceStudy(true);
    }

    function 
    main() {

    //  Get Time Variables 
      
    var vTime = new Date();
      
    vTime getValue("Time"0);
      
    vHour vTime.getHours();
      
    vMin vTime.getMinutes();
      
    vSec vTime.getSeconds();
      
    vDay vTime.getDate();
      
    vMonth vTime.getMonth() +1;
      
    //  completed bar
     
    if (getValue("rawtime"0) != nLastRawTime) {
         
    nLastRawTime getValue("rawtime"0);
         
    BLBarOffset += 1;

    //  New Buy Signal
        
    if ((currentTrendDirection <=0) && 
            (
    high(-1) > high(-2)) ) {
           if (
    OpenContracts 0) {
            
    //Close existing open SHORT Trade
           
    IBtrade.Buy(getSymbol(),OpenContracts,IBBroker.MARKET);
             
    OpenContracts 0;
           }
        
    IBtrade.Buy(getSymbol(),DefEntryContracts,IBBroker.MARKET);
           
    drawShapeRelative(0high(), Shape.UPARROWnullColor.limeShape.ONTOPgetValue("rawtime")+"LE");
             
    OpenContracts DefEntryContracts;
           
    currentTrendDirection 1;
        }

    //  New Sell Signal
        
    if ((currentTrendDirection >=0) && 
            (
    low(-1) > low(-2)) ) {
           if (
    OpenContracts 0) {
            
    //Close existing open LONG Trade
        
    IBtrade.Sell(getSymbol(),OpenContracts,IBBroker.MARKET);
             
    OpenContracts 0;
           }
        
    IBtrade.Sell(getSymbol(),DefEntryContracts,IBBroker.MARKET);
           
    drawShapeRelative(0high(), Shape.DOWNARROWnullColor.redShape.ONTOPgetValue("rawtime")+"SE");
             
    OpenContracts DefEntryContracts;
           
    currentTrendDirection = -1;
        }

     } 
    //  end if Completed Bar

     
    return;


    Here is the Paper Trader Code

    PHP Code:

    var PTtrade = new PaperTradeBroker;
    var 
    DefEntryContracts 300;

    // system variables - do not change !!
    var currentTrendDirection 0;
    var 
    nLastRawTime  0;
    var 
    BLBarOffset  0;
    var 
    OpenContracts 0;
    var 
    vHour;
    var 
    vMin;
    var 
    vSec;
    var 
    vDay;
    var 
    vMonth;


    function 
    preMain() {
        
    setStudyTitle("PT Example");
        
    setCursorLabelName(false);
        
    setPriceStudy(true);
    }

    function 
    main() {

    //  Get Time Variables 
      
    var vTime = new Date();
      
    vTime getValue("Time"0);
      
    vHour vTime.getHours();
      
    vMin vTime.getMinutes();
      
    vSec vTime.getSeconds();
      
    vDay vTime.getDate();
      
    vMonth vTime.getMonth() +1;
      
    //  completed bar
     
    if (getValue("rawtime"0) != nLastRawTime) {
         
    nLastRawTime getValue("rawtime"0);
         
    BLBarOffset += 1;

    //  New Buy Signal
        
    if ((currentTrendDirection <=0) && 
            (
    high(-1) > high(-2)) ) {
           if (
    OpenContracts 0) {
            
    //Close existing open SHORT Trade
             
    PTtrade.Buy(getSymbol(),OpenContracts,PaperTradeBroker.MARKET);
             
    OpenContracts 0;
           }
             
    PTtrade.Buy(getSymbol(),DefEntryContracts,PaperTradeBroker.MARKET);
           
    drawShapeRelative(0high(), Shape.UPARROWnullColor.limeShape.ONTOPgetValue("rawtime")+"LE");
             
    OpenContracts DefEntryContracts;
           
    currentTrendDirection 1;
        }

    //  New Sell Signal
        
    if ((currentTrendDirection >=0) && 
            (
    low(-1) > low(-2)) ) {
           if (
    OpenContracts 0) {
            
    //Close existing open LONG Trade
             
    PTtrade.Buy(getSymbol(),(0-OpenContracts),PaperTradeBroker.MARKET);
             
    OpenContracts 0;
           }
             
    PTtrade.Sell(getSymbol(),(0-DefEntryContracts),PaperTradeBroker.MARKET);
           
    drawShapeRelative(0high(), Shape.DOWNARROWnullColor.redShape.ONTOPgetValue("rawtime")+"SE");
             
    OpenContracts DefEntryContracts;
           
    currentTrendDirection = -1;
        }

     } 
    //  end if Completed Bar

     
    return;

    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      oops...

      forgot to add one CRITICAL item to this code (after reviewing it...

      you need to add....

      if (getCurrentBarIndex() == 0) { after the BLBarOffset += 1; statement.

      You don't want to have this code execute orders on historical bars...

      so, here are the revised versions...

      PHP Code:
      var IBtrade = new IBBroker;
      var 
      DefEntryContracts 300;

      // system variables - do not change !!
      var currentTrendDirection 0;
      var 
      nLastRawTime  0;
      var 
      BLBarOffset  0;
      var 
      OpenContracts 0;
      var 
      vHour;
      var 
      vMin;
      var 
      vSec;
      var 
      vDay;
      var 
      vMonth;


      function 
      preMain() {
          
      setStudyTitle("PT Example");
          
      setCursorLabelName(false);
          
      setPriceStudy(true);
      }

      function 
      main() {

      //  Get Time Variables 
        
      var vTime = new Date();
        
      vTime getValue("Time"0);
        
      vHour vTime.getHours();
        
      vMin vTime.getMinutes();
        
      vSec vTime.getSeconds();
        
      vDay vTime.getDate();
        
      vMonth vTime.getMonth() +1;
        
      //  completed bar
       
      if (getValue("rawtime"0) != nLastRawTime) {
           
      nLastRawTime getValue("rawtime"0);
           
      BLBarOffset += 1;

      //  if current RT bar
        
      if (getCurrentBarIndex() == 0) { 

      //  New Buy Signal
          
      if ((currentTrendDirection <=0) && 
              (
      high(-1) > high(-2)) ) {
             if (
      OpenContracts 0) {
              
      //Close existing open SHORT Trade
             
      IBtrade.Buy(getSymbol(),OpenContracts,IBBroker.MARKET);
               
      OpenContracts 0;
             }
          
      IBtrade.Buy(getSymbol(),DefEntryContracts,IBBroker.MARKET);
             
      drawShapeRelative(0high(), Shape.UPARROWnullColor.limeShape.ONTOPgetValue("rawtime")+"LE");
               
      OpenContracts DefEntryContracts;
             
      currentTrendDirection 1;
          }

      //  New Sell Signal
          
      if ((currentTrendDirection >=0) && 
              (
      low(-1) > low(-2)) ) {
             if (
      OpenContracts 0) {
              
      //Close existing open LONG Trade
          
      IBtrade.Sell(getSymbol(),OpenContracts,IBBroker.MARKET);
               
      OpenContracts 0;
             }
          
      IBtrade.Sell(getSymbol(),DefEntryContracts,IBBroker.MARKET);
             
      drawShapeRelative(0high(), Shape.DOWNARROWnullColor.redShape.ONTOPgetValue("rawtime")+"SE");
               
      OpenContracts DefEntryContracts;
             
      currentTrendDirection = -1;
          }
        
         } 
      // end if getcurrentbarindex == 0
       
      //  end if Completed Bar

       
      return;

      Now for the PT code..

      PHP Code:
      var PTtrade = new PaperTradeBroker;
      var 
      DefEntryContracts 300;

      // system variables - do not change !!
      var currentTrendDirection 0;
      var 
      nLastRawTime  0;
      var 
      BLBarOffset  0;
      var 
      OpenContracts 0;
      var 
      vHour;
      var 
      vMin;
      var 
      vSec;
      var 
      vDay;
      var 
      vMonth;


      function 
      preMain() {
          
      setStudyTitle("PT Example");
          
      setCursorLabelName(false);
          
      setPriceStudy(true);
      }

      function 
      main() {

      //  Get Time Variables 
        
      var vTime = new Date();
        
      vTime getValue("Time"0);
        
      vHour vTime.getHours();
        
      vMin vTime.getMinutes();
        
      vSec vTime.getSeconds();
        
      vDay vTime.getDate();
        
      vMonth vTime.getMonth() +1;
        
      //  completed bar
       
      if (getValue("rawtime"0) != nLastRawTime) {
           
      nLastRawTime getValue("rawtime"0);
           
      BLBarOffset += 1;

      //  current RT bar
        
      if (getCurrentBarIndex() == 0) {

      //  New Buy Signal
          
      if ((currentTrendDirection <=0) && 
              (
      high(-1) > high(-2)) ) {
             if (
      OpenContracts 0) {
              
      //Close existing open SHORT Trade
               
      PTtrade.Buy(getSymbol(),OpenContracts,PaperTradeBroker.MARKET);
               
      OpenContracts 0;
             }
               
      PTtradeBuy(getSymbol(),DefEntryContracts,PaperTradeBroker.MARKET);
             
      drawShapeRelative(0high(), Shape.UPARROWnullColor.limeShape.ONTOPgetValue("rawtime")+"LE");
               
      OpenContracts DefEntryContracts;
             
      currentTrendDirection 1;
          }

      //  New Sell Signal
          
      if ((currentTrendDirection >=0) && 
              (
      low(-1) > low(-2)) ) {
             if (
      OpenContracts 0) {
              
      //Close existing open LONG Trade
               
      PTtrade.Buy(getSymbol(),(0-OpenContracts),PaperTradeBroker.MARKET);
               
      OpenContracts 0;
             }
               
      PTtrade.Sell(getSymbol(),(0-DefEntryContracts),PaperTradeBroker.MARKET);
             
      drawShapeRelative(0high(), Shape.DOWNARROWnullColor.redShape.ONTOPgetValue("rawtime")+"SE");
               
      OpenContracts DefEntryContracts;
             
      currentTrendDirection = -1;
          }

        } 
      // end if getCurrentBarIndex() == 0

       
      //  end if Completed Bar

       
      return;

      Bugs are part of a developers life - sorry about that.

      B
      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        Thats great !!!... Thanks Doji

        Comment


        • #5
          IB

          Forgot to mention a couple oof things

          1. Place Stop when new short or long taken
          2. move stop to breakeven after reduced
          3. cancle stop when trade is over.

          Could you also add these in to your sample script pleae

          thanks

          Comment


          • #6
            are other order formats possible

            Reading your code i was wondering if other order formats such as limit ,trail ,stoplimit etc were also possible ?. Where do i find more documentation about this code?

            Thanks,

            Ruurd.

            Comment


            • #7
              Solutions...

              In EFS (using the IB Broker plug-in), you have to do some of this manually... I'll also add some control variables...

              StrategyisInTrade
              StrategyisShort
              StrategyisLong

              Stops....
              In doing so, you have to assume an entry price (because there is not way to retrieve it from the IBBroker module) and then track the price (current price) to see if it breaches your stop level.

              PTs....
              Are handled the same way - testing every close() for a price breach.

              Moving Stops..
              I use control variables to handle this. In this case, I created "StopMovedToBE" to be our control variable. I set it to false when a trade (entry) is executed, then test for the conditions to move the stop and set it to true after the system moves the stop. Thus, it will only allow this to happen ONCE.

              Now, if you wanted to create a multiple-stage stop moving system, you would use numbers to count the stages of the stop system and not true/false variables. For example, you would have Stage#1, #2, #3, #4 for the stops and increment the stage counter each time an action is taken.

              If you were using something like DO, you could place these orders (stop and PT) as LIMIT orders and check to see when they get filled. But because of the use or the IBBroker module, you have to do it like I'm showing.

              If you have any other questions, please let me know.

              B

              Here is an example...

              PHP Code:
              var IBtrade = new IBBroker;
              var 
              DefEntryContracts 300;
              var 
              vStopLevel1 2.0;
              var 
              vPTLevel1 2.0;
              var 
              StopBELevel 1.50;

              // system variables - do not change !!
              var currentTrendDirection 0;
              var 
              nLastRawTime  0;
              var 
              BLBarOffset  0;
              var 
              OpenContracts 0;

              var 
              nEntryPrice;
              var 
              nPTPrice;
              var 
              nStopPrice;
              var 
              StopMovedToBE false;
              var 
              vHour;
              var 
              vMin;
              var 
              vSec;
              var 
              vDay;
              var 
              vMonth;

              var 
              StrategyisInTrade false;
              var 
              StrategyisShort false;
              var 
              StrategyisLong false;


              function 
              preMain() {
                  
              setStudyTitle("PT Example");
                  
              setCursorLabelName(false);
                  
              setPriceStudy(true);
              }

              function 
              main() {

              //  Get Time Variables 
                
              var vTime = new Date();
                
              vTime getValue("Time"0);
                
              vHour vTime.getHours();
                
              vMin vTime.getMinutes();
                
              vSec vTime.getSeconds();
                
              vDay vTime.getDate();
                
              vMonth vTime.getMonth() +1;
                
              //  completed bar
               
              if (getValue("rawtime"0) != nLastRawTime) {
                   
              nLastRawTime getValue("rawtime"0);
                   
              BLBarOffset += 1;

              //  if current RT bar
                
              if (getCurrentBarIndex() == 0) { 

              //  New Buy Signal
                  
              if ((currentTrendDirection <=0) && 
                      (
              high(-1) > high(-2)) ) {
                     if (
              OpenContracts 0) {
                      
              //Close existing open SHORT Trade
                     
              IBtrade.Buy(getSymbol(),OpenContracts,IBBroker.MARKET);
                       
              OpenContracts 0;
                     }
                  
              IBtrade.Buy(getSymbol(),DefEntryContracts,IBBroker.MARKET);
                     
              drawShapeRelative(0high(), Shape.UPARROWnullColor.limeShape.ONTOPgetValue("rawtime")+"LE");
                       
              OpenContracts DefEntryContracts;
                      
              nEntryPrice close();
                      
              nStopPrice nEntryPrice-vStopLevel1 ;
                      
              nPTPrice nEntryPrice+vPTLevel1 ;
                     
              currentTrendDirection 1;
                     
              StopMovedToBE false;
                     
              StrategyisInTrade true;
                     
              StrategyisShort false;
                     
              StrategyisLong true;
                  }

              //  New Sell Signal
                  
              if ((currentTrendDirection >=0) && 
                      (
              low(-1) > low(-2)) ) {
                     if (
              OpenContracts 0) {
                      
              //Close existing open LONG Trade
                  
              IBtrade.Sell(getSymbol(),OpenContracts,IBBroker.MARKET);
                       
              OpenContracts 0;
                     }
                  
              IBtrade.Sell(getSymbol(),DefEntryContracts,IBBroker.MARKET);
                     
              drawShapeRelative(0high(), Shape.DOWNARROWnullColor.redShape.ONTOPgetValue("rawtime")+"SE");
                       
              OpenContracts DefEntryContracts;
                      
              nEntryPrice close();
                      
              nStopPrice nEntryPrice+vStopLevel1 ;
                      
              nPTPrice nEntryPrice-vPTLevel1 ;
                     
              currentTrendDirection = -1;
                     
              StopMovedToBE false;
                     
              StrategyisInTrade true;
                     
              StrategyisShort true;
                     
              StrategyisLong false;
                  }
                
                 } 
              // end if getcurrentbarindex == 0
               
              //  end if Completed Bar


              //---------------------------------------------------
              //----  Allow moving of stop to breakeven
              //---------------------------------------------------
              if ((StrategyisInTrade) && (!StopMovedToBE)) {
                if ((
              StrategyisLong) && (close() > (nEntryPrice+StopBELevel) ) ){
                  
              nStopPrice nEntryPrice;
                  
              StopMovedToBE true;
                }
                if ((
              StrategyisShort) && (close() < (nEntryPrice-StopBELevel) ) ){
                  
              nStopPrice nEntryPrice;
                  
              StopMovedToBE true;
                }
              }
              //---------------------------------------------------
              //----  END Allow moving of stop to breakeven
              //---------------------------------------------------


              //--------------------------------------------------------
              //---------------  outside completed bar loop
              //---------------  check for stops and PTs
              //--------------------------------------------------------
              if (StrategyisInTrade) {
                if (
              StrategyisShort) {
                 
              //  Check Stops
                  
              if (close() > nStopPrice) {
                  
              //  Stopped out of a short trade
                      //Close existing open SHORT Trade
                     
              IBtrade.Buy(getSymbol(),OpenContracts,IBBroker.MARKET);
                       
              OpenContracts 0;
                     
              StrategyisInTrade false;
                     
              StrategyisShort false;
                     
              StrategyisLong false;
                  }

                 
              //  Check PTs
                  
              if (close() < nPTPrice) {
                  
              //  Hit our short trade PT
                      //Close existing open SHORT Trade
                     
              IBtrade.Buy(getSymbol(),OpenContracts,IBBroker.MARKET);
                       
              OpenContracts 0;
                     
              StrategyisInTrade false;
                     
              StrategyisShort false;
                     
              StrategyisLong false;
                  }
                } 
              // end if (StrategyisShort)

                
              if (StrategyisLong) {
                 
              //  Check Stops
                  
              if (close() < nStopPrice) {
                  
              //  Stopped out of a long trade
                      //Close existing open LONG Trade
                     
              IBtrade.Sell(getSymbol(),(0-OpenContracts),IBBroker.MARKET);
                       
              OpenContracts 0;
                     
              StrategyisInTrade false;
                     
              StrategyisShort false;
                     
              StrategyisLong false;
                  }

                 
              //  Check PTs
                  
              if (close() > nPTPrice) {
                  
              //  Hit our long trade PT
                      //Close existing open LONG Trade
                     
              IBtrade.Sell(getSymbol(),(0-OpenContracts),IBBroker.MARKET);
                       
              OpenContracts 0;
                     
              StrategyisInTrade false;
                     
              StrategyisShort false;
                     
              StrategyisLong false;
                  }
                } 
              // end if (StrategyisLong)

              }  // end if (StrategyisInTrade) 
              //---- END ---------------------------------------------
              //---------------  outside completed bar loop
              //---------------  check for stops and PTs
              //--------------------------------------------------------

               
              return;

              Brad Matheny
              eSignal Solution Provider since 2000

              Comment


              • #8
                Rugosoft...

                The technical docs can be found in the DOCS directory (within the esignal directory). There are other types of orders, but because there is currently no way to cancel the limit orders, I would suggest using my methods for trading.

                You could also look into www.dynaorder.com as another solution.

                B
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment


                • #9
                  IB

                  Thanks doji

                  Ive used bracket trader, which seems to half work the preset targets and stops seem to flaw it(unless im doing soemthing wrong when i try to change the stops.

                  I gonna try tsim+, any help here would be appreciated

                  Comment


                  • #10
                    I have not worked with either..

                    I have worked with DynaOrder, NINJA TRADER and esignal Broker Modules. I have not tried all of the other packages.

                    I, personally, like DO. It takes a little getting used to, but once you figure it out, you can do a whole lot with it (plus I guess it helps to have programming skills). Do is basically designed by programmers for programmers.

                    B
                    Brad Matheny
                    eSignal Solution Provider since 2000

                    Comment


                    • #11
                      Re: Solutions...

                      I realize this is an ancient thread, but I was wondering if the quoted code is a bug, albeit benign:

                      PHP Code:
                      function main() {
                      //  Get Time Variables 
                        
                      var vTime = new Date();
                        
                      vTime getValue("Time"0); 
                      The code first sets vTime to a new Date object, and then immediately overwrites that with the return from getValue(). Yes, it's benign (I think), and could be safely rewritten as:
                      PHP Code:
                      function main() {
                      // Get Time Variables
                         
                      var vTime getValue("Time"0); 
                      Or am I missing something?

                      Thanks.

                      Comment


                      • #12
                        wow this thread is very ancient,
                        yep what you have there will work, no prob

                        Comment

                        Working...
                        X