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

Validate onbar click event

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

    Validate onbar click event

    HI i have chart trader and want to restrict buttonclick event only if current price is above trendPrice2.
    However at the time of press trendprice is 0 and it activates whenever.
    Below code is before OnBarUpdate


    Code:
    protected void Button9Click(object sender, RoutedEventArgs e) // Buy TLB
            {
                if (atmStrategy == null && wtrOrderEntryCommandsState == WTROrderEntryCommandsState.Awaiting)
                {
                    if (atmStrategySelector.SelectedAtmStrategy != null)
                    {
    //                    if  ((trendDirection2==1 && Close[0] > trendPrice2) )
    //                    {        
                            Print("test1" + Time[0]);
    //                    Print("OtrendDirection2" + trendDirection2);
    //                    Print("OcRising2" + cRising2);
                        Print("trendPrice2"+trendPrice2);
    //                    Print("trendSignal2"+trendSignal2);
                            wtrOrderEntryCommandsState = WTROrderEntryCommandsState.BuyTLB;
                            
                            account = accountSelector.SelectedAccount;            
                            account.OrderUpdate += OnOrderUpdate;
                            account.ExecutionUpdate += OnExecutionUpdate;
                            instr = atmStrategySelector.Instrument;
                            atm = atmStrategySelector.SelectedAtmStrategy;
                            cbxSelectedValue = cbx.SelectedValue.ToString();
                            numericStepperValue = numericStepper.Value;
                            qudSelectorValue = qudSelector.Value;
                            
                            buttonsArray[8].Background = Brushes.DarkGray;
                            buttonsArray[8].Foreground = Brushes.Black;                        
                                    
                            Draw.TextFixed(this, "atmNotSelected", "TLB Buy...", TextPosition.BottomRight);
                            ForceRefresh();
    //                    }
                    }
                    else
                    {
                        Draw.TextFixed(this, "atmNotSelected", "ATM Not Selected!", TextPosition.BottomRight);
                        ForceRefresh();
                    }
                }
            }​
    later in my onbarupdate
    Code:
    protected override void OnBarUpdate()
            {
                if (CurrentBar < 3)
                    return;
                
                EMA[0] = ema[0];
                
                close0 = Close[0];
                ema0 = ema[0];
    //            trendDirection2 = trendDirection2;
    //            trendPrice2 = trendPrice2;
    //            trendSignal2 = trendSignal2;
                
                
                if (State != State.Realtime)
                    return;
                int     trendDirection    = BTL.Direction;        //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
                double     trendPrice        = BTL.TrendPrice;    //Tick value at rightmost bar of current trend line
                int     trendSignal        = BTL.Signal;        //1=resistance break, -1=support break
                
                int     trendDirection2    = BTL2.Direction;        //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
                double     trendPrice2        = BTL2.TrendPrice;    //Tick value at rightmost bar of current trend line
                int     trendSignal2    = BTL2.Signal;        //1=resistance break, -1=support break​

    #2
    Hello tkaboris,

    You cant use OnBarUpdate related series like Time[0] or Close[0] directly from a button event. You need to surround that code with a TriggerCustomEvent.

    Code:
    TriggerCustomEvent(o =>
    {
        // your code in here
    }, null);​




    You also have local variables defined inside OnBarUpdate meaning the variables like trendSignal2 are only visible in OnBarUpdate, you need to make those class level variables instead if you want to access them from the button event
    JesseNinjaTrader Customer Service

    Comment


      #3
      ok
      I got them in class variable

      private int trendDirection;// = BTL.Direction; //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
      private double trendPrice; // = BTL.TrendPrice; //Tick value at rightmost bar of current trend line
      private int trendSignal;// = BTL.Signal; //1=resistance break, -1=support break

      private int trendDirection2;// = BTL2.Direction; //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
      private double trendPrice2;// = BTL2.TrendPrice; //Tick value at rightmost bar of current trend line
      private int trendSignal2;// = BTL2.Signal; //1=resistance break, -1=support break​

      and then in dataloaded
      else if (State == State.DataLoaded)
      {
      ema = EMA(EMAPeriod);
      // BTL = AutoTrendHNT8(true, 21, true, false,60, Brushes.Red, Brushes.Green, Brushes.Red, Brushes.Green);
      BTL = BounceTrendLinesInd(Alert, Strength, ShowHistory, LimitHistory, LimitHistoricalLookback, Brushes.Red, Brushes.Green, Brushes.Red, Brushes.Green,1);
      BTL2 = BounceTrendLinesInd(Alert2, Strength2, ShowHistory2, LimitHistory2, LimitHistoricalLookback2, Brushes.Red, Brushes.Green, Brushes.Red, Brushes.Green,1);
      // BTL2 = BounceTrendLinesInd(true, 5, true, false,60, Brushes.Pink, Brushes.Lime, Brushes.Pink, Brushes.Lime);
      // BTL = AutoTrendHNT8(Alert, Strength, ShowHistory, LimitHistory, LimitHistoricalLookback, Brushes.Red, Brushes.Green, Brushes.Red, Brushes.Green);
      trendPrice2 = BTL2.TrendPrice;
      trendSignal2 = BTL2.Signal;
      trendDirection2 = BTL2.Direction;
      trendPrice = BTL.TrendPrice;
      trendSignal = BTL.Signal;
      trendDirection = BTL.Direction;
      Print("OZtrendDirection2"+trendDirection2);
      }​

      but when i print in onbarupdate its always 0
      Print("OZtrendDirection2"+trendDirection2);

      Where do i look so the value is not zero but price level?

      Comment


        #4
        Hello tkaboris,

        If you want to set values to those variables you need to do it in OnBarUpdate. DataLoaded just means data was loaded, no bar processing happened at that point.
        JesseNinjaTrader Customer Service

        Comment


          #5
          HI can you please help me to correctly surround code to custom event? Do i surrount the whole code or just part of it? I would appriciate it

          Code:
          protected void Button9Click(object sender, RoutedEventArgs e) // Buy EMA now TLB Buy
                  {
                      if (atmStrategy == null && wtrOrderEntryCommandsState == WTROrderEntryCommandsState.Awaiting)
                      {
                          if (atmStrategySelector.SelectedAtmStrategy != null)
                          {
                              TriggerCustomEvent(o =>
                              {
                                  // your code in here
                              }, null);​
                              if  ((trendDirection2==cRising2 && Close[0] > trendPrice2) )
                              {                    
                                  wtrOrderEntryCommandsState = WTROrderEntryCommandsState.BuyEMA;
                                  
                                  account = accountSelector.SelectedAccount;            
                                  account.OrderUpdate += OnOrderUpdate;
                                  account.ExecutionUpdate += OnExecutionUpdate;
                                  instr = atmStrategySelector.Instrument;
                                  atm = atmStrategySelector.SelectedAtmStrategy;
                                  cbxSelectedValue = cbx.SelectedValue.ToString();
                                  numericStepperValue = numericStepper.Value;
                                  qudSelectorValue = qudSelector.Value;
                                  
                                  buttonsArray[8].Background = Brushes.DarkGray;
                                  buttonsArray[8].Foreground = Brushes.Black;                        
                                          
                                  Draw.TextFixed(this, "atmNotSelected", "TLB Buy...", TextPosition.BottomRight);
                                  ForceRefresh();
                              }
                          }
                          else
                          {
                              Draw.TextFixed(this, "atmNotSelected", "ATM Not Selected!", TextPosition.BottomRight);
                              ForceRefresh();
                          }
                      }
                  }​

          Comment


            #6
            Hello tkaboris,

            You can place the whole code you have inside the buttons event within the body of the TriggerCustomEvent. Your code would go where the comment is that says "your code here".
            Code:
            protected void Button9Click(object sender, RoutedEventArgs e) // Buy EMA now TLB Buy
            {​
                TriggerCustomEvent(o =>
               {
                  // your code in here
               }, null);​​
            
            }
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,404 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by Shai Samuel, 07-02-2022, 02:46 PM
            4 responses
            95 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by DJ888, Yesterday, 10:57 PM
            0 responses
            8 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            159 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            8 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X