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

Trouble with closing positions

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

    Trouble with closing positions

    Hello,

    I am trying to write a code in which I set limit that won't close the position. Instead the algorithm will monitor the position. Once the position losses 1/2 of the limit, it will close the position. However, The code draw the line but it does not close the position. I inserted the Print() and it is not showing up in the output. I have been trying several ways but I cannot catch the mistake. Would you please help me?

    Code 1 (did not close the position)

    if (Position.MarketPosition == MarketPosition.Short)
    {

    if (Close[0] > Position.AveragePrice - Limit * TickSize)
    {
    if (Close[0] < Position.AveragePrice - (Limit * 0.5) * TickSize)
    CancelOpenEntries(); Print();
    Draw.VerticalLine(this, CurrentBar.ToString(), 0, Brushes.Gray, DashStyleHelper.Dash, 4, true);

    }

    Code 2 (did not close the position)

    if (Position.MarketPosition == MarketPosition.Short)
    {

    if (Close[0] > Position.AveragePrice - Limit * TickSize)
    {
    if (Close[0] < Position.AveragePrice - (Limit * 0.5) * TickSize)
    ExitShort(); Print();
    Draw.VerticalLine(this, CurrentBar.ToString(), 0, Brushes.Gray, DashStyleHelper.Dash, 4, true);

    }

    #2
    Hello Jorge.andres.o,

    Thank you for your post.

    Your syntax doesn't look correct for either of these. I'm not sure if you made a custom method called CancelOpenEntries();, but that's not a method in NinjaScript in the first one.

    In the second, you've got several lines after your condition but you're not grouping them in brackets.

    I'm attaching a simple example script that enters short as soon as you get a down bar, and then exits once half the number of ticks specified by the limit is reached that you can use as a jumping off point for your own logic. This script will only enter once per bar, but tracks for the exit on each tick so the exit is fired as soon as that half-limit is reached.

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

    Comment


      #3
      Kate,

      Thank you very much. I will work on it

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by traderqz, Yesterday, 09:06 AM
      3 responses
      21 views
      0 likes
      Last Post NinjaTrader_ThomasC  
      Started by f.saeidi, Today, 10:19 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by kujista, Today, 06:23 AM
      5 responses
      15 views
      0 likes
      Last Post kujista
      by kujista
       
      Started by traderqz, Today, 12:06 AM
      3 responses
      6 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by RideMe, 04-07-2024, 04:54 PM
      5 responses
      28 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X