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

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.
    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 Brevo, Today, 01:45 AM
        0 responses
        6 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        241 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        6 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Working...
        X