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

Stop Loss Problem

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

    Stop Loss Problem

    Hi All,


    I have a problem to handle exit positions and I will really appreciate if you could assist me on this. At my strategy I have applied Trail Stop, Stop Loss and a condition in order to exit from the current position. However the strategy is not stop out if a condition is confirmed. I applied Print() statments and I understand that the If condition doesnt execute. Please have a look at the code below and let me know your thoughs or if you need the whole code. The strategy is compiled well but the stop loss orders are not working as expected

    .................Initialize()

    SetStopLoss("", CalculationMode.Percent, SL, true);

    ..............OnBarUpdate()

    if ( EMA(5)[0]>EMA(40)[0]
    && Closes[1][0]>EMA(150)[0])

    {

    EnterLong(DefaultQuantity, "")
    SetTrailStop("", CalculationMode.Ticks,30, true);
    Print(Time[0] +" Enter Long");

    }

    else if (Position.MarketPosition== MarketPosition.Long && Close[0] < EMA (30)[0] )
    {
    ExitLong("EMA > Close");
    Print(Time[0]+" Exit Long");
    return;
    }

    #2
    Hi Loukas,

    You can't use SetStopLoss() and SetTrailStop() in the same position. If both are used, the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.


    If you want dynamic stop loss movement with SetStopLoss, the following sample can help:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the explanation Ryan.
      The code is not compile because the operator "+" cannot be applied to operands of type Cbi.Account and double. if (Close[0]< Position.Account + 30 * TickSize) a part of my code is listed below.
      I didnt understand where the stop loss is setting at the statement "SetStopLoss(CalculationMode.Price, Position.AvgPrice);" Is it the same as an ExitShort because of Position.AvgPrice is equall to current price?

      if ( EMA(5)[0]< EMA(40)[0]
      && Closes[1][0] < EMA(150)[0])
      Print(Time[0] + " Short Momentum");
      {
      EnterShort(DefaultQuantity, "");
      Print(Time[0] + " Enter Short");
      }
      }

      if (Position.MarketPosition== MarketPosition.Short)
      {
      if( Close[0] > EMA (30)[0] )
      {
      ExitShort();
      Print(Time[0]+" Exit Short");

      }
      }

      if (Position.MarketPosition== MarketPosition.Short)
      {
      if (Close[0]< Position.Account + 30 * TickSize)

      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      Print(Time[0]+" Exit Short Close price < 30 Ticks");
      }
      }

      Comment


        #4
        Right, you wouldn't be able to add a numeric value to an account object. There's no need for Position.Account here.

        Position.Avg price is the average price of positions. If you're not currently in a position, it would return 0.

        The sample sets a default value when flat, based on ticks. It does this so that when you first enter a position it won't be invalid from reading Position.AvgPrice as 0. Once you're in a trade, it can use Position.AvgPrice to set the price level of stop losses dynamically.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          But how can I set up a condition when the close price is higher by 30 ticks from the open price.

          Comment


            #6
            Close price is higher than open price by at least 30 ticks:

            if (Close[0] > Open[0] + TickSize * 30) { }
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              By open price I mean the price that I enter a position. For example, during a long position, when the close price is above my enter price by 30*ticks then set a stop loss at my enter price.

              Comment


                #8
                This example is in the stop loss modification sample you've been working with:

                if (Position.MarketPosition == MarketPosition.Long)
                {
                // Once the price is greater than entry price+30 ticks, set stop loss to breakeven
                if (Close[0] > Position.AvgPrice + 30 * TickSize)
                {
                SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                }
                }
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks RyaM,
                  I have few more problems with me strategy.
                  1. I have the following condition in my code
                  if ( EMA(5)[0]< EMA(40)[0]
                  && Closes[1][0] < EMA(150)[0])

                  if I add &&RSI(14,3)>30 doesnt make any different at the execution, do you know where the problem is coming from ? It is compiled well. I have already applied Print statments but i didnt understand what cause the problem. I added more conditions but the execution is the same as I have only the EMAs

                  2. How can I trade 1 contract when the close price cross above the High1 and during that trade if the close price cross above the High2 enter a new trade? My code is below.

                  If( CrossAbove( Close[0],High1,1) || (CrossAbove(Close[0],high2,1)
                  {
                  if ( EMA(5)[0]< EMA(40)[0]
                  && Closes[1][0] < EMA(150)[0])

                  {
                  EnterLong()
                  }

                  I need to write 2 different "if" conditions or do I need to enter the below code? Which is the more effective way to do it?

                  protected override void Initialize()
                  {
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.UniqueEntries;
                  }

                  3. I have applied the stoploss conditions which you had assisted me. I have also added a SetStopLoss("",CalculationMode.Percent,0.01). However, at the simulation performance window there are many trades that they have a loss more than 1%( few off them around 4%). How is this possible when I have applied a stoploss which mean that my worse loss should be at 1%

                  Thank yoou for your time

                  Comment


                    #10
                    Loukas,

                    We unfortunately cannot debug your strategy completely. You will need to simplify and use Print statements and TraceOrders output to track what it is doing. This page can assist with these techniques:


                    We have partners that offer debugging and code development if you are not able to make progress in your strategy. See here for our NinjaScript consultants:


                    Here are a few general guidelines based on your last post:
                    If you are unsure about stop loss as percentage, don't use dynamic stop loss updating with multiple set statements. Use only one SetStopLoss().

                    To scale in with separate entry rules, you need separate conditions evaluating these, as well as an understanding of EntriesPerDirection and EntryHandling properties to allow for multiple entries in the same direction.
                    Ryan M.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by samish18, 04-17-2024, 08:57 AM
                    17 responses
                    64 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by rocketman7, Today, 02:12 AM
                    2 responses
                    16 views
                    0 likes
                    Last Post rocketman7  
                    Started by briansaul, Today, 05:31 AM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by PaulMohn, Today, 03:49 AM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by frslvr, 04-11-2024, 07:26 AM
                    6 responses
                    106 views
                    1 like
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X