Announcement

Collapse
No announcement yet.

Bug in tutorial code...?

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

  • Bug in tutorial code...?

    I'm trying to start from some tutorial code given on the Esignal help site, and modify it for my purposes. However every time I try to check the Syntax, I get an error on the following line:

    if (Strategy.isInTrade() == true) && (nNewTrade == 1)) {

    I don't really see anything wrong here... Now, I do notice that there seems to be an extra closed-parenthesis at the end of the line, but even removing it has had no effect. If I trim the line down to only

    if (Strategy.isInTrade() == true)

    Then it no longer returns an error.

    Does anyone know what's going on here? This is the code from the "Sample Profit Code" system used to show how to build a system with simple profit targets and entry & exit signals.

    Thanks,

    Jonathan

  • #2
    Jonathan
    Here is the efs corrected for that specific error.
    You will still get two errors for lines 68 (/*Buy Condition*/) and 73 (/*Sell Condition*/). That is where you enter your own conditions.
    Alex
    Attached Files

    Comment


    • #3
      Again, thanks for coming to my rescue Alexis. My remaining problem is an error saying that "Parameter Number 1 of Function setPriceBarColor is invalid" on line 79. I can't see what the problem is but I'm sure it's my limited understanding that's getting in the way. Here is my entire system code with the problem line in BOLD. Thanks as always for your help...:

      /*----------------------------------------------------------
      Sample Profit Target Code
      ----------------------------------------------------------

      Declare new global variables for the code. Notice we are
      Only adding two new variables to handle the profit target
      Function – nTradeEntryPrice and ProfitTarget1.
      NTradeEntryPrice = the expected price of our entry trade.
      ProfitTarget1 = the projected profit target level (points).
      ----------------------------------------------------------*/

      var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
      var nsignal; // returns the direction of the trading signal
      var nTradeEntryPrice;
      var ProfitTarget1 = 5.0;


      function premain() {

      setPriceStudy(true);

      setStudyTitle("H-A Trading System");

      setCursorLabelName("PT CODE");

      var fp1 = new FunctionParameter("cBull", FunctionParameter.COLOR);
      fp1.setName("Bullish Candles");
      fp1.setDefault(Color.green);

      var fp2 = new FunctionParameter("cBear", FunctionParameter.COLOR);
      fp2.setName("Bearish Candles");
      fp2.setDefault(Color.RGB(155,0,0));
      }

      var haClose = null;
      var haOpen = null;
      var haClose1 = null;
      var haOpen1 = null;
      //var iCntr = 0;
      var vColor1=null;//added by ACM
      var vColor=null;//added by ACM

      function main(cBull, cBear) {

      /*----------------------------------------------------------------
      // If new trade, get entry price – used for our profit target
      ----------------------------------------------------------
      This portion of the code identifies if a new trade has been issued and records the entry price of our trade.
      If no new trade has been triggered (nNewTrade == 1), then this portion of the code is ignored.
      ----------------------------------------------------------*/


      var nState = getBarState();

      if (nState == BARSTATE_NEWBAR) {
      if ((haClose == null || haOpen == null) && close(-1) != null) {
      haClose = close(-1);
      haOpen = open(-1);
      }
      haClose1 = haClose;
      haOpen1 = haOpen;
      vColor1=vColor;//added by ACM
      //iCntr += 1;
      //if (iCntr > 200) iCntr = 0;
      }

      if (haClose1 == null || haOpen1 == null) return;

      haOpen = (haOpen1 + haClose1) / 2;
      haClose = (open() + high() + low() + close()) / 4;
      var haHigh = Math.max(high(), haOpen, haClose);
      var haLow = Math.min(low(), haOpen, haClose);

      //candlesticks
      vColor = Color.black;//modified by ACM
      if (haClose > haOpen) vColor = cBull;
      if (haClose < haOpen) vColor = cBear;

      setPriceBarColor(vColor);

      var retArray = new Array(4);
      retArray[0] = haHigh.toFixed(2)*1;
      retArray[1] = haLow.toFixed(2)*1;
      retArray[2] = haOpen.toFixed(2)*1;
      retArray[3] = haClose.toFixed(2)*1;

      return ;

      if (Strategy.isInTrade() == true && nNewTrade == 1) {
      // This sets the expected entry price of the current short trade
      nTradeEntryPrice = open();
      // This switches off the nNewTrade variable
      nNewTrade = 0; // Turn off NEW TRADE switch
      }


      /*----------------------------------------------------------------
      // Test for Profit Target Breach (ProfitTarget1)
      ----------------------------------------------------------
      This portion of the code identifies if our profit target has been reached and exits our trades.
      ----------------------------------------------------------*/

      if (Strategy.isInTrade() == true && Strategy.isShort() == true) {
      // Check if the profit target has been reached/breached
      if (low() <= (nTradeEntryPrice - ProfitTarget1)) {
      // Profit Target Breached… Execute Cover order.
      Strategy.doCover("Short PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice - ProfitTarget1));
      }
      }

      if (Strategy.isInTrade() == true && Strategy.isLong() == true) {
      // Check if the profit target has been reached/breached
      if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
      // Profit Target Breached… Execute Cover order.
      Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR, Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
      }
      }

      /*----------------------------------------------------------------
      // Identify new trade signals…
      ----------------------------------------------------------------- */

      if (Strategy.isInTrade() == false) {

      if (vColor1==cBear&&vColor==cBull) {
      nNewTrade = 1; // New Trade Trigger
      nsignal = 1; // Buy Signal – Trade Type
      }

      if (vColor1==cBull&&vColor==cBear) {
      nNewTrade = 1; // New Trade Trigger
      nsignal = -1; // Sell Signal – Trade Type
      }
      }

      /*----------------------------------------------------------------
      // Execute Trades ONLY if nNewTrade is triggered ....
      ----------------------------------------------------------------- */

      if (nNewTrade == 1) { //Execute New Trade
      // new or reversed trade position
      if (Strategy.isInTrade() == true) {
      if ((nsignal > 0) && (Strategy.isShort() == true)) {
      Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.NEXTBAR);
      Strategy.doLong("Rev Long", Strategy.MARKET, Strategy.NEXTBAR);
      }
      if ((nsignal < 0) && (Strategy.isLong() == true)) {
      Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.NEXTBAR);
      Strategy.doShort("Rev Short", Strategy.MARKET, Strategy.NEXTBAR);
      }
      } else {
      if ((nsignal > 0)) {
      Strategy.doLong("Go Long", Strategy.MARKET, Strategy.NEXTBAR);
      }
      if ((nsignal < 0)) {
      Strategy.doShort("Go Short", Strategy.MARKET, Strategy.NEXTBAR);
      }
      } // end if IN TRADE
      } // END EXECUTE NEW TRADE
      }

      Comment


      • #4
        Re: Reply to post 'Bug in tutorial code...?'

        Is it possible vColor is not defined if haClose = haOpen ?

        ----- Original Message -----
        From: <[email protected]>
        To: <[email protected]>
        Sent: Saturday, January 31, 2004 3:56 PM
        Subject: Reply to post 'Bug in tutorial code...?'


        > Hello dloomis,
        >
        > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        >

        Comment


        • #5
          Hmmmm... good thought, I suppose that could be a problem but I don't think it is in this case. My previous version of this code works fine, but has no system functionality; it just colors the bars on my price chart based on the Heikin colors (I think the .efs was posted earlier in this thread). If it never ran into a problem before adding the system functions, I don't see why it would now... can anyone confirm/deny this or offer other suggestions?

          THanks much!

          J

          Comment


          • #6
            Jonathan
            1. Check the syntax of function premain(). Should be function preMain()
            2. You omitted to copy the following in preMain()
            setColorPriceBars(true);
            setDefaultPriceBarColor(Color.black);

            Alex

            Comment


            • #7
              Aha... the premain typo is in the original tutorial code! =) Funny, such a simple thing I would've expected to be caught by the syntax checker. *shrug*

              Well, even after adding what you suggested, though I now have my bars correctly colored, I can't get the system functions to actually enter and exit trades, nor place any markers on my charts, but that's a puzzle I think I need to solve on my own. If I'm still stuck by tonight, expect another post...

              Thanks again so very very much!

              Jonathan

              Comment


              • #8
                Just a side note...

                I wrote all of the code in the example you are trying to use. In the process of copying code from the editor into MS WORD, some of the "context" was converted (case and other things).

                I did my best to make sure there were no obvious errors, but some still made it into the examples.

                Within my fileshare group, there are many examples you can use.

                Again, I've been in and out of the office these past few days, so sorry I missed this message. It looks like Alex took good care of your requests.

                Matheny Enterprises FileShare

                If you need any other assistance, please let me know.

                Best regards,

                Brad
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment


                • #9
                  No worries, Brad, I was just surprised that the syntax checker doesn't catch things like case sensitivity, since the code is clearly case sensitive.

                  I'm learning a ton from the responses I've gotten so far, but am still having a tremendous amount of trouble getting the auto papertrading to work. Maybe tomorrow I'll post some of my code that isn't working and you can take a look at it. You can probably pinpoint the problem in seconds that would take me weeks (if ever) to find.

                  Thanks again for all the help, to you & Jason & Alexis & anyone else who's posted collectively!

                  Jonathan

                  Comment


                  • #10
                    Post your current code...

                    Please post your current code here and I'll add the Paper Trader portion for everyone to see. It is actually very simple (3 lines of code). The rest of it is adding the "logic control" for your code.

                    Brad
                    Brad Matheny
                    eSignal Solution Provider since 2000

                    Comment

                    Working...
                    X