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

Multiple Stock / New strategy Issue

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

    Multiple Stock / New strategy Issue

    Hey guys.

    I am posting here a very basic question but I've been trying to make this work and its not making any sense to me let me type you the questions I have so far:


    - I wanted this comparison below to be in percentage, where I define that?

    if (Close[1] > Closes[1][1])
    {
    fuzzy += 1.00;
    }

    - Add("BVSP", PeriodType.Day, 1); I did this to have an external stock used to compare with my stock list but instead of that the backtest is being ran with that symbol too which makes no sense to me, I wanted to have that instrument just for a comparison according with my code.

    - EnterShort(DefaultQuantity, "");

    - The backtest returns me very odd results and sometimes even buys and short this stock I added above not the one is being ran on backtest, why?

    - I set ExitOnClose true but it never does exit only when it reaches final date, why?

    Check the prints I am providing on output.txt the test is being run with "BVSP", again I DO NOT WANT THAT, I only followed the Add documentation to Add new stocks.
    Please try helping me on this .cs file is attached

    Any help is appreciated I am pretty new to your platform
    Attached Files

    #2
    Hello Artarian,

    Thank you for your note.

    Are you wanting the Close[1] compared to a percentage of Closes[1][1]?
    What kind of percentage are you looking to compare?

    When you add an additional data series it will add an additional Bars object which will call OnBarUpdate() and process any orders the same way as the Primary Bars object.

    What you would want to do is use the overload of the order methods that use the specific BarsInProgress check.

    Example -
    EnterShort(0, DefaultQuantity, "");

    The 0 indicates that the order will be submitted to the Primary Bars Object or in your case the one that you are wanting to run the backtest on.

    More information on Multi-Timeframes and Instruments can be read at the link below -
    http://www.ninjatrader.com/support/h...nstruments.htm

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Remaining questions

      Hey there! Thank you so much for your answer, it worked like a charm BUT it answered 1 of my 4 questions let me redo that:

      - I wanted this comparison below to be in percentage, where I define that?
      if (Close[1] > Closes[1][1])
      {
      fuzzy += 1.00;
      }
      Are you wanting the Close[1] compared to a percentage of Closes[1][1]?
      What kind of percentage are you looking to compare?
      the % change like AAPL -2.32% and CSCO +0.59% if -2.32% > 0.59% do something
      How to add external stocks to be compared with without making it participate of my backtest? Cant understand how a simply stock comparison to another would affect this much? I just want to know an external benchmark data.
      Give me please an example of exiting position on the bar close price: check my question again:
      - I set ExitOnClose true but it never does exit only when it reaches final date, why?
      I wanted that position to be closed on same day?

      Comment


        #4
        Artarian,

        I think you are looking for something like this for the percentage -
        Code:
        double aaplPercentageDiff = (Close[0] - Closes[1][1]) / Closes[1][1];
        double cscoPercentageDiff = (Closes[2][0] - Closes[3][1]) / Closes[3][1];
        
        if(aaplPercentageDiff > cscoPercentageDiff)
        {
            // Do something here
        }
        You would want to use BarsInProgress of the overload of the orders so that you are only submitting the orders to the correct Instrument.

        You can also check for BarsInProgress in the OnBarUpdate() and only have it calculate logic based on that.

        Code:
        if(BarsInProgress == 0) //check for primary update
        {
           // Enter Trade logic here
        }
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          The only instrument I am adding is BVSP so I dont get why:

          double aaplPercentageDiff = (Close[0] - Closes[1][1]) / Closes[1][1];
          double cscoPercentageDiff = (Closes[2][0] - Closes[3][1]) / Closes[3][1];

          if(aaplPercentageDiff > cscoPercentageDiff)
          {
          // Do something here
          }

          I see it like:

          double aaplPercentageDiff = (Close[0] - Close[1]) / Close[1];
          Isnt the index above referred to the instrument I am backtesting?
          double cscoPercentageDiff = (Closes[1][0] - Closes[1][1]) / Closes[1][1];
          Isnt the index above referred to the instrument I've added?

          Orders are fine I did enter long with defining 0 as paremeter

          EnterLong(DefaultQuantity, "");

          Would you please let me know how to leave a position in the end of the day? Thats my final question

          Comment


            #6
            Artarian,

            OnBarUpdate() is getting called for every bar object including the BVSP.

            You need to filter out which bar update you want to use.

            If it is just the primary then you can use if(BarsInProgress == 1) return; in the beginning of your OnBarUpdate() method.

            This will stop the code from running if there is an bar update from the secondary bar series.

            For the end of day, the ExitOnClose set to enabled will have the strategy exit any open positions at the end of the session.
            Last edited by NinjaTrader_CalH; 11-18-2014, 09:47 AM.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              if(BarsInProgress == 0)

              Made the trick solving the confusion between the two stocks, now I also know how to play between them in percentages thanks to you, trust me this thread will be very helpful for another Ninjatrader beginners.

              I still dont know how to close my position in the end of EACH day a trade came in.

              if (fuzzy >= 1.50)
              {
              EnterLong(DefaultQuantity, "");
              fuzzy = 0;
              }
              else if (fuzzy <= 1.50)
              {
              EnterShort(0, DefaultQuantity, "");
              fuzzy = 0;
              }
              I wanted this trades to be closed on the end of each day, instead of that the parameter u suggested:

              For the end of day, the ExitOnClose set to enabled will have the strategy exit any open positions at the end of the session.

              Close the trade on the end of backtest

              Comment


                #8
                Guys can anybody help me?

                if (fuzzy >= 1.50)
                {
                EnterLong(DefaultQuantity, "");
                fuzzy = 0;
                }
                else if (fuzzy <= 1.50)
                {
                EnterShort(0, DefaultQuantity, "");
                fuzzy = 0;
                }

                ExitOnClose parameter is not working for me, it exits only after end period of my backtest

                Comment


                  #9
                  Hello artarian,

                  Thank you for your patience.

                  You are testing on 1 Day bars, correct? ExitOnClose will not work on the Day bars. If you want to close at the end of the bar you can use BarsSinceEntry(): http://www.ninjatrader.com/support/h...sinceentry.htm
                  However, in backtesting or when using CalculateOnBarClose = True this will keep the position over the entry bar and then exit on the next bar.

                  Comment


                    #10
                    Yes I am trading daily basis.

                    In which region I should include BarsSinceEntry();?

                    I included in final of my code and returned me 0 trades.

                    And


                    CalculateOnBarClose = true;
                    Didnt change it from 1 trade from beginning to end


                    Here's my code


                    if (fuzzy >= 1.50)
                    {
                    if (BarsSinceEntry() > 1);
                    {
                    EnterLong(DefaultQuantity, "");
                    CalculateOnBarClose = true;
                    }
                    fuzzy = 0;
                    }
                    else if (fuzzy <= 1.50)
                    {
                    if (BarsSinceEntry() > 1);
                    {
                    EnterShort(0, DefaultQuantity, "");
                    CalculateOnBarClose = true;
                    fuzzy = 0;
                    }

                    }
                    Last edited by artarian; 11-18-2014, 06:25 PM.

                    Comment


                      #11
                      artarian,

                      The BarsSinceEntry() are meant to be used for your exit condition in this case.

                      Also, your conditions would never be true as you haven't entered a position and thus BarsSinceEntry would never be true for your entry conditions
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        Still get the issue

                        PHP Code:
                                if (fuzzy >= 1.50)
                                        {
                                            
                                            
                        EnterLong(DefaultQuantity"");
                                            
                        BarsSinceEntry();
                                            
                        CalculateOnBarClose true;
                                            
                                            
                        fuzzy 0;
                                        }
                                        else if (
                        fuzzy <= 1.50)
                                        {
                                            
                                            
                        EnterShort(0DefaultQuantity"");
                                            
                        BarsSinceEntry();
                                            
                        fuzzy 0;
                                            
                        CalculateOnBarClose true;
                                            
                                            
                                        } 
                        This also returns me NO TRADE, cant be that complex to have ur position closed on end of each bar. I just wanted to close my position (if it was opened) in the end of each day and there's no such clear sample on SinceBarsEntry(); on documentation

                        Please I've been stucked on this for 3 days


                        BR

                        Comment


                          #13
                          Artarian,

                          You need to have an initial entry for either Short or Long before using the BarsSinceEntry(), which is also used in the wrong context.

                          I have attached a sample that will outline how to do this from the script.

                          Please see condition 7 for the information.
                          Attached Files
                          Cal H.NinjaTrader Customer Service

                          Comment


                            #14
                            I am running that now and will post the results.

                            That did NOT work, I still enter on beginning of backtest and exit only on final date of it, code is attached

                            Why do you want BarSinceEntry > 2 ? Cant that be tested to close the position in the end of the day? To reproduce REAL scenario isnt that the backtest purpose? or is this a limitation from platform?
                            Attached Files
                            Last edited by artarian; 11-20-2014, 10:19 AM.

                            Comment


                              #15
                              To make this thread readable for anybody

                              Hello guys from Ninja community.

                              I have this EOD prices script attached, all I wanted help with is:

                              I need to Enter long/short when script conditions tells me to do it means on that bar I'll buy or sell on the open price and at close(in the end of the day) my position.

                              If my condition sets true buy at open price of that bar and sell it at close price of that bar(Day trade).

                              Best of all

                              @NinjaTrader_Cal

                              Your region
                              PHP Code:
                                  else if(Position.MarketPosition != MarketPosition.Flat
                                                  
                              && firstTrade == false)
                                              {
                                                  
                              // Check for our fuzzy value, the position is short and that we at 
                                                  // least 2 bars from entry
                                                  
                              if (fuzzy >= 1.50
                                                      
                              && Position.MarketPosition == MarketPosition.Short
                                                      
                              && BarsSinceEntry() > 2)
                                                  {
                                                      
                              EnterLong(DefaultQuantity"");
                                                      
                              fuzzy 0;
                                                  }
                                                  else if(
                              fuzzy <= 1.50
                                                      
                              && Position.MarketPosition == MarketPosition.Long
                                                      
                              && BarsSinceEntry() > 2)
                                                  {
                                                      
                              EnterShort(DefaultQuantity"");
                                                      
                              fuzzy 0;
                                                  }
                                              } 
                              Returns me this error:

                              **NT** Error on calling 'OnBarUpdate' method for strategy 'Vinicius2/0d80c800238c4ead86e8c407815f1958': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.

                              Also I dont want to validate nothing as I said above, ALL I WANTED is when this conditions set true to buy at bar open price and sell it on close price, why two bars?
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Quanto, 04-17-2024, 10:43 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post Quanto
                              by Quanto
                               
                              Started by Irukandji, Today, 03:06 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Irukandji  
                              Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
                              17 responses
                              91 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Started by nleitman, Today, 11:46 AM
                              8 responses
                              20 views
                              0 likes
                              Last Post nleitman  
                              Started by Option Whisperer, 03-20-2024, 07:01 AM
                              3 responses
                              69 views
                              0 likes
                              Last Post ScalpRule  
                              Working...
                              X