Announcement

Collapse
No announcement yet.

limit orders and back testing

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

  • limit orders and back testing

    The below code behaves in a way I cannot explain.
    The limit orders (sometimes) get filled, which is what I coded,
    but they close immediately after one bar.

    Anybody has got an idea??

    var study = new MAStudy(55, 0, "Close", MAStudy.EXPONENTIAL);
    var vEntryLevel =0;
    var vLimitLong;
    var vLimitShort;
    function preMain() {
    setPriceStudy(false);
    }

    function main() {
    var v = study.getValue(MAStudy.MA);
    vLimitLong = close() + (high() - low())*2/3;
    vLimitShort = close + (high() -low()) /3;
    if(v == null)
    return;
    //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)

    if(close() >= v)
    {
    if(Strategy.isShort)
    {
    Strategy.doCover("cover", Strategy.CLOSE,Strategy.NEXTBAR);
    }
    if(!Strategy.isLong())
    // we try to open one at the limit level
    {
    Strategy.doLong("limit long", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitLong);
    }
    }
    if(close() < v)
    {
    if(Strategy.isLong)
    {
    Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
    }
    if(!Strategy.isShort())
    {
    // we try to short one at the limit level
    Strategy.doShort("limit short", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitShort);
    }
    }

    if(Strategy.isLong())
    {
    setBarBgColor(Color.green);
    }
    else if(Strategy.isShort())
    {
    setBarBgColor(Color.red);
    }

    return(vLimitLong);
    }

  • #2
    Solution...

    You have no trade logic in your code. Your code is simply trying to BUY and SELL with the CLOSE is above or below the MA. On a RT chart, the market price (close) may move above and below the MA multiple times as it trades.

    One solution is to add a time stamp to the code. This allows the trading action to only act ONCE per bar. Here is an example...

    PHP Code:
    var study = new MAStudy(550"Close"MAStudy.EXPONENTIAL);
    var 
    vEntryLevel =0;
    var 
    vLimitLong;
    var 
    vLimitShort;
    var 
    TradeActionTimeStamp 0;

    function 
    preMain() {
      
    setPriceStudy(false);
    }

    function 
    main() {
    var 
    study.getValue(MAStudy.MA);
    vLimitLong close() + (high() - low())*2/3;
    vLimitShort close + (high() -low()) /3;
    if(
    == null)
    return;
    //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit) 

    if(close() >= v) {
     if(
    Strategy.isShort) {
      
    Strategy.doCover("cover"Strategy.CLOSE,Strategy.NEXTBAR);
     }
     if (!
    Strategy.isLong()) && (TradeActionTimeStamp != getValue("rawtime"0)) ) { // we try to open one at the limit level
      
    TradeActionTimeStamp getValue("rawtime"0);
      
    Strategy.doLong("limit long"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitLong);
     }
    }

    if(
    close() < v) {
     if(
    Strategy.isLong) {
      
    Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
     }
     if ((!
    Strategy.isShort() && (TradeActionTimeStamp != getValue("rawtime"0)) ) {  // we try to short one at the limit level
      
    TradeActionTimeStamp getValue("rawtime"0);
      
    Strategy.doShort("limit short"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitShort);
     }
    }

    if(
    Strategy.isLong()) {
     
    setBarBgColor(Color.green);
    } else if(
    Strategy.isShort()) {
     
    setBarBgColor(Color.red);
    }

    return(
    vLimitLong);

    This Time Stamp action will prevent the system from entering new trades on the same bar (or flipping positions).

    If you want to add it to the EXITS, you would simply modify the code as follows.. All I did was add a condition to your exit code...

    The condition is "don't allow an exit on the same bar we entered on".

    PHP Code:
    var study = new MAStudy(550"Close"MAStudy.EXPONENTIAL);
    var 
    vEntryLevel =0;
    var 
    vLimitLong;
    var 
    vLimitShort;
    var 
    TradeActionTimeStamp 0;

    function 
    preMain() {
      
    setPriceStudy(false);
    }

    function 
    main() {
    var 
    study.getValue(MAStudy.MA);
    vLimitLong close() + (high() - low())*2/3;
    vLimitShort close + (high() -low()) /3;
    if(
    == null)
    return;
    //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit) 

    if(close() >= v) {
     if ((
    Strategy.isShort) && (TradeActionTimeStamp getValue("rawtime"0)) {
      
    Strategy.doCover("cover"Strategy.CLOSE,Strategy.NEXTBAR);
     }
     if (!
    Strategy.isLong()) && (TradeActionTimeStamp != getValue("rawtime"0)) ) { // we try to open one at the limit level
      
    TradeActionTimeStamp getValue("rawtime"0);
      
    Strategy.doLong("limit long"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitLong);
     }
    }

    if(
    close() < v) {
     if ((
    Strategy.isLong) && (TradeActionTimeStamp getValue("rawtime"0)) {
      
    Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
     }
     if ((!
    Strategy.isShort() && (TradeActionTimeStamp != getValue("rawtime"0)) ) {  // we try to short one at the limit level
      
    TradeActionTimeStamp getValue("rawtime"0);
      
    Strategy.doShort("limit short"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitShort);
     }
    }

    if(
    Strategy.isLong()) {
     
    setBarBgColor(Color.green);
    } else if(
    Strategy.isShort()) {
     
    setBarBgColor(Color.red);
    }

    return(
    vLimitLong);

    If you need more help, let me know.

    Brad
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Affraid this does not bring me forward.
      Have recitified some syntax errors,
      tried the changes,
      the result is still very narrow green or red bands below the chart (for the long and short entries).
      One thing I did notice is that if you leave it running on the screen it apparently behaves OK. When you hit refress the narrow bands are back. The changed code is belo:

      var study = new MAStudy(55, 0, "Close", MAStudy.EXPONENTIAL);
      var vEntryLevel =0;
      var vLimitLong;
      var vLimitShort;
      var TradeActionTimeStamp = 0;

      function preMain() {
      setPriceStudy(false);
      }

      function main() {
      var v = study.getValue(MAStudy.MA);
      vLimitLong = close() + (high() - low())*2/3;
      vLimitShort = close + (high() -low()) /3;
      if(v == null)
      return;
      //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)

      if(close() >= v)
      {
      if(Strategy.isShort)
      {
      Strategy.doCover("cover", Strategy.CLOSE,Strategy.NEXTBAR);
      }
      if ((!Strategy.isLong()) && (TradeActionTimeStamp != getValue("rawtime", 0)) )
      { // we try to open one at the limit level
      TradeActionTimeStamp = getValue("rawtime", 0);
      Strategy.doLong("limit long", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitLong);
      }
      }

      if(close() < v)
      {
      if(Strategy.isLong)
      {
      Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
      }
      if ((!Strategy.isShort()) && (TradeActionTimeStamp != getValue("rawtime", 0)) )
      { // we try to short one at the limit level
      TradeActionTimeStamp = getValue("rawtime", 0);
      Strategy.doShort("limit short", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitShort);
      }
      }

      if(Strategy.isLong())
      {
      setBarBgColor(Color.green);
      }
      else if(Strategy.isShort())
      {
      setBarBgColor(Color.red);
      }

      return(vLimitLong);
      }

      Comment


      • #4
        Check your math....

        Please check your math for your entry prices... I corrected the following functions by adding "()" around the necessary calculations.

        var vLimitLong = ((close() + (high()-low()))*2)/3;
        var vLimitShort = (close() + (high()-low())) /3;

        But, they appear to be wrong.....

        The numbers calculated are between 40% and 80% below the current market price. Thus, your math MUST be wrong.

        Here is the code I've been working on...

        PHP Code:
        var study = new MAStudy(550"Close"MAStudy.EXPONENTIAL);
        var 
        vEntryLevel =0;
        var 
        vLimitLong;
        var 
        vLimitShort;
        var 
        TradeActionTimeStamp 0;

        function 
        preMain() {
        setPriceStudy(true);
            
        setCursorLabelName("HighBO",0);
            
        setCursorLabelName("LowBO",1);

        }

        function 
        main() {
        var 
        study.getValue(MAStudy.MA);
        var 
        vLimitLong = ((close() + (high()-low()))*2)/3;
        var 
        vLimitShort = (close() + (high()-low())) /3;
        if(
        == null)
        return;
        //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit) 

        if(close() >= v
        {
        if(
        Strategy.isShort
        {
        Strategy.doCover("cover"Strategy.CLOSE,Strategy.NEXTBAR);
        }
        if ((!
        Strategy.isLong()) && (TradeActionTimeStamp != getValue("rawtime"0)) ) 
        // we try to open one at the limit level
        TradeActionTimeStamp getValue("rawtime"0);
        Strategy.doLong("limit long"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitLong);
        }
        }

        if(
        close() < v
        {
        if(
        Strategy.isLong
        {
        Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
        }
        if ((!
        Strategy.isShort()) && (TradeActionTimeStamp != getValue("rawtime"0)) ) 
        // we try to short one at the limit level
        TradeActionTimeStamp getValue("rawtime"0);
        Strategy.doShort("limit short"Strategy.LIMITStrategy.NEXTBARStrategy.getDefaultLotSize(), vLimitShort);
        }
        }

        if(
        Strategy.isLong()) 
        {
        setBarBgColor(Color.RGB(0xE00xFF0xE0));

        else if(
        Strategy.isShort()) 
        {
        setBarBgColor(Color.RGB(0xFF0xE00xE0));
        }

        return new Array(
        vLimitLongvLimitShort);

        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          Don't know how or when these brackets got on the wrong place.
          The intention was:
          var vLimitLong = (close() + (high()-low()) *2/3);
          var vLimitShort = close() + (high()-low()) /3;
          or this:
          var vRange = high() -low();
          var vLimitLong = close() + vRange*2/3;
          var vLimitShort = close +vRange/3;
          At present the value is not that important, what I am trying to find out is how can I make the limit orders behave like limit orders for real.
          the corrected code (still behaving in the same funny way) is below:

          var study = new MAStudy(55, 0, "Close", MAStudy.EXPONENTIAL);
          var vEntryLevel =0;
          var vLimitLong;
          var vLimitShort;
          var TradeActionTimeStamp = 0;

          function preMain() {
          setPriceStudy(true);
          setCursorLabelName("HighBO",0);
          setCursorLabelName("LowBO",1);

          }

          function main() {
          var v = study.getValue(MAStudy.MA);
          var vLimitLong = (close() + (high()-low()) *2/3);
          var vLimitShort = close() + (high()-low()) /3;
          var vRange = high() -low();

          if(v == null)
          return;
          //Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)

          if(close() >= v)
          {
          if(Strategy.isShort)
          {
          Strategy.doCover("cover", Strategy.CLOSE,Strategy.NEXTBAR);
          }
          if ((!Strategy.isLong()) && (TradeActionTimeStamp != getValue("rawtime", 0)) )
          { // we try to open one at the limit level
          TradeActionTimeStamp = getValue("rawtime", 0);
          Strategy.doLong("limit long", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitLong);
          }
          }

          if(close() < v)
          {
          if(Strategy.isLong)
          {
          Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
          }
          if ((!Strategy.isShort()) && (TradeActionTimeStamp != getValue("rawtime", 0)) )
          { // we try to short one at the limit level
          TradeActionTimeStamp = getValue("rawtime", 0);
          Strategy.doShort("limit short", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitShort);
          }
          }

          if(Strategy.isLong())
          {
          setBarBgColor(Color.RGB(0xE0, 0xFF, 0xE0));
          }
          else if(Strategy.isShort())
          {
          setBarBgColor(Color.RGB(0xFF, 0xE0, 0xE0));
          }

          return new Array(vLimitLong, vLimitShort);
          }
          PHP Code:
          [CODE][/CODE
          [CODE][PHP]

          Comment

          Working...
          X