Announcement

Collapse
No announcement yet.

Automated Trading

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

  • Automated Trading

    Hello...

    I am new to esignal and would like to know if it is possible to automate trades directly to my integrated broker using the alerts/signals generated through the EFS.

    I have read the knowledge base and bulletin boards and it seems to require a 3rd party package (ie Dynaorder) to achieve this.

    Is it possible to not use a 3rd party and do it directly using esignal's EFS ?

    Appreciate any advice from forum members.

    Thanks.

    C Man

  • #2
    yes...

    with the Cyber Trader or Interactive Brokers plug-in (provided by esignal), you can develop strategies that allow you to BUY/SELL any symbol from within EFS. This plug-in does not provide the same type of diversity as Dynaorder, but it will allow you to develop strategies.

    Look in the DOCS folder (within the esignal folder) for these files...

    "CyberEFS.DOC"
    "IBEFS.DOC"

    These will povide you with some examples. If you need more help, let me know..

    B
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks ! ...Example ?

      Hi Doji !

      Thanks a lot for the quick reply. I read the EFS for IB document.

      I am currently using Interactive Brokers as my integrated broker. Would it be at all possible to give me a simple example of the format of how automated orders would be actually coded ? (sorry obviously I am not a programmer).

      Below is a simple code for a Moving Average crossover. What would the code look like for an "always in" reversal system of 100 shares buying and selling at the market at each crossover ?

      I really appreciate any help you can give me on this !

      C Man



      //{{EFSWizard_Declarations

      var vEMA20 = new MAStudy(20, 0, "Close", MAStudy.EXPONENTIAL);
      var vEMA50 = new MAStudy(50, 0, "Close", MAStudy.EXPONENTIAL);
      var vLastAlert = -1;

      //}}EFSWizard_Declarations 17716


      function preMain() {
      /**
      * This function is called only once, before any of the bars are loaded.
      * Place any study or EFS configuration commands here.
      */
      //{{EFSWizard_PreMain
      setPriceStudy(true);
      setStudyTitle("EMAxOver2050");
      setCursorLabelName("Line1", 0);
      setCursorLabelName("Line2", 1);
      setDefaultBarStyle(PS_SOLID, 0);
      setDefaultBarStyle(PS_SOLID, 1);
      setDefaultBarFgColor(Color.white, 0);
      setDefaultBarFgColor(Color.black, 1);
      setDefaultBarThickness(2, 0);
      setDefaultBarThickness(2, 1);
      setPlotType(PLOTTYPE_LINE, 0);
      setPlotType(PLOTTYPE_LINE, 1);
      //}}EFSWizard_PreMain 52358

      }

      function main() {
      /**
      * The main() function is called once per bar on all previous bars, once per
      * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
      * in your preMain(), it is also called on every tick.
      */

      //{{EFSWizard_Expressions
      //{{EFSWizard_Expression_1
      if (
      vEMA20.getValue(MAStudy.MA) >= vEMA50.getValue(MAStudy.MA)
      ) onAction1()
      //}}EFSWizard_Expression_1 11980

      //{{EFSWizard_Expression_2
      else if (
      vEMA20.getValue(MAStudy.MA) <= vEMA50.getValue(MAStudy.MA)
      ) onAction2();
      //}}EFSWizard_Expression_2 12163

      //}}EFSWizard_Expressions 44795


      //{{EFSWizard_Return
      return new Array(
      vEMA20.getValue(MAStudy.MA),
      vEMA50.getValue(MAStudy.MA)
      );
      //}}EFSWizard_Return 12662

      }

      function postMain() {
      /**
      * The postMain() function is called only once, when the EFS is no longer used for
      * the current symbol (ie, symbol change, chart closing, or application shutdown).
      */
      }

      //{{EFSWizard_Actions
      //{{EFSWizard_Action_1
      function onAction1() {
      vLastAlert = 1;
      }
      //}}EFSWizard_Action_1 5589

      //{{EFSWizard_Action_2
      function onAction2() {
      vLastAlert = 2;
      }
      //}}EFSWizard_Action_2 5582

      //}}EFSWizard_Actions 31632

      Comment


      • #4
        Here you go...

        This example uses the logical control variables to provide conditionals for our trading actions (StrategyisLong.....). These provide our code the ability to know its last actions and correct our trading actions (if necessary).

        The DefEntryContracts and OpenContracts are used to control how many we buy/sell when signals are generated.

        You can see it is pretty simple to do this. If you have any other questions, let me know..

        B

        PHP Code:
        //  declare the IBBroker plug-in.
        var IBtrade = new IBBroker;

        //{{EFSWizard_Declarations

        var vEMA20 = new MAStudy(200"Close"MAStudy.EXPONENTIAL);
        var 
        vEMA50 = new MAStudy(500"Close"MAStudy.EXPONENTIAL);
        var 
        vLastAlert = -1;

        //}}EFSWizard_Declarations 17716

        var DefEntryContracts 1;
        var 
        OpenContracts 0;

        //  declare needed control variables.
        var StrategyisLong false;
        var 
        StrategyisShort false;
        var 
        StrategyisInTrade false;


        function 
        preMain() {
        /**
        * This function is called only once, before any of the bars are loaded.
        * Place any study or EFS configuration commands here.
        */
        //{{EFSWizard_PreMain
        setPriceStudy(true);
        setStudyTitle("EMAxOver2050");
        setCursorLabelName("Line1"0);
        setCursorLabelName("Line2"1);
        setDefaultBarStyle(PS_SOLID0);
        setDefaultBarStyle(PS_SOLID1);
        setDefaultBarFgColor(Color.white0);
        setDefaultBarFgColor(Color.black1);
        setDefaultBarThickness(20);
        setDefaultBarThickness(21);
        setPlotType(PLOTTYPE_LINE0);
        setPlotType(PLOTTYPE_LINE1);
        //}}EFSWizard_PreMain 52358

        }

        function 
        main() {
        /**
        * The main() function is called once per bar on all previous bars, once per
        * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
        * in your preMain(), it is also called on every tick.
        */

        //  Added..  to check for RT Bar.
        if (getCurrentBarIndex() == 0) {

        //{{EFSWizard_Expressions
        //{{EFSWizard_Expression_1
        if (
        vEMA20.getValue(MAStudy.MA) >= vEMA50.getValue(MAStudy.MA)
        onAction1()
        //}}EFSWizard_Expression_1 11980

        //{{EFSWizard_Expression_2
        else if (
        vEMA20.getValue(MAStudy.MA) <= vEMA50.getValue(MAStudy.MA)
        onAction2();
        //}}EFSWizard_Expression_2 12163

        //}}EFSWizard_Expressions 44795

        //  end if getCurrentBarIndex() == 0


        //{{EFSWizard_Return
        return new Array(
        vEMA20.getValue(MAStudy.MA),
        vEMA50.getValue(MAStudy.MA)
        );
        //}}EFSWizard_Return 12662

        }

        function 
        postMain() {
        /**
        * The postMain() function is called only once, when the EFS is no longer used for
        * the current symbol (ie, symbol change, chart closing, or application shutdown).
        */
        }

        //{{EFSWizard_Actions
        //{{EFSWizard_Action_1
        function onAction1() {
        vLastAlert 1;
         if (!
        StrategyisLong) {
          if (
        StrategyisShort) {
              
        IBtrade.Buy(getSymbol(),(OpenContracts +DefEntryContracts),IBBroker.MARKET);
          } else {
              
        IBtrade.Buy(getSymbol(),DefEntryContracts,IBBroker.MARKET);
          }
           
        OpenContracts DefEntryContracts;
           
        StrategyisLong true;
           
        StrategyisShort false;
           
        StrategyisInTrade true;
         }
        }
        //}}EFSWizard_Action_1 5589

        //{{EFSWizard_Action_2
        function onAction2() {
        vLastAlert 2;
         if (!
        StrategyisShort) {
          if (
        StrategyisLong) {
              
        IBtrade.Sell(getSymbol(),(OpenContracts +DefEntryContracts),IBBroker.MARKET);
          } else {
              
        IBtrade.Sell(getSymbol(),DefEntryContracts,IBBroker.MARKET);
          }
           
        OpenContracts DefEntryContracts;
           
        StrategyisLong false;
           
        StrategyisShort true;
           
        StrategyisInTrade true;
         }
        }
        //}}EFSWizard_Action_2 5582

        //}}EFSWizard_Actions 31632 
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          Wow...Thanks !

          Hey Doji.....

          Thank you so much for your help ! You have given me lots of guidelines to work off of.

          Very impressed with what the EFS' can do.


          Really appreciate it !

          C Man

          Comment


          • #6
            How to execute only on bar close ?

            Hi !

            I've been testing the automation code with tick bar charts.

            Seems that if in the current bar if the MA crosses numerous times BEFORE the bar closes....trades will be triggered numerous times as well.

            Is it possible to have the triggers execute only at the CLOSE of the current bar instead of every signal WITHIN the bar before it closes ?


            Very much appreciate the help on this !

            C Man

            Comment


            • #7
              C Man
              You have two solutions available.
              The easiest is to insert setComputeOnClose(); in preMain(). The whole efs at that point will update only at the close of a bar.
              The other is to enclose the conditions and the strategy in
              if(getBarState()==BARSTATE_NEWBAR){
              //your conditions and strategy here
              }

              This solution computes at the close of a bar only what is enclosed in the getBarState() braces. The rest of the efs will work in real time.
              Alex

              Comment


              • #8
                setComputeOnClose(true);

                Hi Alex !

                Thank you for you help on this.

                I have tried to added the line "setComputeOnClose(true);" to the premain.....but seems the signals are not triggering.

                Sorry.....coding newbie here.....what am i missing ?

                C MAN



                // declare the IBBroker plug-in.
                var IBtrade = new IBBroker;

                //{{EFSWizard_Declarations

                var vEMA20 = new MAStudy(20, 0, "Close", MAStudy.EXPONENTIAL);
                var vEMA50 = new MAStudy(50, 0, "Close", MAStudy.EXPONENTIAL);
                var vLastAlert = -1;

                //}}EFSWizard_Declarations 17716

                var DefEntryContracts = 1;
                var OpenContracts = 0;

                // declare needed control variables.
                var StrategyisLong = false;
                var StrategyisShort = false;
                var StrategyisInTrade = false;


                function preMain() {
                /**
                * This function is called only once, before any of the bars are loaded.
                * Place any study or EFS configuration commands here.
                */
                //{{EFSWizard_PreMain
                setPriceStudy(true);
                setStudyTitle("AUTOEMAxOver2050(100)");
                setCursorLabelName("Line1", 0);
                setCursorLabelName("Line2", 1);
                setDefaultBarStyle(PS_SOLID, 0);
                setDefaultBarStyle(PS_SOLID, 1);
                setDefaultBarFgColor(Color.white, 0);
                setDefaultBarFgColor(Color.black, 1);
                setDefaultBarThickness(2, 0);
                setDefaultBarThickness(2, 1);
                setPlotType(PLOTTYPE_LINE, 0);
                setPlotType(PLOTTYPE_LINE, 1);
                //}}EFSWizard_PreMain 54298
                setComputeOnClose(true);

                }

                function main() {
                /**
                * The main() function is called once per bar on all previous bars, once per
                * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
                * in your preMain(), it is also called on every tick.
                */

                // Added.. to check for RT Bar.
                if (getCurrentBarIndex() == 0) {

                //{{EFSWizard_Expressions
                //{{EFSWizard_Expression_1
                if (
                vEMA20.getValue(MAStudy.MA) >= vEMA50.getValue(MAStudy.MA)
                ) onAction1()
                //}}EFSWizard_Expression_1 11980

                //{{EFSWizard_Expression_2
                else if (
                vEMA20.getValue(MAStudy.MA) <= vEMA50.getValue(MAStudy.MA)
                ) onAction2();
                //}}EFSWizard_Expression_2 12163

                //}}EFSWizard_Expressions 44795

                } // end if getCurrentBarIndex() == 0


                //{{EFSWizard_Return
                return new Array(
                vEMA20.getValue(MAStudy.MA),
                vEMA50.getValue(MAStudy.MA)
                );
                //}}EFSWizard_Return 12662

                }

                function postMain() {
                /**
                * The postMain() function is called only once, when the EFS is no longer used for
                * the current symbol (ie, symbol change, chart closing, or application shutdown).
                */
                }

                //{{EFSWizard_Actions
                //{{EFSWizard_Action_1
                function onAction1() {
                if (!StrategyisLong) {;
                if (StrategyisShort) {;
                IBtrade.Buy(getSymbol(), (OpenContracts +100), IBBroker.MARKET);
                } else {;
                IBtrade.Buy(getSymbol(), 100, IBBroker.MARKET);
                };
                OpenContracts = 100;
                StrategyisLong = true;
                StrategyisShort = false;
                StrategyisInTrade = true;
                };
                vLastAlert = 1;
                }
                //}}EFSWizard_Action_1 43458

                //{{EFSWizard_Action_2
                function onAction2() {
                if (!StrategyisShort) {;
                if (StrategyisLong) {;
                IBtrade.Sell(getSymbol(), (OpenContracts +100), IBBroker.MARKET);
                } else {;
                IBtrade.Sell(getSymbol(), 100, IBBroker.MARKET);
                };
                OpenContracts = 100;
                StrategyisLong = false;
                StrategyisShort = true;
                StrategyisInTrade = true;
                };
                vLastAlert = 2;
                }
                //}}EFSWizard_Action_2 43212

                //}}EFSWizard_Actions 113182

                Comment


                • #9
                  C Man
                  If I am not mistaken when setComputeOnClose() is used [BTW no need to insert true in the parenth] the current bar index is never 0 but -1.
                  Try replacing if(getCurrentBarIndex()==0) with if(getCurrentBarIndex()==-1) and see if that works
                  Alex

                  Comment


                  • #10
                    Thanks

                    Hey Alex !

                    I could not get the setComputeOnClose() or the if(getCurrentBarIndex()==-1) to work.

                    But the if(getBarState()==BARSTATE_NEWBAR){
                    // workaround seems to be working now.


                    Thanks a lot for you help !


                    BlueNote

                    Comment


                    • #11
                      Option to scale out ?

                      Hello....

                      While the current code listed below is for an "always in" reversal system.

                      Currently, the way the code is written. The size on all signals is fixed. ie.....if the first trade is 200 shares long, then on the next crossover it will be 400 shares sell, then 400 shares buy... etc for a constant 200 share exposure.

                      Is it possible to have the EFS monitor the actual share size at each crossover to have the option to scale out ?

                      Meaning....if I am long 200 shares and decide to manually scale out 100 shares. At the next crossover short, can it determine that I have 100 shares and will then have to go short 300 shares (rather than 400) to maintain the 200 share exposure ?

                      Thanks for all the help on this thread !

                      BlueNote

                      Comment


                      • #12
                        you can do this...

                        You just have to track the actions of your system with variables..

                        I use the following..

                        DefEntryContracts = 50;
                        OpenContracts = 0;

                        Sometimes I use other too - like...

                        ExitContracts1 = 25;
                        ExitContracts2 = 25;

                        Now, within my code, I control the actions of my system by setting/testing OpenContracts.

                        For example, trying to go long would be like this..

                        if ((!StrategyisLong) && (BuySignalNow)) {
                        if (StrategyisShort) {
                        IBtrade.Buy(getSymbol(), (DefEntryContracts+OpenContracts), IBBroker.MARKET);
                        } else {
                        IBtrade.Buy(getSymbol(), (DefEntryContracts), IBBroker.MARKET);
                        }
                        StrategyisLong = true;
                        StrategyisShort = false;
                        StrategyisInTrade = true;
                        OpenContracts = DefEntryContracts;
                        }


                        This would be a sell signal..
                        if ((!StrategyisShort) && (SellSignalNow)) {
                        if (StrategyisLong) {
                        IBtrade.Buy(getSymbol(), (DefEntryContracts+OpenContracts), IBBroker.MARKET);
                        } else {
                        IBtrade.Buy(getSymbol(), (DefEntryContracts), IBBroker.MARKET);
                        }
                        StrategyisLong = false;
                        StrategyisShort = true;
                        StrategyisInTrade = true;
                        OpenContracts = DefEntryContracts;
                        }

                        Now, you could do the same thing by exiting a portion of the trade at profit targets or whatever.... just adjust the OpenContracts variable.

                        Brad
                        Brad Matheny
                        eSignal Solution Provider since 2000

                        Comment


                        • #13
                          BlueNote
                          In addition to what Brad has already suggested you may also want to consider the getPositionSize() function which returns the number of shares/contracts held.
                          Alex

                          Comment


                          • #14
                            Originally posted by Alexis C. Montenegro
                            BlueNote
                            In addition to what Brad has already suggested you may also want to consider the getPositionSize() function which returns the number of shares/contracts held.
                            Alex
                            Alexis... been trying to follow this thread and understand as its something I would like to do.. this efs is clearly powerful but very obtuse to beginners... could you try to explain if you have the time... how exactly would the getpostionsize function actually be inserted into the code that bluenote is working on below?

                            would you be able to insert that function into his code so that I could see how it would be used to allow the scaling to actually happen?

                            I know thats a lot to ask but its real hard to get to grips with the more complex elements of this

                            Comment


                            • #15
                              getPositionSize()

                              I believe this is only for use with "Strategy" functions (for backtesting within esignal) - not for use with the IBBroker module provided by esignal.

                              If you look thru the documents provided by esignal (in the DOCS folder), you'll see the available functions that can be used to automatically trade with IB.

                              The solution I suggested allows a developer to track the actions of his system internally. If needed, this information could be stored to a file and later reloaded (in the event of a crash or some other action).

                              If you have any specific questions, let us know. Both Alex and I try to keep up on this board to help people.

                              Brad
                              Brad Matheny
                              eSignal Solution Provider since 2000

                              Comment

                              Working...
                              X