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

Problem with multiple conditions

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

    Problem with multiple conditions

    I'm trying to modify the SampleATM Strategies and MA Cross sample strategies to see if I can get a simple two-direction strategy to work that also invokes an ATM. The code that is giving me problems is pasted below. I am trying to combine the ma crossover test with the orderID.Length test and it doesn't like that I'm using && to and the conditions. Is there a better way to structure this since the conditions need to both be in place for the order to be placed? The error code I'm getting is CS1525 but there is no documentation for that code.
    Thanks

    // Submits an entry limit order to buy if the fast ma crosses above the slow ma and initiates an ATM Strategy to manage the entry.
    // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStratSample' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
    if (CrossAbove(SMA(Fast), SMA(Slow), 1)) &&
    orderId.Length == 0 && atmStrategyId.Length == 0 ;
    {
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
    }

    #2
    It always helps if you post complete (!) compiler error messages.

    Comment


      #3
      Try Again

      Let me take another shot at it. First I will explain what I am trying to do. Then I will post the entire code and compiler error base.

      I am using the ATM Sample Strategy to learn more about how to incorporate ATM strategies into a strategy code base. I'm simply trying to substitute code which checks that no ATM order already exists, (at least that's what I think the sample is doing) and combines it with a simple 2 ma crossover test.

      The code that works in the sample that I'm trying to use is:

      if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Action.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);


      This code I believe checks to see if there is already an active order and if there isn't and the current bar is an up bar (close > open) then buy using a limt at the low of this bar and invoke an ATM Strategy called "AtmStrategyTemplate).

      What I'm using to substitute for the existing sample code is the following:

      if (orderId.Length == 0 && atmStrategyId.Length == 0 && (CrossAbove(SMA(Fast)), (SMA(Slow)), 1))

      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
      }


      I"m having trouble with the first line of that code. I keep getting errors saying ") expected" but I've messed with the parantheses count a bunch of times and it doesn't change the message when I try and compile again, so my assumption is that what it doesn't like about that code isn't really what it is reporting. At other times in the evolution of this coding I got a message indicating it didn't like the && usage but that is exactly what is in the sample code so I'm confused.

      So here is the whole code I'm trying to get working, followed by all of the compiler errors:

      //
      // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Strategy;
      #endregion


      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      ///
      /// </summary>
      [Description("")]
      public class SampleATMStrategyMod1 : Strategy
      {
      #region Variables
      private string atmStrategyId = string.Empty;
      private string orderId = string.Empty;
      private int Fast = 8;
      private int Slow = 21;
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      SMA(Fast).Plots[0].Pen.Color = Color.Orange;
      SMA(Slow).Plots[0].Pen.Color = Color.Green;

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

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies"

      // Make sure this strategy does not execute against historical data
      if (Historical)
      return;


      // Submits an entry limit order to buy if the fast ma crosses above the slow ma and initiates an ATM Strategy to manage the entry.
      // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStratSample' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
      if (orderId.Length == 0 && atmStrategyId.Length == 0 && (CrossAbove(SMA(Fast)), (SMA(Slow)), 1))

      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
      }

      if (orderId.Length == 0 && atmStrategyId.Length == 0 && CrossBelow(SMA(Fast), SMA(Slow), 1)) ;

      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Action.Sell, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
      }
      // Check for a pending entry order
      if (orderId.Length > 0)
      {
      string[] status = GetAtmStrategyEntryOrderStatus(orderId);

      // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
      if (status.GetLength(0) > 0)
      {
      // Print out some information about the order to the output window
      Print("The entry order average fill price is: " + status[0]);
      Print("The entry order filled amount is: " + status[1]);
      Print("The entry order order state is: " + status[2]);

      // If the order state is terminal, reset the order id value
      if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
      orderId = string.Empty;
      }
      } // If the strategy has terminated reset the strategy id
      else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
      atmStrategyId = string.Empty;


      if (atmStrategyId.Length > 0)
      {
      // You can change the stop price
      // if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
      // AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

      // Print some information about the strategy to the output window
      Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
      Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
      Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
      Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
      }
      }

      #region Properties
      #endregion
      }
      }


      COMPILER ERRORS:
      NinjaScript File Error Code Line Column
      Strategy\SampleATMStrategyMod1.cs ) expected. 63 82
      Strategy\SampleATMStrategyMod1.cs ; expected. 63 95
      Strategy\SampleATMStrategyMod1.cs ; expected. 63 98
      Strategy\SampleATMStrategyMod1.cs Statement expected. 63 99
      Strategy\SampleATMStrategyMod1.cs ) expected CS1026 - click for info 64 83
      Strategy\SampleATMStrategyMod1.cs Invalid expression term ' ' CS1525 - click for info 64 83
      Strategy\SampleATMStrategyMod1.cs ; expected CS1002 - click for info 64 96
      Strategy\SampleATMStrategyMod1.cs Invalid expression term ' ' CS1525 - click for info 64 96
      Strategy\SampleATMStrategyMod1.cs ; expected CS1002 - click for info 64 98
      Strategy\SampleATMStrategyMod1.cs ; expected CS1002 - click for info 64 99
      Strategy\SampleATMStrategyMod1.cs Invalid expression term ')' CS1525 - click for info 64 99
      Strategy\SampleATMStrategyMod1.cs ; expected CS1002 - click for info 64 100
      Strategy\SampleATMStrategyMod1.cs Invalid expression term ')' CS1525 - click for info 64 100
      Strategy\SampleATMStrategyMod1.cs ; expected CS1002 - click for info 64 101

      I hope you can read this because it looks terrible right now. Is there a better way to submit code or compiler errors?

      Comment


        #4
        Follow on to previous posts

        I have one other question regarding how to do properly conduct C# code development.

        It seems that once I have a strategy or indicator which has some kind of unresolved compiler error, any other strategy or indicator I try to write won't compile because of the errors in the first piece of code. So I guess everything is related and if I want to pursue some other line of code development I should either delete all strategies or indicators that have a compiler error, or at the least put them in some other directory not accessible to the code containing subdirectory.

        Is that correct or is there some kind of simple requirement I either don't know or am overlooking?

        I do appreciate your help as I trudge through the swamp called C# programming.
        (I used to think I was an above average thinker, but C# has sure brought me to my knees.)
        Last edited by daven; 03-27-2008, 11:42 AM. Reason: Typos

        Comment


          #5
          When you compile, NT compiles are scripts into a single DLL. Thus, an error in any file will prevent a successful compilation.

          What I suggest is commenting out blocks of code until you can compile. Then uncomment chunk by chunk until you get to the piece of code that gives you the problem. If you still have problems then, let us know.
          RayNinjaTrader Customer Service

          Comment


            #6
            Problem Code

            I will try that, though I'm pretty sure what code is giving me the problem in the code below. It is somehow unhappy about combining the test for no order with the ma crossover test. If I could get a little guidance on how to structure that section of code it would be enormously helpful.
            Thanks
            daven

            Comment


              #7
              This is the bugger!!

              Okay, you can ignore the big code base a few posts back. When I commented out this particular line (noted in bold), the strategy compiled, so this is the problem.

              if (Historical)
              return;


              // Submits an entry limit order to buy if the fast ma crosses above the slow ma and initiates an ATM Strategy to manage the entry.
              // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStratSample' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****

              // if (orderId.Length == 0 && atmStrategyId.Length == 0 && (CrossAbove(SMA(Fast)), (SMA(Slow)), 1))

              Comment


                #8
                If you are referring to this:

                if (CrossAbove(SMA(Fast), SMA(Slow), 1)) &&
                orderId.Length == 0 && atmStrategyId.Length == 0 ;
                {
                atmStrategyId = GetAtmStrategyUniqueId();
                orderId = GetAtmStrategyUniqueId();
                AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
                }

                it should be:

                if (CrossAbove(SMA(Fast), SMA(Slow), 1) &&
                orderId.Length == 0 && atmStrategyId.Length == 0) ;
                {
                atmStrategyId = GetAtmStrategyUniqueId();
                orderId = GetAtmStrategyUniqueId();
                AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AtmStratSample", atmStrategyId);
                }
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Well that fixed

                  Thanks Ray!! That fixed it. Course now I have a strategy that trades really nuts but at least I'm on the path, (the right one I hope).
                  Sorry for such a dumb question. I really messed with the parentheses but never got it right.
                  daven

                  Comment


                    #10
                    Is it possible to build an array to store user input and pass it through the script? let say I would like to place 2 orders with different price and assigned 2 different ATM strategy to it. I found myself duplicating the variable and statements to accomplish that.

                    thanks

                    Comment


                      #11
                      Tranbo, unfortunately this is outside of what we can support - however I believe a string array would work.
                      BertrandNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by thumper57, 05-11-2024, 04:30 PM
                      12 responses
                      36 views
                      0 likes
                      Last Post thumper57  
                      Started by eladlevi, Today, 08:41 AM
                      3 responses
                      5 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by FAQtrader, 04-25-2024, 12:00 PM
                      9 responses
                      150 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by ETFVoyageur, Yesterday, 02:10 AM
                      5 responses
                      51 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by ETFVoyageur, 05-07-2024, 07:05 PM
                      20 responses
                      163 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X