Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ChartControl.Dispatcher.InvokeAsync

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

    ChartControl.Dispatcher.InvokeAsync

    Hi,
    the following code is to enter order when price cross SMA, taking the value of quantity from Chart trader panel. But somehow neither EnterLong nor EnterShort gets executed, which means these "if CrossAbove/Below" condition never get triggered although in reality price did cross over SMA. Not sure why. Can you help me out here? (is it because the InvoleAsync was not used correctly? )

    thanks !

    =================================================
    protected override void OnBarUpdate()
    {


    ChartControl.Dispatcher.InvokeAsync((Action)(() => {

    Account a = ChartControl.OwnerChart.ChartTrader.Account;
    quantity = ChartControl.OwnerChart.ChartTrader.Quantity;

    // Print("chart trader quantity is " + quantity );


    if (CurrentBar < BarsRequiredToTrade)
    return;

    if (CrossAbove(Close[0], smaSlow, 1))

    {
    EnterLong(quantity, "Long Entry");
    }

    else

    if (CrossBelow(Close[0], smaSlow, 1))
    {
    EnterShort(quantity,"Short Entry");
    }
    }));

    }

    #2
    Hello judysamnt7,

    If this is a NinjaScript Strategy you can use the already provided Account object; no dispatcher will be needed.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    With the CrossAbove() / CrossBelow(), you are using Close[0] instead of Close, so only a single value will be compared and not the current and previous bar.

    Print the values to understand why these have or have not crossed.


    A print for the crossabove using only the current bars value would appear:

    Print(string.Format("{0} | Close[0]: {1} < smaSlow[1]: {2} && Close[0]: {1} > smaSlow[0]: {3}", Time[0], Close[0], smaSlow[1], smaSlow[0]));

    If you were intending to use CrossAbove(Close, smaSlow, 1) the print would appear as:

    Print(string.Format("{0} | Close[1]: {1} < smaSlow[1]: {2} && Close[0]: {3} > smaSlow[0]: {4}", Time[0], Close[1], smaSlow[1], Close[0], smaSlow[0]));

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,
      thanks for your feedback and it is very helpful.
      the reason I was using dispatcher is to retrieve quantity from chart trader panel , which then is used in EnterLong() method. I tried other way and never get this quantity correct.
      for my benefit, can you let me know what's the main purpose of Dispatcher and when I am supposed to use it ?

      Comment


        #4
        Hello judysamnt7,

        I would recommend fetching the quantity in State.DataLoaded and assigning this to a class level variable.

        The example below demonstrates.


        A dispatcher is used to access a different thread without a collision.
        Accessing the UI thread from NinjaScript thread (such as getting a chart trader UIElement) would require a dispatcher.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kujista, 04-25-2023, 02:22 AM
        5 responses
        67 views
        0 likes
        Last Post bltdavid  
        Started by KaizenNU, Today, 06:28 PM
        0 responses
        9 views
        0 likes
        Last Post KaizenNU  
        Started by bertochi, Today, 05:25 PM
        0 responses
        15 views
        0 likes
        Last Post bertochi  
        Started by ntbone, 02-12-2025, 01:51 AM
        4 responses
        27 views
        0 likes
        Last Post ntbone
        by ntbone
         
        Started by XanderT, Today, 03:38 PM
        4 responses
        13 views
        0 likes
        Last Post XanderT
        by XanderT
         
        Working...
        X