• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 usero86, Today, 04:28 AM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_PatrickG  
            Started by bohenriksen, Today, 04:00 AM
            0 responses
            6 views
            0 likes
            Last Post bohenriksen  
            Started by Derjan, Today, 03:09 AM
            0 responses
            7 views
            0 likes
            Last Post Derjan
            by Derjan
             
            Started by CDXTrader, Today, 12:29 AM
            0 responses
            10 views
            0 likes
            Last Post CDXTrader  
            Started by TopGun13, Yesterday, 10:35 PM
            0 responses
            7 views
            0 likes
            Last Post TopGun13  
            Working...
            X