Announcement

Collapse
No announcement yet.

How can I tell if stopped?

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

  • How can I tell if stopped?

    If I set up a strategy which takes a trade and then is stopped, how can I know it is stopped?

    For example, say I want to go long if I'm not in a trade and an indicator goes positive.
    When I go long, I set a stop.
    The trade is stopped, but the indicator remains positive. I do not want to go long again until the indicator has gone negative and then positive again.

    Thanks.

  • #2
    FiveString
    You would do that by using a global variable (called for example Flag) that will have two states only (eg "long" and "short" or 1 and -1).
    When you have a signal to go long you verify that Flag is not already set to "long" at which point you execute your command to go long. Once you have entered the trade you set Flag to "long". For example
    PHP Code:
    if(myLongCondition == true){
        if(
    Flag != "long"Strategy.doLong(...)
        
    Flag "long";

    Then when you get stopped out you do not reset Flag leaving it in its "long" state. This will prevent further entries on the long side because in order to go long Flag must not be equal to "long"
    Then when the short condition is true you repeat the process and set Flag to "short". For example
    PHP Code:
    if(myShortCondition == true){
        if(
    Flag != "short"Strategy.doShort(...)
        
    Flag "short"
    }
    //or if you do not wish to trade the short side
    if(myShortCondition == true){
        if(
    Flag != "short"Flag "short"

    At this point Flag is set to "short" hence allowing for long trades again
    Hope this helps
    Alex

    Comment


    • #3
      Thank you for your response. I have not made myself clear.

      I can already know if I am long or short using the isLong or isShort functions. My problem goes this way:

      1. I go long based on an indicator being positive and set a stop.
      2. While the indicator is positive, I get stopped out.
      3. Even though the indicator remains positive for future bars and I am not long, I don't want to go long until the indicator has gone negative and returns to positive.

      I might be able to jury rig some combination of conditions to do this, but was hoping to have access to some indicator that says, in effect, "stopped out". Then I could execute different conditions and strategy.

      Comment


      • #4
        FiveString
        FWIW the purpose of the variable Flag that I suggested in my reply is not to determine whether the strategy is long or short (which can be easily done with the Strategy.isLong() or Strategy.isShort() methods) but to prevent re-entering a long when the strategy is stopped out and the conditions are still valid for a long entry (ie as described in point 3 of your list).
        Alex

        Comment


        • #5
          Thanks. I've been able to work something out with this idea. I appreciate your help.

          Comment


          • #6
            FiveString
            You are most welcome
            Alex

            Comment

            Working...
            X