• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:
    https://ninjatrader.com/support/help...gstoplimit.htm
    https://ninjatrader.com/support/help...stopmarket.htm

    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.
    Paul H.NinjaTrader Customer Service

    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
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Teebone21, Today, 10:38 AM
        0 responses
        6 views
        0 likes
        Last Post Teebone21  
        Started by J_o_s, 03-30-2020, 12:29 AM
        2 responses
        15 views
        0 likes
        Last Post J_o_s
        by J_o_s
         
        Started by ptpatrader, Today, 10:27 AM
        0 responses
        4 views
        0 likes
        Last Post ptpatrader  
        Started by albertokeira, Today, 09:30 AM
        0 responses
        8 views
        0 likes
        Last Post albertokeira  
        Started by U0000999, Today, 05:44 AM
        0 responses
        10 views
        0 likes
        Last Post U0000999  
        Working...
        X