Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

entry - condition

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

    entry - condition

    Hello,

    I have a question - maybe this is ridiculous - I can´t find the solution:

    How can I have a if(...) {....;} when the if(...) should be the entry of a trade? Of course with conditions everything is clear but within a strategy I need to have a variable or tradecounter++ only when the entry is done (and not referring to the conditions for the entry)

    Thanks
    Tony

    #2
    tonynt,
    you can refer to this example for your need http://www.ninjatrader.com/support/f...ad.php?t=19182

    alternatively you can assign the value of tradeCounter in OnExecution or OnPositionUpdate when the entry gets Filled like
    Code:
    protected override void OnExecution(IExecution execution)
    {
    	if (execution.Order.OrderState == OrderState.Filled)
    		tradecounter++;
    }

    Comment


      #3
      Bukkan,

      thank you for your reply. The sample with tradelimiter I know and this is how I do now. But this doesn´t work when there are 2 condition-setups true (and including "flat") then because of the condition true it is counted++ both setups but only one entry.

      Thank you for your alternatively "OnExecution". This is logical but probably there´s an error on my end because when I do so there is only 1 entry while a certain variable is true (eg HMA55 falling) and after initializing the variable with HMA55 rising then againg there is only one entry while HMA55 falling - no matter which value I set for variable maxTrades. (???)

      Thanks
      Tony

      Comment


        #4
        logic is a logic. if logic is right and coded right it will work.

        what exactly you are trying to do and if possible pls post the code.

        Comment


          #5
          Thank you for your reply,

          the interesting thing is that when I set maxtrades to 6 I have less # of trades than with maxtrades 2

          Here is the basic code (without ATM....)
          public class basic1 : Strategy
          {
          #region Variables
          double stop1 = 0;
          double stop2 = 0;
          double stop3 = 0;
          double stop4 = 0;


          private int tradeCounter = 0;
          private int maxTrades = 6;


          #endregion

          /// <summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          /// </summary>
          protected override void Initialize()
          {
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.UniqueEntries;


          Add(PeriodType.Range,210);//1
          Add(PeriodType.Range,130);//2
          Add(PeriodType.Range,80);//3
          Add(PeriodType.Range,70);//4
          Add(PeriodType.Range,50);//5
          Add(PeriodType.Range,40);//6
          Add(PeriodType.Range,30);//7
          Add(PeriodType.Minute,1);//8

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {if (BarsInProgress !=0)
          return;


          stop1=DonchianChannel(BarsArray[5],3).Upper[0]+30*TickSize;
          stop2=DonchianChannel(BarsArray[5],3).Upper[0]+30*TickSize;

          if (Position.MarketPosition == MarketPosition.Flat)
          { SetStopLoss("S1A", CalculationMode.Ticks, 80, false);
          SetStopLoss("S2A", CalculationMode.Ticks, 80, false);

          Print("setting initial stops at bar: " + Time[0].ToString());

          Variable8 = 0;
          }

          // If a long position is open, allow for stop loss modification to breakeven
          else if (Position.MarketPosition == MarketPosition.Short)
          {
          // Once the price is greater than entry price + 20 ticks, set stop loss to breakeven
          if (GetCurrentBid(0) < Position.AvgPrice - 80 * TickSize)
          //&& step1)
          {
          SetStopLoss("S1A",CalculationMode.Price, stop1,false);
          SetStopLoss("S2A",CalculationMode.Price, stop1,false);
          //SetStopLoss("S3",CalculationMode.Price, stop1,false);
          //SetStopLoss("S4",CalculationMode.Price, stop2,false);
          //step1 = false;
          DrawDot("step1" + CurrentBar, true, 0, Position.AvgPrice - 70 * TickSize, Color.Blue);
          }

          }

          if (Falling(HMA(BarsArray[3],55)))
          {Variable1 = 1;}

          if(Variable1==1)
          {BackColor=Color.MistyRose;}

          if (Rising(HMA(BarsArray[3],55)))

          {Variable1 = 0;
          tradeCounter = 0;
          }



          if (tradeCounter < maxTrades
          && Position.MarketPosition == MarketPosition.Flat
          && Variable1 == 1
          && (Highs[5][0] > KAMA(BarsArray[5],2, 10, 30)[0])
          && (Highs[8][0] > KAMA(BarsArray[8],2, 10, 30)[0])
          && Close[1] > Open[1]
          && Close[0] < Open[0])

          {//tradeCounter++;
          EnterShort(2000, "S1A");
          EnterShort(2000, "S2A");

          Variable8 = 1;
          }

          if (tradeCounter < maxTrades
          && Position.MarketPosition == MarketPosition.Flat
          && Variable1 == 1
          && (Highs[3][0] > KAMA(BarsArray[3],2, 10, 30)[0])
          && (Highs[8][0] > KAMA(BarsArray[8],2, 10, 30)[0])
          && Close[1] > Open[1]
          && Close[0] < Open[0])

          {//tradeCounter++;
          EnterShort(2000, "S1A");
          EnterShort(2000, "S2A");

          Variable8 = 1;
          }



          if (GetCurrentAsk(0) < Position.AvgPrice - 50 * TickSize)
          ExitShort(2000, "SX1","S1A");

          if (GetCurrentAsk(0) < Position.AvgPrice - 80 * TickSize)
          ExitShort(2000, "SX2","S2A");




          if(Variable8==1)
          {BackColor=Color.Silver;}


          if(Variable9==1)
          {BackColor=Color.White;}



          }

          protected override void OnExecution(IExecution execution)

          { if (execution.Order.OrderState == OrderState.Filled)
          tradeCounter++;
          }





          #region Properties

          #endregion
          }
          }



          Thanks
          Tony

          Comment


            #6
            tonynt,
            you got to add filter to check its the entry orders only that get counted.

            like


            { if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
            tradeCounter++;
            }


            also you are resetting the tradeCounter via code

            if (Rising(HMA(BarsArray[3],55)))

            {Variable1 = 0;
            tradeCounter = 0;
            }

            are you sure you want to do that.

            Comment


              #7
              Hello bukkan,

              thank you for your support! I try to check what I have done wrong.

              Why do you mean maybe not to reset tradecounter when the HMA is going other direction? (for your information I do longs from one computer and shorts from other computer). Should I do other way? My idea - in these choppy markets - is to take eg 1 or 2 shorts when eg HMA in higher timeframe is falling, then resetting when HMA rising. Next short when HMA falling again. You think I should reset tradecounter other way?

              Thanks in advance for your hint.

              Thanks
              Tony

              Originally posted by bukkan View Post
              tonynt,
              you got to add filter to check its the entry orders only that get counted.

              like


              { if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
              tradeCounter++;
              }


              also you are resetting the tradeCounter via code

              if (Rising(HMA(BarsArray[3],55)))

              {Variable1 = 0;
              tradeCounter = 0;
              }

              are you sure you want to do that.

              Comment


                #8
                i didnt have the exact idea of what you were doing, and thus i mentioned that. so currently your code works like x trades (as per maxtrades) per slope. if thats you want then its fine.

                just check the OnExecution as i mentioned.

                Comment


                  #9
                  THANKS!

                  Have a great day
                  Tony

                  Originally posted by bukkan View Post
                  i didnt have the exact idea of what you were doing, and thus i mentioned that. so currently your code works like x trades (as per maxtrades) per slope. if thats you want then its fine.

                  just check the OnExecution as i mentioned.

                  Comment


                    #10
                    bukkan,

                    thanks again for your advice. But now appear error-message:

                    "Error on calling OnExecution method for strategy.... Object reference not set to an instance of an object."

                    Please can you tell me where to add for checking null references - I have no idea how this works.

                    Thanks and best regards
                    Tony

                    Originally posted by bukkan View Post
                    tonynt,
                    you got to add filter to check its the entry orders only that get counted.

                    like


                    { if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
                    tradeCounter++;
                    }


                    also you are resetting the tradeCounter via code

                    if (Rising(HMA(BarsArray[3],55)))

                    {Variable1 = 0;
                    tradeCounter = 0;
                    }

                    are you sure you want to do that.

                    Comment


                      #11
                      Hi Tony,

                      In the future please do not use multiple threads to post the same question.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by tonynt View Post
                        bukkan,

                        thanks again for your advice. But now appear error-message:

                        "Error on calling OnExecution method for strategy.... Object reference not set to an instance of an object."

                        Please can you tell me where to add for checking null references - I have no idea how this works.

                        Thanks and best regards
                        Tony
                        most probably it is the system generated orders for which you are getting the issues. check for a null check for the same

                        if (execution.Order == null )
                        {
                        //do something
                        }
                        else
                        {
                        //do something
                        }

                        Comment


                          #13
                          Thank you!

                          Originally posted by bukkan View Post
                          most probably it is the system generated orders for which you are getting the issues. check for a null check for the same

                          if (execution.Order == null )
                          {
                          //do something
                          }
                          else
                          {
                          //do something
                          }

                          Comment


                            #14
                            Hi Ryan,

                            if you would give me an answer to my question (the links I can find on my own), I need not trying to contact the supporter I received already accurate answers.

                            Originally posted by NinjaTrader_RyanM View Post
                            Hi Tony,

                            In the future please do not use multiple threads to post the same question.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bmartz, 03-12-2024, 06:12 AM
                            5 responses
                            32 views
                            0 likes
                            Last Post NinjaTrader_Zachary  
                            Started by Aviram Y, Today, 05:29 AM
                            4 responses
                            12 views
                            0 likes
                            Last Post Aviram Y  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            3 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X