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

Indicator value price level

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

    Indicator value price level

    Hello
    I'm trying to code a strategy that places stop limit orders and cancels them before triggering in case the triggering condition became false.
    The code below doesn't work, as the " longCondFalsePrice" keeps changing with the price.
    Do I have a way of capturing the exact price when my indicator changes state and keep that value, regardless of the price's changes after that?

    if(currentAsk > open && CONDITION TRUE)
    {
    // askLong = GetCurrentAsk(0);
    // if (askLong > open)
    Print ("S = " + Convert.ToString(CONDITION TRUE);
    longCondPrice = GetCurrentAsk(0);
    if (longAtmId.Length == 0)
    {
    Print("++++++++++");
    Print("Long condition at: " + Convert.ToString(longCondPrice) + " @ " + Time[0]);
    }
    // If there is a short ATM Strategy running close it.
    if(shortAtmId.Length != 0 && isShortAtmStrategyCreated)
    {
    AtmStrategyClose(shortAtmId);
    isShortAtmStrategyCreated = false;
    }
    // Ensure no other long ATM Strategy is running.
    if(longOrderId.Length == 0 && longAtmId.Length == 0 && !isLongAtmStrategyCreated && longCondPrice > open)
    {
    longOrderId = GetAtmStrategyUniqueId();
    longAtmId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, currentAsk + Math.Ceiling(offsetLimitStop) * TickSize, currentAsk + Math.Ceiling(offsetLimitStop) * TickSize,
    TimeInForce.Day, longOrderId, "3C", longAtmId, (atmCallbackErrorCode, atmCallBackId) =>
    {
    //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
    if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == longAtmId)
    isLongAtmStrategyCreated = true;
    barNumberOfLongOrder = CurrentBar;
    triggerPriceLong = GetCurrentAsk(0);
    // cancelLong = triggerPriceLong - (CancelOffsetTicks + 1) * TickSize / 2;
    Print("Long ATM created at: " + Convert.ToString(triggerPriceLong) + " - " + Time[0]);
    Print("##########");
    });
    }
    longCondFalsePrice = MRO(() => (longAtmId.Length != 0 && isLongAtmStrategyCreated && CONDITION false);
    if (longAtmId.Length != 0 && isLongAtmStrategyCreated && GomOrderFlowProValues1.NbSupportZones[0] == 0)
    {

    if (currentAsk == longCondFalsePrice)
    Print ("XX>>--Long condition false at: " + Convert.ToString(longCondFalsePrice) + " - " + "Stop Long Price: " + Convert.ToString(stopPriceLong));
    }

    else if ((longAtmId.Length != 0 && isLongAtmStrategyCreated && CONDITION false && currentAsk <= triggerPriceLong - Math.Ceiling(cancelLongShort) * TickSize) // Cancel if condition becomes false and price pulls back by CancelOffsetTicks * TickSize
    // || (longAtmId.Length != 0 && isLongAtmStrategyCreated && currentAsk > longCondFalsePrice + 2 * TickSize && CONDITION false // Cancel if price rises 2 ticks after the condition became false

    #2
    Hello itrader46, thanks for your post.

    It depends on when you want to set the longCondFalsePrice. You can activate it if IsFirstBarOfSession is true if you want to set the value at the beginning of the session. Note that the MRO method can not be used in State.DataLoaded, in case you meant when the script itself changes its state.

    There is also a problem with your MRO method. The longAtmId.Length and isLongAtmStrategyCreated are not series objects, so MRO has no history of those values to look back on. You can make two boolean series with the values of those variables on every bar and evaluate that:

    https://ninjatrader.com/support/helpGuides/nt8/?seriest.htm - Series<T> object

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello
      I am trying to set the longCondFalsePrice when the triggering condition switches from true to false intrabar and use that value into the cancel logic, but I need it to be fixed at the price the condition became false for the first time, rather than it change with the price after the condition became false.
      Basically it would be something like this: when the condition is true, trigger the stop limit order. If the condition became false and price pulls back, but then reverses in the initial direction, cancel the pending order if the condition is still false.
      I'm kinda struggling to find the right syntax for that, though.

      Comment


        #4
        Hello itrader46, thanks for your reply.

        You can set up a "runonce" boolean flag. This is more of a general C# topic of control flow, but it the pseudocode would be something like:

        Code:
        bool runOnce = false;
        
        ...
        
        OnBarUpdate()
        {
        
            if(!runOnce)
            {
        
                //Run the code here
                runOnce = true;        
        
            }
        
        }
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello
          I managed to get the first runOnce boolean working, but not the second one. For some reason, no matter how many if - else combinations I tried, the second runOnce is not seen and the logic goes directly to the cancel else if logic (this is the reason I also created runOnceLong/Short, as it's not working with ).

          PS: I'm sorry, but I don't know how to insert the code window in this message to make it easier readable, like you did


          Code:
          bool runOnce = false;
          bool runOnceLong = false;
          bool runOnceShort = false;
          .........
          OnBarUpdate()
          
          currentBid = GetCurrentBid(0);
          currentAsk = GetCurrentAsk(0);
          open = Bars.GetOpen(0);
          ..........
          
          if(currentAsk > open && CONDITION TRUE) {
           if (!runOnce) FIRST runOnce {
            Print("S = " + Convert.ToString(INDICATOR VALUE) + " - " + "Current Ask: " + Convert.ToString(currentAsk) + " - " + Time[0]);
            runOnce = true;
           }
           longCondPrice = GetCurrentAsk(0);
           if (longAtmId.Length == 0) {
            Print("++++++++++");
            Print("Long condition at: " + Convert.ToString(longCondPrice) + " @ " + Time[0]);
           }
           // If there is a short ATM Strategy running close it.
           if (shortAtmId.Length != 0 && isShortAtmStrategyCreated) {
            AtmStrategyClose(shortAtmId);
            isShortAtmStrategyCreated = false;
           }
          
           if (longOrderId.Length == 0 && longAtmId.Length == 0) {
            if (longCondPrice > open && !isLongAtmStrategyCreated) {
             longOrderId = GetAtmStrategyUniqueId();
             longAtmId = GetAtmStrategyUniqueId();
             AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, currentAsk + offsetLimitStop * TickSize, currentAsk + offsetLimitStop * TickSize,
              TimeInForce.Day, longOrderId, "3C", longAtmId, (atmCallbackErrorCode, atmCallBackId) => {
          
               if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == longAtmId)
                isLongAtmStrategyCreated = true;
               barNumberOfLongOrder = CurrentBar;
               triggerPriceLong = GetCurrentAsk(0);
               Print("Long ATM triggered at: " + Convert.ToString(triggerPriceLong) + " - " + /*" - BarsInProgress: " + BarsInProgress + " - " +*/ Time[0]);
               Print("##########");
              });
            }
           } else if (CONDITION FALSE) {
            if (!runOnceLong) SECOND runOnce NOT WORKING {
             shortCondFalsePrice = GetCurrentBid(0);
             Print("XX>>--Long condition false at: " + Convert.ToString(longCondFalsePrice) + " - " + "Trigger Long Price: " + Convert.ToString(triggerPriceLong) + " - " + Time[0]);
             runOnceLong = true;
            }
           }
          }
          else if (...) {
           ATM cancel logic
          }
          Last edited by NinjaTrader_ChrisL; 10-24-2019, 02:33 PM.

          Comment


            #6
            Hi itrader46,

            To add a code block, click the 'A' in the top right of the editor> Highlight the code> select the '#' symbol. I formatted the code you posted earlier. I can't tell why it's going wrong from the snippet. It seems that (CONDITION FALSE) is never being reached. You can check that with a Print statement to confirm:

            Code:
            else if (CONDITION FALSE) {
             Print("About to run once");
             if (!runOnceLong) {   ...
            Try looking into that part of the script.

            Best regards.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              I have no 'A' in the top right of my editor...
              Attached Files

              Comment


                #8
                Hello itrader46,

                Thanks for your reply.

                The "A" that NinjaTrader_Chris_L refers to is in the forum post you are writing. I've attached an example where the red square shows the "A" to click and then the red circle to show the "code" wraps. Clicking the "A" will show the lower bar.

                Also, you may want to review the forum FAQ for further options: https://ninjatrader.com/support/forum/help


                Click image for larger version

Name:	it46-1.PNG
Views:	334
Size:	25.1 KB
ID:	1075986
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello

                  I think that runOnce logic is quite helpful, but can you please help me understand the first part of it?

                  if(!runOnce) would mean something like 'If runOnce did not run yet'... then run it when 'code' happens?

                  I am looking to find a way to reset the bool runOnce, coz at the moment it only runs once when I set the code, but I would need to reset it to activate every time the conditions are met.

                  So basically, to reset runOnce, would I need something like:
                  if (condition = true)
                  runOnce = false,

                  OR

                  if(...)
                  runOnce ?

                  Also, to include it in the cancellation logic, would I need something like:
                  if(runOnce = true && ... &&...)
                  cancel

                  OR

                  if(runOnce && ...&&...)
                  cancel ?

                  Thank you


                  Originally posted by NinjaTrader_ChrisL View Post
                  Hello itrader46, thanks for your reply.

                  You can set up a "runonce" boolean flag. This is more of a general C# topic of control flow, but it the pseudocode would be something like:

                  Code:
                  bool runOnce = false;
                  
                  ...
                  
                  OnBarUpdate()
                  {
                  
                  if(!runOnce)
                  {
                  
                  //Run the code here
                  runOnce = true;
                  
                  }
                  
                  }

                  Comment


                    #10
                    Hello itrader46,

                    if(!runOnce) means "if runOnce is not true, then do..."

                    To reset the bool, you can check for another condition which should reset it, and then set the bool back to false.

                    I.E. if (Close[0] > Open[0]) runOnce = false;

                    A bool will return a true or a false, and checking (runOnce == true) will also return true/false. if (runOnce) and if (runOnce == true) would mean the same thing.

                    We have light programming education materials in the NinjaTrader 7 Help Guide. More robust C# programming education materials can be found external to NinjaTrader.



                    Please let us know if you have any questions.
                    JimNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by arvidvanstaey, Today, 02:19 PM
                    4 responses
                    11 views
                    0 likes
                    Last Post arvidvanstaey  
                    Started by samish18, 04-17-2024, 08:57 AM
                    16 responses
                    61 views
                    0 likes
                    Last Post samish18  
                    Started by jordanq2, Today, 03:10 PM
                    2 responses
                    9 views
                    0 likes
                    Last Post jordanq2  
                    Started by traderqz, Today, 12:06 AM
                    10 responses
                    18 views
                    0 likes
                    Last Post traderqz  
                    Started by algospoke, 04-17-2024, 06:40 PM
                    5 responses
                    48 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X