Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do i delay buy orders after a stop loss?

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

    How do i delay buy orders after a stop loss?

    Hello everyone,

    So I want to delay buy orders after a stop loss for a certain period of time.

    Its all contained in onbarupdate. The stop loss and buy/sell is in a if else statement. I use position.avg price and close to compare to stop loss. Then nested if statement of barssinceexit() > 100
    { p = 0}.
    I have double p variable and set p = position.avg price after some sell order.

    So once barssincexit is true it makes/resets the stop loss to false and goes to else to buy/sell.

    It works for the first stock but others not. Is the p variable not resetting every time and to each stock? P is not acting like a variable and for each stock. I've played with it and nothing works for the other stocks.


    Thanks,

    #2
    Is this a multi series script that's looking at different instruments, or are you running the same script on different instruments and it only works on one?

    Would you be able to post a sample simplified script that replicates the error?

    I look forward to assisting you further.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Its just just looking at 10 stocks. It works for AAPL stock but not the other 9 stocks.

      Code:
       
      double p;
      protected override void OnBarUpdate()
              {
      			equity = GetAccountValue(AccountItem.CashValue);
      			qty = (Convert.ToInt32(equity)) / 10000;
      			
      			
      			if (Close[0] < ( p * .90))
      			{
      			//SetStopLoss("BUYlong", CalculationMode.Percent, 0.1, false); //this also works
      			ExitLong("SELLlong", "BUYlong");
      			
      				if ((BarsSinceExit() > 100) )
      				{
      				p = Position.AvgPrice;
      				}
      			}
      else
      {code for strategy
      
      {code for buying}
      {code for selling
      p = Position.AvgPrice;}
      
      }

      Comment


        #4
        With multi series scripts there could be several issues going on here that would require some debugging beyond what I can offer with the presented script. First it's typically a good idea to isolate the script into different sections based on each instrument the system is looking at. For a must read article on this: http://www.ninjatrader.com/support/h...nstruments.htm

        Aside from that it looks to me like you're using the same p variable for all instruments. If you're doing this in a multi series script you would need a way to store the position separately for each instrument.

        Also depending on the goals of the script you may need to use advanced order handling for the placement and management of your orders.http://www.ninjatrader.com/support/h...r_handling.htm

        Let me know how I can further assist.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Little off topic here. But I dont want my strategy to re-enter trades with in "x" number of bars after exit. If I use bars since exit my strategy wont trigger b/c the very first entry never follows an exit. I could use that the position was flat at least "x" amout of bars ago but I dont know how you formulate that statement in code..........

          TIA

          Comment


            #6
            There are a couple different ways you could do this using custom tracking. This way came to mind first

            Performance.AllTrades.Count



            You can use the trade count to see if any trades have been place. If not, then you can enter otherwise you would check for enough bars since exit.

            Let me know if I can further assist.
            LanceNinjaTrader Customer Service

            Comment


              #7
              I don't see how you tie those 2 together..... Maybe you could eloborate. Is there no way to state position == flat [1]. Meaning I was flat 1 bar ago.Thx

              Comment


                #8
                if(Performance.AllTrades.Count == 0 || BarsSinceExit() > X)
                //enter trade

                Or perhaps I'm misunderstanding your goals? The only way I can think of right now to check if a previous bar was flat would be to create a custom data series. While possible it doesn't sound like you would need to do this: http://www.ninjatrader.com/support/h...ries_class.htm

                Let me know if I can further assist.
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  i tried inserting that code && Performance.AllTrades.Count == 0 || BarsSinceExit() > 3, but it turns the strategy off

                  Comment


                    #10
                    My initial guess is that BarsSinceExit() is throwing you an error. When you have the output window open what error message do you get? (tools -> output window)

                    For info on using BarsSinceExit(): http://www.ninjatrader.com/support/h...ssinceexit.htm
                    LanceNinjaTrader Customer Service

                    Comment


                      #11
                      ok I see what i needed to add but it is still not waiting at least 3 bars between exits

                      here is my code so that you can see

                      #region Using declarations
                      using System;
                      using System.ComponentModel;
                      using System.Diagnostics;
                      using System.Drawing;
                      using System.Drawing.Drawing2D;
                      using System.Xml.Serialization;
                      using NinjaTrader.Cbi;
                      using NinjaTrader.Data;
                      using NinjaTrader.Indicator;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Strategy;
                      #endregion

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      /// <summary>
                      /// Enter the description of your strategy here
                      /// </summary>
                      [Description("Enter the description of your strategy here")]
                      public class AUGrenkoTESTER: Strategy
                      {
                      #region Variables
                      // Wizard generated variables
                      int positionSize = 1;
                      // User defined variables (add any user defined variables below)

                      // presets
                      int tbip = 1;

                      #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()
                      {
                      Add(PeriodType.Tick,1); // the high granularity dataseries

                      RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;

                      SetStopLoss("", CalculationMode.Ticks, 26, true);



                      CalculateOnBarClose = true;
                      TraceOrders = true;



                      }



                      protected override void OnTermination()
                      {
                      SendMail("","", "STR G OFF", "Disabled");


                      }

                      protected override void OnStartUp()
                      {
                      for (int a = 0; a< BarsArray.Length; a++)
                      Print(this.Name+" Bars data ["+a+"] contains "+BarsPeriods[a].ToString()+" of "+BarsArray[a].Count+" bars, commencing at "+BarsArray[a].Get(0).Time);
                      }



                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {




                      if (BarsInProgress > 0 || CurrentBar < BarsRequired)
                      return; // don't process anything directly on the high granularity timeframe, or early in the chart's data

                      // Condition set 1
                      if ( TriggerLines(34, 8).Plot1_Up[0] > TriggerLines(34, 8).Plot2_Up[0]



                      && Performance.AllTrades.Count == 0 || BarsSinceExit(tbip, "", 0) > 3

                      && Position.MarketPosition != MarketPosition.Short// KEEP THIS


                      )



                      {
                      EnterLong(tbip,positionSize, "L RT");

                      Comment


                        #12
                        Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

                        Please see this guide here for a guide to debugging: http://www.ninjatrader.com/support/f...ead.php?t=3418

                        Try doing something like this to see if you get what you expect

                        Print(BarsSinceExit(tbip, "", 0));
                        LanceNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by hazylizard, Today, 08:38 AM
                        2 responses
                        9 views
                        0 likes
                        Last Post hazylizard  
                        Started by geddyisodin, Today, 05:20 AM
                        2 responses
                        18 views
                        0 likes
                        Last Post geddyisodin  
                        Started by Max238, Today, 01:28 AM
                        5 responses
                        47 views
                        0 likes
                        Last Post Max238
                        by Max238
                         
                        Started by giulyko00, Yesterday, 12:03 PM
                        3 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by habeebft, Today, 07:27 AM
                        1 response
                        16 views
                        0 likes
                        Last Post NinjaTrader_ChristopherS  
                        Working...
                        X