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

problem with entershortstop and setting stoploss

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

    problem with entershortstop and setting stoploss

    Hello,

    i have a problem with setting stoploss in my strategy

    here is a code


    Protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(MaPer1), EMA(MaPer2), 1)
    && ToTime(Time[0]) >= casBeg
    && ToTime(Time[0]) < casEnd
    && Position.MarketPosition == MarketPosition.Flat)
    {

    EnterLongStop(10000,High[0] + tick*TickSize , "Long");


    }


    else if (CrossAbove(EMA(MaPer1), EMA(MaPer2), 1)
    && ToTime(Time[0]) >= casBeg
    && ToTime(Time[0]) < casEnd
    && Position.MarketPosition == MarketPosition.Short)
    {

    EnterLongStop(10000,High[0] + tick*TickSize , "Long");

    }



    // Condition set 2
    if (CrossBelow(EMA(MaPer1), EMA(MaPer2), 1)
    &&ToTime(Time[0]) >= casBeg
    && ToTime(Time[0]) < casEnd
    &&Position.MarketPosition == MarketPosition.Flat
    )
    {

    EnterShortStop(10000,Low[0] - tick*TickSize , "Short");


    }


    else if (CrossBelow(EMA(MaPer1), EMA(MaPer2), 1)
    &&ToTime(Time[0]) >= casBeg
    && ToTime(Time[0]) < casEnd
    &&Position.MarketPosition == MarketPosition.Long
    )
    {

    EnterShortStop(10000,Low[0] - tick*TickSize , "Short");

    }
    my entry orders are placed via EnterShortStop/EnterLongStop

    if i use SetStopLoss in initialize method it doesnt work.
    protected override void Initialize()
    {

    SetStopLoss(500);
    AccountSize = 10000;
    ExitOnClose = false;
    Could you help me or tell me which method is best/correct to place a stop loss ?

    Thank You

    #2
    Hello,

    Thank you for your post.

    You should use the same unique entry signal for the stop that you are using for your entry positions.

    Instead of a single "SetStopLoss(500), please try using unique entry signals for your long and short order using the following signature structore:

    SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)


    Code:
    protected override void Initialize()
    {
    SetStopLoss("Long", CalculationMode.Price, 500, false);
    SetStopLoss("Short", CalculationMode.Price, 500, false);
    
    }
    Please let me know if you have any questions.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Thank You for your reply bu it doesnt works

      When i turn on Traceorders = true;

      protected override void Initialize()
      SetStopLoss("Long",CalculationMode.Price,Position. AvgPrice - sL*TickSize, false);
      SetStopLoss("Short",CalculationMode.Price,Position .AvgPrice + sL*TickSize, false);

      i recieve a message from console

      **NT** Failed to call method 'Initialize' for strategy 'MACross/6f10327f178049aaa9e24b393bdf20ce': 'Position' property can't be accessed from within 'Initialize' method

      Ivan

      Comment


        #4
        Hello,

        If you are using a dynamic stop loss based off your avg.price or position, you will need to then set this in your OnBarUpdate() section of code. You can put this in the same condition you are using for the entry orders:

        Code:
        if (CrossAbove(EMA(MaPer1), EMA(MaPer2), 1)
                        && ToTime(Time[0]) >= casBeg
                        && ToTime(Time[0]) < casEnd 
                        && Position.MarketPosition == MarketPosition.Flat)
                    {
                                  
                    EnterLongStop(10000,High[0] + tick*TickSize , "Long");
                    [B]SetStopLoss("Long",CalculationMode.Price,Position.  AvgPrice -  sL*TickSize, false);[/B]
                    
                    }
        Please review our Help Guide section on this function for further clarification and any additional information regarding its use or limitations:

        MatthewNinjaTrader Product Management

        Comment


          #5
          Hello,

          I doesnt work properly. I read section about managed approach. Problem( im my opinion) is in entry method im using "EnterShortStop(10000,Low[0] - tick*TickSize , "Short");"
          If i use your scenario my strategy doesnt work properly . ( i tried it yesterday)
          Message from log:

          28.7.2011 16:29 Strategy Calculated stop order price for strategy 'MACross' was smaller/equal 0. No stop order placed.
          28.7.2011 16:29 Strategy An Enter() method to submit an entry order at '7.1.2011 12:45:00' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.



          I search inf user forum and im in end wit my ideas. i tried to resetting my stoplosses but it doesnt help

          I thing that the problem i somwhere in relationship between entershortstop <---> setstoploss



          Ivan

          Comment


            #6
            i thing that the problem could from these reasons

            Set() methods that generate orders to exit a position will be ignored if:
            • A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
            • A position is open and an order submitted by an exit method (ExitLongLimit() for example) is active

            Comment


              #7
              Hello,

              Sorry for the confusion - since you're now using Ticks to calculate your stop, you'll want to change the CalulationMode from Price to Ticks:

              SetStopLoss("Long",CalculationMode.Ticks,Position. AvgPrice - sL*TickSize, false);
              MatthewNinjaTrader Product Management

              Comment


                #8
                Thank you but its still doesnt work . My strategy makes many uf unrealistic small losses.


                Im in end with my brain capacity

                I try ask different.


                How can i place stoploss into a strategy where im enetering postion via EnterShortStop/EnterLongStop?? ( not at the market)

                Must is use a unmanaged approach?

                if (CrossBelow(EMA(MaPer1), EMA(MaPer2), 1)
                &&ToTime(Time[0]) >= casBeg
                && ToTime(Time[0]) < casEnd
                &&Position.MarketPosition == MarketPosition.Flat
                )
                {

                EnterShortStop(10000,Low[0] - tick*TickSize , "Short");
                }

                in Initialize it doesnt work and when i use SetStopLoss in OnBarUpdate method it doesnt work too

                Comment


                  #9
                  Hello,

                  This should be possible with the managed approach.

                  Do you have any other references in your code to SetStopLoss?

                  Any other enter methods?

                  I'd suggest simplifying the EnterStopShort and SetStopLoss methods and removing the Position.Avg and tick calculations. Just use a simple int value that you are sure should work.

                  You should also try printing the values of the position calculations to ensure this is working the way you would expect.

                  More information on debugging your NinjaScript files can be found below:

                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    PROBLEM SOLVED

                    ( different strategy coding)

                    // Condition set 1
                    if (CrossAbove(EMA(MaPer1), EMA(MaPer2), 1)
                    && ToTime(Time[0]) >= casBeg
                    && ToTime(Time[0]) <= casEnd

                    )
                    {

                    entrypricelong = (Close[0]+ tick*TickSize);
                    Print(Close[0]+";" +entrypricelong);


                    }

                    if (CrossBelow(EMA(MaPer1), EMA(MaPer2), 1)
                    &&ToTime(Time[0]) >= casBeg
                    && ToTime(Time[0]) <= casEnd

                    )
                    {

                    entrypriceshort = (Close[0]- tick*TickSize);
                    Print(Close[0]+";" +entrypriceshort);
                    }

                    if (Close[0] == entrypricelong)
                    {
                    EnterLong(100000,"Long");

                    }
                    if (Close[0] == entrypriceshort)
                    {
                    EnterShort(100000,"Short");
                    }

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by alifarahani, Today, 09:40 AM
                    6 responses
                    31 views
                    0 likes
                    Last Post alifarahani  
                    Started by Waxavi, Today, 02:10 AM
                    1 response
                    17 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by Kaledus, Today, 01:29 PM
                    5 responses
                    13 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by Waxavi, Today, 02:00 AM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by gentlebenthebear, Today, 01:30 AM
                    3 responses
                    17 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X