Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rule to close trade with bool variable

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

    Rule to close trade with bool variable

    hi to all,

    having problems setting-up a strategy, would appreciate some simple feedback on how to implement what I am missing.

    after going long I want to separate when to use Condition 2 or Condition 3 to close the trade.

    the idea is, if since the last Long trade was opened:

    1. ADX(14) NEVER crossed above 35 only Condition 2 should apply to close the trade and all condition 3 signals should be ignored;
    2. ADX(14) crossed above 35, at least once since trade is open is enough to activate this rule, Condition 3 should apply to close the trade and all condition 2 signals should be ignored;

    I understand I should probably use a bool variable to do this but never used before and I am having problems to apply such filter.

    below a simplified sample of what I have:



    // Condition set 1
    if (Condition to go Long)
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(Stochastics(3, 10, 3).D, 80, 1))

    {
    ExitLong("", "");
    }

    // Condition set 3
    if (CrossBelow(Close, KAMA(Low, 2,5, 30)[5], 1))

    {
    ExitLong("", "");
    }

    #2
    Originally posted by PRibeiro View Post
    hi to all,

    having problems setting-up a strategy, would appreciate some simple feedback on how to implement what I am missing.

    after going long I want to separate when to use Condition 2 or Condition 3 to close the trade.

    the idea is, if since the last Long trade was opened:

    1. ADX(14) NEVER crossed above 35 only Condition 2 should apply to close the trade and all condition 3 signals should be ignored;
    2. ADX(14) crossed above 35, at least once since trade is open is enough to activate this rule, Condition 3 should apply to close the trade and all condition 2 signals should be ignored;
    I understand I should probably use a bool variable to do this but never used before and I am having problems to apply such filter.

    below a simplified sample of what I have:



    // Condition set 1
    if (Condition to go Long)
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(Stochastics(3, 10, 3).D, 80, 1))

    {
    ExitLong("", "");
    }

    // Condition set 3
    if (CrossBelow(Close, KAMA(Low, 2,5, 30)[5], 1))

    {
    ExitLong("", "");
    }
    Code:
     
    private ExitCondition3 = false;
    private ExitCondition2 = false;
    Code:
     
    if (Position.MarketPosition != MarketPosition.Flat [COLOR=darkgreen]&& !ExitCondition3[/COLOR]) //make sure we are in a position, before we perform the evaluation. This is the start of the main exit evaluation block.
    {
    if (CrossAbove(ADX(14), 35, 1))[COLOR=blue] // we can get here only if this condition has not been met since we became unflat.[/COLOR]
    {
    ExitCondition3 = true; [COLOR=blue]//this means that the ADX(14) condition must have been satisfied, or we cannot get here. As this is now true, the initial condition to enter the whole main exit evaluation block means that we shall never again enter this whole block until we are again flat.[/COLOR]
    ExitCondition2 = false;
    }
    else
    {
    ExitCondition3 = false;
    ExitCondition2 = true;
    }
    }
    // Condition set 1
    if (Condition to go Long)
    {
    EnterLong(DefaultQuantity, "");
    }
     
    // Condition set 2
    if (CrossBelow(Stochastics(3, 10, 3).D, 80, 1) && ExitCondition2)
     
    {
    ExitLong("", "");
    }
     
    // Condition set 3
    if (CrossBelow(Close, KAMA(Low, 2,5, 30)[5], 1) && ExitCondition3)
     
    {
    ExitLong("", "");
    } 
    if (Position.MarketPosition == MarketPosition.Flat) //reset when flat
    {
    ExitCondition3 = false;
    ExitCondition2 = false;
    }
    Last edited by koganam; 04-09-2013, 10:00 AM.

    Comment


      #3
      Hi Koganam

      Thanks for your reply

      I am trying to incorporate your suggestion (inserted below in bold) but the position is opened and never closed, none of the closing conditions seems to activate.

      I guess I am doing some basic error.

      Here is what I have right now (many thanks for your help):

      private bool ExitCondition3 = false;
      private bool ExitCondition2 = false;

      #endregion

      protected override void Initialize()
      {
      ExitOnClose = false;
      CalculateOnBarClose = true;
      }

      protected override void OnBarUpdate()
      {

      if (BarsInProgress != 0)
      return;

      if (Position.MarketPosition != MarketPosition.Flat && CrossAbove(ADX(14), 35,BarsSinceEntry()))
      {
      ExitCondition3 = true;
      ExitCondition2 = false;

      }
      else
      {
      ExitCondition3 = false;
      ExitCondition2 = true;

      }

      // Condition set 1
      if (Condition to go Long)
      {
      EnterLong(DefaultQuantity, "");
      }

      // Condition set 2
      if (CrossBelow(Stochastics(3,10,3).D, 80, 1) && ExitCondition2 == true)
      {
      ExitLong("", "");
      }

      // Condition set 3
      if (CrossBelow(Close, KAMA(Low, 2,5, 30)[5], 1) && ExitCondition3 == true)
      {
      ExitLong("", "");
      }

      // Reset Closing Filter
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      ExitCondition3 = false;
      ExitCondition2 = false;
      }

      }
      Last edited by PRibeiro; 04-09-2013, 07:25 AM.

      Comment


        #4
        Originally posted by PRibeiro View Post
        Hi Koganam

        Thanks for your reply

        I am trying to incorporate your suggestion (inserted below in bold) but the position is opened and never closed, none of the closing conditions seems to activate.

        I guess I am doing some basic error.

        Here is what I have right now (many thanks for your help):

        private bool ExitCondition3 = false;
        private bool ExitCondition2 = false;

        #endregion

        protected override void Initialize()
        {
        ExitOnClose = false;
        CalculateOnBarClose = true;
        }

        protected override void OnBarUpdate()
        {

        if (BarsInProgress != 0)
        return;

        if (Position.MarketPosition != MarketPosition.Flat && CrossAbove(ADX(14), 35,BarsSinceEntry()))
        {
        ExitCondition3 = true;
        ExitCondition2 = false;
        }
        else
        {
        ExitCondition3 = false;
        ExitCondition2 = true;
        }

        // Condition set 1
        if (Condition to go Long)
        {
        EnterLong(DefaultQuantity, "");
        }

        // Condition set 2
        if (CrossBelow(Stochastics(3,10,3).D, 80, 1) && ExitCondition2 == true)
        {
        ExitLong("", "");
        }

        // Condition set 3
        if (CrossBelow(Close, KAMA(Low, 2,5, 30)[5], 1) && ExitCondition3 == true)
        {
        ExitLong("", "");
        }

        // Reset Closing Filter
        if (Position.MarketPosition == MarketPosition.Flat)
        {
        ExitCondition3 = false;
        ExitCondition2 = false;
        }

        }
        You requested code for a very specific techical specification. I wrote code to that specification. You have altered the code that I wrote, and now it does not correspond to the logic of the specification. You will have to troubleshoot your code's logic to determine why it is failing.

        I have edited the code to add a few notes to explain what I did. Those notes are in blue. What I added in green is only for code efficiency so that once we know we are using ExitCondition3, we do not need to evaluate what exit condition to use. It makes no difference to the logic, only to the efficiency of execution.
        Last edited by koganam; 04-09-2013, 10:03 AM.

        Comment


          #5
          hi koganam

          No harm intended

          what I presented was a simplified sample of a longer strategy and then was trying to incorporate your helpful reply. No need to crowd-out the thread of excessive info.

          many thanks for your precious help

          kind regards
          Paulo

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sjsj2732, Yesterday, 04:31 AM
          0 responses
          32 views
          0 likes
          Last Post sjsj2732  
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          286 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          283 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          133 views
          1 like
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          91 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Working...
          X