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

Changing SetStopLoss and checking positions

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

    Changing SetStopLoss and checking positions

    Hi, I am trying to make a strategy where it trades whenever a specific condition is met. I was wondering
    - are there any ways that I can check do I have any open position currently running, because I don't want to enter another trade if my next bar meets the conditions. For example, if there are any positions currently opened, I would like to set a bool variable. Once the profit target has been reached or stop loss, I would like to set the bool variable to false.
    - are there any ways that I could control the stop loss. For example, the default SetStopLoss that I will be using when entering the trade is SetStopLoss(12), once my position is having 4 ticks profit, I would like to move the Stoploss to break even, then once my position is having an 8 tick profit, I would like to move the stop loss to 4 ticks profit, etc.
    would it be possible if you may include some examples that have comments with the code explaining what are each line for, since I understand that these content are considered as advanced, Thanks

    #2
    Hello horace chow,

    Thank you for your post.

    The simplest way would be to check that the strategy position is flat using Position.MarketPosition before submitting an entry:

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    //order entry logic would go here
    }

    You could also go the route you mentioned of setting a bool variable when the strategy takes a trade and reset it when the profit target is hit, but in your case the above would be more simple.

    As far as modifying the price of a stop loss order, here is an example from our help guide on updating the prices of those protective orders:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      I have noticed that on the example above, to change the stoploss the line for the code was SetStopLoss(CalculationMode.Price, Position.AveragePrice). I was wondering would there be anyway that I could set the stoploss above the entry price. For example I have entered a Long, and the profit is 8 ticks, I would like to change the stop loss to 4 ticks profit. I was wondering how would I do so? Thanks

      Comment


        #4
        Hello horace chow,

        Thank you for your reply.

        In that case you would want to add the ticks to the average entry price. So for example, the following is modified from that example above and would move the stop loss to the entry price + 4 ticks when the current price is at least 50 ticks above the entry price:

        Code:
                    // If a long position is open, allow for stop loss modification to breakeven
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        // Once the price is greater than entry price+50 ticks, set stop loss to breakeven + 4 ticks
                        if (Close[0] > Position.AveragePrice + 50 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice + (4 * TickSize));
                        }
                    }
        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X