Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plotting Orders and Trade infos on chart

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

    Plotting Orders and Trade infos on chart

    Hello everyone,

    You will find part of my strategy here below.
    The idea is to have a visual representation of the bracket orders with Stop and Target (see attached pic) plus additional infos when the trade is closed.

    I would like to add on the chart the value in ticks of the Stop or Target hit by price and see if there were any slippage when Stop or Target was hit.

    Additionally i would like to increase the font of the draw text....Dont know how !

    Any recommendations to help me improve this part of the code would be more than welcome

    Happy trading

    Chris


    Code:
    namespace NinjaTrader.Strategy
    {
        [Description("Momentum Pimping")]
        public class AAA : Strategy
        {
            #region Variables
            private int contract = 1;        
            private int     barNumberOfOrder     = 0;
            private int        lineLength            = 0;  
            private IOrder entryOrder = null;        
              #endregion
    
            protected override void Initialize()
            {
                   TimeInForce = Cbi.TimeInForce.Day;    
                CalculateOnBarClose = true;    
            }
            protected override void OnBarUpdate()
            {                
                //LONG ENTRY CONDITIONS
                if (Position.MarketPosition == MarketPosition.Flat
                    && Close[0]<Close[1]
                    && Close[1]<Close[2])
                {
                    barNumberOfOrder = CurrentBar;
                    entryOrder = EnterLongLimit(CONTRACT, Close[0], "");
                }
                //SHORT ENTRY CONDITIONS
                if (Position.MarketPosition == MarketPosition.Flat
                    && Close[0]>Close[1]
                    && Close[1]>Close[2])
                {
                    barNumberOfOrder = CurrentBar;
                    entryOrder = EnterShortLimit(CONTRACT, Close[0], "");                
                }        
            }
            protected override void OnExecution(IExecution execution)
            {
                if(execution.Name == "Stop loss")             PlaySound(@"C:\Program Files (x86)\NinjaTrader 7\sounds\StopFilled.wav");    
                else if(execution.Name == "Profit target")     PlaySound(@"C:\Program Files (x86)\NinjaTrader 7\sounds\TargetFilled.wav");            
               
                int Stop             =   10;
                int Target            =   20;
            
                if (entryOrder != null && entryOrder == execution.Order)
                {    
                    SetStopLoss(CalculationMode.Ticks, Stop);
                    SetProfitTarget(CalculationMode.Ticks, Target);    
                }
                  
                lineLength = Math.Max(CurrentBar - barNumberOfOrder, 3);
                if (Position.MarketPosition == MarketPosition.Long)
                {                    
                    PlaySound(@"C:\Program Files (x86)\NinjaTrader 7\sounds\Alert2.wav");
                    DrawLine("Target", false, lineLength, Position.AvgPrice + (Target) * TickSize, -8*lineLength, Position.AvgPrice + (Target) * TickSize, Color.Lime, DashStyle.Solid, 2);
                    DrawLine("Stop", false, lineLength, Position.AvgPrice - (Stop) * TickSize,-8*lineLength, Position.AvgPrice - (Stop) * TickSize, Color.Red, DashStyle.Solid, 2);
                    DrawLine("Entry", false, lineLength, Position.AvgPrice, -8*lineLength, Position.AvgPrice, Color.Tan, DashStyle.Solid, 2);
                    
                    DrawText("TargetValue","Target: "+Convert.ToString(Target)+" Ticks" , 0, Low[0]-12*TickSize, Color.Yellow);
                    DrawText("StopValue","Stop: "+Convert.ToString(Stop)+" Ticks" , 0, Low[0]-10*TickSize, Color.Yellow);
                }
                
                else if (Position.MarketPosition == MarketPosition.Short)
                {                    
                    PlaySound(@"C:\Program Files (x86)\NinjaTrader 7\sounds\Alert2.wav");
                    DrawLine("Target", false, lineLength, Position.AvgPrice - (Target) * TickSize, -8*lineLength, Position.AvgPrice - (Target) * TickSize, Color.Lime, DashStyle.Solid, 2);
                    DrawLine("Stop", false, lineLength, Position.AvgPrice + (Stop) * TickSize, -8*lineLength, Position.AvgPrice + (Stop) * TickSize, Color.Red, DashStyle.Solid, 2);
                    DrawLine("Entry", false, lineLength, Position.AvgPrice, -8*lineLength, Position.AvgPrice, Color.Tan, DashStyle.Solid, 2);
                    
                    DrawText("TargetValue", "Target:"+Convert.ToString(Target)+" Ticks" , 0, High[0]+12*TickSize, Color.Yellow);
                    DrawText("StopValue","Stop:"+Convert.ToString(Stop)+" Ticks" , 0, High[0]+10*TickSize, Color.Yellow);
                }
                if (Position.MarketPosition == MarketPosition.Flat)
                {                
                    RemoveDrawObject("Stop");
                    RemoveDrawObject("Target");
                    RemoveDrawObject("Entry");
                    RemoveDrawObject("TargetValue");
                    RemoveDrawObject("StopValue");
                }    
            }
            #region Properties    
            [Description("")]
            [GridCategory("3.Parameters")]
            public int CONTRACT
            {
                get { return contract; }
                set { contract = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Attached Files

    #2
    Hello,

    Thank you for the questions.

    First for the size of the font, you would need to create a new font object and use the correct overload for the drawing object. Here is an example:

    Code:
    DrawText("Tag", true, "MyText", 0, Close[0], 0, Color.Red, new Font("Arial", 14), StringAlignment.Center, Color.Black, Color.Gray, 100);
    Regarding the target and stop, are you asking how to display after they have filled the price in ticks, or display them as lines as you have pictured while they are active?

    I look forward to being of further assistance.

    Comment


      #3
      Thanks for the feedback.
      Actually i would like to DrawText for every single trade and compare the theoritical Stop/Target with the real exit (i.e. slippage).
      Something like that:

      Code:
      if (Performance.AllTrades.Count > 1)
                  {
                      for (int i=1;    i<Performance.AllTrades.Count; i++)
                      {
                      Trade EachTrade= Performance.AllTrades[Performance.AllTrades.Count - i];
                         
                      int Lasty = Convert.ToInt32(Math.Abs(EachTrade.ProfitPoints)/TickSize);                                           
                          //Print("Loss:   " + Lasty);
                          DrawText("Losstick",    "Loss: "+Convert.ToString(Math.Abs(Stop-Lasty))+" Ticks" , 0, EachTrade.Entry.Price, Color.Yellow);
                          //Print("Profit: " + Convert.ToInt32(Math.Abs((EachTrade.ProfitPoints))/TickSize));
                          DrawText("Profitick","Profit: "+Convert.ToString(Math.Abs(Target-Lasty))+" Ticks" , 0,EachTrade.Entry.Price    , Color.Yellow);
                         
                          //Print("Profit: "+Convert.ToString(Math.Abs(Target-Lasty))+"       "+"Loss: "+Convert.ToString(Math.Abs(Stop-Lasty)));
                      }
                  }
      Any any on how to do this?

      Comment


        #4
        Hello,

        What you have would currently work for iterating over the orders, but you would need to make unique Tag names for each text object and also use the orders Time object to place the text accordingly.

        I have omitted the price calculation logic and simply added a text object for each order in the history in this example:


        Code:
        if (Performance.AllTrades.Count > 1)
        {
                for (int i=1;    i<Performance.AllTrades.Count; i++)
                {
                    Trade EachTrade= Performance.AllTrades[Performance.AllTrades.Count - i];                                       
                    DrawText([B]"Tag" + i[/B], true, "MyText", [B]EachTrade.Entry.Time[/B], EachTrade.Entry.Price,0, Color.Red, new Font("Arial", 14), StringAlignment.Center, Color.Black, Color.Gray, 100);
                }
        }
        With the Time and price, you should be able to calculate the other values you need. In the case you need to reference a previous order or the order before the current being iterated, you already have a variable for the current order, you could make a variable for the prior order as well:

        Code:
        if(i > 1) 
        {
            Trade PriorTrade= Performance.AllTrades[Performance.AllTrades.Count - i - 1]; 
        }
        I look forward to being of further assistance.

        Comment


          #5
          Super, all works fine thanks.
          If i want to keep only the Live Trade on real account the following code don't work, any idea why?

          Code:
                          if (Performance.[B]RealtimeTrades[/B].Count > 1)
                          {                
                              Trade EachTrade= Performance.AllTrades[Performance.AllTrades.Count - 1];                    
                              Entry_real=Math.Round(EachTrade.Entry.Price,2);
                              Points= Math.Round(EachTrade.ProfitPoints/TickSize,2);
                              Stop_th= order.StopPrice/TickSize;
                              Stop_real=EachTrade.Exit.Price/TickSize;
                              Slippy = Math.Abs(Stop_real-Stop_th);
          
                              Print(""Entry Price: "+Entry_real+"    "+order.Name +"  "+ order.StopPrice+"    "+"Exit Value: "+ EachTrade.Exit.Price+"    "+"Slippage: "+Slippy+"    "+"Stop:  "+"   "+Points+" Ticks");
                          }

          Comment


            #6
            Hello,

            Thank you for the reply.

            I am unsure why that specifically is not working, but if you only want to account for RealTime trades you change the Collection being used from AllTrades to RealtimeTrades. Here is the prior example with RealTime instead:


            Code:
            if (Performance.RealtimeTrades.Count > 1)
            {
                    for (int i=1;    i<Performance.RealtimeTrades.Count; i++)
                    {
                        Trade EachTrade= Performance.RealtimeTrades[Performance.RealtimeTrades.Count - i];                                       
                        DrawText("Tag" + i, true, "MyText", EachTrade.Entry.Time, EachTrade.Entry.Price,0, Color.Red, new Font("Arial", 14), StringAlignment.Center, Color.Black, Color.Gray, 100);
                    }
            }
            I look forward to being of further assistance.

            Comment


              #7
              Yes this is what i needed.
              Thanks for the feedback
              cheers
              chris

              Comment


                #8
                Hi,
                I'm coming back to you regarding the Performance.RealTrades Collection.
                While I am printing the Collection perfectly in the outpout window with AllTrades, I have no printing at all when replacing AllTrades by RealTrades...Any idea why?

                Comment


                  #9
                  Hello,

                  Thank you for the reply.

                  I had seen this working on my end during tests, I wanted to check are the orders in question happening in realtime and had you let 4 total executions pass?

                  I will attach the script I had tested with for you to try. The script entails that the count of the collection is greater than 1, so 2 trades or 4 executions would be needed for the first execution of the text.


                  I look forward to being of further assistance.
                  Attached Files

                  Comment


                    #10
                    Hi,

                    I had 2 live trades today on live account with my Strategy.
                    The idea is to print relevant infos on the outpout windows of all previous trades (real account).

                    Furthermore, I don't really understand why do i need to wait for 4 executions to start seeing the info printed?

                    Comment


                      #11
                      Hello,

                      The reason you need two trades or 4 executions for this logic would be because I had kept the condition you posted in the sample to be in line with what you were asking about:
                      Code:
                      if (Performance.AllTrades.Count > 1)
                      which transitioned into the RealtimeTrades question or:

                      Code:
                      if (Performance.RealtimeTrades.Count > 1)
                      {
                      
                      }
                      Greater than 1 would mean at least or equal to 2, so you would need 2 full trades which would consist of at least 4 executions before this logic would work.

                      I look forward to being of further assistance.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      567 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      330 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      548 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      548 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X