Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

First script, and errors in the management of the stop

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

    First script, and errors in the management of the stop

    On the platform deserves'm trying to get familiar with the programming

    (I used other platforms)

    The errors occurred in connection kinetick

    The simulation program open orders, but closed them immediately

    (I'd also like to get some additional advice)

    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Prima versione ea su hammer (con tante abilitazioni)
        /// </summary>
        [Description("Prima versione ea su hammer (con tante abilitazioni)")]
        public class hammered1 : Strategy
        {
            #region Variables
            // Wizard generated variables
            public int per_s = 50; // Default setting for Per_s
           // public int per_b = 50; // Default setting for Per_b
            public int per_2o = 5; // Default setting for Per_2o
            public bool trend = true; // Default setting for Trend
            public int trend_v1 = 15; // Default setting for Trend_v1
            public int trend_v2 = 30; // Default setting for Trend_v2
            public bool rottura_maxmin = true; // Default setting for Rottura_maxmin
            public int rottura_periodo = 15; // Default setting for Rottura_periodo
    		public int rottura_perc=-10;
            public int lotti=1;
    		public int per_stop=101;
    		public int per_take=101;
    		// User defined variables (add any user defined variables below)
    		public int ok_proporzioni=0;
    		public int ok_trend=0;
    		public int ok_rottura_maxmin=0;
    		
    		
            #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()
            {
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			#region CARICAMENTI	
    			
    			 double atr=High[1]-Low[1];
    			 double min=0;
    			 double massimo=0;
    			 ok_proporzioni=0;
    			 ok_trend=0;
    			 ok_rottura_maxmin=0;
    			 if(Close[1]>Open[1]){massimo=Close[1]; min=Open[1];} else { massimo=Open[1]; min=Close[1]; }  
    			
    			#endregion //fatti a inizio caricamento barra
    			
    			#region CONTROLLI PREORDINE E FILTRI
    			
    			#region Proporzioni 
    			if (min-Low[1]>=atr*per_s/100 && High[1]-massimo<=atr*per_2o/100){ok_proporzioni=1;}
    			if (High[1]-massimo>=atr*per_s/100 && min-Low[1]<=atr*per_2o/100){ok_proporzioni=2;}
     			#endregion	    //ok_proporzioni(dell ammer)=1 candela long 2 short 
    			
    			#region Trend presente
    			if (ok_proporzioni>0)
    			{
    				if(trend==true)
    				{
    					if(SMA(trend_v1)[1]>SMA(trend_v2)[1]){ok_trend=1;}
    					if(SMA(trend_v1)[1]<SMA(trend_v2)[1]){ok_trend=2;}
    					if(SMA(trend_v1)[1]==SMA(trend_v2)[1]){ok_trend=0;}
    				}
    				else
    				{
    					ok_trend=-1;
    				}
    			}
    			#endregion // ok_trend(2 sma indicano la dir) 1=long 2=short -1=filtro disabilitato 0=2medie stesso valore
    			
    			#region Rottura minmax
    			
    			if (ok_proporzioni>0)
    			{
    				if(rottura_maxmin==true)
    				{
    					double point=ATR(5)[1]*rottura_perc/100;
    					if(Low[1]<=MIN(Low,rottura_periodo+1)[0]-point){ok_rottura_maxmin=1;}
    					if(High[1]>=MAX(High,rottura_periodo+1)[0]+point){ok_rottura_maxmin=2;}
    					
    				}
    				else
    				{
    					ok_rottura_maxmin=-1;
    				}
    			}
    			
    			
    			#endregion // ok_rottura_maxmin(solo se l'ombra viola estremi) 1=long 2=short -1=filtro disabilitato 0=hammer non rompe estremi
    			
     			#endregion
    			
    			if(Position.MarketPosition == MarketPosition.Flat && ok_proporzioni==1 && (ok_trend==1 || trend==false) && (ok_rottura_maxmin==1 || rottura_maxmin==false) )
    			{
    			EnterLong(lotti, "L");
    			SetProfitTarget( CalculationMode.Ticks, (Close[1]-Low[1])*per_take/100);	
    			SetStopLoss( CalculationMode.Ticks, (Close[1]-Low[1])*per_stop/100);	
    			}
    			if(Position.MarketPosition == MarketPosition.Flat && ok_proporzioni==2 && (ok_trend==2 || trend==false) && (ok_rottura_maxmin==2 || rottura_maxmin==false) )
    			{
    			EnterShort(lotti, "S");
    			SetProfitTarget( CalculationMode.Ticks, (High[1]-Close[1])*per_take/100);	
    			SetStopLoss( CalculationMode.Ticks, (High[1]-Close[1])*per_stop/100);	
    				
    			}
    			
            }
    
            #region Properties
    
            #endregion
        }
    }

    #2
    Hello ashman,
    Welcome to the forum and I am happy to assist you.

    Please use the Print function to debug your code, particularly try to find out what your stop and target values are.

    In your code you might have the situation where you are submitting the stop or target at the entry price itself and thus you got filled immediately.

    For more debugging tips please refer to this post http://www.ninjatrader.com/support/f...ead.php?t=3418

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I see what joydeep means though your stop and target are executed together at the same price.

      Last edited by raefon72; 04-09-2012, 10:26 PM.

      Comment


        #4
        testing I noticed that the value that was imparted (ex) was corrected to 0.0012, while 12
        then the value I wanted had to be divided by the tricksize

        Comment


          #5
          Hi ashman, thanks, so with those changes included would the code now work better and as expected for you?



          TraceOrders would be also a very helpful debugging tool for reviewing the order events seen 'under the hood'.

          Comment


            #6
            Thank you for the function

            another my system of control was to control in a simulation (historical data) how the script reacted to various situations

            then using a print screen that gave me the significant output

            as in the upper left corner where a list could be seen varX = 5 and maybe in the course of the simulation
            changed to varX = 10

            what I need to know is
            - Make a tense can see the program to work, not having the finished result, but how to activate the program without receiving current data but historical (the speed is increased)
            - And finally as to print in a corner (eg top left) without that it moves with the increase of the candles, anchored on the screen

            Comment


              #7
              Hello ashman,
              You can use the Market Replay to test your strategy/indicator. You can increase the speed too in Market Replay. Please refer here to know more about the Market Replay http://www.ninjatrader.com/support/h...ket_replay.htm

              Please use the DrawTextFixed method to draw text at a fixed location http://www.ninjatrader.com/support/h...wtextfixed.htm

              Please let me know if I can assist you any further.
              JoydeepNinjaTrader Customer Service

              Comment


                #8
                i wanted to add a bit of analysis of the volumes, first trying to create some indicator and then transfer the code in the program

                I'm following the procedure is this
                - Add (PeriodType..) With external variable for the decision time


                - totalvolumes = Volumes [1] [0]; (the last bars of the second periodtype)

                how do I add the volumes to buy or sell?

                - buy= GetCurrentAskVolume(0) [1];

                Obviously these values ​​must be based on the period that I decided to analyze!

                Comment


                  #9
                  Hello ashman,
                  GetCurrentAskVolume is not a data series. It only returns the last ask volume.


                  Can you explain a bit more by what exactly you mean when you say, "how do I add volume to buy and sell?"

                  I look forward to assisting you further.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    i need to write an indicator to show me the volumes buy or sell
                    not only of this candle but also of those past and which are based on an independent series, different from that on which the indicator is loaded

                    I'm trying to create a marker with a logic that implement to the program

                    Comment


                      #11
                      Hello ashman,
                      You can add a bid and a ask data series to calculate the sum.

                      Add(string instrumentName, PeriodType periodType, int period, MarketDataType marketDataType)


                      However the accuracy of the historical data will depend on the granularity of the data series you wish to add.

                      You have refer to the BuySellPressure indicator (Tools>Edit NinjaScript>Indicators, select BuySellPressure, and click on the Ok button) that comes with NinjaTrader for further reference.

                      Please let me know if I can assist you any further.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        I set
                        - I set CalculateOnBarClose= false;
                        - add(PeriodType.Minute,60);

                        - if (barsInPRogress==1)
                        in this condition and now I want to load a moving average to 200 times but calculated on the hourly candles

                        med200=SMA(1,200)[1];

                        I must be able to have the media in this case hourly. whot can i do?
                        how do I put the average at 200 bars of 4 hours?

                        thanks a lot

                        Comment


                          #13
                          Hello ashman,
                          To calculate the SMA of the secondary bar series please use the below code

                          Code:
                          double sma200 = SMA(Closes[1]. 200)[0];


                          Similarly you can add another bar series and calculate to calculate the SMA for 4 hours.

                          Please let me know if I can assist you any further.
                          JoydeepNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          649 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          370 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          109 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          574 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          576 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X