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

Setting a Trailing Stop?

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

  • timmbbo
    replied
    Yep, those two threads are related. Thanks, Paul.

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello timmbbo,

    Thanks for your question.

    I believe the answer is related to the timing of the asynchronous events which I mention in your other thread here: http://ninjatrader.com/support/forum...ad.php?t=95788

    Leave a comment:


  • timmbbo
    replied
    Paul, I have a follow up on this. My code is:

    EnterLong(1);
    Print(Position.MarketPosition);
    Print(MarketPosition.Long);
    if (Position.MarketPosition == MarketPosition.Long) {
    ....

    The output is:
    Flat
    Long

    So, my If clause never hits, even though I'm successfully in a Long position. I can see the long position executed on the chart, so I know it's there. Suggestions? Thanks

    Leave a comment:


  • timmbbo
    replied
    Thanks Paul. I have my little script now executing properly.

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello timmbbo,

    Thanks for your reply.

    To have a code section only run once per bar, you can use the system bool FirstTickOfBar

    if (FirstTickOfBar)
    {
    // code processed here only once per bar
    }
    //code here is executed on every tick


    Reference: http://ninjatrader.com/support/helpG...ttickofbar.htm

    For further study on the subject: http://ninjatrader.com/support/forum...ad.php?t=19387

    Leave a comment:


  • timmbbo
    replied
    Paul, thanks - is helpful. Setting CalculateOnBarClose to false, as you recommend, then has other consequences for my code. I do have a section of code that I want executed upon bar close. So, it then appears I need one section of code executed upon bar close, and another section of code executed upon tick.

    How do I accommodate executing code both upon Bar Close and Upon Tick? Thanks

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello timmbbo,

    Thanks for your post.

    Correct, in the managed approach you cannot use the SetTrailStop and SetStopLoss methods on the same order.

    What you could do in this case is to exclusively use SetStopLoss(), initially setting it to 5 ticks and then later by comparing the current price to entry price adjusting the value of the SetStopLoss through your code. Note that you will need to use CalculateOnBarClose = false so that you are working with the current price

    Once in a position, you can obtain the entry price from Position.AvgPrice.
    For example if you were long:

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] >= Position.AvgPrice + 2 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Close[0] - 3 * TickSize);// set 3 ticks behind current price
    }
    }

    Note I have shown a very simple starting example, for it to work effectively you would need to use bools so that the trail stop does not go backwards when price reverses and so that the setstop is continually adjusted up as price continues up in the matter you desire.

    Leave a comment:


  • timmbbo
    started a topic Setting a Trailing Stop?

    Setting a Trailing Stop?

    1)
    Hi there, how do I set an initial stop, and a trailing stop? Say I want to enter an order with a 5 tick stop loss. Then, for every two ticks in profit, I want to set a trailing stop 3 ticks behind my current position. I would think the code would go something like?

    SetStopLoss(CalculationMode.Ticks, 5);
    SetTrailStop("", CalculationMode.Ticks, 3, false);

    However, I can use both the SetStopLoss, and the SetTrailStop together. How to do? Thanks

Latest Posts

Collapse

Topics Statistics Last Post
Started by Waxavi, 04-19-2024, 02:10 AM
3 responses
41 views
0 likes
Last Post gaz0001
by gaz0001
 
Started by Max238, Today, 01:28 AM
2 responses
26 views
0 likes
Last Post NinjaTrader_ChristopherJ  
Started by Shansen, 08-30-2019, 10:18 PM
25 responses
949 views
0 likes
Last Post NinjaTrader_BrandonH  
Started by JonesJoker, 04-22-2024, 12:23 PM
8 responses
42 views
0 likes
Last Post JonesJoker  
Started by timko, Today, 06:45 AM
0 responses
5 views
0 likes
Last Post timko
by timko
 
Working...
X