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

SopLoss and draw dor not working

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

    SopLoss and draw dor not working

    Hello,

    I am trying to create a strategy. It is done but I can't add a %stop and I would like to use draw dots in the stopl loss level. This is what I have:

    protected override void OnBarUpdate()
    {

    //Registramos el numero total de operaciones en el iniciio de la sesion
    if (Bars.FirstBarOfSession)
    {
    NumAntOperaciones = Performance.AllTrades.Count;
    }
    //Comprobamos que no se ha alcanzado el maximo numero de operaciones diario.
    if (Performance.AllTrades.Count - NumAntOperaciones >= NTrades)
    {
    return;
    }

    double SRLargos = Position.AvgPrice - Position.AvgPrice * (stopRisk / 100);
    double SRCortos = Position.AvgPrice + Position.AvgPrice * (stopRisk / 100);

    Then I have my conditions

    And finally this:

    //Stop Largos
    ExitLongStop(SRLargos,"Stop PCT", "");
    DrawDot(CurrentBar.ToString() + "largos",true, 0,SRLargos, Color.Red);

    //Stop Cortos

    ExitShortStop(SRCortos,"Stop PCT", "");
    DrawDot(CurrentBar.ToString() + "cortos" ,true,0,SRCortos, Color.Green);

    When I do the backtest this is what I got, and I realzie the Stop loss is not working because I have huge losses and they shouldn't be that size thanks to the stop. Also in the chart tab the chart is so small...you will see. I attach the picture

    Thanks
    Attached Files

    #2
    Hello dvercher,
    You are trying to draw the dot before a price has been assigned to it. You can draw the dot on the OnOrderUpdate event. A sample code will be like

    Code:
    protected override void OnOrderUpdate(IOrder order)
    {
    	if (order != null && (order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted) && order.Name == "Stop PCT")
    		DrawDot(CurrentBar.ToString() + "cortos" ,true,0,order.StopPrice, Color.Green);
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Not sure if I understood well, but I get the same problem. This is what I changed:

      #region FLAT
      if (Position.MarketPosition == MarketPosition.Flat)

      My Conditions



      #endregion




      #region LONG
      if (Position.MarketPosition == MarketPosition.Long)

      //Stop Largos
      ExitLongStop(SRLargos,"Stop PCT", "");
      DrawDot(CurrentBar.ToString() + "largos",true, 0,SRLargos, Color.Red);

      #endregion


      #region SHORT
      if (Position.MarketPosition == MarketPosition.Short)
      //Stop Cortos

      ExitShortStop(SRCortos,"Stop PCT", "");
      DrawDot(CurrentBar.ToString() + "cortos" ,true,0,SRCortos, Color.Green);

      #endregion

      Any idea?

      Thanks

      Comment


        #4
        Hello dvercher,
        To assist you further may I know:
        Are you putting the DrawDot code in the OnBarUpdate event or in the OnOrderUpdat event?

        I look forward to assisting you further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Hi,

          It is unde:

          protected override void OnBarUpdate()

          Thanks

          Comment


            #6
            Hello dvercher.
            Please move the DrawDot code to the OnOrderUpdate event as I mentioned in this post and see if you can get the dot drawn on the right place.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              I attach a picture but now I get errors when compiling

              Thanks
              Attached Files

              Comment


                #8
                Hello dvercher,
                You have placed the OnOrderUpdate codes within the OnBarUpdate method

                OnOrderUpdate is a method which handles the Order update event args and should be placed separately

                Code:
                protected override void OnBarUpdate()
                {
                   //all OnBarUpdate codes goes here
                }
                
                protected override void OnOrderUpdate(IOrder order)
                {
                     //on OnOrderUpdate codes goes here
                }
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hi Joydeep,

                  I just want to put a porcentual stop loss with the draw dots, I know it is simple and I have some examples of how to do it, the thing is that none of the examples use what you are trying to tell me.

                  I appreciate your help but what you are asking me is too hard for me and I guess there is a much easier way of doing it than using Iorders.

                  Any idea of how to do it? If not it is ok. Thanks for your time and help

                  Comment


                    #10
                    Hello dvercher,
                    Please find the attach code which depicts how to draw a dot when a stop order is placed using the Set() method. You have to modify it further to suit your need.
                    Attached Files
                    JoydeepNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Rapine Heihei, 04-23-2024, 07:51 PM
                    2 responses
                    30 views
                    0 likes
                    Last Post Max238
                    by Max238
                     
                    Started by Shansen, 08-30-2019, 10:18 PM
                    24 responses
                    942 views
                    0 likes
                    Last Post spwizard  
                    Started by Max238, Today, 01:28 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post Max238
                    by Max238
                     
                    Started by rocketman7, Today, 01:00 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post rocketman7  
                    Started by wzgy0920, 04-20-2024, 06:09 PM
                    2 responses
                    28 views
                    0 likes
                    Last Post wzgy0920  
                    Working...
                    X