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

Couple of issues with getting a strategy to work as intended

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

    Couple of issues with getting a strategy to work as intended

    ​Hi guys,

    I'm working on what I thought would start off as a decently simple strategy. Taking trades off of daily ATR levels, intraday. The idea is it would be like an autotrader on pivot points. Every resistance level gets sold, and every support level gets bought. Here are the problems I am having.

    1. Figuring out how to only place a trade at each level once, until that trade gets closed out. Then have that level "available" to be traded at again. I tried making a bool variable for each price I was using, but for some reason that's not working 100%.

    2. Figuring out why some levels are getting skipped in whole. The market comes to the price, and just skips right over it.

    You can see in the picture, a level was just entirely skipped. And then after the trade closes out in profit, a level that was traded, doesn't get entered into again.
    Click image for larger version

Name:	image_63935.png
Views:	274
Size:	58.2 KB
ID:	1135978

    #2
    Here is the code I am using to enter trades. prices is a double list that contains all the ATR prices I want to trade. pricec is the bool list corresponding to each price levels. If price comes to level 1, bool spot 1 gets flagged. If price gets to level 5, bool #5 gets flagged. Once that trade is complete, the bool list gets reset so that the level is available again.

    Code:
     if(prices.Contains(GetCurrentBid()) && Close[0] < Closes[1][0]) // Buy
    {
    spot = prices.IndexOf(GetCurrentBid());
    
    if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
    {
    EnterLongLimit(prices[spot],"ATR");
    Entry = 1;
    }
    else if( pricec[spot] == false)
    {
    Entry *= 2;
    EnterLongLimit(Entry,prices[spot],"ATR");
    }
    }
    
    if(prices.Contains(GetCurrentAsk()) && Close[0] > Closes[1][0]) // Sell
    {
    spot = prices.IndexOf(GetCurrentAsk());
    
    if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
    {
    EnterShortLimit(prices[spot],"ATR");
    Entry = 1;
    }
    else if(pricec[spot] == false)
    {
    Entry *= 2;
    EnterShortLimit(Entry,prices[spot],"ATR");
    }
    }
    I am flagging the bools in the orderupdate method
    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
    if(order.Name == "ATR" && orderState == OrderState.Filled)
    {
    int spot = prices.IndexOf(limitPrice);
    pricec[spot] = true;
    if(order.OrderAction == OrderAction.Buy)
    SetProfitTarget("ATR",CalculationMode.Price,p rices[spot+1]);
    else if(order.OrderAction == OrderAction.SellShort)
    SetProfitTarget("ATR", CalculationMode.Price, prices[spot-1]);
    }
    }

    Comment


      #3
      Hello Omegaknight,

      Are you using TraceOrders and prints to understand the behavior?


      Is the order being submitted or ignored?

      Is the order being submitted with the expected price?

      Are your conditions evaluating as true?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Click image for larger version

