Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A little code help?

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

    A little code help?

    I have listed some code below for a simple SMA crossover system that I am using to test a trailing ATR stop. I can't seem to get it to work though.

    Does anyone have any ideas why it won't work?
    Thanks in advance,
    Jim



    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;
    }

    protectedoverridevoid OnBarUpdate()
    {

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetTrailStop(CalculationMode.Price,
    0);
    }

    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    SetTrailStop(CalculationMode.Price, Position.AvgPrice - ATR(
    14)[1]);
    }

    if (SMA(3)[0] > SMA(21)[0])
    {
    EnterLong(DefaultQuantity,
    "");
    }

    if (SMA(3)[0] <= SMA(21)[0])
    {
    ExitLong(
    "", "");
    }

    #2
    Hi Jim,

    You'll want to use SetStopLoss() rather than SetTrailStop(). Trail stop is used when you want a stop to trail the market by the specified tick or percent.



    Change to SetStopLoss() and let us know if it works any better for you.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply.
      I tried substituting SetStopLoss For the trailing stop, and it still doesn't seem to work.
      By the way, the goal is to get the stop to trail 1 ATR below the highest price.

      Any other ideas?
      Thanks,
      Jim

      Comment


        #4
        What isn't working right? Walk us through what you're doing to verify the functionality and let us know what you're seeing happen instead of the intended result.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          When I run the code that I listed below, with either a trailing stop OR a stop loss, and then run a back test in strategy analyzer on CAT for 10 years of daily data, there are no instances of the stop being triggered, even though there are many cases where either stop should have been triggered, trail or not.

          Comment


            #6
            Make sure you've compiled the latest version of the strategy by right clicking > Compile and then running the backtest again.

            Are you still submitting a stop loss at price 0 as your first snippet?

            Try instead setting a high initial value when flat.
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            SetStopLoss(CalculationMode.Ticks, 200);
            }

            Remove the separate exit condition if you want to evaluate stop loss functionality.

            Use TraceOrders output to track strategy order submission.

            Print all values and verify with tools > Output window. Help for using these debugging tools is available here:
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks a lot for the help.
              Setting the SetStopLoss(CalculationMode.Ticks, 200);
              Rather than 0 fixed the problem.
              It now works well.

              Thanks again,
              Jim

              Comment


                #8
                Originally posted by jjlamb24 View Post
                Thanks a lot for the help.
                Setting the SetStopLoss(CalculationMode.Ticks, 200);
                Rather than 0 fixed the problem.
                It now works well.

                Thanks again,
                Jim
                I would suggest that you experiment with that initial value. Especially see if using your disaster stop value will let it work. You never want your initial stop loss to be larger than the absolute maximum that you are willing to lose in the time period (or per given position, depending on your mindset).

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by mattbsea, Today, 05:44 PM
                0 responses
                4 views
                0 likes
                Last Post mattbsea  
                Started by RideMe, 04-07-2024, 04:54 PM
                6 responses
                31 views
                0 likes
                Last Post RideMe
                by RideMe
                 
                Started by tkaboris, Today, 05:13 PM
                0 responses
                2 views
                0 likes
                Last Post tkaboris  
                Started by GussJ, 03-04-2020, 03:11 PM
                16 responses
                3,282 views
                0 likes
                Last Post Leafcutter  
                Started by WHICKED, Today, 12:45 PM
                2 responses
                20 views
                0 likes
                Last Post WHICKED
                by WHICKED
                 
                Working...
                X