I'm trying to create an indiciator that places a buy and sell button at the top left of a chart when clicked would place a stop limit buy order at the high of previous candle (Buy Button) and stop limit sell order at the low of previous candle ( Sell Button) . It needs to use the selected ATM from the chart trader to determine contract amout and brackets. Below is the code I have :
using NinjaTrader.Gui;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.Data;
using NinjaTrader.Cbi;
using System.Windows.Controls;
using System.Windows.Media;
namespace NinjaTrader.NinjaScript.Indicators
{
public class TKCandleTrader : Indicator
{
private Button buyButton;
private Button sellButton;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Buy/Sell Buttons with Chart Trader ATM Strategy";
Name = "TKCandleTrader";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
}
else if (State == State.Configure)
{
// Create buttons
buyButton = new Button
{
Content = "Buy",
Background = Brushes.Green,
Foreground = Brushes.White,
Width = 50,
Height = 25,
Margin = new System.Windows.Thickness(5)
};
sellButton = new Button
{
Content = "Sell",
Background = Brushes.Red,
Foreground = Brushes.White,
Width = 50,
Height = 25,
Margin = new System.Windows.Thickness(5)
};
// Add buttons to the chart
ChartControl.Dispatcher.InvokeAsync(() =>
{
var stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
VerticalAlignment = System.Windows.VerticalAlignment.Top,
HorizontalAlignment = System.Windows.HorizontalAlignment.Left
};
stackPanel.Children.Add(buyButton);
stackPanel.Children.Add(sellButton);
ChartControl.WPFContainer.Children.Add(stackPanel) ;
});
// Add event handlers
buyButton.Click += BuyButton_Click;
sellButton.Click += SellButton_Click;
}
}
private void BuyButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
// Calculate stop limit price
double stopPrice = High[1] + TickSize;
double limitPrice = stopPrice;
// Place buy stop limit order using the ATM strategy from Chart Trader
string atmStrategyId = AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, stopPrice, limitPrice, TimeInForce.Day, null, null);
if (string.IsNullOrEmpty(atmStrategyId))
{
// Handle error: ATM strategy failed to create
Print("Error: Failed to create ATM strategy for Buy order.");
}
else
{
Print("Buy order placed with ATM strategy ID: " + atmStrategyId);
}
}
private void SellButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
// Calculate stop limit price
double stopPrice = Low[1] - TickSize;
double limitPrice = stopPrice;
// Place sell stop limit order using the ATM strategy from Chart Trader
string atmStrategyId = AtmStrategyCreate(OrderAction.Sell, OrderType.StopLimit, stopPrice, limitPrice, TimeInForce.Day, null, null);
if (string.IsNullOrEmpty(atmStrategyId))
{
// Handle error: ATM strategy failed to create
Print("Error: Failed to create ATM strategy for Sell order.");
}
else
{
Print("Sell order placed with ATM strategy ID: " + atmStrategyId);
}
}
}
}
region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TKCandleTrader[] cacheTKCandleTrader;
public TKCandleTrader TKCandleTrader()
{
return TKCandleTrader(Input);
}
public TKCandleTrader TKCandleTrader(ISeries<double> input)
{
if (cacheTKCandleTrader != null)
for (int idx = 0; idx < cacheTKCandleTrader.Length; idx++)
if (cacheTKCandleTrader[idx] != null && cacheTKCandleTrader[idx].EqualsInput(input))
return cacheTKCandleTrader[idx];
return CacheIndicator<TKCandleTrader>(new TKCandleTrader(), input, ref cacheTKCandleTrader);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TKCandleTrader TKCandleTrader()
{
return indicator.TKCandleTrader(Input);
}
public Indicators.TKCandleTrader TKCandleTrader(ISeries<double> input )
{
return indicator.TKCandleTrader(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TKCandleTrader TKCandleTrader()
{
return indicator.TKCandleTrader(Input);
}
public Indicators.TKCandleTrader TKCandleTrader(ISeries<double> input )
{
return indicator.TKCandleTrader(input);
}
}
}
#endregion
I've attached the compile errors that I'm getting. What can i do to fix these please?
Thanks
TK
Comment