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

Nonstop Submitted Orders and Stops

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

    Nonstop Submitted Orders and Stops

    Hello,

    i have a problem and can't figure out what's wrong. I think i probably overlooked something.

    I often have multiple entries and stops for the same price, especially with trailing stops (check the first attached photo for an example).

    The main Dataseries from the Chart is a 1Day Series and i added a 1 Tick Dataseries for my entries.
    So i do stuff like GetCurrentBid on the Tick Dataseries.
    And also for the EnterLong and EnterShort i use the index of the Tick Dataseries.

    Heres an example how i do the entries:
    Code:
    if (BarsInProgress == 2)
                {
                        if (Open[0] > _daylieVa.VAH[1])
                        {
                            if (_daylieVa.VAH[1] == GetCurrentAsk(2))
                            {
                                EnterLong(2, 1, "");
                                Print("EnterLong from VAH --> VAH:" + _daylieVa.VAH[1] + " CurrentAsk:" + GetCurrentAsk(2));
                            }
                        }
                        else
                        {
                            if (_daylieVa.VAL[1] == GetCurrentAsk(2))
                            {
                                EnterLong(2, 1, "");
                                Print("EnterLong from VAL --> VAL:" + _daylieVa.VAL[1] + " CurrentAsk:" + GetCurrentAsk(2));
                            }
                        }
    
                    }
    NT Manages the Stops, i defined them in State.Configure like this.

    Code:
    else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Second, 1);
                    AddDataSeries(Data.BarsPeriodType.Tick, 1);
                    _daylieVa = DaylieVA(false, new TimeSpan(0), new TimeSpan(0), 70);
                    AddChartIndicator(_daylieVa);
    
                    if (TrailingStop > 0)
                        SetTrailStop("", CalculationMode.Ticks, TrailingStop, false);
                    else if (StopLoss > 0)
                        SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    
                    if (TakeProfit > 0)
                        SetProfitTarget("", CalculationMode.Ticks, TakeProfit);
                }
    Thanks in advance.

    Best regards
    Simon

    #2
    Hello squeepo,

    Thanks for your post and welcome to the Ninjatrader forums!

    In looking at your code, it is not clear what BarsInProgress 2 actually is. The chart bars will be BarsInProgress 0, the first added data series in your script will be BarsInProgress 1. Can you show the code you are using for all of the AddDataseries()?

    Are you running this on live data?

    What Calculate setting are you using?

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul,

      thanks for your fast reply.

      The Indicator i wrote needs a 1 Second Dataseries so i also need it in the Strategy.

      In the second code part from my initial post you can see the AddDataSeries() for the 1 Second and 1 Tick in the first few lines.

      So i have the 1 Day main Dataseries (0), 1 second Dataseries (1) and 1 Tick Dataseries (2).

      The screenshot i attached was historical Data in a chart, not MarketReplay or Backtest. I Attched another Screenshot where you can see the Timeline.

      The Calculate setting is set to OnEachTick because the indicator is using OnMarketData.

      Thank you!
      Attached Files
      Last edited by squeepo; 03-11-2020, 07:33 AM.

      Comment


        #4
        Hello Simon,

        Thanks for your reply.

        Sorry I missed what you originally posted.

        With the Calculate setting of OnEachTick, this means that it is likely your entry conditions are remaining true tick after tick and so an entry order is sent on each tick while the conditions remain true. The order processing is asynchronous to your code execution which means that your code is executing far faster than the order can be transmitted.

        You would need to add further logic in your code to control/limit the orders issued. There are a couple of way to do this depending on what you would like to do.
        For example to limit to one order per day/bar, you could create an int variable called savedBar and in the entry conditions check to see if the CurrentBars[0] is not equal to savedBar and when that is true the entry occurs and you then save the CurrentBars[0] into the savedBar variable to prevent further orders on the bar/day.
        Alternately, you can also use Bool variables that you can control to first allow an entry and to then change the bool state as the entry is made to prevent further orders until such time/condition that warrants resetting the bool to allow the next trade.



        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello Paul,

          i think i understood what you mean so i decided to give the order processing 10 Minutes because i want to allow more then 1 order per day.

          I saved the current Tickbar time in a variable and added 10 Minutes.
          For the entry condition i checked if the saved time was <= current Tickbar time.

          Code:
          if (_daylieVa.VAH[1] == GetCurrentAsk(2) && _entryTime <= Times[2][0].TimeOfDay)
                                  {
                                      EnterLong(2, 1, "LongEntry");
                                      _entryTime = Times[2][0].TimeOfDay.Add(TimeSpan.FromMinutes(10));
                                      Print("EnterLong from VAH --> VAH:" + _daylieVa.VAH[1] + " CurrentAsk:" + GetCurrentAsk(2) + " CurrentTime:" + Times[2][0].TimeOfDay + " EntryTime:" + _entryTime);
                                  }
          And this was the result :
          Click image for larger version

