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

Stoping trades until X minutes from last loss

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

    Stoping trades until X minutes from last loss

    I am trying to block new trades for 30 minutes in case last trade was a loss. I am using onExecution, and storing the ToTime(Time[0]) into timeToLoss variable which is compared to ToTime(Time[0]) before ordering new trades.. (IF (ToTime-timeToLoss> 3000) buy or sell..
    But It is not working,, I cnt find what is wrong!!

    protected override void OnExecution(IExecution execution)
    {
    trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
    if (trade_ProfitLoss_Current < 0)
    {
    timeToLoss = ToTime(Time[0]);

    }
    else
    {
    timeToLoss = 0;
    }

    }

    #2
    Hello dafonseca,

    Thank you for your post.

    Is the trade_ProfitLoss_Current returning a valid currency value?
    Is the condition if (trade_ProfitLoss_Current < 0) returning true?

    You can check this by adding Print statements like the following:
    Code:
    trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
    Print(trade_ProfitLoss_Current);
    if (trade_ProfitLoss_Current < 0)
    {
    Print("PnL is less than Zero");
    Please let me know if these debugging steps provide information in the Output window (Tools > Output window).

    Comment


      #3
      I ve run the strategy on oil future, but nothing has been printed and the strategy has generated just one trade ( it is expected to generate several trades without timeToLoss checking). Any clue? below the if condition to order generation is showed (timeNextTradeAfterLoss was defined as 30.000)

      protected override void OnExecution(IExecution execution)
      {
      trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
      Print(trade_ProfitLoss_Current);
      if (trade_ProfitLoss_Current < 0)
      {
      Print("PnL is less than Zero");
      timeToLoss = ToTime(Time[0]);
      }
      }

      if (ToTime(Time[0])- (timeToLoss) > timeNextTradeAfterLoss)

      {
      if ((CrossAbove(Medians[1], Bollinger( BarsArray[1], NumberStdDev, Period).Lower, 1))&& (RSI(RSICycle,1).Avg[0] > UpperLimit))
      {
      EnterShort(0,positionTaken, "FirstSeriesShort");

      }
      }
      Last edited by dafonseca; 03-15-2013, 09:23 AM.

      Comment


        #4
        Still not working

        Ive uploaded here a strategy analyzer after including:
        protected override void OnExecution(IExecution execution)
        {
        trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
        Print(trade_ProfitLoss_Current);
        if (trade_ProfitLoss_Current < 0)
        {
        Print("PnL is less than Zero");
        timeToLoss = ToTime(Time[0]);
        }
        }

        Should "Pnl is le......" appear on the chart along with the buy and sell signal?? How this debbuging "proccess" work? where should I check for it?
        Attached Files

        Comment


          #5
          dafonseca, the prints would be shown in NT's output window if triggered.



          It looks like you're working with the Set() methods here, those would not offer native IOrder returns for working in the more advanced order management methods like OnOrderUpdate / OnExecution, however you could use the workaround shown in this reference for monitoring them nonetheless -



          Once the stop loss would report filled, you could store the time and compare to current bar time in OnBarUpdate to retrigger a new entry for your script.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            managing time from last loss

            I am still having problems on this. I am trying to debbug and understand the mechanism. So I insert the below part in my code: I am trying to get the closing time of the last loss' trade and store it on timeToLoss to be used against ToTime(Time[0]0 --->( && (ToTime(Time[0])-timeToLoss > timeNextTrade )) as a condition to trigger a new order.
            The idea is : do not open a new trader for 30 minutes if last trade left a loss.
            But as you can see on attachment the timetoLoss (showed on open and exit a position) to do match what i expected, Any toughs??
            Another question would be why timetoLoss shows a time when there was no loss trade at a specific day. As i see on a day that there were no losses trades timeToLoss would be zero.

            protected override void OnExecution(IExecution execution)
            {
            priorTradesCumProfit = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
            if (priorTradesCumProfit < 0)
            {
            timeToLoss = ToTime(Time[0]);
            }
            }
            Attached Files
            Last edited by dafonseca; 06-02-2013, 11:28 AM.

            Comment


              #7
              Hello dafonseca,

              Thank you for your update on this matter.

              Can you send your strategy to support[at]ninjatrader[dot]com with 'Attn: Patrick - 810301' in the subject line and a reference to this thread in the body of the e-mail so I may investigate this matter further?
              Last edited by NinjaTrader_PatrickH; 06-02-2013, 03:57 PM.

              Comment


                #8
                One problem I ve detected is that
                priorTradesCumProfit = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);

                returns the P&L of the last trade even if this trade took place one or two days ago (the idea is consider the current day only).
                and it also returns the opening time of a trade (instead of the closing time)

                Still, on the code I sent you, priorTradesCumProfit retuns the time of all last trades no matter of its P&L..(profit or loss)
                Last edited by dafonseca; 06-02-2013, 08:44 PM.

                Comment


                  #9
                  Hello dafonseca,

                  Thank you for your response.

                  I will review your code and follow up with you via e-mail.

                  I look forward to assisting you further.

                  Comment


                    #10
                    Hello dafonseca,

                    Thank you for your patience.

                    The Position.GetProfitLoss() will only pull the unrealized profit of an open position, so if you carry over positions from one day to the next it will show the PnL of a previous day (i.e. the open position).

                    The timeToLoss = ToTime(Time[0]); will be the time that if (priorTradesCumProfit < 0) returns true, not the time of the close of the trade. If you are in a losing trade then the timeToLoss is going to keep updating with the current time. That time will be the close of a bar if you have CalculateOnBarClose set to True.

                    Since you are pulling the condition in the OnExecution() method you are only going to get an update on the timeToLoss when an order is executed, Position.GetProfitLoss() will be null if you have no open position - meaning your trade closed.

                    What PnL are you looking for? Unrealized (open trade) or Realized (closed trades)?
                    If it is the Realized PnL I would suggest using the OnPositionUpdate() method rather than OnExceution() to pull and store the time of a loss: http://www.ninjatrader.com/support/h...tionupdate.htm

                    Please let me know if you have any questions.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by rbeckmann05, Yesterday, 06:48 PM
                    1 response
                    12 views
                    0 likes
                    Last Post bltdavid  
                    Started by llanqui, Today, 03:53 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post llanqui
                    by llanqui
                     
                    Started by burtoninlondon, Today, 12:38 AM
                    0 responses
                    10 views
                    0 likes
                    Last Post burtoninlondon  
                    Started by AaronKoRn, Yesterday, 09:49 PM
                    0 responses
                    15 views
                    0 likes
                    Last Post AaronKoRn  
                    Started by carnitron, Yesterday, 08:42 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post carnitron  
                    Working...
                    X