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

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.
    Gaby V.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by MatthewLesko, 05-21-2021, 09:37 PM
    4 responses
    163 views
    0 likes
    Last Post Mahmad1323  
    Started by samish18, Today, 03:02 PM
    3 responses
    15 views
    0 likes
    Last Post bltdavid  
    Started by Jakub, Today, 11:06 AM
    1 response
    16 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by samish18, Today, 03:50 AM
    10 responses
    19 views
    0 likes
    Last Post samish18  
    Started by Mestor, 06-03-2018, 10:09 AM
    16 responses
    2,872 views
    0 likes
    Last Post myaha1000  
    Working...
    X