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

Set stop loss and profits fixed when static mode activated

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

    Set stop loss and profits fixed when static mode activated

    Hello Ninjatrader Team!

    I am developing a strategy for RENKOBARS 10/5 in which the entry is made when the price breaks the Swing indicator, buying above and selling below. The Stop is set with a limit order which trails as the indicator moves (TRAILING MODE). Additionally, a Fibonacci retracement is drawn in the space between the upper and lower Swing(see image). Finally, the Profit has been calculated at Fibonacci levels of 25% and 61.8% (2 contracts) OR 61.8% and 100% (defined by user).

    Also I have created a STATIC MODE so when true, the stop loss and profits are fixed so that even though the swing indicator upsates, they remain fixed at the entry point swing.

    I tried to do this with a bolean called StopLossSet = False and set to true when the entry has been made. I didnt set up the Profits to be static because I couldnt make the stop to work... see in the image how the stop and profits are being recalculated as the new swings come up...

    How can I do this? How can I block the values updating? Please take be aware that I ONLY want this when the STATIC MODE is True, if it's False then it will update the Stop with the new swings that come up (TRAILING MODE).

    I attach the complete strategy without the bolean.

    Here the code I made with the bolean:


    Code:
    // Set 15 / LONG ===>  SWING            
    
                if ((Close[0] > Open[0])
                     && (Close[1] > Open[1])
                     && (Close[2] > Open[2])
                     && (Swing1.SwingHigh[2] > Close[2])
                     && (Swing1.SwingHigh[1] < Close[1])
                     && (Close[0] > Swing1.SwingHigh[2])
                     && (Buy_activar == true)
                     && (Times[0][0].TimeOfDay >= Hora_inicio.TimeOfDay)
                     && (Times[0][0].TimeOfDay <= Hora_cierre.TimeOfDay)
                     && (Trade_1 == true)
                     && (Trade_2 == true)                
                     && (Filtro_momentum == false)                
                     && (Cruce_EMAS    == false)
                     && (Entrada_PSAR    == false)
                     && (Entrada_Swing    == true)
                     && (stopLossSet == false)
                    )
                {
                    EnterLong(Ncontratos_trade1, @"EntryLong1");
                    EnterLong(Ncontratos_trade2, @"EntryLong2");
                    
                    Draw.FibonacciRetracements(this, @"BuscandoLaVelaR3 Fibonacci retracements_1", true, 0, Swing1.SwingLow[0], 0, Swing1.SwingHigh[0]);
                    
                    LimiSupFB = (Swing1.SwingHigh[0]);
                    LimInfFB = (Swing1.SwingLow[0]);
                    RangoFB = LimiSupFB-LimInfFB;
                    PS162b = Instrument.MasterInstrument.RoundToTickSize(LimiSupFB + (RangoFB*Porcentaje_fibo_TP1/100));
                    PS200b = Instrument.MasterInstrument.RoundToTickSize(LimiSupFB + (RangoFB*Porcentaje_fibo_TP2/100));
                    stopLossSet == true
                    
                }
                
                 // Set 16 / SHORT ===>  SWING
                if ((Close[0] < Open[0])
                     && (Close[1] < Open[1])
                     && (Close[2] < Open[2])
                     && (Swing1.SwingLow[2] < Close[2])
                     && (Swing1.SwingLow[1] > Close[1])
                     && (Close[0] < Swing1.SwingLow[2])
                     && (Sell_activar == true)
                     && (Times[0][0].TimeOfDay >= Hora_inicio.TimeOfDay)
                     && (Times[0][0].TimeOfDay <= Hora_cierre.TimeOfDay)
                     && (Trade_1 == true)
                     && (Trade_2 == true)            
                     && (Filtro_momentum == false)                
                     && (Cruce_EMAS    == false)
                     && (Entrada_PSAR    == false)
                     && (Entrada_Swing    == true)
                     && (stopLossSet == false)
                    )
                {
                    EnterShort(Ncontratos_trade1, @"EntryShort1");
                    EnterShort(Ncontratos_trade2, @"EntryShort2");
                    
                    Draw.FibonacciRetracements(this, @"BuscandoLaVelaR3 Fibonacci retracements_1", true, 0, Swing1.SwingLow[0], 0, Swing1.SwingHigh[0]);
                    LimiSupFB = (Swing1.SwingHigh[0]);
                    LimInfFB = (Swing1.SwingLow[0]);
                    RangoFB = LimiSupFB-LimInfFB;
                    PS162s = Instrument.MasterInstrument.RoundToTickSize(LimInfFB - (RangoFB*Porcentaje_fibo_TP1/100));
                    PS200s = Instrument.MasterInstrument.RoundToTickSize(LimInfFB - (RangoFB*Porcentaje_fibo_TP2/100));
                    stopLossSet == true
                }    ​
    
                // Set 39 - 2 LONG - SWING - STATIC MODE
    
                if (
                    (SL_Swing == true)
                    && (SL_Psar == false)
                    && (SL_TssuperTrend == false)
                    && (Position.MarketPosition == MarketPosition.Long)
                    && (Trade_1 == true)
                    && (Trade_2 == true)
                    && (Modo_estatico == true)
                    && (Modo_trailing == false)
                    && (stopLossSet == true)
                    )
                {
                    ExitLongStopMarket(Convert.ToInt32(Ncontratos_trade1), LimInfFB, @"StopLong1", @"EntryLong1");
                    ExitLongStopMarket(Convert.ToInt32(Ncontratos_trade2), LimInfFB, @"StopLong2", @"EntryLong2");
                    
                }
                
                 // Set 40 - 2 SHORT - SWING - STATIC MODE
                if (
                    (SL_Swing == true)
                    && (SL_Psar == false)
                    && (SL_TssuperTrend == false)
                    && (Position.MarketPosition == MarketPosition.Short)
                    && (Trade_1 == true)
                    && (Trade_2 == true)
                    && (Modo_estatico == true)
                    && (Modo_trailing == false)
                    && (stopLossSet == true)
                    )
                {
                    ExitShortStopMarket(Convert.ToInt32(Ncontratos_trade1), LimiSupFB, @"StopShort1", @"EntryShort1");
                    ExitShortStopMarket(Convert.ToInt32(Ncontratos_trade2), LimiSupFB, @"StopLong2", @"EntryLong2");
                            
                }    ​
    
    
                    
                    
                }​


    ​​Click image for larger version  Name:	strategy_1.png Views:	0 Size:	45.4 KB ID:	1290567Click image for larger version  Name:	strategy_2.png Views:	0 Size:	56.8 KB ID:	1290568​​
    Last edited by tradingnasdaqprueba; 02-09-2024, 07:33 AM.

    #2
    Hello tradingnasdaqprueba,

    Just to check, is the Modo_estatico bool the static bool?

    Submit the exit orders from OnExecutionUpdate() when the entry order fills.


    Below is a link to an example ProfitChaseStopTrailExitOrdersExample.


    In OnBarUpdate() require Modo_estatico to be false to update the price of the exit orders.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      Yes, the Modo_estatico bool is the static bool that I use.

      Could you please make a simple example of this?

      Thanks!!!

      Comment


        #4
        Hello tradingnasdaqprueba,

        The ProfitChaseStopTrailExitOrdersExample does provide an example of this, though the logic is somewhat different. Instead of a Modo_estatico that prevents the updating of the stop price, it uses a TrailStopLoss to allow the updating of the stop price.

        On line 237 the exit stop is submitted in OnExecutionUpdate().
        On lines 359 to 362 the TrailStopLoss is declared. On line 198 to 204 if TrailStopLoss is true the exit stop price is updated in OnBarUpdate().
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          I don´t have too much experience with OnExecutionUpdate(). Could you please give me some tips how should I change my code to accomplish this?

          Thanks a lot!

          TN

          Comment


            #6
            Hello tradingnasdaqprueba,

            Below is a link to the help guide on OnExecutionUpdate().


            This override method runs when an order fills (an execution). OnExecutionUpdate() should be declared in the scope of the class.
            Check the execution.Name to ensure this is the entry order.

            Model your code after the example I have provided you.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Segwin, 05-07-2018, 02:15 PM
            14 responses
            1,789 views
            0 likes
            Last Post aligator  
            Started by Jimmyk, 01-26-2018, 05:19 AM
            6 responses
            837 views
            0 likes
            Last Post emuns
            by emuns
             
            Started by jxs_xrj, 01-12-2020, 09:49 AM
            6 responses
            3,293 views
            1 like
            Last Post jgualdronc  
            Started by Touch-Ups, Today, 10:36 AM
            0 responses
            13 views
            0 likes
            Last Post Touch-Ups  
            Started by geddyisodin, 04-25-2024, 05:20 AM
            11 responses
            63 views
            0 likes
            Last Post halgo_boulder  
            Working...
            X