Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Exposing Drawing Objects to a Strategy (by an Indicator)

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

    Exposing Drawing Objects to a Strategy (by an Indicator)

    Hello,
    I have this indicator that I downloaded from Ninjatrader ecosystem/Trader app share and other than a fancy volume histogram at the bottom, it draws various graphic symbols on the chart (such as a Blue Square, Yellow Up Triangle, Green Diamond etc etc right under (or above) the candlestick price bars).
    I wish to use this indicator in a Strategy and I have been thinking "How do I get the strategy to be able to see the drawing symbols?"

    My first thought was that I need to modify the indicator and add this code so that a strategy would just use the numeric value of the new Plot histogram to decode the graphic symbols painted::
    private int symbol;
    AddPlot(new Stroke(Brushes.Orange, 3), PlotStyle.Bar, "SymbolCode");
    then later:
    symbol = 0 ; // initialise to zero i.e. NO condition i.e. no VSA Symbol painted
    if (isUpThrustBar[0] && !isNewConfirmedUpThrustBar[0])
    {
    Draw.Square(this, "MySquare" + CurrentBar, true, 0, High[0] + 6 * TickSize, Brushes.Red);
    symbol= symbol | 0x01 ; // Set bit 1 by ORing with 01
    }
    if (reversalLikelyBar)
    {
    Draw.Diamond(this, "MyDiamond" + CurrentBar, true, 0, High[0] + 6 * TickSize, Brushes.Blue);
    symbol = symbol | 0x02 ; // Set bit 2
    }
    if (isNewConfirmedUpThrustBar[0])
    {
    Draw.TriangleDown(this, "MyTriangleDown" + CurrentBar, true, 0, High[0] + 6 * TickSize, Brushes.Red);
    symbol = symbol | 0x04 ; // Set bit 3
    }
    if (strenghtInDownTrend)
    {
    Draw.Square(this, "MySquare" + CurrentBar, true, 0, Low[0] - 6 * TickSize, Brushes.Lime);
    symbol = symbol | 0x08 ; // Set bit 4
    }
    etc etc for the other graphic symbols - there are 14 in total

    and so on .. and then:
    Values[4] = (double) symbol; // assign value for (plotting) but especially for use by Strategies

    But the NT compiler throws a wobbly saying it can't do the cast.
    (I only added the cast after it gave an error initially, and I thought "Of course Mark, the plotting uses "double"s and you have Integer")
    But it keeps not wanting to compile
    (By the way I did add the other bit to 'properties'
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SymbolCode
    {
    get { return Values[4]; }
    }

    ************************************************** *
    So that was attempt/approach #1 and maybe that could be fruitful or maybe not.

    So I searched in your forums and the NT documentation and I wonder whether I should
    be using the function/method DrawObjects.ToList() in my (yet to write) strategy and iterate thru
    the graphics objects list using this method (approach #2) to find out if the indicator has painted any of those graphics objects (i.e. to retrieve the graphics)

    Can a clever Ninja person suggest the best approach please?

    Thanks, Mark.
    Last edited by mark38; 12-31-2020, 02:34 PM.

    #2
    Hello Mark, thanks for your post and welcome to the NinjaTrader Forums.

    The best way to expose a signal for draw objects is to create a custom series in the indicator. We have an example here that demonstrates the idea with links to relevant documentation:

    https://ninjatrader.com/support/help...alues_that.htm

    For the indicator you are working with, you would add a boolean series for each different draw object. All custom Series<T> objects will have a slot index for every bar on the chart. When there is no draw object i.e. no condition to draw, mark the series with False, otherwise when it draws a symbol mark the series with True.

    Please let me know if you have any questions on this material.
    Chris L.NinjaTrader Customer Service

    Comment


      #3

      Hello Chris,

      Thanks for the information and suggestions.

      I went straight to work and did the modifications by downloading the code at the link you provided and took time to understand how it relates to what you said.
      So I got my Series<bool> variables and all that modified so that it should work.

      But I should mention, that even before my first post, I got some compile errors and then I tried creating a different indicator etc etc and evidently got my Ninja into some inconsistent state, because at one point I had two differently named indicators listed in the NinjaScript Editor and yet when I would try to add that second indicator onto a Chart, that second indicator wasn't listed and instead the first indicator was there twice! i.e. two indicators with the same name!

      So I then deleted those indicators on which I worked on recently
      and then I tried to compile this new indicator with the Series<bool> stuff according to your suggestion but unfortunately it gave errors.
      It said the error was with this indicator (and not some other) and the line on which it said was the error, was blank!

      --------------
      I read somewhere, that in order to improve execution speed, that all these Ninja assemblies (i.e. Indicators, strategies etc.) are compiled into one big module and thus if a single ninjascript file creates errors, it won't allow one to ignore it and continue developing other stuff - the errors MUST be resolved. That brings me to a side question - can this 'monolithic' compilation method be turned off, at least during code development, so as to be more forgiving to the programmer? I can turn it back on, when the code (in whichever version and variation of the indicator works, having discarded all the failed prototypes). Can we do this?
      --------------

      I then tried to create a 'blank' indicator with the NinjaScript editor to see if at least that works, but straight after Generate, it gave me an error: (even though the indicator code did come up)
      """"
      Error
      Unhandled exception: Index was out of range. Must be non-negative and less than the size of the collection.
      Parameter name: index
      """"
      Because code created by the wizard can't have errors in it, I thought I needed to clear more of the old indicator stuff that I was doing to try to get my Ninja into a 'sane' state.

      So I decided to try to delete all the indicators that I did since I first started having problems.
      Then I openned some of the existing indicators, SMA or whatever it was (the built-in ones) and compiled it, and it was OK. Tried another one. Still OK.
      So for one last test I copy&paste the whole code of a built-in indicator KeyReversalUp into a new Indicator created by the NT editor/Wizard. I called it KeyRevUp (I figure I need this because I couldn't keep the same name) and after pasting the KeyReversalUp code I made a global Search&Replace:
      KeyReversalUp
      to
      KeyRevUp

      and then I tried to compile expecting that it should compile OK.
      Alas, even through I did not change the code at all (except for the global Search&Replace), the compiler gave an error:

      KeyRevUp.cs 'NinjaTrader.Custom.Resource' does not contain a definition for 'NinjaScriptIndicatorDescriptionKeyRevUp' CS117 Line 41

      now my line 41 (which I didn't touch, by the way) has:
      Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionKeyRevUp;

      So at this point, I am thinking along 2 lines:
      1) Either my Ninja is still in some inconsistent state which needs fixing first, or
      2) My Ninja is OK, but when 'cloning' indicators there is something else I need to do.

      What are your views on the matter?

      Regards, Mark

      (By the way many thanks for helping, I appreciate it's holiday time - "vacation" in the US parlance :-)

      Comment


        #4
        Hello,

        Well I fixed up the code so that it now compiles.
        For the benefit of any readers, there were a few mistakes.
        1) A simple misplacing of a block of code (it was inside curly brackets of another block whereas it should be outside of it)
        2) I was missing the [0] - since its' iSeries<bool> it is like an array and needs that subscript, rather than just bool.
        3) I commented out the new stuff in #properties - I am not sure if I will need it for the Strategy to access the variables or not.
        If it needs it, I would have to uncomment it and recompile.

        So compiling is great. Only one snag.
        When I try to add the indicator to my chart, its' name is not in the drop-down list of available indicators.
        And one of the indicators is listed twice - i.e. it is as if there were two indicators with an identical name. I'm sure that is wrong.

        So I think I still have some inconsistency in my Ninja. Hmmm....
        Mark.

        Comment


          #5
          Hello Mark, thanks for your reply.

          All scripts have a "Name" property. This is set up in State.SetDefaults. This is the name you will see in the indicators list. Please also check the Log tab of the Control Center for any initialization errors your indicator is having. Please share those errors if you are unsure on how to fix. The duplicate names in the list are coming from two separate indicators with different class names, but the same "Name" property.

          There is no way to change the way NinjaTrader compiles the NinjaTrader.Custom.DLL.

          I look forward to assisting.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hello Guys,
            I wanted to know if Mark was able to get over the chart objects issue.
            Can you share if you are able to use this in a strategy or simply to output the chart objects occurrences into the Alert Logs or to an external file.
            regards,
            AH

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            11 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            10 views
            0 likes
            Last Post carnitron  
            Started by strategist007, Yesterday, 07:51 PM
            0 responses
            11 views
            0 likes
            Last Post strategist007  
            Started by StockTrader88, 03-06-2021, 08:58 AM
            44 responses
            3,980 views
            3 likes
            Last Post jhudas88  
            Started by rbeckmann05, Yesterday, 06:48 PM
            0 responses
            9 views
            0 likes
            Last Post rbeckmann05  
            Working...
            X