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

Saving a value under OBU

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

    Saving a value under OBU

    hi

    i'm trying to figure out how to save a value once a condition occurs, but the problem is that my condition becomes true each time newer data is loaded under OBU. I am only interest in the first instance.

    for example

    a= profit of instrument 1
    b = profit of instrument 2

    condition = if a+b > 100
    {
    closes[1][0];
    Closes[2][0];
    }

    but since a+b > 100 multiple times. the closes that i am trying to retrieve change. Ideally, i am only interest in the First time the condition becomes true, then save the closes of those instruments. I want to use those closes to set a stoploss.

    #2
    Originally posted by calhawk01 View Post
    hi

    i'm trying to figure out how to save a value once a condition occurs, but the problem is that my condition becomes true each time newer data is loaded under OBU. I am only interest in the first instance.

    for example

    a= profit of instrument 1
    b = profit of instrument 2

    condition = if a+b > 100
    {
    closes[1][0];
    Closes[2][0];
    }

    but since a+b > 100 multiple times. the closes that i am trying to retrieve change. Ideally, i am only interest in the First time the condition becomes true, then save the closes of those instruments. I want to use those closes to set a stoploss.
    Code:
    private bool ConditionHappened = false;
    Code:
    if (a +b > 100 && !ConditionHappened)
    {
    //handle the business
    ConditionHappened = true;
    }
    
    if (ConditionToReset) //we need to reset the gate at some point, so that we can again enter the earlier block if we want to
    {
    ConditionHappened = false;
    }

    Comment


      #3
      Hello calhawk01,

      Koganam provides a perfect example of using a bool to ensure these values are only set once and provides more in his example in case you want to capture the values again after another condition returns true.

      Comment


        #4
        Originally posted by koganam View Post
        Code:
        private bool ConditionHappened = false;
        Code:
        if (a +b > 100 && !ConditionHappened)
        {
        //handle the business
        ConditionHappened = true;
        }
        
        if (ConditionToReset) //we need to reset the gate at some point, so that we can again enter the earlier block if we want to
        {
        ConditionHappened = false;
        }
        Thanks koganam. Whats the purpose of !ConditionHappened? I'm assuming you meant that ABC condition happened?

        Code:
                    if (Positions[0].MarketPosition == MarketPosition.Long 
                    && Positions[1].MarketPosition == MarketPosition.Short
                        && a + b > profit)
                        {
                        //handle the business
                        Conditionhappened = true;
                        }
                        
                    if (Conditionhappened == true)            
                        {
                        SetStopLoss("Weakbuy", CalculationMode.Ticks, 8, false);
                         SetStopLoss("Strongsell", CalculationMode.Ticks, 8, false);
                        }
        The above bool method works perfectly, but for some reason only one of the stoploss is being set. The stoploss on strongsell is not being set. I even tried creating individual 'conditionhappened' bools for each instrument. It seems the strategy can only set 1 stoploss at a time? How can i set stoploss on multiple instruments?

        i have no errors in traceorder:

        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Weakbuy' Mode=Ticks Value=8 Currency=0 Simulated=False
        3/25/2015 11:46:54 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Strongsell' Mode=Ticks Value=8 Currency=0 Simulated=False

        Comment


          #5
          Hello calhawk01,

          Thank you for your response.

          The '!' symbol in front of a bool checks that it is false, this is the same as if(ConditionHappened == false) or if(ConditionHappened != true).

          For the SetStopLoss(), you would need an entry called Weakbuy and one for Strongsell in place. Do you have these entry orders submitted? Or is just one submitted?

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello calhawk01,

            Thank you for your response.

            The '!' symbol in front of a bool checks that it is false, this is the same as if(ConditionHappened == false) or if(ConditionHappened != true).

            For the SetStopLoss(), you would need an entry called Weakbuy and one for Strongsell in place. Do you have these entry orders submitted? Or is just one submitted?

            I have two entries. Each entry has a unique entry name (weak buy and strong sell). And I'm trying to set a stopliss based on each one of those entries.. As demonstrated in my previous reply. But only one of them is being set

            Comment


              #7
              Hello calhawk01,

              Are you using unique signal names?

              Do the from entry signal names in the SetStopLoss call match the signal name of the entry order?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello calhawk01,

                Are you using unique signal names?

                Do the from entry signal names in the SetStopLoss call match the signal name of the entry order?



                I am. I'm not in front of my computer right but the laymen terms of the entry are:

                If position [0] flat and position [1] flat and close > 0

                {
                Enter short ES "strongsell"
                Enter long NQ "weakbuy"
                }

                Then if abc and condition happened equals true

                {set stop loss "strongsell" 8 tick
                Set stop loss "weakbuy" 8 ticks}

                The above only sets stop loss on strongsell.

                Comment


                  #9
                  Hello calhawk01,

                  Thank you for your response.

                  Please try setting EntryHandling to UniqueEntries and EntriesPerDirection to 1.

                  Comment


                    #10
                    Originally posted by calhawk01 View Post
                    I am. I'm not in front of my computer right but the laymen terms of the entry are:

                    If position [0] flat and position [1] flat and close > 0

                    {
                    Enter short ES "strongsell"
                    Enter long NQ "weakbuy"
                    }

                    Then if abc and condition happened equals true

                    {set stop loss "strongsell" 8 tick
                    Set stop loss "weakbuy" 8 ticks}

                    The above only sets stop loss on strongsell.
                    Are those signal names or IOrder identifiers?

                    Regardless, in order to tie exits to entries, you must use the correct overload. In this case it must be:

                    SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)

                    Comment


                      #11
                      I am already using this?
                      Code:
                      SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)
                      here is the code from entry to exit: This is all under OBU

                      Code:
                        
                      
                      OnBarUpdate:::::
                      
                        
                      if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat)
                      
                      {
                      EnterLong(0,NQquantity,"StrongBuy") //0 is the dataseries on primary instrument
                      EnterShort(1,ESquantity,"WeakSell") //1 is the data series on secondary index
                      }
                      
                           
                      if (Positions[0].MarketPosition == MarketPosition.Long 
                                  && Positions[1].MarketPosition == MarketPosition.Short
                                      && a + b > profit)
                                      {
                                      //handle the business
                                      Conditionhappened = true;
                                      }
                                      
                                  if (Conditionhappened == true)            
                                      {
                                      SetStopLoss("Weakbuy", CalculationMode.Ticks, 8, false);
                                       SetStopLoss("Strongsell", CalculationMode.Ticks, 8, false);
                                      }
                      the above gets me in the trade for both ES and NQ. but stoploss doesnt get entered for both?

                      EntryHandling to UniqueEntries and EntriesPerDirection to 1
                      already looked into that, I have unque entries and entries per direction is set to 1, and i've tried 2 and 4 as well.
                      Last edited by staycool3_a; 04-01-2015, 03:44 PM.

                      Comment


                        #12
                        Hello calhawk01,

                        I overlooked that you are using EnterShort() and EnterLong() at the same time. One of these will be ignored based on the internal order handling rules: http://www.ninjatrader.com/support/h...d_approach.htm

                        If you want to go long and short at the same time you would need two strategies, one for going long and one for going short.

                        Comment


                          #13
                          Originally posted by NinjaTrader_PatrickH View Post
                          Hello calhawk01,

                          I overlooked that you are using EnterShort() and EnterLong() at the same time. One of these will be ignored based on the internal order handling rules: http://www.ninjatrader.com/support/h...d_approach.htm

                          If you want to go long and short at the same time you would need two strategies, one for going long and one for going short.
                          Pat thats not true.. i have over fifty strats using the same method. this is a multi intrument strategy. ive added a secondary dataseries

                          Code:
                                  protected override void Initialize()
                                  {
                          
                                      CalculateOnBarClose = true;
                                      
                                      Add("NQ 06-15", PeriodType.Minute, 1);
                                      
                                  }
                          OBU:

                          Code:
                                      if( Positions[1].MarketPosition == MarketPosition.Flat 
                                          && Positions[0].MarketPosition == MarketPosition.Flat)
                                      {
                                          EnterShort(0,eSQuantity, "Strongsell");
                                          EnterLong(1,nQQuantity, "Weakbuy");
                                      }
                          why do you think stoploss is not working?

                          Comment


                            #14
                            Originally posted by calhawk01 View Post
                            I am already using this?
                            Code:
                            SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)
                            here is the code from entry to exit: This is all under OBU

                            Code:
                              
                            
                            OnBarUpdate:::::
                            
                              
                            if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat)
                            
                            {
                            EnterLong(0,NQquantity,"StrongBuy") //0 is the dataseries on primary instrument
                            EnterShort(1,ESquantity,"WeakSell") //1 is the data series on secondary index
                            }
                            
                                 
                            if (Positions[0].MarketPosition == MarketPosition.Long 
                                        && Positions[1].MarketPosition == MarketPosition.Short
                                            && a + b > profit)
                                            {
                                            //handle the business
                                            Conditionhappened = true;
                                            }
                                            
                                        if (Conditionhappened == true)            
                                            {
                                            SetStopLoss("Weakbuy", CalculationMode.Ticks, 8, false);
                                             SetStopLoss("Strongsell", CalculationMode.Ticks, 8, false);
                                            }
                            the above gets me in the trade for both ES and NQ. but stoploss doesnt get entered for both?



                            already looked into that, I have unque entries and entries per direction is set to 1, and i've tried 2 and 4 as well.
                            If conditionhappened is set true- where is it turned off? If it is always true- then the stoploss keeps getting reset, right? So it is working - it's just setting the stoploss every OBU to something new which is probably further away.

                            Comment


                              #15
                              Hello calhawk01,

                              Thank you for your response.
                              Originally posted by calhawk01 View Post
                              Pat thats not true.. i have over fifty strats using the same method. this is a multi intrument strategy. ive added a secondary dataseries
                              You are correct, thanks for pointing that out.
                              After testing in several different ways, I have found the issue isolated to Positions[1].MarketPosition == MarketPosition.Short.
                              There appears to be an issue calling the Positions[1] to initialize the SetStopLoss(). I can call the MarketPosition and even print it's value but it will not initialize the SetStopLoss().

                              I will forward this to development to look into further.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mgco4you, Today, 09:46 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by wzgy0920, Today, 09:53 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Rapine Heihei, Today, 08:19 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Rapine Heihei, Today, 08:25 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Rapine Heihei  
                              Started by f.saeidi, Today, 08:01 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X