Announcement

Collapse
No announcement yet.

Newbie Needs help with strategy.golong

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

  • Newbie Needs help with strategy.golong

    Hi,
    I've created the following code to add alerts and text when a long trade should be made but only when it is not already in trade.

    the problem is it generates the alerts and text for every bar that crosses the bollinger band.

    I'm not sure if there is a problem with setting the trades or the stategy.long part of the code
    please help


    function ntrade() {
    if (
    close() > BollHigh.getValue(BollingerStudy.UPPER)
    && close() > open()
    && !Strategy.isLong()
    ){
    LS = open();
    Alert.addToList(getSymbol(), "BB Pi: Long: Stop @ "+LS, Color.RGB(128,255,128), Color.RGB(0,128,0));
    Alert.playSound("d:\\Program Files\\eSignal\\Sounds\\GoLong.wav");
    drawTextRelative(0, low()-.5, "L", Color.RGB(0,0,0), Color.RGB(255,0,255), Text.LEFT, "Arial", 8);
    Strategy.doLong("", Strategy.OPEN(+1), Strategy.NEXTBAR, Strategy.DEFAULT, 0);


    // Strategy.setStop(LS);

    vLastAlert = 1;
    }
    }

  • #2
    Jacquesl
    You have an incorrect syntax in the Strategy.doLong() command. Strategy.OPEN(+1) is not valid and should be Strategy.MARKET. The price will be that of the next bar as defined by Strategy.NEXTBAR.
    Alex

    Comment


    • #3
      Thanks Alex,

      I've fixed it but it still adds the alerts to each bar as if it is not in a trade??? instead of just the first one. i want it to open the trade and then close it on further conditions but it seems to open trades every time (as if it ignores the && !Strategy.isLong() part of the conditions)

      Jacques

      Comment


      • #4
        Jacques
        Are you running this in real time? If so you cannot use Strategy.NEXTBAR since the efs has no way of knowing what the price of the next bar will be hence it does not execute the Strategy.doLong() command.
        What you need to do is check if at a completed bar the conditions exist at the prior bar at which point you apply Strategy.MARKET, Strategy.THISBAR
        Alex

        Comment


        • #5
          Alex,

          the changes make sense, not sure what the effect is of the replay mode. I still get the alerts on all the bars that meet the coditions but ignore the && !Strategy.isLong() condition

          New code looks like this:

          var BollHigh = new BollingerStudy(20, "High", 2);
          var vLastAlert = -1;





          function preMain() {
          /**
          * This function is called only once, before any of the bars are loaded.
          * Place any study or EFS configuration commands here.
          */



          setPriceStudy(true);
          setStudyTitle("Bollinger Band pierce");
          setComputeOnClose(true);

          if (
          (Strategy.isShort())
          ) Strategy.doCover("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
          else if (
          (Strategy.isLong())
          ) Strategy.doSell("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);




          }

          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.
          */
          var LS = null;




          if (!Strategy.isInTrade()){

          ntrade();
          }

          return null;
          }

          function postMain() {


          }


          function ntrade() {
          if (
          close() > BollHigh.getValue(BollingerStudy.UPPER)
          && close() > open()
          && !Strategy.isLong()
          ){
          LS = open();
          Alert.addToList(getSymbol(), "BB Pi: Long: Stop @ "+LS, Color.RGB(128,255,128), Color.RGB(0,128,0));
          Alert.playSound("d:\\Program Files\\eSignal\\Sounds\\GoLong.wav");
          drawTextRelative(0, low()-.5, "L", Color.RGB(0,0,0), Color.RGB(255,0,255), Text.LEFT, "Arial", 8);
          Strategy.doLong("", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);


          Strategy.setStop(LS);

          vLastAlert = 1;
          }
          }

          Comment


          • #6
            Jacques
            You did not implement the changes I suggested which is why you still get alerts triggered on every bar. Regardless there are too many logic errors in the the script to attempt fixing that one issue. I would suggest that you start from scratch and write the study using only the Formula Wizard (at least initially). Once you have done that post the script and we can proceed from there.
            I would also suggest that you go through some of the Help Guides available in the EFS KnowledgeBase and in particular the one on the Formula Wizard.
            Alex

            Comment

            Working...
            X