Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy window

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

    strategy window

    Hi, I am working on a strategy including a window, which is openable by a button.

    see attached code.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class ExampleStrategy : Strategy
    {
    // Button and Interface
    private Chart chartWindow;
    private DependencyObject searchObject;
    private bool isToolBarButtonAdded;
    private Button OpenExampleButton;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "ExampleStrategy";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    }
    else if (State == State.Configure)
    {

    }

    else if (State == State.Historical)
    {

    if (!isToolBarButtonAdded)
    Dispatcher.Invoke((Action)(() =>
    {
    chartWindow = Window.GetWindow(ChartControl.Parent) as Chart;
    if (chartWindow == null) return;

    foreach (DependencyObject item in chartWindow.MainMenu)
    {
    if (AutomationProperties.GetAutomationId(item) == "OpenExampleButton")
    {
    isToolBarButtonAdded = true;
    }
    }
    if (!isToolBarButtonAdded)
    {
    OpenExampleButton = new Button { Content = "Example", Visibility = Visibility.Collapsed };

    foreach (TabItem tab in chartWindow.MainTabControl.Items)
    if ((tab.Content as ChartTab).ChartControl == ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
    OpenExampleButton.Visibility = Visibility.Visible;

    chartWindow.MainMenu.Add(OpenExampleButton);
    OpenExampleButton.Click += OpenExampleButtonClick;
    AutomationProperties.SetAutomationId(OpenExampleBu tton, "Example");
    }
    }));
    }

    else if (State == State.Terminated)
    {
    if(chartWindow != null)
    {
    if (OpenExampleButton != null)
    {
    Dispatcher.Invoke((Action)(() =>
    {
    chartWindow.MainMenu.Remove(OpenExampleButton);
    OpenExampleButton.Click -= OpenExampleButtonClick;
    OpenExampleButton = null;
    }));
    }
    chartWindow = null;
    }
    }

    }

    private void OpenExampleButtonClick(object sender, RoutedEventArgs e)
    {
    Globals.RandomDispatcher.BeginInvoke(new Action(() => new ExampleWindow().Show()));
    }

    protected override void OnBarUpdate()
    {

    }

    }

    }

    public class ExampleWindow : NTWindow
    {
    public ExampleWindow()
    {
    Caption = "Example";
    Width = 500;
    Height = 500;
    TabControl tabControl = new TabControl();
    TabControlManager.SetIsMovable(tabControl, false);
    TabControlManager.SetCanAddTabs(tabControl, false);
    TabControlManager.SetCanRemoveTabs(tabControl, false);

    Content = tabControl;
    tabControl.AddNTTabPage(new ExamplePage());
    }
    public class ExampleWindowFactory : INTTabFactory
    {

    public NTWindow CreateParentWindow()
    {
    return new ExampleWindow();
    }

    public NTTabPage CreateTabPage(string typeName, bool isNewWindow = false)
    {
    return new ExamplePage();
    }
    }

    public class ExamplePage : NTTabPage
    {
    public ExamplePage()
    {
    Content = LoadXaml();
    }

    public override void Cleanup()
    {
    }

    protected override string GetHeaderPart(string variable)
    {
    return "Example";
    }

    private DependencyObject LoadXaml()
    {
    try
    {
    Page page;
    FileStream fs = new FileStream(System.IO.Path.Combine(NinjaTrader.Core .Globals.UserDataDir, @"bin\Custom\Strategies\EXAMPLE.xaml"), FileMode.Open);
    page = (Page)XamlReader.Load(fs);

    DependencyObject pageContent = page.Content as DependencyObject;

    return pageContent;
    }
    catch (Exception)
    {
    return null;
    }
    }

    protected override void Restore(XElement element) { }
    protected override void Save(XElement element) { }
    }
    }

    XAML

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:t="clr-namespace:NinjaTrader.Gui.Tools;assembly=NinjaTrad er.Gui">

    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.5*" />
    <ColumnDefinition Width="0.5*" />
    </Grid.ColumnDefinitions>

    <Button Grid.Column="0" Grid.Row="0" Content="Example Button"/>
    </Grid>

    </Page>

    How do I add a Button_Click Method (started several attempts), which can be used within the strategy code? So how whould I for example trigger a buy order by clicking the button? Is the window class declaration in the right spot?

    could you please give me an example of how to do that.

    regards and thanks

    #2
    Hello keepsimple,

    Thank you for writing in.

    I would suggest taking a look at the NinjaTrader 8 version of the Long/Short Toolbar Buttons strategy for some hints on how to fire off orders when pressing buttons: http://www.ninjatrader.com/support/f...d=8&linkid=697
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hi Zachary,

      I do know how to fire orders by pressing a button, perhaps i was a little unprecise, problem is I cannot make the button available in the strategy code because the button is located in the window class, the better question whould have been, how to i get access to the window button or its Click_Method within the example strategy class?

      regards

      Comment


        #4
        Hello keepsimple,

        Thank you for the clarification.

        I'll be looking into this further and will return with my findings.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Hello keepsimple,

          Thank you for your patience.

          You can utilize a static variable within the Strategy class that can be modified by a different class. So, once that button is clicked in the window, have it change that static variable. And then, based on the variable's value, you can have your strategy do something.

          I've provided a small example script demonstrating this. In this script, I have my strategy class and a separate custom class I've called TestClass.

          In TestClass' constructor, I have it change the strategy class' Test variable.

          You can see the variable is modified through the prints I've placed in the Strategy class.
          Attached Files
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            thanks zachary,

            i will work with that, if i have additional questions i will post back.

            regards

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Waxavi, Today, 02:10 AM
            0 responses
            2 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            5 views
            0 likes
            Last Post TradeForge  
            Started by Waxavi, Today, 02:00 AM
            0 responses
            2 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by elirion, Today, 01:36 AM
            0 responses
            4 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by gentlebenthebear, Today, 01:30 AM
            0 responses
            4 views
            0 likes
            Last Post gentlebenthebear  
            Working...
            X