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

long/short strat and profit&stop targets

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

    long/short strat and profit&stop targets

    hi, i'm working a long short strategy. both long and short have individual stoploss and target variables

    variables:
    longstoploss
    longprofit
    shortstoploss
    shortprofit

    i have setstoploss and setprofit in on bar update, ie:

    if(xxxxxxx)
    {
    golong;
    setstoploss(longstoploss)
    setprofit(longprofit)
    }

    if(xxxxxxx)
    {
    goshort;
    setstoploss(shortstoploss)
    setprofit(shortprofit)
    }

    **NOTE: please ignore the incorrect code that i'm posting above, the code i have in strategy is correct.

    question: it seems that my positions are exiting prior to stoploss/profit target being hit, rather, when the opposite signal becomes true, the position is entered on the opposite side. for example, i'm long with X profit target and Y stoploss, Xprofit target and Xstoploss has not been hit, but short signal becomes valid, system exits the position and goes short.

    how can i avoid the above? what i'm trying to do is, stay in the trade, once entered, and only exit once stoploss or profit target is hit. once either stoploss or profit target is hit, the system evaluates the data series for the next long/short trade.

    thank you in advance.

    #2
    Hello calhawk01,

    You may want to check to make sure that your Position is flat before entering the market to prevent this from happening as NinjaTrader will reverse your position if and entry in the opposite direction is called.

    Example:
    Code:
    if (Position.MarketPosition == Position.Flat)
    {
     // Entry Order
    }
    JCNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JC View Post
      Hello calhawk01,

      You may want to check to make sure that your Position is flat before entering the market to prevent this from happening as NinjaTrader will reverse your position if and entry in the opposite direction is called.

      Example:
      Code:
      if (Position.MarketPosition == Position.Flat)
      {
       // Entry Order
      }
      http://www.ninjatrader.com/support/h...etposition.htm
      i will try that later, do you suggest implementing the above clauses around both the long logic and short logic?

      ie:

      if position = flat
      {
      long logic
      setstoploss
      setprofit
      }

      if position = flat
      {
      short logic
      setstoploss
      setprofit
      }

      right?

      Comment


        #4
        Code:
                {    
                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                    if (Close[0] > Position.AvgPrice + longprofit)
                        {
                        SetTrailStop(CalculationMode.Ticks, (longprofit/.25)/2);
                        }
                    }
                    if (Position.MarketPosition == MarketPosition.Short)
                    {
                    if (Close[0] < Position.AvgPrice - shortprofit)
                        {
                        SetTrailStop(CalculationMode.Ticks, (shortprofit/.25)/2);
                        }
                    }            
        
                   if (Position.MarketPosition == MarketPosition.Short
                    if (.................))
                    {
                        EnterShortLimit(DefaultQuantity, High[0]);
                        SetStopLoss(CalculationMode.Ticks,shortstop/.25);
                    }
                    if (ToTime(Time[0]) == ToTime(15, 55, 0))
                    {
                        ExitShort("", "");
                    }
        
                   if (Position.MarketPosition == MarketPosition.Long
                    if (...............))
                    {
                        EnterLongLimit(DefaultQuantity, Low[0]);
                        SetStopLoss(CalculationMode.Ticks,longstop/.25);
                    }
                    if (ToTime(Time[0]) == ToTime(15, 55, 0))
                    {
                        ExitLong("", "");
                    }
                
                }
        hi the above only analyzez the logic/ enters longshort position only if im flat. but the trailstoploss is not being activAted. Any idea why the if position==long;short is not being analyzed
        ?? thNk u

        Comment


          #5
          Hello calhawk01,

          The SetStopLoss() method can not be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.

          JCNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_JC View Post
            Hello calhawk01,

            The SetStopLoss() method can not be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.

            http://www.ninjatrader.com/support/h...etstoploss.htm
            how can i ref different signal names if i only have the ''original'' signal. ie: how can i enter a position w/ initial stoploss and later turn it into a trailingstop after X ticks
            Last edited by staycool3_a; 06-26-2013, 11:36 AM.

            Comment


              #7
              Hello calhawk01,

              Thank you for your response.

              This could not be done with the same signal name referenced from the entry for both the SetStopLoss() and SetTrailStop().

              You would either need to just use SetStopLoss() in OnBarUpdate() (this will act as a trailing stop as it is always updated on the bar update), or you will need to create your own conditions for exit and exit trail.

              Please let me know if you have any questions.

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello calhawk01,

                Thank you for your response.

                This could not be done with the same signal name referenced from the entry for both the SetStopLoss() and SetTrailStop().

                You would either need to just use SetStopLoss() in OnBarUpdate() (this will act as a trailing stop as it is always updated on the bar update), or you will need to create your own conditions for exit and exit trail.

                Please let me know if you have any questions.
                so.. SetStopLoss(10 ticks) in OBU acts like a trailing stop? i had no idea....

                ie; if i set a 5 tick stoploss in OBU.. and instrument moved 5 ticks, then the distance at that moment is currentprice-5 ticks? rather than 10 ticks if i added the stoploss in initialized?

                Comment


                  #9
                  Originally posted by calhawk01 View Post
                  so.. SetStopLoss(10 ticks) in OBU acts like a trailing stop? i had no idea....

                  ie; if i set a 5 tick stoploss in OBU.. and instrument moved 5 ticks, then the distance at that moment is currentprice-5 ticks? rather than 10 ticks if i added the stoploss in initialized?
                  can someone verify this? b/c i'm pretty sure a stoploss in OBU doesn't turn into a trailstop in any sense. from my experience a a 4 tick stoploss stays a 4 tick stoploss. ie if the market goes 1000 points and comes back down.. the stoploss will be avgprice-4ticks

                  Comment


                    #10
                    Originally posted by calhawk01 View Post
                    can someone verify this? b/c i'm pretty sure a stoploss in OBU doesn't turn into a trailstop in any sense. from my experience a a 4 tick stoploss stays a 4 tick stoploss. ie if the market goes 1000 points and comes back down.. the stoploss will be avgprice-4ticks
                    depends on what calculateonbarclose is set to.

                    so if false - then yes
                    if true - well, it will wait until the bar closes

                    you should be recording or downloading data for market replay, and testing your code and learning in there before running live.

                    Comment


                      #11
                      Originally posted by sledge View Post
                      depends on what calculateonbarclose is set to.

                      so if false - then yes
                      if true - well, it will wait until the bar closes

                      you should be recording or downloading data for market replay, and testing your code and learning in there before running live.
                      right, that's what i've been doing for the past 2 months

                      Comment


                        #12
                        Originally posted by calhawk01 View Post
                        can someone verify this? b/c i'm pretty sure a stoploss in OBU doesn't turn into a trailstop in any sense. from my experience a a 4 tick stoploss stays a 4 tick stoploss. ie if the market goes 1000 points and comes back down.. the stoploss will be avgprice-4ticks

                        If you keep changing the stoploss in OBU, then it is very dynamic.

                        Haven't seen your code, and it sounds like you are not taking precautions to NOT RESET the stoploss if it was to go against you, so your stoploss will keep being 4 ticks away.

                        SetTrailStop looks like it should be only called 1 time, and not again.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by burtoninlondon, Today, 12:38 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post burtoninlondon  
                        Started by AaronKoRn, Yesterday, 09:49 PM
                        0 responses
                        13 views
                        0 likes
                        Last Post AaronKoRn  
                        Started by carnitron, Yesterday, 08:42 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post carnitron  
                        Started by strategist007, Yesterday, 07:51 PM
                        0 responses
                        13 views
                        0 likes
                        Last Post strategist007  
                        Started by StockTrader88, 03-06-2021, 08:58 AM
                        44 responses
                        3,982 views
                        3 likes
                        Last Post jhudas88  
                        Working...
                        X