Announcement

Collapse
No announcement yet.

Automated Trading: Multiple Orders Loadin EFS

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

  • Automated Trading: Multiple Orders Loadin EFS

    I have set up automatic trades using buy(), sell(), buyMarket()...I am pretty much accomplishing what I need.

    However, every time I load up my script, it executes tons of orders to my integrated broker (Interactive Broker). I assume, as the script loads, and my orders are triggered for historical data, the orders are sent in (simulated of course at this point). I have tried limiting this several ways, but have not had any luck. I attempted using BARSTATE_ALLBARS (i.e. script is initializing) and only trigger orders when the variable vState == BARSTATE_ALLBARS. However, when I add this code, no orders are triggered at all.


    PHP Code:

    var vLastAlert  = -1;

    function 
    preMain() {

        
    setPriceStudy(false);
        
    setStudyTitle("ADX DI(+) DI(-) v3.1.2");
        
    setCursorLabelName("DI (+)"0);
        
    setCursorLabelName("DI (-)"1);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarFgColor(Color.navy0);
        
    setDefaultBarFgColor(Color.red1);
        
    setDefaultBarThickness(90);
        
    setDefaultBarThickness(91);
        
    setPlotType(PLOTTYPE_HISTOGRAM0);
        
    setPlotType(PLOTTYPE_HISTOGRAM1);
     
        var 
    fp1 = new FunctionParameter("ADXr"FunctionParameter.NUMBER);
            
    fp1.setLowerLimit(0);
            
    fp1.setDefault (3);   
     

    }
    function 
    main(ADXrTrade) {

        var 
    vADXDM = new ADXDMStudy(ADXr);

        if (
            
    vADXDM.getValue(ADXDMStudy.PDI) > vADXDM.getValue(ADXDMStudy.NDI)
           ) 
    onAction1()
           else if (
            
    vADXDM.getValue(ADXDMStudy.PDI) < vADXDM.getValue(ADXDMStudy.NDI)
           ) 
    onAction2();

        return new Array(
            
    vADXDM.getValue(ADXDMStudy.PDI),
            
    vADXDM.getValue(ADXDMStudy.NDI)
            
        );
        
        var 
    nState getBarState();
    }
    function 
    onAction1(nState) {
         
        var 
    vCurrentHour    =   getHour(0);
        var 
    vCurrentMinute  =   getMinute(0)/100;
        var 
    vCurrentTime    =   vCurrentHour vCurrentMinute;
          
        
    setBarBgColor(Color.RGB(192,255,160));
        if (
    vLastAlert != 1Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET .5), Strategy.THISBARStrategy.ALL0);
        if (
    vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET .5), Strategy.THISBARStrategy.ALL0);
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doLong("", (Strategy.MARKET .5), Strategy.THISBAR3001);
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 nState == BARSTATE_ALLBARSbuy(getSymbol());
        
    vLastAlert 1;
       }
    function 
    onAction2(nState) {
           
        var 
    vCurrentHour    =   getHour(0);
        var 
    vCurrentMinute  =   getMinute(0)/100;
        var 
    vCurrentTime    =   vCurrentHour vCurrentMinute;
            
        
    setBarBgColor(Color.RGB(255,255,160));
        if (
    vLastAlert != 2Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime <15.44Strategy.doSell("", (Strategy.MARKET .5), Strategy.THISBARStrategy.ALL0);
        if (
    vCurrentTime 15.44Strategy.doSell("", (Strategy.MARKET .5), Strategy.THISBARStrategy.ALL0);
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doShort("", (Strategy.MARKET -.5), Strategy.THISBAR3001);
        if (
    vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 nState == BARSTATE_ALLBARSsell(getSymbol());
        
    vLastAlert 2;
       } 
    Any help would be greatly appreciated, and thanks for all of the help to date, it has quickly move me forward with EFS.

    Regards,
    ZMendel

  • #2
    zmendel,



    Normally when you are using the same script for historical bar processing/back testing and realtime you need to determine if you are processing historical bars or processing realtime first.

    When setComputeOnClose() is not being used realtime processing can be determined by a combination of getBarState() == BARSTATE_NEWBAR and getCurrenttBarindex() == 0.

    Then a "flag" or global variable is set typically called bRealtime depending on historical or realtime processing determination.
    PHP Code:
    if (getBarState() == BARSTATE_NEWBAR && gerCurrentBarIndex() == 0bRealtime true
    Depending on the setting of that flag, you will either execute the strategy methods or the generic broker calls (buy, sell), but not execute both.

    You will also have to keep track of your position status when running realtime by setting another global variable flag such as vPosition as the Strategy.isLong() functions are not available in realtime, so you need to keep track of you long, short, flat status.

    Regarding the script posted, not sure if Strategy.MARKET +.5
    will work in leiu of a limit order , assuming that was your intent.

    In addition when evaluating on barindex 0 that crossover can flip flop back and forth within the same bar generating a trade each time which may or may not be what you want.

    The DMI is a indicator favorite of mine and I've done work testing it out, and if you had success with this strategy power to you, if you are not happy with the results IMHO may want to add another filter as that crossover can occur too frequently sometimes, especially during periods of low market activity. Alot of people combine it with ADX or some other trend indicator. One thing I found, alot has to do with the volatility. For volatility monsters like OIH it may work fine as it is, for less volatile symbols, you could get hurt.

    Glad to hear you are running simulated, I found it also very useful to run tick replays as that method simulates as closely as possible realtime trading.

    Best of luck, hope this helps and it does well... let us know.


    Glen
    Last edited by demarcog; 11-10-2007, 12:23 PM.
    Glen Demarco
    [email protected]

    Comment


    • #3
      Dear Glen:

      First, thank you very much for your help. It is truly appreciated!
      I have inserted your suggested changes. What I did was:
      1- Created a variable called bRealTime and set it to false.
      2- I inserted your script in function main() that then sets bRealTime to true if we are in "real time" trading (first tick of a new bar).
      3- I added to my buyMarket(), sellMarket() commands the bRealTime valiable to be evaluated.

      It looks like this solves the problem of the multiple orders upon loading up the script. However, no order now triggers whatsoever, even if my statement evaluates as true for a buy() or sell(). I have placed the script below.

      Also, thanks you for the other suggestions (need to watch position size...trading on low volume...). Once I get the orders triggering, I will work these suggestions in. Actually, I have several ideas on how to get around the low volume/false orders. Certainly, no one wants orders triggered that take you in and out of a position quickly. Once I work them in I will show you how I intend to do that.

      You were also correct about the Strategy.MARKET + .5, that does not work. I was trying to add $0.50 to certain Market orders in back testing, which I like very much as well. Back testing tends to be a rather theoretical world, and I was trying to work in some slippage. I took it out for now and will play with it later.

      Here is the code that I mentioned above. Thanks for all your help. Regards, ZMendel

      PHP Code:

      var vLastAlert  = -1;
      var 
      bRealTime   false;

      function 
      preMain() {

          
      setPriceStudy(false);
          
      setStudyTitle("ADX DI(+) DI(-) v3.1.2");
          
      setCursorLabelName("DI (+)"0);
          
      setCursorLabelName("DI (-)"1);
          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarStyle(PS_SOLID1);
          
      setDefaultBarFgColor(Color.navy0);
          
      setDefaultBarFgColor(Color.red1);
          
      setDefaultBarThickness(90);
          
      setDefaultBarThickness(91);
          
      setPlotType(PLOTTYPE_HISTOGRAM0);
          
      setPlotType(PLOTTYPE_HISTOGRAM1);
       
          var 
      fp1 = new FunctionParameter("ADXr"FunctionParameter.NUMBER);
              
      fp1.setLowerLimit(0);
              
      fp1.setDefault (3);   
       

      }
      function 
      main(ADXr) {

          var 
      vADXDM = new ADXDMStudy(ADXr);

          if (
              
      vADXDM.getValue(ADXDMStudy.PDI) > vADXDM.getValue(ADXDMStudy.NDI)
             ) 
      onAction1()
             else if (
              
      vADXDM.getValue(ADXDMStudy.PDI) < vADXDM.getValue(ADXDMStudy.NDI)
             ) 
      onAction2();

          return new Array(
              
      vADXDM.getValue(ADXDMStudy.PDI),
              
      vADXDM.getValue(ADXDMStudy.NDI)
              
          );
          
          if (
      getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == 0bRealTime true;

      }
      function 
      onAction1(bRealTime) {
           
          var 
      vCurrentHour    =   getHour(0);
          var 
      vCurrentMinute  =   getMinute(0)/100;
          var 
      vCurrentTime    =   vCurrentHour vCurrentMinute;
            
          
      setBarBgColor(Color.RGB(192,255,160));
          if (
      vLastAlert != 1Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
          if (
      vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doLong("", (Strategy.MARKET), Strategy.THISBAR3001);
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 bRealTime == truebuyMarket(getSymbol(), 300"SMART");
          
      vLastAlert 1;
         }
      function 
      onAction2(bRealTime) {
             
          var 
      vCurrentHour    =   getHour(0);
          var 
      vCurrentMinute  =   getMinute(0)/100;
          var 
      vCurrentTime    =   vCurrentHour vCurrentMinute;
              
          
      setBarBgColor(Color.RGB(255,255,160));
          if (
      vLastAlert != 2Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime <15.44Strategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
          if (
      vCurrentTime 15.44Strategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doShort("", (Strategy.MARKET), Strategy.THISBAR3001);
          if (
      vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 bRealTime == truesellMarket(getSymbol(), 300"SMART");
          
      vLastAlert 2;
         } 

      Comment


      • #4
        Originally posted by zmendel
        Dear Glen:

        First, thank you very much for your help. It is truly appreciated!
        I have inserted your suggested changes. What I did was:
        1- Created a variable called bRealTime and set it to false.
        2- I inserted your script in function main() that then sets bRealTime to true if we are in "real time" trading (first tick of a new bar).
        3- I added to my buyMarket(), sellMarket() commands the bRealTime valiable to be evaluated.

        It looks like this solves the problem of the multiple orders upon loading up the script. However, no order now triggers whatsoever, even if my statement evaluates as true for a buy() or sell(). I have placed the script below.

        Also, thanks you for the other suggestions (need to watch position size...trading on low volume...). Once I get the orders triggering, I will work these suggestions in. Actually, I have several ideas on how to get around the low volume/false orders. Certainly, no one wants orders triggered that take you in and out of a position quickly. Once I work them in I will show you how I intend to do that.

        You were also correct about the Strategy.MARKET + .5, that does not work. I was trying to add $0.50 to certain Market orders in back testing, which I like very much as well. Back testing tends to be a rather theoretical world, and I was trying to work in some slippage. I took it out for now and will play with it later.

        Here is the code that I mentioned above. Thanks for all your help. Regards, ZMendel

        PHP Code:

        var vLastAlert  = -1;
        var 
        bRealTime   false;

        function 
        preMain() {

            
        setPriceStudy(false);
            
        setStudyTitle("ADX DI(+) DI(-) v3.1.2");
            
        setCursorLabelName("DI (+)"0);
            
        setCursorLabelName("DI (-)"1);
            
        setDefaultBarStyle(PS_SOLID0);
            
        setDefaultBarStyle(PS_SOLID1);
            
        setDefaultBarFgColor(Color.navy0);
            
        setDefaultBarFgColor(Color.red1);
            
        setDefaultBarThickness(90);
            
        setDefaultBarThickness(91);
            
        setPlotType(PLOTTYPE_HISTOGRAM0);
            
        setPlotType(PLOTTYPE_HISTOGRAM1);
         
            var 
        fp1 = new FunctionParameter("ADXr"FunctionParameter.NUMBER);
                
        fp1.setLowerLimit(0);
                
        fp1.setDefault (3);   
         

        }
        function 
        main(ADXr) {

            var 
        vADXDM = new ADXDMStudy(ADXr);

            if (
                
        vADXDM.getValue(ADXDMStudy.PDI) > vADXDM.getValue(ADXDMStudy.NDI)
               ) 
        onAction1()
               else if (
                
        vADXDM.getValue(ADXDMStudy.PDI) < vADXDM.getValue(ADXDMStudy.NDI)
               ) 
        onAction2();

            return new Array(
                
        vADXDM.getValue(ADXDMStudy.PDI),
                
        vADXDM.getValue(ADXDMStudy.NDI)
                
            );
            
            if (
        getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == 0bRealTime true;

        }
        function 
        onAction1(bRealTime) {
             
            var 
        vCurrentHour    =   getHour(0);
            var 
        vCurrentMinute  =   getMinute(0)/100;
            var 
        vCurrentTime    =   vCurrentHour vCurrentMinute;
              
            
        setBarBgColor(Color.RGB(192,255,160));
            if (
        vLastAlert != 1Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
            if (
        vCurrentTime 15.44Strategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doLong("", (Strategy.MARKET), Strategy.THISBAR3001);
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 bRealTime == truebuyMarket(getSymbol(), 300"SMART");
            
        vLastAlert 1;
           }
        function 
        onAction2(bRealTime) {
               
            var 
        vCurrentHour    =   getHour(0);
            var 
        vCurrentMinute  =   getMinute(0)/100;
            var 
        vCurrentTime    =   vCurrentHour vCurrentMinute;
                
            
        setBarBgColor(Color.RGB(255,255,160));
            if (
        vLastAlert != 2Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime <15.44Strategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
            if (
        vCurrentTime 15.44Strategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44Strategy.doShort("", (Strategy.MARKET), Strategy.THISBAR3001);
            if (
        vLastAlert != vCurrentTime 9.29 vCurrentTime 15.44 bRealTime == truesellMarket(getSymbol(), 300"SMART");
            
        vLastAlert 2;
           } 
        ZMendel,

        You are very welcome, happy to help out.

        Not exactly sure why it's not trading I would check that "SMART" route as I thought that was something that is set in the IB Symbol dictionary.

        It would be easier enough to eliminate as the casue by just trying a run a quick test without it.

        Also normally you would use that flag to execute either the strategy methods or the generic broker functions but not both.

        Regarding the low volume, low volatility filters, one easy test is to not take the crossover when ADX is below 20 or definately in the single low teen values as the market is basically doing nothing during those periods and an almost guarantee whipshaw time period.

        I have samples to show you but they do not use the Wizard format. Eventually you may want to use the editor to create you own template rather then the wizard.

        I did find a example that I had which used the wizard format and thought you might find useful.

        I hesitate to post the code because it is not structured well , not intended to be profitableand horrible to read . I was then as you are now just trying to figure out how to code a realtime strategy.

        It should still work....and you can definately use it to get you past this obstacle which is more important. Again not sure exactly the logic issue but using this to make a few changes should move you along.

        Glen
        Glen Demarco
        [email protected]

        Comment


        • #5
          zMendel,

          Here is the code I described, couldn't edit the prior post to edit it.

          PHP Code:

          /* Example of basic strategy to illustrate how to code an strategy EFS that is  Back Test, Real Time/Bars, Real Time/Ticks & tick replay capable*/ 
           
          // variables used to hold flags which control print/display options
          var bDebugPrintEnable true;
          var 
          bTraceEnable true;
          var 
          bTextEnable true;
          var 
          bSoundEnable true;
          var 
          bTradeLogging false;
          var 
          sFilename

          // file variable to log trade information to
          var fTrades = new File("TradeLog.txt");

          // Global variables
          var vPos          =   0;                          // indicates position status, 0, -1, +1                    
          var bRealtime     =   false;                      // true if running realtime - flag determines if strategy object or generic broker functions are used
          var vOSC1         =   osc(520false);          // builtin study
          var vFixedStop    =   .0050;                      // to use for fixed stop logic....note 4 decimals for currencies
          var vFixedProfit  =   .0100;                      // to use for profit target, also 4 decimals
          var bBarTrade     =   false;                      // flag to prevent "false signals" when multiple trades occor on same bar
          var bAllowRevs    =   false;                      // flag to indicate whether to allow reversals based on indicator 
          var vRTTradeCnt     =   0;                        // variable to to track # of RT executions

          function preMain() {
            
              
          setPriceStudy(true);
              
          setStudyTitle("##s0wizard");

          }
          /* Strategy - long when OSC value is above/below zero and high/low current realtime price is above/below high/low of prior indicator or "setup bar"
             currently main is called when all bars are loading, when a bar is complete, and tick by tick in realtime.                                       */
          function main() {      
           
            if (
          getBarState() === BARSTATE_NEWBAR && getCurrentBarIndex() === 0bRealtime true;

            if (
          vOSC1.getValue(-2) == null) return;                                    // make sure indicator initializes
            
            
          if (vOSC1.getValue(-1) > && high(0) > high(-1)) LongCond();
            
              else if ( 
          vOSC1.getValue(-1) <= && low(0) < low(-1)) ShortCond();
            
            if ( 
          vPos === 1)  setBarBgColor(Color.RGB(0,128,128));
            if ( 
          vPos === -1setBarBgColor(Color.RGB(255,0,0));
            
              return 
          null;
          }

          function 
          postMain() {
            
          }
          //* note strategy assumes no price gaps as stop/entry price will always be recorded by strategy object as valid.
          function LongCond() {
              if (
          vPos != )       // If we are not already Long
              
          {
                   
          debugPrintln("LongCond @: Bar#=" +getCurrentBarCount() + " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond() +
                  
          " OSC="+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " high(0)=" high(0)+ 
                   
          " high(-1)=" high(-1) + " RT="+bRealtime " IX=" +getCurrentBarIndex() + " BarState=" +getBarState() + " TradesDone = " +vRTTradeCnt);
                   
                   if (
          vPos != && !bRealtime){ Strategy.doLong(""Strategy.STOPStrategy.THISBARStrategy.DEFAULT, high(-1));vRTTradeCnt++; debugPrintln("BT Long @" +high(-1) + " +1 Tick" " Pos= " +vPos +" SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                   if (
          vPos == && bRealtime && getCurrentBarIndex() === ){ buyMarket(getSymbol(), 100); vRTTradeCnt++; debugPrintln(" Exec GB buyMarket X 1 - Long @" +high(0) +  " Pos=" +vPos +" SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear()+ " " getHour() + ":" +getMinute() + ":" getSecond()  + " Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                   if (
          vPos == -&& bRealtime && getCurrentBarIndex() === ){ buyMarket(getSymbol(), 100);vRTTradeCnt++;  buyMarket(getSymbol(), 100);vRTTradeCnt++; debugPrintln(" Exec GB buyMarket X 2 - Long @" +high(0) + " Pos=" +vPos +  " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                     
                   
          vPos 1;
              }  
          // end if not already long - if ( vPos != 1) 
              
          // LongCond Function End
             
          function ShortCond() {
             if (
          vPos != -)
             {
                   
          PrintDebugInfo(); //test debug print
                   
          debugPrintln("ShortCond @: Bar#= " +getCurrentBarCount() + " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " " getHour() + ":" +getMinute() + ":" getSecond()  +" OSC= "+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " low(0)=" low(0)+ 
                   
          " low(-1)=" low(-1) + " RT= "+bRealtime " IX=" +getCurrentBarIndex()+ " BarState=" +getBarState() + " TradesDone=" +vRTTradeCnt);
                    
                   if (
          vPos != -&& !bRealtime) {Strategy.doShort(""Strategy.STOPStrategy.THISBARStrategy.DEFAULT, low(-1));vRTTradeCnt++;debugPrintln("BT Short @" +low(-1)  + " -1 Tick"" Pos= " +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond() +" Bar#=" +getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                   if (
          vPos == 0  && bRealtime && getCurrentBarIndex() === ){ sellShortMarket(getSymbol(), 100);vRTTradeCnt++;debugPrintln(" Exec GB SellShortMarket X 1 - Short @" +low(0) +" Pos=" +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear()+ " " getHour() + ":" +getMinute() + ":" getSecond()  + " Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                   if (
          vPos == +&& bRealtime && getCurrentBarIndex() === ){ sellMarket(getSymbol(), 100);vRTTradeCnt++; sellShortMarket(getSymbol(), 100);vRTTradeCnt++;debugPrintln(" Exec GB SellMarket X 1 & SellShortMarket X 1 - Short @" +low(0) +" Pos=" +vPos " SYM=" +getSymbol() +  ", " getMonth() + "/" getDay() + "/" getYear() + " "getHour() + ":" +getMinute() + ":" getSecond()  +" Bar#= "+getCurrentBarCount()+ " TradesDone=" +vRTTradeCnt);}
                   
                   
          vPos = -1;
                           
              }  
          // end if not already short - if ( vPos != -1) 
             
            
          // ShortCond Function End
             
          function PrintDebugInfo() {

            
          debugPrintln" SYM=" +getSymbol() + " Int= " getInterval() + " Bar#=" +getCurrentBarCount() +  " RT=" +bRealtime " IX=" +getCurrentBarIndex()+
                         
          " BarState=" +getBarState() +  ", " getMonth() + "/" getDay() + "/" getYear() + 
                         + 
          " "getHour() + ":" +getMinute() + ":" getSecond() +
                        
          " OSC="+vOSC1.getValue(-1).toFixed(4) + " vPos=" +vPos " high(0)=" high(0)+ 
                         
          " high(-1)=" high(-1) + " low(0)=" low(0)+ 
                         
          " low(-1)=" low(-1) +  " TradesDone = " +vRTTradeCnt);
          // end of PrintDebugInfo function 
          Glen Demarco
          [email protected]

          Comment


          • #6
            Dear Glen:

            I have gone through your code and have added in what looks to be the key items to execute the trade. However, I still don't trigger the buyMarket() or SellMarket(). Any thoughts on what I might be missing?

            Thanks again, ZMendel

            PHP Code:

            var vLastAlert  = -1;
            var 
            bRealTime   false;

            function 
            preMain() {

                
            setPriceStudy(false);
                
            setStudyTitle("ADX DI(+) DI(-) v3.1.2");
                
            setCursorLabelName("DI (+)"0);
                
            setCursorLabelName("DI (-)"1);
                
            setDefaultBarStyle(PS_SOLID0);
                
            setDefaultBarStyle(PS_SOLID1);
                
            setDefaultBarFgColor(Color.navy0);
                
            setDefaultBarFgColor(Color.red1);
                
            setDefaultBarThickness(90);
                
            setDefaultBarThickness(91);
                
            setPlotType(PLOTTYPE_HISTOGRAM0);
                
            setPlotType(PLOTTYPE_HISTOGRAM1);
             
                var 
            fp1 = new FunctionParameter("ADXr"FunctionParameter.NUMBER);
                    
            fp1.setLowerLimit(0);
                    
            fp1.setDefault (3);   
             

            }
            function 
            main(ADXr) {

                if (
            getBarState() === BARSTATE_NEWBAR && getCurrentBarIndex() === 0bRealTime true;

                var 
            vADXDM = new ADXDMStudy(ADXr);

                if (
                    
            vADXDM.getValue(ADXDMStudy.PDI) > vADXDM.getValue(ADXDMStudy.NDI)
                   ) 
            onAction1()
                   else if (
                    
            vADXDM.getValue(ADXDMStudy.PDI) < vADXDM.getValue(ADXDMStudy.NDI)
                   ) 
            onAction2();

                return new Array(
                    
            vADXDM.getValue(ADXDMStudy.PDI),
                    
            vADXDM.getValue(ADXDMStudy.NDI)
                    
                );
                
            }
            function 
            onAction1() {
                 
                var 
            vCurrentHour    =   getHour(0);
                var 
            vCurrentMinute  =   getMinute(0)/100;
                var 
            vCurrentTime    =   vCurrentHour vCurrentMinute;
                  
                
            setBarBgColor(Color.RGB(192,255,160));
                if (
            vLastAlert != 1Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime 15.44 && !bRealTimeStrategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
                if (
            vCurrentTime 15.44 & !bRealTimeStrategy.doCover("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime 15.44 && !bRealTimeStrategy.doLong("", (Strategy.MARKET), Strategy.THISBAR3001);
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime 15.44 && bRealTime && getCurrentBarIndex === 0buyMarket(getSymbol(), 300"SMART");
                
            vLastAlert 1;
               }
            function 
            onAction2() {
                   
                var 
            vCurrentHour    =   getHour(0);
                var 
            vCurrentMinute  =   getMinute(0)/100;
                var 
            vCurrentTime    =   vCurrentHour vCurrentMinute;
                    
                
            setBarBgColor(Color.RGB(255,255,160));
                if (
            vLastAlert != 2Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime <15.44 && !bRealTimeStrategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
                if (
            vCurrentTime 15.44Strategy.doSell("", (Strategy.MARKET), Strategy.THISBARStrategy.ALL0);
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime 15.44 && !bRealTimeStrategy.doShort("", (Strategy.MARKET), Strategy.THISBAR3001);
                if (
            vLastAlert != && vCurrentTime 9.29 && vCurrentTime 15.44 && bRealTime && getCurrentBarIndex === 0sellMarket(getSymbol(), 300"SMART");
                
            vLastAlert 2;
               } 

            Comment


            • #7
              Zmendel,

              I'm trading and debugging my own code so I don't have alot of time to actually take you code and debug it myself right now. Plus it's actually something that you may find beneficial as it's always a necessary part of any strategy developed. Until I get some time you can try the following suggestions.

              One question first, does it generate any trades when backtesting?

              If it does then it should be easier to figure out. If not then it's a bigger logic problem.

              Did you try it without the "SMART" designation?



              Usually the only way to know for sure is to use the debugPrintln() statement.

              Because these onAction functions only contain a few lines each, I would add a debugPrintln("at line xxx") to one statment at a time.

              This way you will see what conditional statements are being executed, ie., if this is what you are expecting to occur.

              If not then you can focus you attention to whatever variable is causing that statment not to execute, ie., debugPrintln("vLastalert=" + vLastalert) to see if the vLastalert is what you expect.
              Glen Demarco
              [email protected]

              Comment

              Working...
              X