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

Storing Variables at execution

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

    Storing Variables at execution

    Hi Guys, how do you store variables prior to execution then call them up after the execution? For example if I entered a trade and wanted to use the EMA value that existed prior to the execution as a target.

    I'm struggling to find any examples on the forum on how to do this?

    Thanks
    DJ

    #2
    Hello djkiwi,
    I am happy to assist you.

    You can declare a private field for your need. A basic code snippet would look like this

    In Variable
    Code:
    private double myEMATarget = 0; //here we store our value
    In OnBarUpdate (or any other place where you assign and call the variable myEMATarget

    Code:
    if (condition) //condition for long entry
    {
      EnterLong(..);
      myEMATarget = EMA(14)[0]; //get the value of EMA 14
    }
    
    //and our exit condition
    
    //execute only if we are in a long
    if (Position.MarketPosition == MarketPosition.Long)
    {
            //if last price is greater than myEMATarget (we assigned a value to myEMATarget when we made our entry) we exit our trade
    	if (Close[0] > myEMATarget)
    			ExitLong(..);
    }
    Hopefully you can built upon the code snippet which I provided above.

    Please let me know if I can be of any further help.

    Regards,
    Joydeep.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hello,

      I´m trying to store a closeprice (or might also be the GetCurrentAsk()) to a variable when price crossabove a certain level. It works that the bool shows correct true and false when crossing above or below and I´d like to store that value when the crossabove occurred. But the value of the variable is always updated.

      if(CrossAbove(Close, R1,1))
      Variableup = true;
      VariableR1 = Close[0];
      Print("VariableR1: " + VariableR1); shows me in output window that the variable is updated with every new close.

      What do I have to change please so that the price when crossed is stored and doesnt change?

      Thank you
      Tony
      Last edited by tonynt; 12-28-2015, 12:20 PM. Reason: translation error

      Comment


        #4
        Hello,

        Thank you for the question.

        Please feel free to start new topics for any new questions as this is a very old thread that is not necessarily related.

        It looks like you are missing curly braces, this would be a basic fundamental of "if" statements and how they work in C#. you can read more about these here: http://www.techotopia.com/index.php/...th_if_and_else

        An if statement without curly braces can execute 1 command, in your example:

        Code:
        if(CrossAbove(Close, R1,1))
        Variableup = true;
        VariableR1 = Close[0];

        You have 2 commands to execute but have no curly braces, by the nature of how C# works, this would only apply to
        Variableup = true;

        Instead you would need to add the curly braces so more than 1 command can be executed or:

        Code:
        if(CrossAbove(Close, R1,1))
        {
            Variableup = true;
            VariableR1 = Close[0];
        }
        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello,

          thank you for your reply.

          I have added the curly braces but the variable still does update after the cross so that I don´t have the value when the cross has occurred stored to the variable.

          ??

          Thank you
          Tony
          Last edited by tonynt; 12-28-2015, 01:26 PM. Reason: Typing error

          Comment


            #6
            Hello,

            That would have been the solution for what was provided, I would suggest creating a new indicator and test the specific case outside of your current script to better locate the error.

            After creating the test script, if you are still seeing the same, post the test script and I would be happy to look at that specific syntax being used in which is not producing the expected result.

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello,

              thank you for your support. Sent an email with few lines of output window.

              I didnt mention - this is directly coded in a strategy (with COBCfalse), not an indicator.

              Thank you
              Tony

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by f.saeidi, Today, 10:19 AM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by kujista, Today, 06:23 AM
              5 responses
              15 views
              0 likes
              Last Post kujista
              by kujista
               
              Started by traderqz, Yesterday, 09:06 AM
              2 responses
              18 views
              0 likes
              Last Post traderqz  
              Started by traderqz, Today, 12:06 AM
              3 responses
              6 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by RideMe, 04-07-2024, 04:54 PM
              5 responses
              28 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X