Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Market Buy Order indicator

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

    Market Buy Order indicator

    I've been working on an indicator that places market buy order when price crosses a line placed with a mouse click.
    1. is there perhaps ready made indicator that does that?
    2. is this a safe way to place order, what corner cases should I pay attention to? What's the best way to prevent multiple orders?
    PHP Code:
    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    
    
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class AltLeftClickHorizontalLineOrder : Indicator
    {
    private ChartScale chartScale;
    private Point clickPoint = new Point();
    private double convertedPrice;
    private string lineTag;
    private string textTag;
    private bool lineDrawn = false;
    private bool orderPlaced = false;
    private Order entryOrder;
    private Account myAccount;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Draws a horizontal line at the price level where the left mouse button is clicked while holding the Alt key and places a buy order if the price crosses the line.";
    Name = "AltLeftClickHorizontalLineOrder";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    DisplayInDataBox = false;
    DrawOnPricePanel = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    }
    else if (State == State.Historical)
    {
    if (ChartControl != null)
    {
    foreach (ChartScale scale in ChartPanel.Scales)
    if (scale.ScaleJustification == ScaleJustification)
    chartScale = scale;
    
    ChartControl.MouseLeftButtonDown += MouseClicked;
    }
    }
    else if (State == State.DataLoaded)
    {
    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
    
    if (myAccount != null)
    {
    myAccount.ExecutionUpdate += OnExecutionUpdate;
    }
    }
    else if (State == State.Terminated)
    {
    if (ChartControl != null)
    ChartControl.MouseLeftButtonDown -= MouseClicked;
    
    if (myAccount != null)
    {
    myAccount.ExecutionUpdate -= OnExecutionUpdate;
    }
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (lineDrawn && Close[0] > convertedPrice && !orderPlaced)
    {
    Print("Price has crossed the line. Current Price: " + Close[0] + ", Line Price: " + convertedPrice);
    
    if (entryOrder == null || entryOrder.OrderState == OrderState.Cancelled || entryOrder.OrderState == OrderState.Rejected)
    {
    Print("Entering order...");
    string ocoId = Guid.NewGuid().ToString(); // Generate a unique OCO ID
    entryOrder = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, 1, 0, 0, ocoId, "BuyOrder", Core.Globals.MaxDate, null);
    myAccount.Submit(new[] { entryOrder });
    orderPlaced = true; // Set the flag to indicate that an order has been placed
    }
    }
    }
    
    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
    if (Keyboard.IsKeyDown(Key.LeftAlt))
    {
    clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartControl as IInputElement).X, ChartControl.PresentationSource);
    clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartControl as IInputElement).Y, ChartControl.PresentationSource);
    
    convertedPrice = Instrument.MasterInstrument.RoundToTickSize(chartS cale.GetValueByY((float)clickPoint.Y));
    
    if (lineDrawn)
    {
    // Remove existing line and text if already drawn
    RemoveDrawObject(lineTag);
    RemoveDrawObject(textTag);
    }
    
    // Draw new line and text
    lineTag = "PriceLine" + CurrentBar;
    textTag = "PriceText" + CurrentBar;
    Draw.HorizontalLine(this, lineTag, convertedPrice, Brushes.Red);
    Draw.Text(this, textTag, convertedPrice.ToString(), 0, convertedPrice, Brushes.Red);
    
    lineDrawn = true;
    orderPlaced = false;
    }
    }
    
    private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
    {
    // if (e.Order == entryOrder && e.Execution.Quantity > 0)
    {
    Print("Order executed at price: " + e.Execution.Price);
    
    // Remove the line and text once the order is executed
    RemoveDrawObject(lineTag);
    RemoveDrawObject(textTag);
    lineDrawn = false;
    orderPlaced = false;
    }
    }
    }
    }
    
    ​ 
    
    Last edited by MiCe1999; 06-17-2024, 07:15 PM.

    #2
    Hello MiCe1999,

    Thank you for your post.

    I have seen indicators shared by other vendors or users that demonstrate submitting orders by clicking on the chart.

    I would not be able to make a recommendation if this is a "safe" way to submit orders, you would need to assess for yourself if you are comfortable with the way the code is performing / risk undertaken by trading live.

    To prevent multiple orders I suggest keeping track of the number of entries placed through code, then you can limit further entries if this limit has been reached.

    This thread will remain open for other users who may want to provide feedback.

    Comment

    Latest Posts

    Collapse

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