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 rotating position, stop and target

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

    Problem with rotating position, stop and target

    Hello. Excuse my English. I'm a novice programmer. I have this code:

    if (Position.MarketPosition == MarketPosition.Flat)

    if (entryOrder == null && Position.MarketPosition == MarketPosition.Flat

    && Volume[0] > Volume[1])

    {
    entryOrder = EnterLongStop(Convert.ToInt32(DefaultQuantity),Hig h[0] + TickSize*2,"LongEntry"+ CurrentBar);
    }

    if (entryOrder == null && Position.MarketPosition == MarketPosition.Flat

    && Volume[0] > Volume[1])
    {
    entryOrder = EnterShortStop(Convert.ToInt32(DefaultQuantity),Lo w[0] - TickSize*2,"ShortEntry"+ CurrentBar);
    }

    So far so good. Long and short positions are opened.
    But if I put this code:

    if (entryOrder != null && Position.MarketPosition == MarketPosition.Long)
    {
    SetStopLoss("LongEntry" + CurrentBar, CalculationMode.Price,(Low[0]- 2*TickSize), false);
    }

    the short entries are no longer produced. I don't understand logic. Could you explain? thank you.
    I want to do this:
    the condition is given long entry, stop and long objective.
    If they are not reached and the condition is given to turn to short.
    I don't get it.

    #2
    Hello julifro,

    Thanks for your post.

    There are a few issues with the code posted.
    1. You are assigning order objects in OnBarUpdate(). As a best practice, Order objects should be assigned in OnOrderUpdate
    2. You are assigning entryOrder as both your long entry order and your short entry order
    3. As you are using the Managed Approach, it will not be possible to create a bracketed entry since the Managed Approach does not allow pending entry orders in opposite directions. The short entry is ignored because the long entry is submitted first.
    4. SetStopLoss() is being used when you are long you and it should also should be reset when the Strategy is flat, before the associated Entry order is placed. This ensures that when the entry order gets filled, NinjaTrader submits the stop loss at an appropriate level.
    If you are new to programming and NinjaScripts, I may suggest to design strategies using the Strategy Wizard and then to expand onto maintaining a Strategy Wizard strategy and an unlocked copy. Strategy Wizard 301 and NinjaScript Editor 401 can demonstrate using these tools in detail and also demonstrates creating an unlocked copy of the code. Below are some publicly available resources that can be used to work through the items above.

    Strategy Wizard 301 - https://www.youtube.com/watch?v=FmBiNYsf1e8

    NinjaScript Editor 401 - https://www.youtube.com/watch?v=K8v_2ONBbqI

    The rules of the Managed Approach should be understood to know about how some orders may be ignored due to internal rules.

    Managed Approach - https://ninjatrader.com/support/help...d_approach.htm

    If you would like to use SetStopLoss dynamically in OnBarUpdate(), please refer to the example below for properly resetting the stop loss.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    Once you have become well versed with these concepts, moving on to using Order objects can be referenced in the example and linked documentation below.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by adeelshahzad, Today, 03:54 AM
    3 responses
    16 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by yertle, Today, 08:38 AM
    4 responses
    13 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by bill2023, Yesterday, 08:51 AM
    6 responses
    27 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by NinjaTrader_ChelseaB, 01-08-2017, 06:59 PM
    80 responses
    19,667 views
    5 likes
    Last Post NinjaTrader_ChelseaB  
    Started by dappa, Today, 09:18 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X