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 TAJTrades, Today, 09:01 AM
      4 responses
      26 views
      0 likes
      Last Post TAJTrades  
      Started by quantyse_ntforum, 03-10-2025, 09:05 AM
      14 responses
      242 views
      0 likes
      Last Post dakensei  
      Started by ntwong, 10-08-2024, 08:22 PM
      10 responses
      227 views
      0 likes
      Last Post akvevo
      by akvevo
       
      Started by philmg, 04-12-2025, 06:35 AM
      9 responses
      63 views
      0 likes
      Last Post NinjaTrader_Helom  
      Started by amoghgajankush, Today, 09:47 AM
      2 responses
      16 views
      0 likes
      Last Post amoghgajankush  
      Working...
      X