Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profitable exits being ignored

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

    Profitable exits being ignored

    I am using Enter and Exit orders to unwind positions that are profitable after 3 ticks or better. They are both being ignored. Can't figure out why.

    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Close[0] > Close[1])

    {
    EnterLongLimit(1, GetCurrentBid(), "Ofc Long");
    }
    // States that if position is profitable by 3 ticks at bid price, exit position at bid price
    if (GetCurrentBid() >= Position.AvgPrice + 3 * Ticksize)
    {
    ExitLongLimit (GetCurrentBid(), "Profit Long");
    }

    // States that if bar following entry long closes against initial position, exit long and go flat.
    if (Close[0] < Position.AvgPrice)
    {
    ExitLongLimit (GetCurrentBid(), "Ofc Long");
    }

    Thank You, Jamie Hendrix

    #2
    Jamie, please set TraceOrders = true in the Initialize() portion of your strategy to see why these orders are being ignored. You can also add some Print() statements to send output to the debugging window (Tools -> Output Window) to verify these code blocks are actually executing:
    Code:
    if   (Close[0] > Close[1])
                
                {
    Print("entering long limit at bar: " + Time[0].ToString());
                    EnterLongLimit(1, GetCurrentBid(), "Ofc Long");
                }
                    // States that if position is profitable by 3 ticks at bid price, exit position at bid price
                    if (GetCurrentBid() >= Position.AvgPrice + 3 * Ticksize)
                    {
    Print("trying to exit long at bar: " + Time[0].ToString());
                        ExitLongLimit (GetCurrentBid(), "Profit Long");
                    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin, Thanks very much.... I feel like I am very close.... I have an ATM strategy that I am comfortable working with..... is there any way to call that strategy into use via the automated code? If not, where can I find out how to code the parameters from that strategy into my automated system.

      Thanks for your help, Jamie Hendrix

      Comment


        #4
        There is such a way. Please take a look at the sample provided by NinjaTrader at Tools -> Edit NinjaScript -> Strategy -> SampleAtmStrategy for additional information.
        AustinNinjaTrader Customer Service

        Comment


          #5
          I loaded the ATM Strategy template and have tried many things... I even cut and paste most of the code right into my own..... it just does not want to cooperate.... I have an ATM template that I use in manual trading and I would like to incorporate it into my automated system..... all I am missing in my auto system is an auto breakeven which this would take care of.....

          I added it to a basic entry signal to test it.... here is what I have.... What am I doing wrong?......

          // Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
          // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
          if ((Close[0] >= Close[1] + 3 * TickSize ) && BarsSinceExit() >=2 || BarsSinceExit() == -1)
          {
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, GetCurrentBid(), 0 , TimeInForce.Day, orderId, "Ofc", atmStrategyId);
          }

          if ((Close[0] <= Close[1] - 3 * TickSize ) && BarsSinceExit() >=2 || BarsSinceExit() == -1)
          {
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Limit, GetCurrentAsk(), 0 , TimeInForce.Day, orderId, "Ofc", atmStrategyId);
          }


          // Check for a pending entry order
          if (orderId.Length > 0)
          {
          string[] status = GetAtmStrategyEntryOrderStatus(orderId);

          // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
          if (status.GetLength(0) > 0)
          {
          // Print out some information about the order to the output window
          Print("The entry order average fill price is: " + status[0]);
          Print("The entry order filled amount is: " + status[1]);
          Print("The entry order order state is: " + status[2]);

          // If the order state is terminal, reset the order id value
          if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
          orderId = string.Empty;
          }
          } // If the strategy has terminated reset the strategy id
          else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
          atmStrategyId = string.Empty;


          if (atmStrategyId.Length > 0)
          {
          // You can change the stop price
          //if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
          AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

          // Print some information about the strategy to the output window
          Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
          Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
          Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
          Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
          }
          }


          Thanks Austin, Jamie Hendrix

          Comment


            #6
            Hi Jamie, were you able to get the SampleAtmStrategy working by itself? That is a necessary first step before you start using your own strategies. Please keep in mind these strategies do not work on historical data (backtesting).
            AustinNinjaTrader Customer Service

            Comment


              #7
              Austin. I loaded the Sample Atm Strategy by itself but nothing happened... no entries??? Not sure what to do next.

              Jamie

              Comment


                #8
                Austin, I got it figured out, just didn't realize about the historical thing.... just zoned out on that one I guess. Two other things I am trying to figure out..... Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two... can't use "to time" cause I use Tick charts. Number two is that I would like it to stop trading after a certain cumulative profit has been reached for the day ( Like once it hits $100, stop trading or 2 points then stop)?

                Thanks man, I am almost there. Jamie Hendrix

                Comment


                  #9
                  OK Austin I have the first bar thing pretty much figured out... just need the cumulative profit trigger (still has me stumped..... I also want to create a price bracket of the first half hour of trading (630- 700) and have my system only recognize buy signals above this range and only recognize sell signals below the range....

                  For example. Low price of range (630 - 700) == 1280 High price of range (630- 700) == 1300
                  recognize long signal > 1300 and recognize Short signal < 1280.

                  THanks man. Jamie Hendrix

                  Comment


                    #10
                    Hi JamieHendrix,

                    Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two.
                    Yes, you can work with BarsSinceSession or FirstBarOfSession to identify the first bar of a session.

                    I also want to create a price bracket of the first half hour of trading (630- 700) and have my system only recognize buy signals above this range and only recognize sell signals below the range....
                    This sample can help with getting the highest and lowest values of a time range.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by JamieHendrix View Post
                      Austin, I got it figured out, just didn't realize about the historical thing.... just zoned out on that one I guess. Two other things I am trying to figure out..... Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two... can't use "to time" cause I use Tick charts. Number two is that I would like it to stop trading after a certain cumulative profit has been reached for the day ( Like once it hits $100, stop trading or 2 points then stop)?

                      Thanks man, I am almost there. Jamie Hendrix
                      Code:
                      if (Bars.FirstBarOfSession) return;
                      ;

                      before the first line that processes strategy orders, should take care of that.

                      Comment


                        #12
                        Thanks for your reply man. Do you happen to know how to get indicators to start calculating starting from the first bar? Mine are continuing from the previous days close... totally throwing everything off for about an hour or so.

                        Comment


                          #13
                          Originally posted by JamieHendrix View Post
                          Thanks for your reply man. Do you happen to know how to get indicators to start calculating starting from the first bar? Mine are continuing from the previous days close... totally throwing everything off for about an hour or so.
                          Unfotunately, that will depend entirely on how the code is written, but basically, you record the barNumber for the FirstBarOfSession, then create an escape condition to not calculate in the dead zone from that bar to the number of bars that need to exist before calculations start.

                          Code:
                           
                          private int DeadZoneStartBar = 0;
                          private int DeadZoneEndBar = 7; // or whatever you need
                          private barsRequiredToStartCalculations = 7; // or whatever you need
                          Code:
                           
                          if (Bars.FirstBarOfSession)
                          {
                             DeadZoneStartBar = CurrentBar;
                             DeadZoneEndBar = CurrentBar + barsRequiredToStartCalculations;
                          }
                           
                          if (CurrentBar >= DeadZoneStartBar && CurrentBar < DeadZoneEndBar) return;
                           
                          // do the rest of stuff here.

                          Comment


                            #14
                            Thanks for your help... it worked as far as waiting to execute N bars before it trades but it has no effect on when the EMA is calculated.... the chart is taking the last 20 bars of yesterdays close and using that data to calculate in todays open.... this totally throws off at least the first hour of the day.... I can tell it to wait until the second hour to start, but I would rather not.

                            Again, thanks so much for your help.

                            Jamie Hendrix

                            Comment


                              #15
                              Originally posted by JamieHendrix View Post
                              Thanks for your help... it worked as far as waiting to execute N bars before it trades but it has no effect on when the EMA is calculated.... the chart is taking the last 20 bars of yesterdays close and using that data to calculate in todays open.... this totally throws off at least the first hour of the day.... I can tell it to wait until the second hour to start, but I would rather not.

                              Again, thanks so much for your help.

                              Jamie Hendrix
                              If you want the EMA to do the same, then you have 3 choices that I can think of:
                              1. Write your own EMA code inside the strategy, and use this escape
                              2. Create an instance of an EMA, and use this escape
                              3. Write a custom EMA script that has the same restriction, and use a public parameter to pass the correct periods into this custom EMA.
                              Option 1 is self-contained, the other 2 are not.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by halgo_boulder, Today, 08:44 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post halgo_boulder  
                              Started by drewski1980, Today, 08:24 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post drewski1980  
                              Started by rdtdale, Yesterday, 01:02 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post rdtdale
                              by rdtdale
                               
                              Started by TradeSaber, Today, 07:18 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post TradeSaber  
                              Started by PaulMohn, Today, 05:00 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post PaulMohn  
                              Working...
                              X