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

Set Stoploss order, Cancel Order

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

    #16
    Hello,
    In my strategy i have few blocks : initialization, looking for sygnal, managing orders
    Drawing objects called on OnBarUpdate() method.
    The notify() used to visualize the sygnal.

    Initialize() method:

    Code:
    		protected override void Initialize()
            {			
    			// init embedded indicators
    			InitializeFractals();
    			InitializeWorkSessions();
    			InitializeSygnalParabolic();
    			InitializeParentParabolic();
    			InitializePatterns();
    			InitializeMovings();
    			
    			// create & populate filter list by name to filter of potential sygnal
    			createFilterList();
    			
    			this.lastHigh = new AkPoint();
    			this.lastLow = new AkPoint();
    			
    			CalculateOnBarClose = false;
    			
    			//EntriesPerDirection = 1; 
        		//EntryHandling = EntryHandling.AllEntries; 
            }
    OnBarUpdate :

    Code:
            protected override void OnBarUpdate()
            {	
    			// no positions --> look for sygnal
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				if (FirstTickOfBar)
    				{
    					// no sygnals, we start to check every bar as potential sygnal
    					LookForTradeSygnal();
    					return;
    				}
    			}	
    			
    			// if no orders found and have sygnal & sygnal is not filtered
    			if (entryOrder == null && entrySygnal != null && !entrySygnal.Filtered)
    				ManageOrders();
    			
            }
    Inside LookForTradeSygnal() I call notify() method :

    Code:
    		private void LookForTradeSygnal()
    		{
    			if (CurrentBar < 5 || CurrentBar < BarsRequired)
    				return;	
    
    			// force update akForwardPatterns indicator to recalculate basic wave points
    			BasicPointsCalculation();
    			//this.parentSar.Update();
    			
    			// get sygnal type
    			AkSygnalType type = getSygnalType(); 
    
    			if (type == AkSygnalType.None)
    				return;
    			
    			// create sygnal object
    			entrySygnal = createEntrySygnal(CurrentBar, type);
    				
    			// process all filters
    			processAllFilters(filtersList);
    			
    			// draw sygnal
    			if (!entrySygnal.Filtered)
    				[B]notify(entrySygnal);	[/B]		
    		}
    notify():

    Code:
    		private void notify(AkSygnal sygnal)
    		{
    			Color upBarColor = Color.Lime;
    			Color downBarColor = Color.Red;
    			Color upArrowColor = Color.Yellow;
    			Color downArrowColor = Color.Yellow;
    						
    			switch (sygnal.Type) 
    			{
    				case AkSygnalType.Buy:
    					BarColorSeries[0] = upBarColor;
    					DrawArrowUp("upArrow" + CurrentBar, false, 0, Low[0], upArrowColor);
    					DrawLine("breakeven" + CurrentBar.ToString(), false, CurrentBar - lastHigh.Index, sygnal.Breakeven, CurrentBar - sygnal.Index, sygnal.Breakeven, Color.Black, DashStyle.Dash, 1);
    					DrawLine("stoploss" + CurrentBar.ToString(), false, CurrentBar - lastHigh.Index, sygnal.Stoploss, CurrentBar - sygnal.Index, sygnal.Stoploss, Color.Red, DashStyle.Dash, 1);
    					DrawFibonacciRetracements("fibo" + CurrentBar, false, CurrentBar - lastHigh.Index, sygnal.Breakeven, CurrentBar - sygnal.Index, sygnal.Stoploss);
    					break;
    				case AkSygnalType.Sell:
    					BarColorSeries[0] = downBarColor;
    					DrawArrowDown("downArrow" + CurrentBar, false, 0, High[0], downArrowColor);
    					DrawLine("breakeven" + CurrentBar.ToString(), false, CurrentBar - lastLow.Index, sygnal.Breakeven, CurrentBar - sygnal.Index, sygnal.Breakeven, Color.Black, DashStyle.Dash, 1);
    					DrawLine("stoploss" + CurrentBar.ToString(), false, CurrentBar - lastLow.Index, sygnal.Stoploss, CurrentBar - sygnal.Index, sygnal.Stoploss, Color.Red, DashStyle.Dash, 1);
    					DrawFibonacciRetracements("fibo" + CurrentBar, false, CurrentBar - lastLow.Index, sygnal.Breakeven, CurrentBar - sygnal.Index, sygnal.Stoploss);
    					
    					break;
    			}
    		}

    Comment


      #17
      Originally posted by NinjaTrader_Cal View Post
      Akushyn,

      You will need to use CalculateOnBarClose set to false for the every tick portion but you can use the bool FirstTickOfBar to get when a new bar is created, much like running CalculateOnBarClose true.

      http://www.ninjatrader.com/support/h...ttickofbar.htm
      When i use like you recomend above my embed indicators goes crazy. Seems that they start to work on every tick update , but should on every bar update.

      it looks like in attached file
      Attached Files

      Comment


        #18
        Hello,

        Thank you for your response.

        Please try building the code for the LookForTradeSygnal() and Notify() within the OnBarUpdate() method and test again. If you can, please provide a test script using these custom methods that produce this issue on the chart.

        Comment


          #19
          Originally posted by NinjaTrader_PatrickH View Post
          Hello,

          Thank you for your response.

          Please try building the code for the LookForTradeSygnal() and Notify() within the OnBarUpdate() method and test again. If you can, please provide a test script using these custom methods that produce this issue on the chart.
          I can send you a code, but it will not work & even not compilable, cause I use 6 my custom indicators.
          And the other reason , that inside i use custom class types (AkSygnalType, AkSygnal, AkPoint, AkWave... etc)

          I think, that the issue produce because all of my indicators work OnBarClose = true and strategy works OnBarClose = false.
          And when i use OnBarclose = false they goes crazy.
          Any ideas?
          Attached Files
          Last edited by akushyn; 09-09-2014, 11:06 AM.

          Comment


            #20
            Hello akushyn,

            Thank you for your response.

            Using hard coded values for CalculatOnBarClose in indicators can cause issue when you are calling them from a strategy that is using the opposite CalculateOnBarClose. Try not setting the CalculateOnBarClose exclusively in the Initialize() method.

            Comment


              #21
              Originally posted by NinjaTrader_PatrickH View Post
              Hello akushyn,

              Thank you for your response.

              Using hard coded values for CalculatOnBarClose in indicators can cause issue when you are calling them from a strategy that is using the opposite CalculateOnBarClose. Try not setting the CalculateOnBarClose exclusively in the Initialize() method.
              Thank you for the answer!

              So, if my indicators work on bar close and strategy on every tick , should i set on bar close for my indicators inside OnBarClose method? (after initialization)

              Have another idea, what do you think ?
              * if I don't have positions set OnBarClose = true;
              * And if I have position set inside onBarclose method the OnBarClose property to false (use it like a switcher)
              Last edited by akushyn; 09-09-2014, 11:32 AM.

              Comment


                #22
                Hello akushyn,

                Thank you for your response.

                The only way to truly do what you suggest would be to set the CalculateOnBarClose to False in the Initialize() method of the indicators and strategy, then use FirstTickOfBar in the Indicator's OnBarUpdate() method. You can find a reference sample on this exact idea at the following link: http://www.ninjatrader.com/support/f...ad.php?t=19387

                Comment


                  #23
                  Originally posted by NinjaTrader_PatrickH View Post
                  Hello akushyn,

                  Thank you for your response.

                  The only way to truly do what you suggest would be to set the CalculateOnBarClose to False in the Initialize() method of the indicators and strategy, then use FirstTickOfBar in the Indicator's OnBarUpdate() method. You can find a reference sample on this exact idea at the following link: http://www.ninjatrader.com/support/f...ad.php?t=19387
                  Thank you for the response.

                  As I understand correctly , I should change calculation behavior in all of my embedded indicators and set CalculateOnBarClose = false and use FirstTickOfBar if statement.

                  In this case, you know, everywhere I use 0 bar (CurrentBar) I have to use for now first bar from right (CurrentBar - 1 )
                  Is that right?

                  Have anothe question: once I open NinjaTrader, try to add the strategy to the chart but my strategy is not avaliable in the strategy list. When i open tools/edit ninjascript/ strategy the strategy is avaliable to edit.
                  What can be a problem ?

                  Comment


                    #24
                    Hello akushyn,

                    Thank you for your response.

                    You would just use barsAgo = 0 to pull the current bar, FirstTickOfBar ensures you are processing at the close of the previous bar as demonstrated in the reference sample.

                    When you open the Strategies menu and then close it, go to the Log tab of the NinjaTrader Control Center. Do you see any errors? If so, what do they report?

                    Comment


                      #25
                      Originally posted by NinjaTrader_PatrickH View Post
                      Hello akushyn,

                      Thank you for your response.

                      You would just use barsAgo = 0 to pull the current bar, FirstTickOfBar ensures you are processing at the close of the previous bar as demonstrated in the reference sample.

                      When you open the Strategies menu and then close it, go to the Log tab of the NinjaTrader Control Center. Do you see any errors? If so, what do they report?
                      Hello,

                      Will try to continue the strategy implementing.
                      I've tried to do like you describe above. Still have visual difference (when CalculateOnBarClose = false & true)

                      Do the next:
                      1) change Initialize() method

                      Code:
                      		protected override void Initialize()
                              {
                      			Add(new Plot(Color.Black, PlotStyle.Line, "Parabolic SAR"));
                                              Overlay = true; 
                      			
                      			// calculate every tick of bar update
                      			[B]CalculateOnBarClose = false;[/B]
                              }
                      2) Change the way of OnBarUpdate():

                      Code:
                              protected override void OnBarUpdate()
                              {
                      			if (!FirstTickOfBar)
                      				return;
                      				
                      			// do the logic
                               }
                      What should I change else ?
                      Thank you in advance!!!

                      Attached to pics to see the difference.
                      Attached Files
                      Last edited by akushyn; 10-31-2014, 01:55 PM.

                      Comment


                        #26
                        Hello akushyn,

                        Thank you for your response.

                        If you press F5 to reload NinjaScript on the CalculateOnBarClose = False indicator, does it reload as the CalculateOnBarClose = True would display?

                        May we review your indicator code on our end as you have recently updated it?

                        Comment


                          #27
                          Originally posted by NinjaTrader_PatrickH View Post
                          Hello akushyn,

                          Thank you for your response.

                          If you press F5 to reload NinjaScript on the CalculateOnBarClose = False indicator, does it reload as the CalculateOnBarClose = True would display?

                          May we review your indicator code on our end as you have recently updated it?
                          yes, when i refresh ninjascript indicator display ok.
                          indiicator in attached file
                          Attached Files

                          Comment


                            #28
                            Hello akushyn,

                            Thank you for your patience.

                            I am seeing the same behavior on my end. I am testing by adding the following to the end of the OnBarUpdate() method:
                            Code:
                            			Print(Time[0] + " Value = " + Value[0]);
                            			Print("COBC: " + CalculateOnBarClose.ToString());
                            This will give me the timestamp of the bar and the value, as well as the CalculateOnBarClose bool to differentiate between the two instances of the indicator.

                            You can test this on your end as well. I will follow up here with my findings and any information on this indicator.
                            Last edited by NinjaTrader_PatrickH; 11-03-2014, 02:30 PM.

                            Comment


                              #29
                              Hello akushyn,

                              Thank you for your patience.

                              I was wrong on the following. You would need to use the barsAgo object as 1 to reference the previous close as the FirstTickOfBar means you are processing on the current building bar.
                              Originally posted by NinjaTrader_PatrickH View Post
                              You would just use barsAgo = 0 to pull the current bar, FirstTickOfBar ensures you are processing at the close of the previous bar as demonstrated in the reference sample.

                              Comment


                                #30
                                Originally posted by NinjaTrader_PatrickH View Post
                                Hello akushyn,

                                Thank you for your patience.

                                I am seeing the same behavior on my end. I am testing by adding the following to the end of the OnBarUpdate() method:
                                Code:
                                			Print(Time[0] + " Value = " + Value[0]);
                                			Print("COBC: " + CalculateOnBarClose.ToString());
                                This will give me the timestamp of the bar and the value, as well as the CalculateOnBarClose bool to differentiate between the two instances of the indicator.

                                You can test this on your end as well. I will follow up here with my findings and any information on this indicator.
                                Print test failed.
                                * values different
                                * visual effect crazy

                                (((
                                Attached Files

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by bortz, 11-06-2023, 08:04 AM
                                47 responses
                                1,603 views
                                0 likes
                                Last Post aligator  
                                Started by jaybedreamin, Today, 05:56 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post jaybedreamin  
                                Started by DJ888, 04-16-2024, 06:09 PM
                                6 responses
                                18 views
                                0 likes
                                Last Post DJ888
                                by DJ888
                                 
                                Started by Jon17, Today, 04:33 PM
                                0 responses
                                4 views
                                0 likes
                                Last Post Jon17
                                by Jon17
                                 
                                Started by Javierw.ok, Today, 04:12 PM
                                0 responses
                                12 views
                                0 likes
                                Last Post Javierw.ok  
                                Working...
                                X