Name:	2020-03-11_15h19_39.png
Views:	308
Size:	43.7 KB
ID:	1090069

          As you can see it entered the market every 10 Minutes and the CurrentAsk is always the same.

          I checked some entries in the manual and i think it has to do something with GetCurrentAsk(2) / GetCurrentBid(2).

          And i also tried to use OnExecutionUpdate or OnOrderUpdate.
          Before i called EnterLong() or EnterShort() i set a variable called orderPending to true.

          I also gave the orders a name like "LongEntry" and "ShortEntry".
          Then in the OnOrderUpdate i checked for the name and orderState Filled, Rejected or Cancled if one of this States were found i set the orderPending variable to false.

          So with this it shouldn't be possible to enter a ton of orders before the first was filled, rejected or canceled.

          But with this it resets only once and does not recognize the next order.
          My orderPrending variable stays true forever, it never enters the OnOrderUpdate Method ever again.


          Click image for larger version

Name:	2020-03-11_16h28_24.png
Views:	304
Size:	5.9 KB
ID:	1090070


          Click image for larger version

Name:	2020-03-11_16h29_09.png
Views:	319
Size:	45.5 KB
ID:	1090071

          I attched the whole strategy file so i needed you can have a look at the code.

          Thank you.

          Comment


            #6
            Hello Simon,

            Thanks for your reply.

            I recommend that you use print statements to validate the GetCurrentAsk() and GetCurrentBid() in each OnBarUpdate and not just conditionally. Testing here, with your other indicator commented out I see changing bid and ask values whn barsInProgress = 2.


            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello Paul,

              i changed some stuff and had some tests going.
              I read somewhere that with OnMarketData we have to use OnEachTick but i couldn't find it again and in the manual for OnMarketData i also found nothing about OnEachTick.
              Is it really needed because in my comparison it looked the same with OnPriceChange and OnEachTick but it was historical Data.

              So for now i changed it to OnPriceChange.
              I removed the 1 Tick Dataseries and use the 1 Second Dataseries now for my entries, it's good enough i think.
              Also i block new entries until 60 Seconds ( 60 1 Second bars ) have passed so the orders have some time to get filled.

              And instead of an equal comparison with GetCurrentBid or GetCurrentAsk i use CrossAbove and CrossBelow with Lows and Highs for the 1 Second Dataseries.

              I also did a Backtest to get more information about the trades made by the strategie, but i'm not sure if this could really be possible.
              The Trades look good, there are 3 - 30 Minutes between the trades made but with the trailing stops it's kinda weird.
              I have set EntriesPerDirecton to 1 and Trailing stop with 20 Ticks and every order gets stopped out at the same price.
              Maybe you can have a look at the Screenshot with the trades and tell me what you think.
              Also the Trailing Stops have no timestamp only the date so it's even harder to figure out whats going on, is it normal with stops / trailing stops ?

              And please have a look at the last trade, can you tell me why the trail stop didn't work there?

              Thank you very much for your effort.

              Comment


                #8
                Hello Simon,

                Thanks for your reply.

                When backtesting or when looking at historical trades on a chart, the GetCurrentAsk() and GetCurrent Bid() will return the close value of the bar.
                Reference: https://ninjatrader.com/support/help...currentbid.htm https://ninjatrader.com/support/help...currentask.htm

                Please verify that you do have the tick data needed for your backtest. 1 second bars are built from 1 tick bars so you do need that tick data over the duration of the test period
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by cls71, Today, 04:45 AM
                0 responses
                1 view
                0 likes
                Last Post cls71
                by cls71
                 
                Started by mjairg, 07-20-2023, 11:57 PM
                3 responses
                213 views
                1 like
                Last Post PaulMohn  
                Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                4 responses
                544 views
                0 likes
                Last Post PaulMohn  
                Started by GLFX005, Today, 03:23 AM
                0 responses
                3 views
                0 likes
                Last Post GLFX005
                by GLFX005
                 
                Started by XXtrader, Yesterday, 11:30 PM
                2 responses
                12 views
                0 likes
                Last Post XXtrader  
                Working...
                X