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

Can you please look at this strategy and see what is wrong?

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

    Can you please look at this strategy and see what is wrong?

    Can you please help me figure out why the following strategy is always in a position? It should only be in a position if the rules are met. But, as soon as one is closed, the next one opens up. Please help!!

    Here is the code:

    namespace NinjaTrader.Strategy
    {
    public class testv1 : Strategy
    {
    #region Variables
    private DataSeries movAvgVal;
    private DataSeries movAvgVal2;
    private DataSeries tmp_avg;

    private int avgLength = 40;
    private int atrLength = 40;
    private double upBand = 0;
    private double dnBand = 0;

    #endregion

    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    movAvgVal = new DataSeries(this);
    movAvgVal2 = new DataSeries(this);
    tmp_avg = new DataSeries(this);
    }

    protected override void OnBarUpdate()
    {
    tmp_avg[0] = (High[0] + Low[0] + Close[0])/3;
    if(CurrentBar < 41) return;
    movAvgVal[0] = SMA(tmp_avg,40)[0];
    movAvgVal2[0] = SMA(tmp_avg,40)[0];
    upBand = movAvgVal[0] + ATR(atrLength)[0];
    dnBand = movAvgVal[0] - ATR(atrLength)[0];

    if ( movAvgVal[0] > movAvgVal[1] )
    {
    EnterLongStop(upBand, "Long Buy");

    }

    else if ( movAvgVal[0] < movAvgVal[1] )
    {
    EnterShortStop(dnBand, "Short Buy");
    }

    if ((Position.MarketPosition == MarketPosition.Long))
    {
    ExitLongStop(movAvgVal2[0], "Long Exit");
    }


    else if ((Position.MarketPosition == MarketPosition.Short))
    {
    ExitShortStop(movAvgVal2[0], "Buy to Cover");
    }
    }

    #2
    You do this when you are trying to assign values: tmp_avg[0] = blah blah.

    This is incorrect. Please see the Help Guide article on DataSeries. You want to use .Set to do it:

    tmp_avg.Set(values);
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      thanks for the quick reply. I'm reading about it right now.






      Originally posted by Josh View Post
      You do this when you are trying to assign values: tmp_avg[0] = blah blah.

      This is incorrect. Please see the Help Guide article on DataSeries. You want to use .Set to do it:

      tmp_avg.Set(values);

      Comment


        #4
        Josh,

        I read the DataSeries section, made the changes as you suggested, but still got the same results.

        Here is the section I changed:

        protected override void OnBarUpdate()
        {

        tmp_avg.Set((High[0] + Low[0] + Close[0])/3);
        if(CurrentBar < 41) return;
        movAvgVal.Set(SMA(tmp_avg,40)[0]);
        movAvgVal2.Set(SMA(tmp_avg,40)[0]);

        upBand = movAvgVal[0] + ATR(atrLength)[0];
        dnBand = movAvgVal[0] - ATR(atrLength)[0];
        .....



        Originally posted by Josh View Post
        You do this when you are trying to assign values: tmp_avg[0] = blah blah.

        This is incorrect. Please see the Help Guide article on DataSeries. You want to use .Set to do it:

        tmp_avg.Set(values);

        Comment


          #5
          You will need to debug your strategy then. Please use Print() throughout your code and check the values of your conditions. See these two tips: http://www.ninjatrader-support.com/v...ead.php?t=3418
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            I'm working on this right now. Thanks. I'll let you know how it goes.


            Originally posted by Josh View Post
            You will need to debug your strategy then. Please use Print() throughout your code and check the values of your conditions. See these two tips: http://www.ninjatrader-support.com/v...ead.php?t=3418
            http://www.ninjatrader-support.com/v...ead.php?t=3627

            Comment


              #7
              After some serious debugging and print statements, I think I found the problem. However, I can't seem to fix it.

              The problem appears to be with my statements to exit the long position and short position:

              ExitLongStop(movAvgVal2[0], "Long Exit");

              If I change these to ExitLong() and ExitStop(), it gets out the way it should, HOWEVer, it gets out at the wrong price. It waits until the close to exit. It should exit when the current price crosses Moving Average.

              I modified the code to:
              if ((Position.MarketPosition == MarketPosition.Long) && Low[0] < SMA(20)[0])
              {
              ExitLong();
              }

              but, it still doesn't work. I tried every combination of ExitLongStop, ExitLongStopLimit, ExitLimit, etc, but no luck.

              Please help.


              Thanks.

              Originally posted by Josh View Post
              You will need to debug your strategy then. Please use Print() throughout your code and check the values of your conditions. See these two tips: http://www.ninjatrader-support.com/v...ead.php?t=3418
              http://www.ninjatrader-support.com/v...ead.php?t=3627

              Comment


                #8
                If you want your strategy to process immediately as opposed to once at the end of each bar you will want to change CalculateOnBarClose to false.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I will try that.


                  Originally posted by Josh View Post
                  If you want your strategy to process immediately as opposed to once at the end of each bar you will want to change CalculateOnBarClose to false.

                  Comment


                    #10
                    I changed it to False, but I get the exact same results. Nothing changed. It's still doing the same thing. It will wait until the close, and then exit at the open of the next bar.

                    Originally posted by Josh View Post
                    If you want your strategy to process immediately as opposed to once at the end of each bar you will want to change CalculateOnBarClose to false.
                    Last edited by paco99; 08-04-2008, 12:59 AM.

                    Comment


                      #11
                      If you are backtesting this is expected behavior. Please see the Help Guide article on discrepancies between backtesting and real-time.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks. I will read that. Can you please confirm if my code will exit the trade at the moment (or close to it) when the price crosses the SMA(20)?


                        if ((Position.MarketPosition == MarketPosition.Long) && Low[0] < SMA(20)[0])
                        {
                        ExitLong();
                        }

                        My concern is the "Low[0]" part. I want it to exit as soon as the
                        price hits the SMA(20), and not wait for the close. will my code do it
                        above?

                        Thanks!



                        Originally posted by Josh View Post
                        If you are backtesting this is expected behavior. Please see the Help Guide article on discrepancies between backtesting and real-time.

                        Comment


                          #13
                          Looks ok to me. If you are not sure, then you should debug your strategy as per here: http://www.ninjatrader-support.com/v...ead.php?t=3418

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by CortexZenUSA, Today, 12:53 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by CortexZenUSA, Today, 12:46 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by usazencortex, Today, 12:43 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post usazencortex  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          168 responses
                          2,265 views
                          0 likes
                          Last Post sidlercom80  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X