Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why isn't this working? Please Help!

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

    Why isn't this working? Please Help!

    Hello,

    I've been working on coding my strategy in order to backtest it. I'm trying to simulate a trailing stop on my last lot ("Long1c") so that my SetStopLoss trails the price by 50 ticks. Since I'm new to coding, I can't figure out what the errors mean or how to fix the issue. Any help would be much appreciated! Here is that section of code:

    // If a short position is open, allow for stop loss modification to breakeven
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+40 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 40 * TickSize)
    {
    SetStopLoss(
    "Long1b", CalculationMode.Price, Position.AvgPrice, true);
    SetStopLoss(
    "Long1c", CalculationMode.Price, Position.AvgPrice, true);
    }
    if (Close[0] > Position.AvgPrice + 60 * TickSize)

    SetStopLoss(
    "Long1c", CalculationMode.Price, Position.AvgPrice + 10 * TickSize, true);
    for (Position.AvgPrice + 60; Position.AvgPrice + 60 < 100; Position.AvgPrice + 60++)
    {
    SetStopLoss(
    "Long1c", CalculationMode.Price, Position.AvgPrice + 10++ * TickSize, true);
    }

    My biggest issue, I think, is how to reference the price in a way that the SetStopLoss could follow behind and yet not lose ground. I'm pretty sure that my way of referencing it is wrong... perhaps I'm approaching the situation wrong all together?
    Also, since I'm not trying to automate the strategy, does that change the way it needs to be addressed?

    Thank you for any help that you can give!
    Last edited by arichpiano; 03-13-2012, 10:05 PM. Reason: More information

    #2
    Have you tried using the SetTrailStop() method to see if that works for you?



    VT

    Comment


      #3
      arichpiano, can you please clarify what errors exactly you would get here and what you mean by you would not try to automate this strategy? The code would only work for trailing a strategy position, it would not work on your manual entered orders to provide a trailing stop on a discretionary basis. For those uses our ATM's would be best.

      Comment


        #4
        VTtrader,

        No, it won't that I'm aware of. This strategy begins as a 40-tick stoploss, when a 40-tick profit target is hit, the stoploss is set to breakeven. At my 60-tick profit target, the stoploss switches to a trailing stop and does so until the exit. Unfortunately, I cannot use both on the same lot. Now if there was a way to switch between them mid-trade, that would work. Otherwise I'm stuck trying to code a SetStopLoss to somehow trail the forward price movement.

        Bertrand,

        Well, what I mean is that the sole purpose for coding this strategy (for now) is to backtest it against hard data. So in that kind of atmosphere (where there is no incoming data), does that make this easier to code? Or would it be coded the same way?

        I have used the ATMs quite a bit, and I'm very familiar with them. My goal here is to take my ATM strategy and backtest it.

        The errors I get are:
        "Only assignment, call, increment, decrement, and new object expressions can be used as a statement."
        "The operand of an increment or decrement operator must be a variable,
        property, or indexer."

        Thanks for your help!

        Comment


          #5
          I think your errors are coming from here:
          for (Position.AvgPrice + 60; Position.AvgPrice + 60 < 100; Position.AvgPrice + 60++)

          Try something like this:
          for (i = Position.AvgPrice + 60; i < 100; i++)

          You were trying to use "Position.AvgPrice + 60" AS a variable instead of initializing a variable to that value and then using the variable.

          Hope that helps.

          VT

          Edit: The SetStopLoss inside the "for loop" won't work either. this: "Position.AvgPrice + 10++ ", would be problematic. You need to calculate the price outside the SetStopLoss method. SEE NEXT POST
          Last edited by VTtrader; 03-14-2012, 09:04 PM.

          Comment


            #6
            Just to clarify my edit. That whole "for loop" won't work. Not only is the syntax wrong, the logic won't do what you want it to. I wasn't clear with what you were trying to do, so I didn't pay attention to the logic, just the syntax.

            Even if the syntax was correct for the loop, once you enter the loop, it would increase the StopLoss incrementally until you were stopped out, with the price never changing.

            You need to check on each price change if the stop should be moved, then set the StopLoss to it's new value. You can't do it inside a loop, the price doesn't change inside the loop.

            Does that clarify it at all?

            VT

            Comment


              #7
              Yes, that clarifies and makes sense.

              I think that puts me back to the question of how would I code it in such a way that it would check on each price change and adjust it appropriately? The reason I used a loop was to attempt to mimic the trailing stop with a SetStopLoss, which appears to have been a bad choice. Any suggestions? You're very helpful! Thank you!

              Comment


                #8
                You have a couple of issues and a couple of options.

                First of all, if you're looking for following tick by tick the price change, you can't when backtesting (unless you use a multi-time frame strategy). With live data or using replay, you can adjust the stops in OnMarketData(), or even OnBarUpdate() if CalculateOnBarClose = false. On Historical bars though you can't use OnMarketData() since there isn't any, and OnBarUpdate only gets called once per bar regardless of COBC being true or false. For backtesting you could only move the stops once per bar.

                if you wanted to set a trailing stop using live or replay, or with the backtesting constraints I mentioned, you would do something like this.

                Define 2 new variables:
                private bool trailStarted = false;
                private double oldPrice = 0.0;

                After you've moved the stop to breakeven from your old code and want to start the trail:
                if (!trailStarted && Close[0] > Position.AvgPrice + 60 * TickSize) //Checks that the trail isn't already started, and initializes it.
                {
                trailStarted = true;
                oldPrice = Close[0];
                SetStopLoss("Long1c", CalculationMode.Price, Position.AvgPrice + 10 * TickSize, true);
                }

                if (trailStarted && Close[0] > oldPrice) //Moves the trail only when price has moved higher
                {
                SetStopLoss("Long1c", CalculationMode.Price, Close[0] - ticksBack * TickSize, true);
                oldPrice = Close[0];
                }

                That is just a basic quick example, there are lots of variations that would work. "ticksBack" would just be the number of ticks you're trailing, it could be hard coded or a variable. The idea is that once your criteria to start the trail is met, you set the flag that it is started, capture the Close[0] that triggered it, and set the first position. Once it's started, the trail is moved up everytime the price is greater than the old price.

                It's a rough start but hopefully will give you the idea.

                VT
                Last edited by VTtrader; 03-14-2012, 10:41 PM.

                Comment


                  #9
                  VT,

                  Thank you so much for your help! I'm going to try your suggestion and see how it works!

                  Comment


                    #10
                    BTW, you will want to make sure you reset the "trailStarted" flag back to false when the position is closed out, so that it is ready for the next time.

                    VT

                    Comment


                      #11
                      Hello,

                      I tried your suggestions and tested the strategy on market replay.

                      When entering a position, only one position would execute instead of the three lots.

                      That one position would also have a stop placed at the same price, so the strategy would be immediately stopped out, but it would get back in at a different price. I'm not sure what's going on or how to fix it...

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      648 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      369 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      108 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      572 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      573 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X