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?
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;
}
}
}
}
​

Comment