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

Strategy not implementing orders in live market

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

    Strategy not implementing orders in live market

    I have deployed a strategy which doesn't seem to be placing market orders live. When conditions are met the order does not show up but when I make the strategy inactive and then active again, the positions shows up as a historical.

    Is there some secret settings I need to know about?

    I do not have chart trader active, only a chart with an active strategy running.

    #2
    Hello ShruggedAtlas,

    I would recommend using prints to understand the behavior.

    Add a print with the time to the same action block of the condition set that places the order so we can see when the condition triggers the order. Add a print to OnOrderUpdate to print the order.ToString() so that we can see what happens with the order.

    Then include the output with your next post.

    Below is a public link to a forum post that demonstrates using prints to understand behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I have added print statements to each entry and they show up just fine on the historical output. I have also added the OnOrderUpdate code block and seems to be showing all the historical orders as accepted and filled.

      I'm not entirely sure what to do with this. All the historical positions are showing just fine. But the strategy, I assume will continue to ignore the conditions in real time.

      Here is the complete strategy down to the properties:
      ----------------------------------------------------------------------
      Code:
      public class AccumulateShortSwingLowExit : Strategy
      	{
      		private int Initial_Position_Size;
      		private RSI RSI1;
      		private RSI RSI2;
      		private Swing Swing1;
      		private MAX MAX1;
      		private EMA EMA1;
      		private ADX ADX1;
      		private SMA SMA1;
      		private MACD MACD1;
      		private EMA EMA2;
      
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				Description									= @"Accumulating position until trend swings the other way";
      				Name										= "AccumulateShortSwingLowExit";
      				Calculate									= Calculate.OnBarClose;
      				EntriesPerDirection							= 8;
      				EntryHandling								= EntryHandling.UniqueEntries;
      				IsExitOnSessionCloseStrategy				= false;
      				ExitOnSessionCloseSeconds					= 30;
      				IsFillLimitOnTouch							= false;
      				MaximumBarsLookBack							= MaximumBarsLookBack.TwoHundredFiftySix;
      				OrderFillResolution							= OrderFillResolution.Standard;
      				Slippage									= 4;
      				StartBehavior								= StartBehavior.WaitUntilFlat;
      				TimeInForce									= TimeInForce.Gtc;
      				TraceOrders									= false;
      				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
      				StopTargetHandling							= StopTargetHandling.PerEntryExecution;
      				BarsRequiredToTrade							= 20;
      				// Disable this property for performance gains in Strategy Analyzer optimizations
      				// See the Help Guide for additional information
      				IsInstantiatedOnEachOptimizationIteration	= true;
      				RSIOnHigherTF					= 80;
      				RSIOnLowerTF					= 95;
      				RSI_Period					= 2;
      				Swing_Period					= 10;
      				Account_Size					= 10000;
      				PipTarget					= 50;
      				Initial_Position_Size					= 1000;
      			}
      			else if (State == State.Configure)
      			{
      				//AddDataSeries("EURUSD", BarsPeriod.BarsPeriodType, 4 * BarsPeriod.Value, Data.MarketDataType.Last);
      				AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 240, Data.MarketDataType.Last);
      				AddDataSeries("EURUSD", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
      				AddPlot(Brushes.Lime, "Average price"); //PLOTTING AVERAGE ENTRY PRICE
      				
      				//SetStopLoss(CalculationMode.Percent, 0.03);
      			}
      			else if (State == State.DataLoaded)
      			{				
      				RSI1				= RSI(Closes[0], Convert.ToInt32(RSI_Period), 2);
      				RSI2				= RSI(Close, Convert.ToInt32(RSI_Period), 2);
      				Swing1				= Swing(Close, 10);
      				MAX1				= MAX(High, 14); //minimum low last 14 bars
      				EMA1				= EMA(Close, 55);
      				ADX1				= ADX(Closes[1], 14);
      				SMA1				= SMA(Close,200);
      				MACD1				= MACD(Closes[1], 34, 55, 13);
      				EMA2				= EMA(ADX1, 14);
      			}
      		}
      
      		protected override void OnBarUpdate()
      		{
      			if (BarsInProgress != 0) 
      				return;
      
      			if (CurrentBars[0] < 1
      			|| CurrentBars[1] < 1)
      			return;
      			
      			Values[0][0] = (Position.AveragePrice != 0)?Position.AveragePrice:Close[0];
      			
      			//----------------------LONG ENTRIES----------------------------------------
      			 // LE1 ENTRY
      			if ((Position.MarketPosition == MarketPosition.Flat)
      				 && (RSI1.Default[0] > RSIOnHigherTF)
      				 && (RSI2.Default[0] > RSIOnLowerTF)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (MACD1.Diff[0] > 0))
      			{
      				EnterShort(Convert.ToInt32(100), @"SE1");
      			}
      			
      			 // LE1 EXIT
      			if ((Position.MarketPosition == MarketPosition.Short)
      				 && (Position.Quantity == 100)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				Print("LE1 Entry time is:");
      				Print(Time[0]);
      				ExitShort(Convert.ToInt32(100), @"SExit1", @"SE1");
      			}
      			
      			 // LE2 ENTRY
      			if ((Position.Quantity == 100)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE2 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(200), @"SE2");
      			}
      			
      			 // LE2 EXIT
      			if ((Position.Quantity == 300)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(200), @"SExit2", @"SE2");
      			}
      			
      			 // LE3 ENTRY
      			if ((Position.Quantity == 300)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE3 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(600), @"LE3");
      			}
      			
      			 // LE3 EXIT
      			if ((Position.Quantity == 900)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(600), @"LExit3", @"LE3");
      			}
      			
      			 // LE4 ENTRY
      			if ((Position.Quantity == 900)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE4 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(1800), @"LE4");
      			}
      			
      			 // LE4 EXIT
      			if ((Position.Quantity == 2700)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(1800), @"LExit4", @"LE4");
      			}
      			
      			 // LE5 ENTRY
      			if ((Position.Quantity == 2700)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE5 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(5400), @"LE5");
      			}
      			
      			 // LE5 EXIT
      			if ((Position.Quantity == 8100)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(5400), @"LExit5", @"LE5");
      			}
      			
      			 // LE6 ENTRY
      			if ((Position.Quantity == 8100)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE6 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(16200), @"LE6");
      			}
      			
      			 // LE6 EXIT
      			if ((Position.Quantity == 24300)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(16200), @"LExit6", @"LE6");
      			}
      			
      			 // LE7 ENTRY
      			if ((Position.Quantity == 24300)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      			{
      				Print("LE7 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(48600), @"LE7");
      			}
      			
      			 // LE7 EXIT
      			if ((Position.Quantity == 72900)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(48600), @"LExit7", @"LE7");
      			}
      			
      			 // LE8 ENTRY
      			if ((Position.Quantity == 72900)
      				 && (Close[0] > High[1])
      				 && (Close[0] > Swing1.SwingHigh[0])
      				 && (Close[0] > Position.AveragePrice - .002)
      				 && (Close[0] > MAX1[1])
      				 && (MACD1.Diff[0] > 0))
      				 
      			{
      				Print("LE8 Entry time is:");
      				Print(Time[0]);
      				EnterShort(Convert.ToInt32(145800), @"LE8");
      			}
      			
      			 // LE8 EXIT
      			if ((Position.Quantity == 218700)
      				 && (Close[0] < Swing1.SwingLow[0]))
      			{
      				ExitShort(Convert.ToInt32(145800), @"LExit8", @"LE8");
      				ExitShort(Convert.ToInt32(48600), @"LExit7", @"LE7");
      				ExitShort(Convert.ToInt32(16200), @"LExit6", @"LE6");
      				ExitShort(Convert.ToInt32(5400), @"LExit5", @"LE5");
      				ExitShort(Convert.ToInt32(1800), @"LExit4", @"LE4");
      				ExitShort(Convert.ToInt32(600), @"LExit3", @"LE3");
      				ExitShort(Convert.ToInt32(200), @"LExit2", @"LE2");
      				ExitShort(Convert.ToInt32(100), @"LExit1", @"LE1");
      			}
      			 //****** CLOSE ALL ON TRADE FAILURE ******************************
      			//if ((Position.Quantity == 218700)
      			//	 && (Close[0] < Low[1]))
      			//{
      			//	ExitLong(Convert.ToInt32(145800), @"LExit8", @"LE8");
      			//	ExitLong(Convert.ToInt32(48600), @"LExit7", @"LE7");
      			//	ExitLong(Convert.ToInt32(16200), @"LExit6", @"LE6");
      			//	ExitLong(Convert.ToInt32(5400), @"LExit5", @"LE5");
      			//	ExitLong(Convert.ToInt32(1800), @"LExit4", @"LE4");
      			//	ExitLong(Convert.ToInt32(600), @"LExit3", @"LE3");
      			//	ExitLong(Convert.ToInt32(200), @"LExit2", @"LE2");
      			//	ExitLong(Convert.ToInt32(100), @"LExit1", @"LE1");
      			//}
      	
      			
      		}
      			protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                                          int quantity, int filled, double averageFillPrice,
                                          Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
      			{
      			  Print("The most current order state is: " + order.OrderState);   // OrderState.PartFilled
      			  Print("This particular order update state is: " + orderState); // OrderState.Working
      			}
      Attached Files
      Last edited by ShruggedAtlas; 06-04-2018, 09:46 AM.

      Comment


        #4
        Hello ShruggedAtlas,

        The script is not printing the order.ToString() as suggested, however, the screenshot suggests something is possibly filling..

        In the output screenshot it shows 'This particular order update state: Filled'..
        Is this not what you are wanting?

        Can you also print the State with the prints so we can see if this is real-time or historical?

        Can we have the text from the output window instead of a screenshot?
        (Right-click the output window > select Save as)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Yes everything shows is historical only - The "filled" notice reflects historical. I would like to see that happening in real time but it is not.

          I will have to research this order.tostring(0) I don't know how to implement it.

          Am I to put in the OnOrderUpdate block something like:

          Print(order.ToString());

          ??

          I have done this and received the following Output which is somewhat cryptic. Again, this all appears to be based on the historical bars. I don't see how any of this reflects real time in any way

          I have put the following print statements which represent all the basic conditions for an entry - All were met before the last entry but was NOT taken in real time by the strategy

          Code:
          			if (MACD1.Diff[0] > 0)
          			{
          				Print("Current 4 hr MACD Diff at " + Time[0] + " is greater than zero - condition met");
          			}
          			
          			if (Close[0] > High[1])
          			{
          				Print("Close greater than prior High at " + Time[0] + " - condition met");
          			}
          			if (Close[0] > Swing1.SwingHigh[0])
          			{
          				Print("Close greater than swing high at " + Time[0] + " - condition met");
          			}
          			if (Close[0] > Position.AveragePrice - .002)
          			{
          				Print("Close greater than average entry price at " + Time[0] + " - condition met");
          			}
          			if (Close[0] > MAX1[1])
          			{
          				Print("Close greater than maximum high last 14 bars at " + Time[0] + " - condition met");
          			}
          Unfortunately I have no idea when the next entry will come along - i could take a couple days before I can test this with another live entry - frustrating.

          Are there any common mistakes people make that might cause a strategy to not enter a position even when all the conditions have been met?
          Attached Files
          Last edited by ShruggedAtlas; 06-04-2018, 10:39 AM.

          Comment


            #6
            Hello ShruggedAtlas,

            To print the order as a string:
            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)
            {
            [B]Print(order.ToString());[/B]
            }
            Which print is the condition with the action block containing the order submission being triggered in real-time? How do you know whats real-time and whats historical on your end if you are not printing this?

            Are you not able to test the script with Playback and Market Replay data?

            Are you not adding prints with the time to the same action block of the condition set that places the order as directed?
            (Putting a print in a different condition only lets you know that other condition was triggered.)
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I was able to use the code as you suggested and saw nothing in the output to suggest any problems. There is nothing about the output that indicates a difference between historical or real time bars since my orders always happen at the end of the hourly bars.

              also, i have to assume the output reflects historical bars since the orders are currently in the past. - To be clear, the positions are currently active - but only became active after I noticed the strategy failed to take the entry this morning and only showed up after I re-activated the strategy which then made the order a historical order.

              I can continue to run this code along with the new print statements but it may take a couple of days for another entry to happen so trouble shooting this could literally take days or weeks. I do have time stamps on all these print statements.

              I hadn't thought of doing a market replay to see if this will trigger in "real time"

              I'll try that rather than waiting days for the next entry.

              Comment


                #8
                Hello ShruggedAtlas,

                You would need to have printed the State with the print as suggested in post #4.

                Below is a public link to the help guide on State.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok It seems to work in the replay. Other than adding print statements, I have not changed the code in any way so I still cannot understand why it did not take the entry this morning. But I discovered a new problem when I saw the order place in real time.

                  I have 8 total orders the strategy can make from LE1 - LE8 - as you can see from the pic included, the strategy took all of the orders all at once rather than on by one. This is a whole new problem
                  Attached Files

                  Comment


                    #10
                    I'm not sure where the State == State.Historical conditions is supposed to go.

                    Should I put it within the OnBarUpdate{} or within OnStateChange()?

                    I'll try both and see if this makes any difference - of course i have no idea what this does or how it might change anything - especially now that I see the entry is being taken (though incorrectly) in the market replay.

                    Ok it seems to reject historical orders and force only live positions to show up - sort of.

                    This is working in replay so I have to assume it will work in live markets. But i have the new problem of all the entries happening at once. I have no way to solve the original problem - perhaps it has something to do with my new issue? Anyway, I'll have to come back to this, in the meantime i'll have to try to solve the new issue.
                    Last edited by ShruggedAtlas; 06-04-2018, 11:53 AM.

                    Comment


                      #11
                      Hello ShruggedAtlas,

                      This would not be a condition.

                      You would only be printing the State object and you would add this to the print that is in the same action block as the order submission.

                      Print(string.Format("{0} | State: {1} | other info...", Time[0], State));
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Radano, 06-10-2021, 01:40 AM
                      19 responses
                      606 views
                      0 likes
                      Last Post Radano
                      by Radano
                       
                      Started by KenneGaray, Today, 03:48 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post KenneGaray  
                      Started by thanajo, 05-04-2021, 02:11 AM
                      4 responses
                      470 views
                      0 likes
                      Last Post tradingnasdaqprueba  
                      Started by aa731, Today, 02:54 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post aa731
                      by aa731
                       
                      Started by Christopher_R, Today, 12:29 AM
                      0 responses
                      11 views
                      0 likes
                      Last Post Christopher_R  
                      Working...
                      X