Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Adding Two Conditions to An Existing Strategy

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

    Help with Adding Two Conditions to An Existing Strategy

    Hello all,

    I am working with a pre-existing strategy (a divergence strategy that I dl'ed from here that I have been tweaking for the last year or so).

    I'm trying to add two conditions to it. So I created, using the wizard, code that has two conditions, regarding ADX and Linear Regression slope. When I transferred it to code, it looks like this:

    Code:
     // Condition set 1
                if (ADX(60)[0] > 0
                    && LinRegSlope(60)[0] > 0)
                {
                }
    Fair enough. So I am trying to add those few lines of code to my divergence strategy. Keep in mind, I am not a coder. I am philosopher and a theologian So anyway, here is that above code smashed into my existing code.

    Code:
           /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1 - Positive Divergence
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2) ||
    				(Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2) ||
    				(Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2)) &&
    				Position.MarketPosition == MarketPosition.Flat)
    				(LinRegSlope(60)[0] > 0
    				&& ADX(60)[0] > 0);
                {
                    EnterLong(1, "Long");
    				justEntered = true;
                }
    I get an "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" error.


    Any advice to make this strategy to compile correctly?

    #2
    Originally posted by outstretchedarm View Post
    Hello all,

    I am working with a pre-existing strategy (a divergence strategy that I dl'ed from here that I have been tweaking for the last year or so).

    I'm trying to add two conditions to it. So I created, using the wizard, code that has two conditions, regarding ADX and Linear Regression slope. When I transferred it to code, it looks like this:

    Code:
     // Condition set 1
                if (ADX(60)[0] > 0
                    && LinRegSlope(60)[0] > 0)
                {
                }
    Fair enough. So I am trying to add those few lines of code to my divergence strategy. Keep in mind, I am not a coder. I am philosopher and a theologian So anyway, here is that above code smashed into my existing code.

    Code:
           /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1 - Positive Divergence
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2) ||
                    (Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2) ||
                    (Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2)) &&
                    Position.MarketPosition == MarketPosition.Flat)
                    (LinRegSlope(60)[0] > 0
                    && ADX(60)[0] > 0);
                {
                    EnterLong(1, "Long");
                    justEntered = true;
                }
    I get an "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" error.


    Any advice to make this strategy to compile correctly?
    I am going to assume that your condition before the addition of your newest filter, did compile correctly. In that case, you are missing the && operator between your old and new conditions. You also have a semi-colon terminating your "if" condition, turning it into a null statement.

    Without going into your bracketing, which may or may not be made more efficient, and only adding your newest code to the old, it seems that what you need would be:
    Code:
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD,  1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(cciPeriod,  "cci", positiveD, 9,26).Plot0[0] == 2) ||
                    (Divergence(ergodicfastPeriod, "ergodic", positiveD,  1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(macdfastPeriod,  "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2) ||
                    (Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0]  == 2 && Divergence(macdfastPeriod, "macd", positiveD,  macdsignalPeriod,macdslowPeriod).Plot0[0] == 2)) &&
                    Position.MarketPosition == MarketPosition.Flat &&
                    (LinRegSlope(60)[0] > 0
                    && ADX(60)[0] > 0) )
                {
                    EnterLong(1, "Long");
                    justEntered = true;
                }
    Depending on what you really intend to evaluate, you may be able to remove the brackets that encase your newly encapsulated filter.

    Comment


      #3
      thanks for your reply.

      well, I've tried

      Code:
              Position.MarketPosition == MarketPosition.Flat) &&
               (LinRegSlope(60)[0] > 0
               && ADX(60)[0] > 0);
      {
              EnterLong(1, "Long");
      	justEntered = true
      and get "invalid term expression &&" in line 66 (the first line)

      and "; expected" in line 66 (the last line).

      When I add the semicolon, to the last line, all I get is the first error about &&.

      I wonder what problem the compiler has with && in this case?

      Comment


        #4
        outstretchedarm, the compile issue likely stems from the bracketing used here - you would need to be very mindful about it. I would suggest removing code sections until you can properly compile and then slow add one by one back in to find the culprit here.

        Comment


          #5
          Originally posted by outstretchedarm View Post
          thanks for your reply.

          well, I've tried

          Code:
                  Position.MarketPosition == MarketPosition.Flat) &&
                   (LinRegSlope(60)[0] > 0
                   && ADX(60)[0] > 0);
          {
                  EnterLong(1, "Long");
              justEntered = true
          and get "invalid term expression &&" in line 66 (the first line)

          and "; expected" in line 66 (the last line).

          When I add the semicolon, to the last line, all I get is the first error about &&.

          I wonder what problem the compiler has with && in this case?
          In the particular code snippet that you posted,

          Code:
          Position.MarketPosition == MarketPosition.Flat) &&
                   (LinRegSlope(60)[0] > 0
                   && ADX(60)[0] > 0);
          has no meaning. It is not even a valid statement, as there is a closed bracket that has no corresponding open bracket.

          If you were looking for a conditional statement, one correct one would be:

          Code:
          if ((Position.MarketPosition == MarketPosition.Flat) &&
                   (LinRegSlope(60)[0] > 0
                   && ADX(60)[0] > 0))
          {
          // Do stuff;
          }
          just to preserve your bracketing.

          You CANNOT, and MUST NOT terminate an "if" clause with a semi-colon, or you render the statement useless, as it becomes a null statement.

          Comment


            #6
            thanks for the feedback. I'm working on it now, and will post back my results.

            ok this is realtime...

            ok, I got to work (compiled) by using phrasing that used in the code of #2 above. Thanks! Now to run some tests

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            666 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            377 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X