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

Get drawn Object's color - Error on calling 'OnBarUpdate' method on bar 0

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

    #16
    its here:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class OptimusStrategy : Strategy
        {
            private Optimus Optimus1;
            //private ADX ADX1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "Optimus";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                    //AddChartIndicator(Optimus(Close, true, false, true, false, false, false, true, true, true, 0, true, true, true, true, true, true, true, false, false, false, @"[email protected]", false, 160, 80, false, 40));
                    Optimus1 = Optimus(Close, true, false, true, false, false, false, true, true, true, 0, true, false, true, true, true, true, true, false, false, false, @"[email protected]", false, 160, 80, false, 40);
                
                    int OldCount = 0;
                
                }
                
    
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
        /*  //TEMP REMOVING THE TIME REQUREMENT TO TEST TOMORROW AM
             // TRADING TIMES ALLOWED
                if (
                     // 3pm to 4:30pm
                    ((Times[0][0].TimeOfDay >= new TimeSpan(15, 10, 0))
                     && (Times[0][0].TimeOfDay <= new TimeSpan(16, 30, 0)))
                     // 6pm to 12am
                     || ((Times[0][0].TimeOfDay >= new TimeSpan(18, 0, 0))
                     && (Times[0][0].TimeOfDay <= new TimeSpan(23, 30, 0))))
                {
        */
                    
                /*
                 // Set 1
                if ((Optimus1.mStandardBuy[0] ) && (Position.MarketPosition == MarketPosition.Flat))
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"OptimusBuy");
                }
                
                 // Set 2
                if ((Optimus1.mStandardSell[0] )&& (Position.MarketPosition == MarketPosition.Flat))
                {
                    EnterShort(Convert.ToInt32(DefaultQuantity), @"OptimusSell");
                }
                */
                
                if(OldCount != DrawObjects.Count)
                   {
                    OldCount = DrawObjects.Count;​

    also as a side q.. how can I convert

    Code:
                    foreach (DrawingTool draw in DrawObjects.ToList()))
    {
    
    if (draw is DrawingTools.TriangleUp)
                    {
                    Print ("Tag name: "+draw.Tag);
                    DrawingTools.TriangleUp temp = draw as DrawingTools.TriangleUp;​
    ...
                    {​
    into a normal for loop like

    Code:
    for( int x = DrawObjects.Count; if x >= 0; x-- )   // this is how I would code this in MQL4
    
    if (draw is DrawingTools.TriangleUp)
                    {
                    Print ("Tag name: "+draw.Tag);
                    DrawingTools.TriangleUp temp = draw as DrawingTools.TriangleUp;​
    
    ....
    and still be able to check if its an up triange
    - the reason I ask is I would like to find the last drawn object not loop thru each and every one of then

    thanks again for both of these!!
    Last edited by Lele2k24; 03-11-2024, 12:26 PM.

    Comment


      #17
      Hello,

      Thank you for your response.

      It looks like oldCount is locally defined in OnStateChange(), which is why you cannot access it from OnBarUpdate(). You can either define this in OnBarUpdate(), or at the class level (this way you could access the variable from anywhere in the script).

      For your second question, you could loop through the list this way:

      Code:
      List<IDrawingTool> drawingTools = DrawObjects.ToList();
      
      for (int index = 0; index < drawingTools.Count; index++)
      {
      Print(drawingTools[index].Tag);
      }
      Please let us know if you have any further questions. ​
      Gaby V.NinjaTrader Customer Service

      Comment


        #18
        Thank you..

        Can you please give me an example of class level variable?

        If I set oldCount to 0 on each bar update, i think it would defeat the purpose of letting the bot know when a new drawing has been drawn on the chart..lol

        Thanks again.. learning tons

        Comment


          #19
          Hello Lele2k24,

          An example of a class level variable is like your Optimus1 variable. This variable is declared at the class level.

          Please let me know if you have any other questions.
          Gaby V.NinjaTrader Customer Service

          Comment


            #20
            Good morning,
            Can you please explain why break stops the for loop even if the condition is not met:

            Code:
             List<IDrawingTool> drawingTools = DrawObjects.ToList();
                              
                            for (int index = 0; index < drawingTools.Count; index++)
                            {
                                 if (drawingTools[index].Tag is "BuySig" )
                                {
                                    Print("BuySig"  + drawingTools[index].Tag);
                                    break;
                                    
                                    
                                }
                                  else if (drawingTools[index].Tag is "SellSig" )
                                {
                                    Print("SellSig"  + drawingTools[index].Tag);
                                    break;
                                
                    
                                    
                                }
                            }    ​
            this only gives me the BuySig print.. it never continues to check if the sell matches as well. I had 6 other conditions and it never checked any.. just stopped at the 1st one... like it hit break even tho it was not a BuySig tab that came up


            I also tried with just ifs... not else if.. same thing.. just printed BuySig only

            I am confused....

            I would like the bot to check to see if each condition is met and then if found then stop the for loop.... if not continue on.. not just break after the 1st if statement..


            without the breaks, it just lists the BuySig and SellSigs together.. which would open and close the trades each bar.. not good..

            Please advise.

            Thank you!

            Comment


              #21
              Hello Lele2k24,

              It only gives one BuySig print because you have specified the loop to break after you have found the drawing tagged as BuySig. Once the loop breaks, it will not start again until the next pass of OnBarUpdate(). It will loop through the collection of all drawing objects applied to the chart each time, so if the BuySig drawing object is always on the chart - it will always break once the loop gets to that drawing object.

              if (drawingTools[index].Tag is "BuySig" )
              {
              Print("BuySig" + drawingTools[index].Tag);
              break;
              }
              Gaby V.NinjaTrader Customer Service

              Comment


                #22
                thank you for that explanation.

                I then make a full circle back to my original question

                how can I get the color of the last drawn object..

                Code:
                       if(drawingTools[0].Tag is "BuySig" )
                                    {
                                        Print(index+ " "  + drawingTools[index].Tag);
                                        
                                    //    Print(draw.Tag);
                        
                                        
                                    }​
                instead of the .Tag.. how can I get the color/brush of the drawn object? I know now the last object drawn is drawingTools[0].. how can I get the color of that item?

                also is there a way to see the text.. the original drawn items are texts, not actual triangles

                Code:
                DrawText("▲", Green_Brush, false, true);
                Code:
                DrawText("▼", Red_Brush, false, true);
                (the tag is tied to the bar number so it keeps going up ... D5964 > D5965 > D5966 etc) the color and the text would tell me what to do

                thanks again!

                Comment


                  #23
                  Hello Lele2k24,

                  Please see below:

                  Code:
                  foreach (DrawingTool draw in DrawObjects.ToList())
                  {
                  if (draw is DrawingTools.Text)
                  {
                  DrawingTools.Text myDrawText = draw as DrawingTools.Text;
                  
                  Print("Text object: " + myDrawText.Tag + " TextBrush: " + myDrawText.TextBrush);
                  }
                  }​

                  I would also like to note if you have access to the logic that the indicator is using to Draw either the red text or the green text, you can implement this logic into your strategy to know when there would be a red or green triangle text.
                  Last edited by NinjaTrader_Gaby; 03-12-2024, 09:42 AM.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #24
                    thank you...

                    how would I get this to work with drawingTools[0]

                    Code:
                            List<IDrawingTool> drawingTools = DrawObjects.ToList();
                                      
                                         if (drawingTools[0] is DrawingTools.Text)
                                        {
                                        DrawingTools.Text myDrawText = drawingTools[0] as DrawingTools.Text;
                                        
                                        Print("Text object: " + myDrawText.Tag + " TextBrush: " + myDrawText.TextBrush);
                                                          
                                        }    ​
                    its not printing the text or the color

                    number of drawings 3708 OldCount 3708
                    0 D6191
                    drawingTools.Count-1 D6189
                    number of drawings 3709 OldCount 3709
                    0 D6192
                    drawingTools.Count-1 D6191
                    number of drawings 3711 OldCount 3711
                    0 D6193
                    drawingTools.Count-1 D6192
                    number of drawings 3712 OldCount 3712
                    0 D6196
                    drawingTools.Count-1 D6194​
                    the idea is not to iterate thru all drawings on the chart and just look at the last object drawn.. I have tried to access the original drawings.. even draw my own objects and there must be something going on in the background since what I see on the chart and what is happening with the strategy are two different things .. like it keeps saying there is a new sell but no new down arrows are being printed.. ( I used a bool to tell me if there is a new item - buy-sells-closes and they keep activating each bar) thus I am back to wanting to emulate what I would do when I look at the chart..

                    Thanks again!

                    Comment


                      #25
                      Hello Lele,

                      You could do:

                      Code:
                                  
                      
                      List<IDrawingTool> drawingTools = DrawObjects.ToList();
                      
                      for (int index = 0; index < drawingTools.Count; index++)
                      {
                           if (drawingTools[index] is DrawingTools.Text)
                            {
                                 DrawingTools.Text myDrawText = drawingTools[index] as DrawingTools.Text;
                                          
                                 Print("Text objects: " + myDrawText.Tag + " TextBrush: " + myDrawText.TextBrush);
                            }
                                      
                      }​
                      In my testing, the most recent drawing object (object drawn on the last closed bar) is the first drawing object in the collection.
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #26
                        thank you,. how do I check thru each of the conditions which out going thru all possible drawings.. that's my issue with foreach.. like it will find sells and buys in there.. I just need to check that last one only and check for the text of each.. buys/sells/closing...

                        thanks again!

                        Comment


                          #27
                          Hello Lele,

                          Which conditions are you referring to?

                          My previous example wasn't using a foreach loop, it is just a for loop.
                          Gaby V.NinjaTrader Customer Service

                          Comment


                            #28
                            How would I access the actual text of these?

                            thank you!

                            Comment


                              #29
                              Hello,

                              That would be myDrawText.DisplayText.

                              Please let us know if you have any other questions.
                              Gaby V.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Sebastian - TwinPeaks, Yesterday, 01:31 PM
                              4 responses
                              25 views
                              0 likes
                              Last Post Sebastian - TwinPeaks  
                              Started by FAQtrader, 04-25-2024, 12:00 PM
                              7 responses
                              98 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by bee1943, Today, 09:55 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post bee1943
                              by bee1943
                               
                              Started by NUVERSA, Today, 09:31 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by rene69851, 05-02-2024, 03:25 PM
                              2 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_Kimberly  
                              Working...
                              X