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

Help on optimizing my script

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

    Help on optimizing my script

    Hi Guys,

    I was wondering if somebody could be kind enough to advise on how I can optimize the script of my trailing stop loss, maybe using some sort of iteration or indexing:

    double stopLoss = 20 * TickSize;
    if(Close[0] > Position.AvgPrice + stopLoss && Close[0] < Position.AvgPrice + 2 * stopLoss)
    {
    double newStop = Position.AvgPrice + stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 2 * stopLoss && Close[0] < Position.AvgPrice + 3 * stopLoss)
    {
    double newStop = Position.AvgPrice + 2 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 3 * stopLoss && Close[0] < Position.AvgPrice + 4 * stopLoss)
    {
    double newStop = Position.AvgPrice + 3 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 4 * stopLoss && Close[0] < Position.AvgPrice + 5 * stopLoss)
    {
    double newStop = Position.AvgPrice + 4 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }

    #2
    would this work? could somebody advise if the script presents some errors? I am planning in having this in the OnBarUpdate() method:

    Code:
    double stopLoss = 20 * TickSize;
    
    for ( int i = 1; i <10; i++ )
    { 
    	if(Close[0] > Position.AvgPrice + i * stopLoss && Close[0] < Position.AvgPrice + (i+1) * stopLoss)
    		{
    			double newStop = Position.AvgPrice + i * stopLoss;
    			stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    		}
    }
    Originally posted by yades View Post
    Hi Guys,

    I was wondering if somebody could be kind enough to advise on how I can optimize the script of my trailing stop loss, maybe using some sort of iteration or indexing:

    double stopLoss = 20 * TickSize;
    if(Close[0] > Position.AvgPrice + stopLoss && Close[0] < Position.AvgPrice + 2 * stopLoss)
    {
    double newStop = Position.AvgPrice + stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 2 * stopLoss && Close[0] < Position.AvgPrice + 3 * stopLoss)
    {
    double newStop = Position.AvgPrice + 2 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 3 * stopLoss && Close[0] < Position.AvgPrice + 4 * stopLoss)
    {
    double newStop = Position.AvgPrice + 3 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }
    if(Close[0] > Position.AvgPrice + 4 * stopLoss && Close[0] < Position.AvgPrice + 5 * stopLoss)
    {
    double newStop = Position.AvgPrice + 4 * stopLoss;
    stopOrder = ExitLongStop(0,true,newStop,"stop","long");
    }

    Comment


      #3
      With out testing your code and just reading it:

      Do it in reverse order with else if statements.

      If you use your for loop in reverse order, you will need to break out or exit when the first condition hits. You will hve to look up that command in c#.


      Originally posted by yades View Post
      Hi Guys,

      I was wondering if somebody could be kind enough to advise on how I can optimize the script of my trailing stop loss, maybe using some sort of iteration or indexing:

      double stopLoss = 20 * TickSize;
      if(Close[0] > Position.AvgPrice + stopLoss && Close[0] < Position.AvgPrice + 2 * stopLoss)
      {
      double newStop = Position.AvgPrice + stopLoss;
      stopOrder = ExitLongStop(0,true,newStop,"stop","long");
      }
      if(Close[0] > Position.AvgPrice + 2 * stopLoss && Close[0] < Position.AvgPrice + 3 * stopLoss)
      {
      double newStop = Position.AvgPrice + 2 * stopLoss;
      stopOrder = ExitLongStop(0,true,newStop,"stop","long");
      }
      if(Close[0] > Position.AvgPrice + 3 * stopLoss && Close[0] < Position.AvgPrice + 4 * stopLoss)
      {
      double newStop = Position.AvgPrice + 3 * stopLoss;
      stopOrder = ExitLongStop(0,true,newStop,"stop","long");
      }
      if(Close[0] > Position.AvgPrice + 4 * stopLoss && Close[0] < Position.AvgPrice + 5 * stopLoss)
      {
      double newStop = Position.AvgPrice + 4 * stopLoss;
      stopOrder = ExitLongStop(0,true,newStop,"stop","long");
      }

      Comment


        #4
        or:
        int StopStep = (close-AP)/SL
        double newStop = Position.AvgPrice + stopLoss * StopStep ;

        Comment


          #5
          Hello yades,

          It is mainly going to depend on how you are going to want your code to be processed, in how you would optimize it. "if else" statements would be a good option for this like sledge has suggested since they appear to be the same exit order.

          With that said, most scripts unless very complex you would not notice much of a difference in optimizing, but is a good practice.
          JCNinjaTrader Customer Service

          Comment


            #6
            Originally posted by Baruch View Post
            or:
            int StopStep = (close-AP)/SL
            double newStop = Position.AvgPrice + stopLoss * StopStep ;
            not sure how to use that.. whould this make sense?
            Code:
            void OnBarUpdate()
            {
            for ( int i = 1; i <10; i++ )
            { 
            	if(Close[0] > Position.AvgPrice + i * stopLoss && Close[0] < Position.AvgPrice + (i+1) * stopLoss)
            		{
            			double newStop = Position.AvgPrice + i * stopLoss;
            			stopOrder = ExitLongStop(0,true,newStop,"stop","long");
            		}
            }
            }

            Comment


              #7
              You don't need loops, just what I wrote. (Just make correct syntax)
              You wanted to optimize.

              Comment


                #8
                Hi JC, thanks for your reply. I probably didn't express myself in the proper manner. Rather than optimize, I meant shorten my code, as this is part of an extensive code and I am looking to shorten the number of lines. What I am doing is trailing my stop loss in order to lock in profit on each bar update when I gain a multiple of my original stop loss, I was wondering if something like this would work:

                Code:
                void OnBarUpdate()
                {
                for ( int i = 1; i <10; i++ )
                { 
                	if(Close[0] > Position.AvgPrice + i * stopLoss && Close[0] < Position.AvgPrice + (i+1) * stopLoss)
                		{
                			double newStop = Position.AvgPrice + i * stopLoss;
                			stopOrder = ExitLongStop(0,true,newStop,"stop","long");
                		}
                }
                }
                Originally posted by NinjaTrader_JC View Post
                Hello yades,

                It is mainly going to depend on how you are going to want your code to be processed, in how you would optimize it. "if else" statements would be a good option for this like sledge has suggested since they appear to be the same exit order.

                With that said, most scripts unless very complex you would not notice much of a difference in optimizing, but is a good practice.

                Comment


                  #9
                  Baruch, I am not sure we are talking the same language. What are you refering to with AP? is this the AvgPrice? and is SL the stopLoss? If the answer is yes to both this code returns exactly the close value:
                  int StopStep = (close-AP)/SL
                  double newStop = Position.AvgPrice + stopLoss * StopStep ;
                  example:
                  AvgPrice 1.30
                  Close 1.3060
                  stopLoss 0.005
                  (initial stop loss = 1.2950)

                  What I need is to trail the stop to 1.3050, whilst using StopStep = (close-AP)/SL = (1.3060-1.30)/0.005 =1.2 , therefore newStop = Position.AvgPrice + stopLoss * StopStep = 1.305+0.005*1.2 = 1.3060

                  Please correct me if I am misreading you

                  Originally posted by Baruch View Post
                  You don't need loops, just what I wrote. (Just make correct syntax)
                  You wanted to optimize.

                  Comment


                    #10
                    Hello yades,

                    Thanks for the clarification.

                    Yes, using a "for" loop will be the least amount of lines used and it does look like it will work.
                    JCNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by yades View Post
                      Baruch, I am not sure we are talking the same language. What are you refering to with AP? is this the AvgPrice? and is SL the stopLoss? If the answer is yes to both this code returns exactly the close value:


                      example:
                      AvgPrice 1.30
                      Close 1.3060
                      stopLoss 0.005
                      (initial stop loss = 1.2950)

                      What I need is to trail the stop to 1.3050, whilst using StopStep = (close-AP)/SL = (1.3060-1.30)/0.005 =1.2 , therefore newStop = Position.AvgPrice + stopLoss * StopStep = 1.305+0.005*1.2 = 1.3060
                      Please correct me if I am misreading you
                      You are absolutely correct. The only mistake you made is that StopStep is not an integer as I requested.

                      Comment


                        #12
                        Hmmm v v smart! I must say thanks, as this simplifies my logic massivly
                        Originally posted by Baruch View Post
                        You are absolutely correct. The only mistake you made is that StopStep is not an integer as I requested.

                        Comment


                          #13
                          HI sledge, I opted for a shorter route, however I want to make sure I understand what you mean. Assuming I would use the for loop. Do you recon there would be any change in the output by using reverse order compared to not using it, below the 2 cases:

                          Code:
                          [B]for ( int i = 1; i <10; i++ )[/B]
                          { 
                          	if(Close[0] > Position.AvgPrice + i * stopLoss && Close[0] < Position.AvgPrice + (i+1) * stopLoss)
                          		{
                          			double newStop = Position.AvgPrice + i * stopLoss;
                          		}
                          }
                          
                          OR 
                          REVERSE for LOOP
                          [B]for ( int i = 10; i >0; i-- )[/B]
                          { 
                          	if(Close[0] > Position.AvgPrice + i * stopLoss && Close[0] < Position.AvgPrice + (i+1) * stopLoss)
                          		{
                          			double newStop = Position.AvgPrice + i * stopLoss;
                          		}
                          }
                          Originally posted by sledge View Post
                          With out testing your code and just reading it:

                          Do it in reverse order with else if statements.

                          If you use your for loop in reverse order, you will need to break out or exit when the first condition hits. You will hve to look up that command in c#.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Harry, 05-02-2018, 01:54 PM
                          10 responses
                          3,203 views
                          0 likes
                          Last Post tharton3  
                          Started by cre8able, Yesterday, 01:16 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post cre8able  
                          Started by ChartTourist, Today, 08:22 AM
                          0 responses
                          6 views
                          0 likes
                          Last Post ChartTourist  
                          Started by LiamTwine, Today, 08:10 AM
                          0 responses
                          2 views
                          0 likes
                          Last Post LiamTwine  
                          Started by Balage0922, Today, 07:38 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post Balage0922  
                          Working...
                          X