Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Compile errors

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

    Compile errors

    Hi,

    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
    Attached Files

    #2
    Hello snoinvest,

    Thank you for your post.

    Atm Strategy methods like AtmStrategyCreate() can only be used in strategy scripts. If you wanted to use the Add On approach, you could use StartAtmStrategy() instead.



    As the error suggests, ChartControl does not have a definition for WPFContainer. If you are trying to create custom order buttons on your chart I suggest looking at this example which demonstrates:

    Gaby V.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by martyn73, 09-06-2019, 01:27 PM
    5 responses
    298 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by Battle, Today, 03:37 PM
    0 responses
    3 views
    0 likes
    Last Post Battle
    by Battle
     
    Started by Indranikaushal, Today, 03:18 PM
    0 responses
    3 views
    0 likes
    Last Post Indranikaushal  
    Started by Rraannddyy, 09-15-2021, 06:14 PM
    5 responses
    997 views
    0 likes
    Last Post nytrader83  
    Started by M_ichel, Yesterday, 02:21 PM
    4 responses
    34 views
    1 like
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X