Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Buy Price at Limit

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

    Buy Price at Limit

    Hi ,
    I want to develop a simple strategy to enter Market at a specific price when 1st two candles of day are green and supertrend is lower than close . I have written the following script


    protected override void OnBarUpdate()
    {
    if (Bars.BarsSinceNewTradingDay >= 2)
    {
    bool cond1 = Close[Bars.BarsSinceNewTradingDay] > Open[Bars.BarsSinceNewTradingDay];
    bool cond2 = Close[Bars.BarsSinceNewTradingDay - 1] > Open[Bars.BarsSinceNewTradingDay - 1 ];
    bool cond3 = TSSuperTrend1.UpTrend[1] < Close[1];
    bool cond4 = TSSuperTrend1.UpTrend[Bars.BarsSinceNewTradingDay] < Close[Bars.BarsSinceNewTradingDay];
    var buffer = 0.50;
    if(Close[0] > 700)
    {
    buffer = 1;
    }
    if(Close[0] > 2000)
    {
    buffer = 4;
    }
    if(cond1 && cond2 && cond3 && cond4)
    {
    var buyPrice = 0.00;
    var stopLoss = 0.00;
    if(High[Bars.BarsSinceNewTradingDay] > High[Bars.BarsSinceNewTradingDay - 1] )
    {
    buyPrice = High[Bars.BarsSinceNewTradingDay] + buffer;
    stopLoss = Low[Bars.BarsSinceNewTradingDay];

    }
    else
    {
    buyPrice = High[Bars.BarsSinceNewTradingDay - 1 ] + buffer;
    stopLoss = Low[Bars.BarsSinceNewTradingDay - 1] ;
    }
    EnterLongMIT(buyPrice,"ST");
    SetStopLoss("ST",CalculationMode.Price,stopLoss,tr ue);

    }

    }

    When I backtest it , the orders are filled below the buyPrice . I want to enter the market only if it's above buyPrice.

    #2
    Hello shishir_nse,

    Thanks for your post.

    The EnterLongMIT type order would need to be placed below the current market price. If you place the buy limit order above the current price the order will naturally fill at a lower price because a limit type order will fill at the limit or better price.

    To place a limit order above the current price, you would use an EnterLongStopLimit() or EnterLongStopMarket()
    Reference:



    Please note: The SetStopLoss() method will retain the last price that was applied to it which means that on the next entry if the stop price has not been adjusted you could end up with an invalid or incorrectly price order. In your code all you need to do is to set the stop order value before placing the entry order.

    Comment


      #3
      Thanks for the reply orders Entryorders are placed correctly now , but StopLoss is not working properly . This is message is displayed in output

      A Buy order placed at '2019-08-09 10:45:00 AM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.

      Code

      SetStopLoss("ST",CalculationMode.Price,stopLoss,tr ue);
      EnterLongStopMarket(buyPrice);

      Comment


        #4
        Hello shishir_nse,

        Thanks for your reply.

        Th error message likely refers to the EnterLongStopMarket() order. The entry order for for the EnterLongStopMarket must be placed above the current price.

        I would suggest using print statements to debug your strategy by printing the values of buyPrice and stopLoss and Close[0] to get a better understanding of what is being used where/when. Please see our debugging tips here: https://ninjatrader.com/support/help...script_cod.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        50 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X