Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Script Breakeven limit or stop order depending to the position of the current price

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

    Script Breakeven limit or stop order depending to the position of the current price

    Do you know a script that allows this?

    BE at 0 or +1,+2,+3 ...depending on the keyboard shortcut chosen and also, in the case of a buy position when I press the keyboard shortcut button, if the current price is below my buy position then we have an automatic limit order at BE and if the current price is above the buy price then an automatic stop order at BE.

    To summarize: no matter where the current price is in relation to my position entry, when I press the hotkey button a limit or stop order will be set to BE 0 or +1,+2,+3 depending on the choice of the button.
    And if it is not possible to do it with the keyboard shortcut, you can do it just with a button on the screen.

    Thank's


    #2
    Hello gravdigaz6,

    You could certainly create something like this.

    Below are links to examples of capturing keypresses.
    https://ninjatrader.com/support/foru...756#post831756
    https://ninjatraderecosystem.com/use...ket-internals/
    https://ninjatraderecosystem.com/use.../a1chartnotes/

    And links to example logic of moving stop prices.
    https://ninjatrader.com/support/foru...269#post802269
    https://ninjatrader.com/support/foru...596#post806596

    As the keypress events are in an non-data-driven thread, TriggerCustomEvent() would need to be used to synchronize the data series to get price information to use for the stops.
    https://ninjatrader.com/support/help...ustomevent.htm

    If you are new to coding I'd also like to provide a link to a forum post with helpful resources on getting started.


    If you intending to ask if there is already an existing script, this can be asked in the file sharing discussion section of the forums.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I have used this - it adds an order if the position is in a current losing state or moves a line to Breakeven + 1 tick, you can change this for an order if you wish.
      It needs a bit of adaptation but the raw stuff is there.
      It does NOT take into consideration where the close[0] is at the currentBid or currentAsk situation which I handle elsewhere. Happens rarely but you do need to be aware that this code will throw an error in this instance.

      Code:
                              #region Breakeven - Long
                              //double b = Close[0]- Position.AveragePrice;
                              double BEPrice = Instrument.MasterInstrument.RoundToTickSize(Position.AveragePrice + TickSize);
                      
                              if(Direction > 0)
                              {
                                   
                              if(Close[0] < Position.AveragePrice) // IN LOSS
                              {
                                  targetOrder.LimitPriceChanged = BEPrice;
                                  ChangeOrder(targetOrder,Position.Quantity,targetOrder.LimitPriceChanged,0);    
                              }
                              
                              else if(Close[0]> Position.AveragePrice)// IN PROFIT &&  MyEP1 >= GetCurrentBid(0))
                              {
                                  if (BuildingLongStopLine)
                                          {
                                              longStopPrice = BEPrice;
                                              DrawLine(longStopPrice,"LXLine");
                                          }
                                          else
                                              ExitLongStopLimit(0,true,Position.Quantity,BEPrice,BEPrice,"BE","EL");
                              }
                              else  //ERROR
                                  PlayBeepSound();
                              }
                              #endregion
                  
                              #region Breakeven - Short
                               BEPrice = Instrument.MasterInstrument.RoundToTickSize(Position.AveragePrice - TickSize);
                              if(Direction < 0)
                              {
                                  
                              if(Close[0]> Position.AveragePrice) // IN LOSS
                              {
                                  targetOrder.LimitPriceChanged = BEPrice;
                                  ChangeOrder(targetOrder,Position.Quantity,targetOrder.LimitPriceChanged,0);    
                              }
                              
                              else if(Close[0] < Position.AveragePrice)// IN PROFIT
                              {
                                    
                                  if (BuildingShortStopLine)
                                          {
                                              shortStopPrice = BEPrice;
                                              DrawLine(shortStopPrice,"SXLine");
                                          }
                                          else
                                              ExitShortStopLimit(0,true,Position.Quantity,BEPrice,BEPrice,"BE","ES");
                              }
                              else  //ERROR
                                  PlayBeepSound();
                              }
                              #endregion
      ​
      Last edited by Mindset; 03-25-2025, 02:42 PM. Reason: removed extraneous code

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X