Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

setting stop loss on an MA

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

    setting stop loss on an MA

    Hi I'm having trouble setting my stop loss on the MA of each bar observed... currently i have my code as such:

    SetStopLoss("LONG1", CalculationMode.Ticks, EMA(40)[0], false);
    SetProfitTarget("LONG1", CalculationMode.Ticks, 300);

    can u please tell me how to phrase it correctly? And what if I want to shift it to, say, 100 ticks below the 40 EMA?

    #2
    Try using CalculationMode.Price instead of .Ticks.

    Comment


      #3
      thanks for coming back, but changed to CalculationMode.Price and couldn't get it to work... zero trades initiated in the backtest the moment I add in the SetStopLoss line

      Comment


        #4
        What exactly is the problem you're having? I'm pretty sure

        SetStopLoss("LONG1", CalculationMode.Price, EMA(40)[0], false);

        should give you a working stop order. Do you have the EMA(40) added to your strategy?

        Comment


          #5
          so i'm testing a simple trade as the code below:

          When I run the backtests, it runs fine without the SetStopLoss line (i.e. it goes long when my conditions are met), but when I add in the stop loss, i get all zeroes (no trades done )


          protected override void Initialize()
          {
          SetProfitTarget("LONG1", CalculationMode.Ticks, 200);
          SetStopLoss("LONG1", CalculationMode.Price, EMA(40)[0], false);


          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1 LONG
          if (EMA(20)[0] > EMA(40)[0]
          && CrossAbove(RSI(Close, 14, 3), 60, 10)

          EnterLong(10000, "LONG1");

          }
          }

          Comment


            #6
            Have you tried adding the EMA in Initialize() before you set the stoploss, like this?

            protected override void Initialize()
            {
            Add(EMA(40));
            SetProfitTarget("LONG1", CalculationMode.Ticks, 200);
            SetStopLoss("LONG1", CalculationMode.Price, EMA(40)[0], false);

            CalculateOnBarClose = true;
            }

            If that doesn't do it then I'm not sure what the problem is. I'm not very good at this though so I'm sure someone else will be able to figure it out tomorrow.

            Edit: Another thing to try would be to see if it works if you put the SetStopLoss() line in OnBarUpdate() instead of Initialize(), or maybe also try putting it in OnStartUp() instead of Initialize().
            Last edited by Radical; 06-26-2012, 11:29 PM.

            Comment


              #7
              Edit: Another thing to try would be to see if it works if you put the SetStopLoss() line in OnBarUpdate() instead of Initialize(). <--- ah, it works now

              thanks alot~ first time trying to code a strategy, been ripping my hair out

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              656 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              371 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              109 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              574 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              579 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X