Name:	Screenshot 2021-01-13 212221.png
Views:	196
Size:	756.5 KB
ID:	1136683Hi Chelsea,

        Yes, I am using TraceOrders, the orders are not getting submitted, it's as if the strategy doesn't even see the price as it goes by, but it will trade the prices before/after it. It was also trade the price that it skips on a later bar. Here is a picture with output and examples from the levels not getting traded or getting skipped and getting traded later.

        Comment


          #5
          Hello Omegaknight,

          Please save the output from the output window to a text file and include this with your post.

          We are happy to assist with analyzing the output from prints and trace orders.

          If an order is not being submitted, print the time of the bar, print all values used in the condition along with labels. Be sure you have watched the video linked the post I have provided.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea,

            Here is a textfile of the output. The first trade in the output works correctly, but the second trade happens after the strategy skips a price, takes a trade at the next price, and then uses the skipped price as the profit target (as it should).

            I've inserted the code I use to determine if I should enter a trade as well, in hopes that will shed some light. I'm using GetCurrentBid() to check prices, but I get this same issue when using Close[0], or Close[0]-TickSize, or Close[0]+TickSize, when I was trying to check one tick before price got there.

            Code:
            if(prices.Contains(GetCurrentBid()) && Close[0] < Closes[1][0]) // Buy
            {
            Print("Time is: "+Time[0].ToString() + " Price Index is: "+ prices.IndexOf(GetCurrentBid()).ToString());
            spot = prices.IndexOf(GetCurrentBid());
            Print("Buy price is hit. Price is: "+prices[spot].ToString() + " Price Check is: "+pricec[spot].ToString());
            Print("S1: "+S1[0].ToString());
            Print("S2: "+S2[0].ToString());
            Print("S3: "+S3[0].ToString());
            Print("S4: "+S4[0].ToString());
            Print("S5: "+S5[0].ToString());
            Print("S6: "+S6[0].ToString());
            Print("S7: "+S7[0].ToString());
            Print("S8: "+S8[0].ToString());
            Print("S9: "+S9[0].ToString());
            Print("S10: "+S10[0].ToString());
            Print("R1: "+R1[0].ToString());
            Print("R2: "+R2[0].ToString());
            Print("R3: "+R3[0].ToString());
            Print("R4: "+R4[0].ToString());
            Print("R5: "+R5[0].ToString());
            Print("R6: "+R6[0].ToString());
            Print("R7: "+R7[0].ToString());
            Print("R8: "+R8[0].ToString());
            Print("R9: "+R9[0].ToString());
            Print("R10: "+R10[0].ToString());
            foreach(double k in prices)
            {
            Print("Prices Contains: "+k.ToString());
            }
            if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
            {
            EnterLongLimit(prices[spot],"ATR");
            Entry = 1;
            }
            else if( pricec[spot] == false)
            {
            Entry *= 2;
            EnterLongLimit(Entry,prices[spot],"ATR");
            }
            }
            
            foreach(double D in prices)
            {
            if(Close[0] == D)
            Print("Price: "+ Close[0].ToString()+"Time is: "+Time[0].ToString()+ " Price Index is: "+ prices.IndexOf(Close[0]).ToString());
            
            }
            
            if(prices.Contains(GetCurrentAsk()) && Close[0] > Closes[1][0]) // Sell
            {
            Print("Time is: "+Time[0].ToString()+ " Price Index is: "+ prices.IndexOf(GetCurrentBid()).ToString());
            spot = prices.IndexOf(GetCurrentAsk());
            Print("Sell price is hit. Price is: "+prices[spot].ToString() +" Price Check is: "+pricec[spot].ToString());
            Print("S1: "+S1[0].ToString());
            Print("S2: "+S2[0].ToString());
            Print("S3: "+S3[0].ToString());
            Print("S4: "+S4[0].ToString());
            Print("S5: "+S5[0].ToString());
            Print("S6: "+S6[0].ToString());
            Print("S7: "+S7[0].ToString());
            Print("S8: "+S8[0].ToString());
            Print("S9: "+S9[0].ToString());
            Print("S10: "+S10[0].ToString());
            Print("R1: "+R1[0].ToString());
            Print("R2: "+R2[0].ToString());
            Print("R3: "+R3[0].ToString());
            Print("R4: "+R4[0].ToString());
            Print("R5: "+R5[0].ToString());
            Print("R6: "+R6[0].ToString());
            Print("R7: "+R7[0].ToString());
            Print("R8: "+R8[0].ToString());
            Print("R9: "+R9[0].ToString());
            Print("R10: "+R10[0].ToString());
            foreach(double k in prices)
            {
            Print("Prices Contains: "+k.ToString());
            }
            if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
            {
            EnterShortLimit(prices[spot],"ATR");
            Entry = 1;
            }
            else if(pricec[spot] == false)
            {
            Entry *= 2;
            EnterShortLimit(Entry,prices[spot],"ATR");
            }
            }
            Here is a screenshot of the chart for the first couple of trades.
            Click image for larger version

