• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Positons Exits based on AccountPos

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

    Positons Exits based on AccountPos

    We all have occasional computer downtime, therefore strategies that have open positions get disabled and we are left with account positions that are only exitable on re-enabling of the strategy by using the following logic:

    // Exits
    if (BarsInProgress == i
    && PositionsAccount[i].MarketPosition == MarketPosition.Short
    && Closes[i][0] <= MIN(Closes[i],5)[0])

    {
    EnterLong(i,PositionsAccount[i].Quantity,"ExitShort"+i);
    }

    The problem is that the first action from EnterLong is to close the short positions... then add a new long position. So to get around this problem we try using:


    if (BarsInProgress == i
    && PositionsAccount[i].MarketPosition == MarketPosition.Short
    && Closes[i][0] <= MIN(Closes[i],5)[0])

    {
    ExitShort(i,PositionsAccount[i].Quantity,"SwingShort"+i,"ExitShort"+i);
    }

    Problem here is that as the strategy was disabled... that "SwingShort" positions entry is no longer valid and visible to the newly enabled strategy.

    Conclusion: All swing traders need a way to continue managing existing positions upon strategy re-enable. Is there a way to achieve this with occasional computer downtime when you cannot rely on the strategy position object? Regards and thx to all
    Last edited by elliot5; 08-29-2016, 01:07 AM.

    #2
    Hello everington_f,

    It is possible for NinjaTrader to resume a strategy as long as the market data produced by the market has not caused the strategy to calculate a new position.

    I've detailed this previously in another thread which I am linking below.
    http://ninjatrader.com/support/forum...117#post463117
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thankyou Chelsea - I do not see the option on my NT8:

      Click Tools -> Options... -> select the Strategies tab -> select the NinjaScript sub-tab
      Select the radio button option Immediately submit live working historical orders -> Click OK
      When starting a new instance of the strategy set the Sync account position parameter to True

      I am not sure that the thread post you mentioned helps me understand the issue better in terms of what I am trying to do with the mentioned code.

      Will ExitShort work on a restarted strategy?

      Regards
      Last edited by elliot5; 08-29-2016, 08:26 AM.

      Comment


        #4
        Hi everington_f,

        Those instructions were previously written for NinjaTrader 7.

        The Tools -> Strategies -> NinjaScript -> 'On starting a real-time strategy' option has been moved to the strategy parameters as 'Start behavior'.

        In NT8 Beta, right-click the chart -> select Strategies... (or right-click the Strategies tab of the Control Center and select New Strategy...)
        Select the strategy (in the chart Strategies window click the New button)
        In the parameters set Start behavior to Immediately submit (or Immediately submit, synchronize account).

        The information about needing intra-bar granularity is the same.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thankyou Chelsea I already have the strategy set to Submit immediately - but again upon restart it appears that the ExitShort code will not detect the execution that had taken place before the computer restart......

          How can we code an exit to a pre-restart account position? See my code pls to appreciate the issue
          Last edited by elliot5; 08-29-2016, 08:46 AM.

          Comment


            #6
            Hello everington_f,

            Did the script calculate a new position from the time it was disabled to when it was re-enabled?

            May I confirm you have 1 tick granularity added to the script and the orders are being submitted to the 1 tick series?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The script was closed over the weekend, with 7 positions on friday close, and has not added or closed a position since the open this morning becuause the exit code is not working. It is run on an array of 30 stocks, ADD () on 510 minute bars... from the strategies tab. OnBarUpdate with OnPriceChange the GUI imput. Regards
              Last edited by elliot5; 08-29-2016, 10:34 AM.

              Comment


                #8
                Hello everington_f,

                May I confirm that for each series added a 1 tick series of each of the series is added as well for historical 1 tick granularity?

                The 1-tick granularity is so that the script calculates the same historically as it does in real time (or as close as possible).

                Just to confirm, the script placed an exit order before it was disabled, however, upon re-enabling the position shows up as if that specific exit has not occurred, is this correct?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  No 1 tick series has been added to the array list. The strategy is not being backtested.. only run live. There were no exit orders placed before it was disabled. There were 7 positions... no open orders... then disabled. Then restarted.... what is required is the ability for the logic to exit those positions in certain conditions once re-enabled.

                  if (BarsInProgress == i
                  && PositionsAccount[i].MarketPosition == MarketPosition.Short
                  && Closes[i][0] <= MIN(Closes[i],5)[0])

                  {
                  ExitShort(i,PositionsAccount[i].Quantity,"SwingShort"+i,"ExitShort"+i);
                  }

                  Comment


                    #10
                    Hello everington_f,

                    Do you have if (Historical) return; at the beginning of OnBarUpdate in the script?

                    If so, when restarting the script will not take a historical position at all and will always start flat.

                    If you do not have this at the beginning of OnBarUpdate then your script is indeed running a backtest (historical) before running live data.

                    All scripts when enabled process the available historical data as a backtest before running live data. This backtest is used to calculate a historical position, and historical trades, and attempt to match these to recorded trades in the database made at the same time and price and from the same strategy.

                    If the historical trades do not match the real-time trades exactly, they will not be matched and will not be resumed.

                    Try adding the SampleMACrossOver script to a chart and enable this. You will notice that the chart is full of trades even though the script has not been running in real time. These trades are theoretical backtest trades.
                    Right-click that chart -> select Strategy Performance -> select the SampleMACrossOver. In this submenu you are able to view Historical performance (backtest), Real-time performance, or both Historical and Real-time performance.

                    Below is a link to the help guide on differences between Real-Time and Backtest for NT8.
                    http://ninjatrader.com/support/helpG...ime_vs_bac.htm
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Yes I do have the check

                      if(State == State.Historical)
                      return;


                      So the removal of this check should enable a restarted strategy to be in a strategy position equal to that if it had not be disabled?

                      Regards and many thx

                      Comment


                        #12
                        Hello everington_f,

                        By adding if (Historical) return; to the script, the script is no longer working historically, and will not resume any orders and cannot cancel any orders made previously.

                        The strategy will always start as flat.

                        If you want your strategy to resume properly, it must process the same orders historically that it does in realtime. This means it must backtest. For accuracy in historical data, you also need 1 tick intra-bar granularity.

                        Please see the post that I have linked you at the beginning of this thread, which details this.
                        http://ninjatrader.com/support/forum...117#post463117
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Having removed the Historical check, now the strategy cannot enable owing to an out of range error...I have a currentbars check in place already......

                          // if(State == State.Historical)
                          // return;

                          for (int j = 0; j < CurrentBars.Length; j++)
                          {
                          if (CurrentBars[j] < 3)
                          return;
                          }

                          Comment


                            #14
                            Hello everington_f,

                            You should always check that an index exists before trying to use that index..

                            For example if the furthest you look is 5 bars back, make sure there are 5 bars of historical data before calling 5 bars back.

                            It appears something like this is going on in the script.

                            Which line of code is causing the error?
                            Perhaps 3 bars is not enough bars for what is being called back in the script?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Adding tick bar series

                              I have solved that barsloading error problem by just increasing the number from 3 to 50. How do we add granularity to the strategy that currently loads:

                              else if (State == State.Configure)
                              {
                              AddDataSeries("ADS", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("BAS", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("BAYN", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("BMW", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("BEI", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("CBK", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("ALV", Data.BarsPeriodType.Minute, 510);
                              AddDataSeries("DCX", Data.BarsPeriodType.Minute, 510);

                              Regards and thx

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by razeus, Today, 12:00 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by kitwit, Today, 09:47 AM
                              5 responses
                              20 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by guidoisot, Today, 11:17 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by razeus, Today, 10:11 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Jim  
                              Started by tonynt, Today, 10:18 AM
                              5 responses
                              16 views
                              0 likes
                              Last Post tonynt
                              by tonynt
                               
                              Working...
                              X