Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is There a way to create a flip flop switch with NT?

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

    Is There a way to create a flip flop switch with NT?

    Hello!

    I need some help with a switch device. Let me try to explain (sorry for my bad english):


    The system I'm trying to write is not exactly this one, but it uses a similar mechanism.

    I use 2 variables.

    BuyModeOn and ShortModeOn. When BuyModeOn == 1 and theres a buy signal, I want the system to execute my buy. When BuyModeOn == 0, I want the system to ignore my buy signals. The same with ShortModeOn.

    So, lets say BuyModeOn goes 1 when Price moves above an MA. The code I tried is like this:



    Code
    Code:
    OnBarUpdate()
    
    int BuyModeOn = 0;
    int ShortModeOn = 0;
    EMA1 = EMA(10);
    EMA2 = EMA(100);
    
    if (Close[1] < EMA1[1]  && Close[0] > EMA1[0]) //its a cross above
    {
       BuyModeOn = 1; //I want it to remain on even if it cross below EMA1 again!
       ShortModeOn = 0;
       if (//buycondition at the same bar...)
       {
          EnterLong()
       }
    }
    
    if (Close[1] > EMA2[1]  && Close[0] < EMA2[0]) //its a cross below
    {
       BuyModeOn = 0; //It only becames ShortMode when cross below EMA2;
       ShortModeOn = 1;
       if (//shortcondition at the same bar...)
       {
          EnterShort()
       }
    }
    
    if (BuyModeOn == 1 && buycondition...)
    {
       EnterLong()
    }
    
    if (ShortModeOn == 1 && shortcondition...)
    {
       EnterShort()
    }
    So, the code works... but only at the EXACTLY time of the cross above or below. It seems like the ModeOn values dont hold for the rest of the bars. Why is that? Do I need to make it inside a loop?

    PS: While I was writing the thread, I just had the though that its because the variables are inside the OnBarUpdate(), initially setted to 0... So in every bar it becomes as 0... I'll try it soon, but I'll post it anyway... I may be wrong after all.

    Thanks in advance!!
    Last edited by Cadelao; 07-28-2014, 07:02 PM.

    #2
    Cadelao, welcome to our forums here - did you get a chance to test your PS statement? I think that's exactly what's happening here.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Yes. I tried putting it in the Variables section. It compiles with no errors, but I cant activate the strategy in the chart. I set ENEBLED TRUE, click OK, and it doesnt activate.

      Comment


        #4
        Thanks, any error shown in your NT log tab afterwards? If you increase the amount of data loaded and then try to enable it again, any different outcome seen for your setup?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I managed to solve the problem. It really was because the variables were inside the OnBarUpdate() Method. When I put it in the Variables section, it worked.

          So I started writing another strategy. I usually always use variables like BuyModeOn etc. For this new strategy, as I was writing the code, I saw the need to create a lot of new variables. Then I just declared them in the Variables. No Wizard here, only script writing.

          When I tried to compile, it showed errors, even in the strategy I had finished and compiled before!

          Yesterday the error was:

          "The name "BuyModeOn" does not exist in the current context"

          Ok... I saved, closed and went to sleep. Today I tried to compile again... This time, the error was:

          The type 'NinjaTrader.Strategy.MyStrategy10' already contains a definition for "Factor".

          Ok... I dont know why it changes so much. Actually, can anyone explain how to use variables? Do I need to create the variable with Uppercase? And use it with Uppercase? Do I need to create something in the Propriets section? I already seached but found nothing about these details...

          Well, I'm going to reinstall NT7. See if it solve.

          Comment


            #6
            Thanks for getting back to us Cadelao, generally all NinjaScript files are compiled into one assembly to be used at runtime, so all scripts would have to be error free - it's no unusual to see a compile error in a seemingly not related file showing up. So there should be no need now to reinstall any part of NT. Just run a compile and the double click on the error you get, the editor will take you to the offending section then.

            Is Factor a user input for your script?

            The the usual convention would be having the public property in uppercase, with the associated variable lowercase.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Yes. Factor is a variable I created to be edited in the parameters section.

              So I should use it like this:

              #Variables
              LowerCase..

              OnBarUpdate()
              LowerCase...

              #Propriets
              Uppercase...

              ???

              Comment


                #8
                Here's a great link to introduce you to the syntax and setup needed - http://www.ninjatrader.com/support/f...ead.php?t=5782

                In OnBarUpdate() you would refer to your uppercase public property as well then.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Thanks, Bertrand.

                  So, I reinstalled NT7. A clean install. The only thing I did was set up my data feed connection. Then I created a new strategy, unlocked the code, erased all the lines and pasted the code that was giving error. Tried to compile and it returned the same error.

                  Do you know what it is?

                  Comment


                    #10
                    Which code snippet did you exactly try to use? Would you mind attaching it here or emailing us the cs file via Help > Mail to Support?

                    Thanks
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      This is the part of the code where FACTOR variable appears.

                      Code:
                      #region Variables
                      private double factor = 0.8;
                      private int buyprice = 0;
                      private int shortprice = 0;
                      
                      OnBarUpdate()
                      
                      if (something...)
                      {
                           BuyPrice = High[0]+((High[0]-Low[0])*Factor);
                      }
                      
                      if (something...)
                      {
                           ShortPrice = Low[0]-((High[0]-Low[0])*Factor);
                      }
                      
                      #region Properties
                      
                      [Description("")]
                      [GridCategory("Parameters")]
                      public double Factor //THATS THE LINE IT ACUSES ERROR.
                      {
                      get { return factor; }
                      set { factor = Math.Max(0.000, value); }
                      }
                      Last edited by Cadelao; 07-31-2014, 09:32 AM.

                      Comment


                        #12
                        Thanks Cadelao, that code part appears just fine.

                        If the uppercase Factor is the issue, do you have that perhaps double defined? Perhaps as variable as well?
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks, Bertrand. I managed to make it work.

                          The problem was the type of data. I didnt know price data was double type, and it was somehow causing that error when trying to assign to high1 or low1.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          4 responses
                          21 views
                          0 likes
                          Last Post alifarahani  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          16 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by PhillT, Today, 02:16 PM
                          2 responses
                          7 views
                          0 likes
                          Last Post PhillT
                          by PhillT
                           
                          Started by Kaledus, Today, 01:29 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by frankthearm, Yesterday, 09:08 AM
                          14 responses
                          47 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Working...
                          X