Name:	Screenshot 2021-01-14 210618.png
Views:	174
Size:	308.8 KB
ID:	1136840

            Thanks.
            Attached Files
            Last edited by Omegaknight; 01-14-2021, 09:07 PM.

            Comment


              #7
              Hello Omegaknight,

              This is Jim, responding on behalf of Chelsea who is out of the office at this time.

              Looking at the output window from post #4 I see trace orders feedback for working orders and I do not see TraceOrders feedback for ignored orders. This is telling me that your logic is not allowing the order submission methods to be reached. If you remove your prints, and then add prints beside the order submission methods and test again, you can confirm that those order submission methods are not being reached and pinpoint bars that you want to check your logic. For example:


              Code:
              if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
              {
                  Print(String.Format("Time[0]: {0} Close[0]: {1} CurrentBar: {2} ATR Short Limit Submitted", Time[0], Close[0], CurrentBar));
                  EnterShortLimit(prices[spot],"ATR");
                  Entry = 1;
              }
              else
              {
                  Print("Order was not submitted at " + Time[0] + " " + CurrentBar);
              }
              If the logic allows submission, you will then see a print when the order submission method is hit, along with the timestamp and current close price so you can reference with your chart.

              If you do not see the submission print, this means that the logic controlling this order submission does not allow the order submission method to be hit. You will then need to add prints outside of that controlling condition to see how that condition is evaluating. From the test above, you can note the timestamp and the CurrentBar index when you expected the logic to become true, and you can use if statements to filter your prints to only print out debug information on that bar.

              I.E.
              Code:
              if (CurrentBar == 500)
              {
                  Print("Position: " + Position.MarketPosition);
                  Print"pricec[spot] " + pricec[spot]);
              }
              if(Position.MarketPosition == MarketPosition.Flat && pricec[spot] == false)
              {
                  ...
              We look forward to assisting.
              JimNinjaTrader Customer Service

              Comment


                #8
                Hi Jim,

                I did something a little different to test the strategy was even seeing the prices. This is a better example of what I mean when I say it's as if the strategy is ignoring the prices.

                I added in this code:
                Code:
                if(prices.Contains(GetCurrentBid()) || prices.Contains(GetCurrentAsk()))
                {
                Print(String.Format("Time[0]: {0} Close[0]: {1} CurrentBar: {2} ", Time[0], Close[0], CurrentBar));
                check = Draw.Text(this, "ATR"+CurrentBar, false, "●",1, High[1] + 2*TickSize,0, Brushes.Red,new SimpleFont("Arial",16),TextAlignment.Center,null,n ull,0);
                }
                And now every time the market comes to one of my prices, we should see a red dot on that bar. For some reason that isnt happening though. I attached an output text so you can see, as well as a screenshot from the same time as the previous pictures. You can see here that the prices are getting skipped some times, and then not skipped at other times.
                Click image for larger version

Name:	Screenshot 2021-01-16 101821.png
Views:	220
Size:	66.5 KB
ID:	1137031

                Comment


                  #9
                  Hello Omegaknight,

                  I only see one instance where an Enter order method was ignored at 1/3/2021 8:17:00 PM. This looks to be because you are long and have submitted a short limit entry, which violates the internal rules of the Managed Approach.

                  Internal rules of the Managed Approach - https://ninjatrader.com/support/help...antedPositions

                  If you do not see prints/dots in places that you expect them, it means your logic did not allow the print/Draw.Text to be reached.

                  I suggest using prints to check your logic controlling this action as it does not sound like GetCurrentAsk/GetCurrentBid are in your prices collection.

                  I.E. I suggest checking the following and asking yourself/checking the items below with more debugging steps:

                  if(prices.Contains(GetCurrentBid()) || prices.Contains(GetCurrentAsk()))

                  prices.Contains(GetCurrentBid()) is this false when you expect it to be true?
                  prices.Contains(GetCurrentAsk()) is this false when you expect it to be true?

                  You will want to check how you are adding those price levels to the prices collection if you expected those levels to be there.

                  Please also note that GetCurrentAsk and GetCurrentBid will equal the close price when we are processing historical data.

                  We look forward to assisting.
                  JimNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by agclub, 04-21-2024, 08:57 PM
                  4 responses
                  18 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by Irukandji, Today, 04:58 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post Irukandji  
                  Started by fitspressoburnfat, Today, 04:25 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post fitspressoburnfat  
                  Started by Skifree, Today, 03:41 AM
                  1 response
                  4 views
                  0 likes
                  Last Post Skifree
                  by Skifree
                   
                  Started by usazencort, Today, 01:16 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post usazencort  
                  Working...
                  X