Announcement

Collapse
No announcement yet.

Code structure + a couple more questions

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

  • Code structure + a couple more questions

    Hi,

    I have performed a quick search on here and the KnowledgeBase for a guide/pointers on the "correct" way to structure/indent code. Unfortunately with no luck. Does anyone have any suggestions?

    Also in the following code;

    1) Will nTradeEntryPrice obtain the purchase price of my trade?
    2) And I want to use the atr value, (var atrLevel) at the point when the purchase is made, as the basis of my stop loss point. Is this calculated in the correct section of the code?

    Thanks for any help in advance.
    -----------------------------------------------------------------------------------
    PHP Code:
    var vDonch null;
    var 
    vDonch2 null;
    var 
    nSignal;
    var 
    nTradeEntryPrice;
    var 
    nStopLevel;
    var 
    atrLevel;
    var 
    inTrade;
    var 
    nNewTrade;

    function 
    preMain() {

        
    setPriceStudy(true);
        
    setStudyTitle("Donchian");
        
    setCursorLabelName("20 Day High"0);
        
    setCursorLabelName("20 Day Low"1);
        
    setCursorLabelName("10 day High Exit"2);
        
    setCursorLabelName("10 day Low Exit"3);
        
    setColorPriceBars(true);
        
    setDefaultBarFgColor (Colorblue0);
        
    setDefaultBarFgColor (Colorred1);
        
    setDefaultBarFgColor (Colorgreen2);
        
    setDefaultBarFgColor (Colorgreen3);
        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setDefaultBarThickness(12);
        
    setDefaultBarThickness(13);
        
    setDefaultPriceBarColor(Color.black);

    }

    function 
    main() {

        if (
    vDonch == nullvDonch = new DonchianStudy (20,0);
        if (
    vDonch2 == nullvDonch2 = new DonchianStudy (10,0);
        

    // identify entry price

    if (nNewTrade == 1) {
        
    nTradeEntryPrice open();
        
    nNewTrade 0;
        
    atrLevel atr(20);
        
    inTrade 1;

        if (
    Strategy.isLong() == truesetPriceBarColor(Color.green);
        if (
    Strategy.isShort() == truesetPriceBarColor(Color.red);

    }

    // stop loss/profit exit

    if (inTrade == && nSignal 0) {
        if (
    low() <= (nTradeEntryPrice nStopLevel)) {

        
    Strategy.doSell("Stop Sell"Strategy.LIMITStrategy.THISBARStrategy.ALLnStopLevel);

        
    inTrade 0
        }
    }

    else if (
    low() <= vDonch2.getValue(DonchianStudy.LOWER, -1)) { 
        var 
    longExit vDonch2.getValue(DonchianStudy.LOWER, -1);
        if (
    open() < longExitlongExit open(); {

        
    Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,longExit);

        
    inTrade 0;  
        }
    }

    if (
    inTrade == && nSignal 0) {
        if (
    high() >= (nTradeEntryPrice nStopLevel)) {

        
    Strategy.doCover("Cover",Strategy.LIMITStrategy,THISBAR,Strategy.ALL,nStopLevel);

        
    inTrade 0;
        }
    }

    else if (
    high() >= vDonch2.getValue(DonchianStudy.UPPER, -1)) {
        var 
    shortExit vDonch2.getValue(DonchianStudy.UPPER, -1);
        if (
    open() > shortExitshortExit open(); {

    Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,shortExit);

        
    inTrade 0;
        }
    }

    //identify entry signal

    if (inTrade == 0) {
    if (
    high() >= vDonch.getValue(DonchianStudy.UPPER, -1)) {
        
    nSignal 1;
        
    nNewTrade 1;
        }
    }

    if (
    inTrade == 0) {
    if (
    low() <= vDonch.getValue(DonchianStudy.LOWER, -1)) {
        
    nSignal = -1;
        
    nNewTrade 1;
        }
    }

    //execute trade

    if  ((nNewTrade == 1) && (nSignal 0)) {

        
    Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
        
    nStopLevel nTradeEntryPrice -  (atrLevel 2);
        
    }

    if  ((
    nNewTrade == 1) && (nSignal 0)) {

        
    Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBAR);
        
    nStopLevel nTradeEntryPrice +  (atrLevel 2);
        
    }

    return new Array 

    (
    vDonch.getValue (DonchianStudy.UPPER),
    vDonch.getValue (DonchianStudy.LOWER),
    vDonch2.getValue (DonchianStudy.UPPER),
    vDonch2.getValue (DonchianStudy.LOWER));



  • #2
    theStranger,

    Here are two links regarding some standards used in writing code ...


    CamelCase - http://en.wikipedia.org/wiki/Intercap

    Programming Style - http://en.wikipedia.org/wiki/Coding_standard

    Comment


    • #3
      Re: Code structure + a couple more questions

      Hello thestranger,

      Originally posted by thestranger
      Hi,

      I have performed a quick search on here and the KnowledgeBase for a guide/pointers on the "correct" way to structure/indent code. Unfortunately with no luck. Does anyone have any suggestions?

      Also in the following code;

      1) Will nTradeEntryPrice obtain the purchase price of my trade?
      2) And I want to use the atr value, (var atrLevel) at the point when the purchase is made, as the basis of my stop loss point. Is this calculated in the correct section of the code?

      Thanks for any help in advance.
      -----------------------------------------------------------------------------------
      1) Yes, because your entry calls are using MARKET/NEXTBAR constants. Keep in mind this is only during the back test.
      2) The location where you're setting the value of atrLevel is incorrect. What is happening is that the atrLevel will be using the value of the atr at the close of the bar following the previous trade signal. You should move the assignment for atrLevel to the top of main outside of any conditional statements. Also, using the following would be the proper way to get the value you need from the atr series.

      atrLevel = atr(20, 0);
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment


      • #4
        Thanks a lot for the links stevehare2003. Hopefully i can improve my structuring.

        And thanks once again JasonK. Just one more question on my code. In relation to the atr(20, 0) series. Should i declare atrLevel as null in the global variables and then put if (atrLevel == null) atrLevel = atr(20, 0) in the first part of the main; Or is this not necessary?

        I'm thinking the same for the donchian study. Is this null statement necessary? Or can i simply put vDonch = new DonchianStudy (20, 0);

        Thanks

        Comment


        • #5
          Hello thestranger,

          Originally posted by thestranger
          Thanks a lot for the links stevehare2003. Hopefully i can improve my structuring.

          And thanks once again JasonK. Just one more question on my code. In relation to the atr(20, 0) series. Should i declare atrLevel as null in the global variables and then put if (atrLevel == null) atrLevel = atr(20, 0) in the first part of the main; Or is this not necessary?
          No. Remember this rule of thumb when working with the EFS2 built-in studies. If you specify a bar index in the parameter list, the return result will be a single value. If you do not ask for a specific bar index, a Series Object of the study will be created and assigned to your variable, or container.

          var atrLevel = atr(20, 0); // This retrieves one value

          var xAtr = atr(20); // Creates a Series Object of the atr() study.

          As it pertains to your specific formula, you do not have any need for using the Series of the atr, so just retrieve the specific bar's value that you need. It would not make much difference if atrLevel were global or local. Local will work just fine.

          If you were to do this:

          PHP Code:
          var atrLevel null;

          function 
          main() {
              if (
          atrLevel == nullatrLevel atr(200);
              ....

          atrLevel would be set to the first (oldest) bar's value of the atr() study and would never change. If you wanted to work with the Series Object of the atr() study, this is how you would do it.

          PHP Code:
          var xAtr null;  // This will contain a Series Object.

          function main() {
              if (
          xAtr == nullxAtr atr(20);  // this happens only once.

              
          var atrLevel xAtr.getValue(0);  // retrieve the current value of the xAtr study.
              
          ....

          The above accomplishes the same thing as just asking for the bar index of the atr() study. It's just a little more code.


          I'm thinking the same for the donchian study. Is this null statement necessary? Or can i simply put vDonch = new DonchianStudy (20, 0);

          Thanks
          You're on the right track, however, you're using the EFS1 Donchian study object, which does not create a Series Object. You should use the following.

          PHP Code:
          function main() {
              var 
          nDonch_Upper  upperDonchian(200);  // current value
              
          var nDonch_Upper2 upperDonchian(100);  // current value
              
          var nDonch_Lower  lowerDonchian(200);  // current value
              
          var nDonch_Lower2 lowerDonchian(100);  // current value

              // use the above variables in your conditional statements

              
          ....

              return new Array(
          nDonch_UppernDonch_LowernDonch_Upper2nDonch_Lower2);

          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            JasonK you have been a great help again. I think i'm slowly picking things up. I didnt pick up the differences between EFS1 & EFS2.

            Comment


            • #7
              I spoke too soon

              I have made the changes you suggested. However i now get an error "nDonch_Lower2 has no properties". I tried to dissect the code to just use the entry section, still no luck. It seems to me that the donchian values are not being delivered. After having a quick search on the forum i noticed a similar thread. I therefore tried to insert a line of code directly after this part;

              var nDonch_Upper = upperDonchian (20, 0);
              var nDonch_Upper2 = upperDonchian (10, 0);
              var nDonch_Lower = lowerDonchian (20, 0);
              var nDonch_Lower2 = lowerDonchian (10, 0);

              if(nDonch_Upper || nDonch_Upper2 || nDonch_Lower || nDonch_Lower2 == null) return;

              Although it removed the error in the syntax check my chart is blank and no entries are triggered. I tried a few variations of the above but without success.

              Any ideas?

              PHP Code:
              var nSignal;
              var 
              nTradeEntryPrice;
              var 
              nStopLevel;
              var 
              atrLevel null;
              var 
              inTrade;
              var 
              nNewTrade;

              function 
              preMain() {

                  
              setPriceStudy(true);
                  
              setStudyTitle("Donchian");
                  
              setCursorLabelName("20 Day High"0);
                  
              setCursorLabelName("10 Day High"1);
                  
              setCursorLabelName("20 day Low"2);
                  
              setCursorLabelName("10 day Low"3);
                  
              setColorPriceBars(true);
                  
              setDefaultBarFgColor (Color.blue0);
                  
              setDefaultBarFgColor (Color.red1);
                  
              setDefaultBarFgColor (Color.green2);
                  
              setDefaultBarFgColor (Color.green3);
                  
              setDefaultBarThickness(10);
                  
              setDefaultBarThickness(11);
                  
              setDefaultBarThickness(12);
                  
              setDefaultBarThickness(13);
                  
              setDefaultPriceBarColor(Color.black);

              }

              function 
              main() {

                  var 
              nDonch_Upper upperDonchian (200);
                  var 
              nDonch_Upper2 upperDonchian (100);
                  var 
              nDonch_Lower  lowerDonchian (200);
                  var 
              nDonch_Lower2 lowerDonchian (100);
                  if (
              atrLevel == nullatrLevel atr(200);
                  

              // identify entry price

              if (nNewTrade == 1) {
                  
              nTradeEntryPrice open();
                  
              nNewTrade 0;
                  
              inTrade 1;

              }

              // stop loss/profit exit

              if (inTrade == && nSignal 0) {
                  if (
              low() <= (nTradeEntryPrice nStopLevel)) {

                  
              Strategy.doSell("Stop Sell"Strategy.LIMITStrategy.THISBARStrategy.ALL,nStopLevel);

                  
              inTrade 0
                  }
              }

              else if (
              low() <= nDonch_Lower2.getValue(-1)) { 
                  var 
              longExit nDonch_Lower2.getValue(-1);
                  if (
              open() < longExitlongExit open(); {

                  
              Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,longExit);

                  
              inTrade 0;  
                  }
              }

              if (
              inTrade == && nSignal 0) {
                  if (
              high() >= (nTradeEntryPrice nStopLevel)) {

                  
              Strategy.doCover("Cover",Strategy.LIMITStrategy.THISBAR,Strategy.ALL,nStopLevel);

                  
              inTrade 0;
                  }
              }

              else if (
              high() >= nDonch_Upper2.getValue(-1)) {
                  var 
              shortExit nDonch_Upper2.getValue(-1);
                  if (
              open() > shortExitshortExit open(); {

              Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,shortExit);

                  
              inTrade 0;
                  }
              }

              //identify entry signal

              if (inTrade == 0) {
              if (
              high() >= nDonch_Upper, -1) {
                  
              nSignal 1;
                  
              nNewTrade 1;
                  }
              }

              if (
              inTrade == 0) {
              if (
              low() <= nDonch_Lower, -1) {
                  
              nSignal = -1;
                  
              nNewTrade 1;
                  }
              }

              //execute trade

              if  ((nNewTrade == 1) && (nSignal 0)) {

                  
              Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
                  
              setPriceBarColor(Color.green);
                  
              nStopLevel nTradeEntryPrice -  (atrLevel 2);
                  
              }

              if  ((
              nNewTrade == 1) && (nSignal 0)) {

                  
              Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBAR);
                  
              setPriceBarColor(Color.red);
                  
              nStopLevel nTradeEntryPrice +  (atrLevel 2);
                  
              }

              return new Array (
              nDonch_UppernDonch_LowernDonch_Upper2nDonch_Lower2);


              Comment


              • #8
                thestranger
                There are several errors in the script. For example the following
                var nDonch_Upper = upperDonchian (20, 0);
                var nDonch_Upper2 = upperDonchian (10, 0);
                var nDonch_Lower = lowerDonchian (20, 0);
                var nDonch_Lower2 = lowerDonchian (10, 0);

                are all values and not series objects hence you cannot later in the script use nDonch_Upper2.getValue(-1) or nDonch_Lower2.getValue(-1) in your conditions.
                As Jason explained in his reply in order to be able to use the getValue() method you must have declared the series object first. So either you declare nDonch_Upper, nDonch_Upper2, etc as series ie
                var nDonch_Upper = upperDonchian (20);
                var nDonch_Upper2 = upperDonchian (10);
                var nDonch_Lower = lowerDonchian (20);
                var nDonch_Lower2 = lowerDonchian (10);

                in which case you can then use the getValue() method to retrieve the values from the series (as you are currently doing in your script) or you retrieve the value of the study directly inside the conditions by using the barIndex parameter ie upperDonchian(20,-1). For example
                else if (high() >= upperDonchian(20,-1)) {
                Also later in the script you have a couple of conditions that are incorrect for example if (high() >= nDonch_Upper, -1). Depending on the solution you decide to implement you will need to correct these conditions with the appropriate syntax.
                Alex

                Comment


                • #9
                  Admittedly that was a daft mistake! Thanks for your help.

                  Comment

                  Working...
                  X