Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Stopl Loss MIN/MAX wrong closing price

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

    Stopl Loss MIN/MAX wrong closing price

    Hello NT7,

    I have a frustrating problem/quesstion

    I have a stretegy where im using stop loss based on MIN/MAX function.

    Code:

    if (BarsSinceEntry("Long1") == 0)
    {
    SetStopLoss("Long1", CalculationMode.Price,min, false);
    DrawLine(min.ToString(), false, 2, min, 0, min, Color.Black, DashStyle.Dot, 2);


    }


    if (BarsSinceEntry("Short1") == 0)
    {
    DrawLine(max.ToString(), false, 2, max, 0, max, Color.Black, DashStyle.Dot, 2);
    SetStopLoss("Short1", CalculationMode.Price,max, false);

    }
    variables min/max are defined:

    min = MIN(Low,pocetBar)[1];
    max = MAX(High,pocetBar)[1];

    SL is working corectly bud still exist one bad situation , which is over my mind/programming skills.

    If my position is open and then next bar is value of my stoploss targeted , position is not closed on SL value , bu is closed on open price of next bar.

    Can somebody help me in my problem?
    Thank you!!! In Attachment is a screenshot

    dedomraz
    Attached Files

    #2
    dedomraz, please work with TraceOrders to understand when exactly your stoploss is submitted and at which exact value according to your calculations :



    Are you resetting your stop loss to a default value when you're flat in your Strategy Position?

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Are you resetting your stop loss to a default value when you're flat in your Strategy Position?
      YES here is my code

      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("Short1", CalculationMode.Ticks,4000, false);
      SetStopLoss("Long1", CalculationMode.Ticks,4000, false);
      }


      EDIT!!! sorry Bertrand but im my output file were wrong data. Iam apologize!!!!
      TraceOrders output to my problem:

      28. 1. 2011 6:00:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short1' Mode=Ticks Value=4000 Currency=0 Simulated=False
      28. 1. 2011 6:00:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long1' Mode=Ticks Value=4000 Currency=0 Simulated=False
      28. 1. 2011 6:00:00 Entered internal PlaceOrder() method at 28. 1. 2011 6:00:00: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Short1' FromEntrySignal=''
      29. 1. 2011 6:00:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short1' Mode=Price Value=92,83 Currency=0 Simulated=False
      29. 1. 2011 6:00:00 Amended stop order: Order='NT-00138/Backtest' Name='Stop loss' State=Working Instrument='CL ##-##' Action=BuyToCover Limit price=0 Stop price=92,83 Quantity=1 Strategy='ADXhunter' Type=Stop Tif=Gtc Oco='NT-00122-326' Filled=0 Fill price=0 Token='2f4761663e284b558eb1245f4a23164a' Gtd='1. 12. 2099 0:00:00'

      My problem is only this one situation ( in attchment in post before)

      If my position is open and then next bar is value of my stoploss targeted , position is not closed on SL value , bu is closed on open price of next bar.
      thanks dedomraz
      Last edited by dedomraz; 06-06-2011, 06:06 AM. Reason: mistake

      Comment


        #4
        No worries dedomraz - so were you able to resolve / understanding your issue? Please keep in mind you submit / work with StopMarket orders here meaning they do not have to fill at a specific price but will turn into a regular market order to exit your position once triggered by price.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          OK Bertrand thank you for your patience. I will describe my problem in more complex space.

          I have a strategy with static stol loss , which is based on a "dynamic variable" based on MIN/MAX

          min = MIN(Low,numberOfBar)[1];
          max = MAX(High,numberOfBar)[1];

          theese variables are calculate from OnBarUpdate() method

          Logic is set my SL in case of long trade:

          on Lowest value, number of Bars ( my variable) , under entry price

          SetStopLoss("Long1", CalculationMode.Price,min, false);

          in case of short trade:

          on Highest value, number of Bars ( my variable) , above entry price

          SetStopLoss("Short1", CalculationMode.Price,max, false);


          OK so where can i place my SetStopLoss method??

          I have 2 or 3 options

          1.options

          SetStopLoss place under protected override void Initialize() method

          it doesnt work because input value to setstoploss is is a variable from onbarupdate


          2. options

          SetStopLoss place under protected override void OnBarUpdate()

          it work but not correctly because my SL is not static but is dynamic


          3. options

          SetStopLoss place under if (BarsSinceEntry("Long1") == 0)
          {
          SetStopLoss("Long1", CalculationMode.Price,min, false);
          DrawLine(min.ToString(), false, 2, min, 0, min, Color.Black, DashStyle.Dot, 2);.

          This work 99% case BUT.

          There is 1 situation incorrect.

          When a new position is open and next bar is my stop loss targeted, position is not closed on my SL value, but is closed on next bar open value.

          Could me help with better idea how can i write stop loss method with static position but with dynamic input variable {min = MIN(Low,numberOfBar)[1]; max = MAX(High,numberOfBar)[1];} from OnBarUpdate() method?

          is my question understandable?

          Thank you much

          Comment


            #6
            Hello,

            This is Brett responding for bertrand.

            You need to make sure to SetStopLoss() before entry.

            For Example:

            SetStopLoss("Long1", CalculationMode.Price,min, false);
            EnterLong();


            This above will work correctly.

            The below will not:


            EnterLong();
            SetStopLoss("Long1", CalculationMode.Price,min, false);


            This should do what you need.

            Comment


              #7
              Thank for your answer but when i use you scenario my Stoploss is dynamic. What i need is set static my stop loss under my entry.


              when i use your scenario

              protected override void OnBarUpdate()
              {
              min = MIN(Low,pocetBar)[1];
              max = MAX(High,pocetBar)[1];

              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss("Short1", CalculationMode.Ticks,4000, false);
              SetStopLoss("Long1", CalculationMode.Ticks,4000, false);
              }

              //LONG
              if (
              ADX(ADXper)[0] > ADX(ADXper)[1]
              &&DM(dMper).DiPlus[1] > DM(dMper).DiMinus[1]
              &&DM(dMper).DiPlus[0] > DM(dMper).DiPlus[1]
              &&DM(dMper).DiPlus[2] > DM(dMper).DiPlus[1]
              &&ADX(ADXper)[0] > aDXlevel)

              {
              SetStopLoss("Long1", CalculationMode.Price,min, false);
              EnterLong(1,"Long1");
              }
              .....

              }


              my stop loss is moving dynamically.

              What i need is a static stoploss BUT static stop loss with variable from onbarupdate method

              Thanks!!!

              Comment


                #8
                Hello,

                Ok this looks better.

                Now you need to tell NinjaTrader not to SetStopLoss if you are in a trade, one way you can do this is as below.

                if (
                ADX(ADXper)[0] > ADX(ADXper)[1]
                &&DM(dMper).DiPlus[1] > DM(dMper).DiMinus[1]
                &&DM(dMper).DiPlus[0] > DM(dMper).DiPlus[1]
                &&DM(dMper).DiPlus[2] > DM(dMper).DiPlus[1]
                &&ADX(ADXper)[0] > aDXlevel


                && Position.MarketPosition == MarketPosition.Flat)


                This will only run your entry code once. Submit the order for long once, then also set your StopLoss Once and not update it again until flat for the next order.

                Let me know if I can be of further assistance.

                Comment


                  #9
                  Intersting idea,

                  tonight i will try to program your idea and i will answer tomorrow!!!

                  Thanks!

                  Comment


                    #10
                    NinjaTrader_Brett, Bertrand

                    Your Solution is correct. My strategy works fine!!!!
                    I appreciate your useful reply and sorry for my stupid question...

                    dedomraz

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by r68cervera, Today, 05:29 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post r68cervera  
                    Started by geddyisodin, Today, 05:20 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post geddyisodin  
                    Started by JonesJoker, 04-22-2024, 12:23 PM
                    6 responses
                    35 views
                    0 likes
                    Last Post JonesJoker  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    12 responses
                    3,241 views
                    0 likes
                    Last Post Leafcutter  
                    Started by AveryFlynn, Today, 04:57 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post AveryFlynn  
                    Working...
                    X