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

Simple Trading of Two Securities

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

    Simple Trading of Two Securities

    I am trying to write a very basic code to trade two securities: VWELX and VWINX

    Is there still an example out there of how to make this work? I have added the second security in the Initialize Section, but I cannot get the Strategy to recognize Entry of one while Exit of the other. I am trying to run a very simple 50/200 cross test. When 50/200 is under 1, buy VWINX, When >1, sell VWINX, but VWELX. Easy.

    Also, do you know if there are any books dedicated to code writing for Ninjatrader specifically?

    Thanks!

    #2
    Hello dunwoodjr, and thank you for your question.

    The Help Guide section on multiple time frames and instruments contains several code samples. I am providing a link.

    http://ninjatrader.com/support/helpG...nstruments.htm

    The Sample MA Crossover strategy covered in the NinjaScript Editor video provides a very good backbone for the strategy you are trying to create. You will simply need to use the plural versions of your methods that reference BarsArray, such as Closes, Highs, etc. I am providing a link to this recorded training webinar from the NinjaTrader 7 Training YouTube channel.

    https://www.youtube.com/watch?v=K8v_...56536A44DD7105

    While I can not endorse any product, I do know that there are books that have been written specifically for NinjaScript programming.

    Please let us know if we can assist further.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      This is how Stupid I am...

      {
      SMA(Fast).Plots[0].Pen.Color = Color.Orange;
      SMA(Slow).Plots[0].Pen.Color = Color.Green;

      Add(SMA(Fast));
      Add(SMA(Slow));

      Add("VWINXADJ", PeriodType.Day, 1);

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick).
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CrossAbove(SMA(Fast), SMA(Slow), 1))
      EnterLong();
      else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
      EnterLong(1,1,"VWINXADJ");
      }

      I have read over 100 webpages...searched Amazon...Googled a 1,000 different ways to figure this out...spent 24 hours now just trying to figure out how to simply have the system exit one fund and buy another on a CrossOver.

      I have been able to code many other things using a simple security but I can't find any training material anywhere, which is making it very difficult to try and code anything beyond the basic. I am ready to go live with NJT and trade...my account is open at Interactive Brokers. I just don't have the efficiency to code what I want to do. Having said this...

      1) Can you please just help me this one thing above...

      2) I know you can not recommend anything particular, but in general...if I wanted to really understand how to code NJT with proficiency, should I learn C+ first then come back to coding NJT specifically or is there a more direct route? Your general opinion please...

      Thanks!!
      Last edited by dunwoodyjr; 04-13-2016, 10:13 AM.

      Comment


        #4
        Hello dunwoodyjr,

        Your code is actually very close to complete. I am submitting a modified code sample, and will discuss my changes.

        Code:
                protected override void Initialize()
                {
                    Add("NQ 06-16", PeriodType.Day, 1);
                    Add(SMA(Closes[1], Fast));
                    Add(SMA(Slow));
        
                    CalculateOnBarClose = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick).
                /// </summary>
                protected override void OnBarUpdate()
                {
                    if (CrossAbove(SMA(Closes[1], Fast), SMA(Close, Slow), 1))
                    {
                        Print("Entering long on primary instrument");
                        EnterLong();
                    }
                    else if (CrossBelow(SMA(Closes[1], Fast), SMA(Close, Slow), 1))
                    {
                        Print("Entering long on secondary instrument");
                        EnterLong(1,1,"This is a signal, not an instrument");
                    }
                }
        Changes,
        • It was easier for me to test this on the NQ instrument. You should change "NQ 06-16" back to your second instrument
        • You will want to use double quotes, "", rather than single quotes, '', for strings
        • I moved the place where you added your data series above your two SMA plots, so that I could use it in one of the SMA's plots.
          • If it was below your SMA indicators, as in your code, you would refer to it with index 3, as in Closes[3]
        • I removed the two lines of code where you colored your SMA plots for clarity. Every remaining line is necessary for the solution to your query
        • For each of your SMAs, I added a data series. I bolded this because it is the most important change.
          • You were previously using SMA(period), which is short for SMA(Close, period), which in turn is short for SMA(Closes[0], period).
          • In other words, each of your SMAs was only working on your primary data series. We are now comparing Closes[1] against Close, or in other words, your secondary against your primary data series
          • If I misinterpreted what you were intending to do, this explanation should aid you in correcting your code. Please let me know if this is not clear, and I can modify my code sample
        • I changed your signal name to make the difference between signal and instrument names clear.
        • I added code to make it easier to debug your strategy using the NinjaTrader Control Center -> Tools -> Output Window

        With regard to your second question, C++ is a sibling language to C#. Both have a parent language, C. NinjaTrader is in my opinion a good platform in and of itself to learn C# on, especially if you are going to primarily program with NinjaScript. However, if you are seeking to become a more proficient programmer generally, I would recommend of the three languages above learning C# directly. C does not have any built-in object oriented programming support (although many libraries provide some OO implementations), and C++ has a lot of language specific keywords and concepts that will not translate to C#.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by tonynt, Today, 11:50 AM
        0 responses
        2 views
        0 likes
        Last Post tonynt
        by tonynt
         
        Started by Welovethelibrary, Today, 11:44 AM
        0 responses
        2 views
        0 likes
        Last Post Welovethelibrary  
        Started by goodknight777, Today, 11:39 AM
        0 responses
        5 views
        0 likes
        Last Post goodknight777  
        Started by MohammedAmine, Today, 10:44 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by Nolyn, Today, 10:59 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X