Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Any idea what I've got wrong here?

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

    Any idea what I've got wrong here?

    I have a strategy that enters trades in 4 markets. I want to get flat if the total profit exceeds the User Defined Variable "reward" or drops below the User Defined Variable "risk". This is the code. I suspect I am making a simple error, but do not see it.

    totprofloss = Positions[0].GetProfitLoss(Closes[0] [0],PerformanceUnit.Currency) +
    Positions[1].GetProfitLoss(Closes[1][0],PerformanceUnit.Currency) +
    Positions[2].GetProfitLoss(Closes[2][0],PerformanceUnit.Currency) +
    Positions[3].GetProfitLoss(Closes[3][0],PerformanceUnit.Currency);

    if (totprofloss > reward || totprofloss < (risk * -1))

    {

    for (int i = 0; i <= 3; i++)
    {

    if (Positions[i].MarketPosition == MarketPosition.Long)
    {ExitLong();}
    if (Positions[i].MarketPosition == MarketPosition.Short)
    {ExitShort();}
    }

    #2
    Hi jh1962, what exactly is going wrong?

    Just looking at the code, I would guess that all of your positions aren't being closed out correctly because the Exit() methods don't include a BarsInProgress identifier.

    If your secondary instrument's signal name was named "secondary long entry," you could use ExitLong() like this, with 1 as the BarsInProgress index because the primary BIP = 0, secondary = 1, etc.:
    Code:
    //ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)
    ExitLong(1, Positions[1].Quantity, "secondary long exit", "secondary long entry");
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks Austin, for the reply. You have probably pointed me in the right direction, but that leads me to ask what I can count on being consistent. I am applying the strategy to an ES chart and then using Add() to add the ZB, GC and 6E. I had thought that Positions[0] would always refer to the ES, Positions[1] always to the ZB, etc. Is this not true? If not, which array would consistently hold the positions in the position that they were added to the chart? You can tell that I am new to NT.

      Thanks,

      Jamie

      Comment


        #4
        Jamie, your understanding of Positions is correct, the reference is consistent as long as you don't change the added order of instrumemts. I would suggest working Austin's comment in (working with ExitLong and ExitShort in multiseries context).

        Also: which behavior do you get now? No positions closed, only particular ones closed....?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Each position is treated individually. Whatever the risk and reward variables are set to is where each market will exit individually. I want the totprofloss to apply to the entire portfolio of four positions, and if either risk or reward is reached, to exit all four positions.

          Thanks for your help on this.

          Comment


            #6
            Ok, but what behavior / issue do you get now, you need to debug the code step by step to achieve what you want - are the risk / reward variables calcuated as you would expect? Did you 'print' those values to the output window already?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Again, thanks for the quick response. Pick any position at random. Let's say the ZB. If the risk is set to 500 and the reward is set to 400, the ZB trade will exit if the ZB trade itself reaches either of those p/l. I want the ZB (and the others) to exit if the TOTAL p/l for all 4 markets reaches either parameter. In other words if the ZB is at breakeven, but the combined totals of ES, GC and 6E are plus 400 (or minus 500) I want all 4 trades closed. I have used the Print statement and viewed the Output window, but it is not helpful. The program is tracking the variable totprofloss correctly, but is not exiting the trades when it should - as I said, treating each market individually for exit purposes. Let me know if you need any other info.

              Comment


                #8
                Ok I see, then I would suggest to amend the code as per Austin's reply (using the ExitLong / ExitShort overload offering the barsInProgress parameter) and also tieing entries and exits from all your bars objects togethers via the signal names. To doublecheck your counter is working as you expect, just code it out invidually for each instrument, then recheck.

                You also want to enable TraceOrders, as this give you valuable info to debug your strategies order behavior - http://www.ninjatrader-support2.com/...ead.php?t=3627
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  OK. I may have to add a lot of code, but I don't mind as long as it works. But first, please help me with this:

                  If BarsInProgress == 0, then I am dealing with ES and Positions [0] would be ES, Positions[1] would be ZB, etc. Is that correct?

                  If BarsInProgress == 1 (which is ZB), would Positions[0] still be ES and Positions[1] still be ZB, etc.?

                  Comment


                    #10
                    Correct, Positions has a BarsInProgress parameter and thus provided this corresponds to the correct bars array used in the add method it's not sensitive to the BarsInProgress calling it.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      OK, Bertrand. Appreciate the help. I will recode the program as per the instructions I have been given here. I will then reply with a "Yes, it worked" or with more questions if it doesn't.

                      Thanks.

                      Comment


                        #12
                        Still doesn't work

                        Here is the code. It is complete, except that I have snipped the code for BarsInProgress ==2 and ==3, as they are the same as BarsInProgress == 1 with changes appropriate to the particular section. Any advice would be appreciated.

                        protected override void OnBarUpdate()
                        {
                        if (BarsInProgress == 0)
                        {
                        if (Position.MarketPosition == MarketPosition.Flat
                        && EMA(Fast)[0] > EMA(Slow)[0])
                        EnterShort(DefaultQuantity, "ESS");

                        if (Position.MarketPosition == MarketPosition.Flat
                        && EMA(Fast)[0] < EMA(Slow)[0])
                        EnterLong(DefaultQuantity, "ESL");

                        totprofloss = Positions[0].GetProfitLoss(Closes[0][0],PerformanceUnit.Currency) +
                        Positions[1].GetProfitLoss(Closes[1][0],PerformanceUnit.Currency) +
                        Positions[2].GetProfitLoss(Closes[2][0],PerformanceUnit.Currency) +
                        Positions[3].GetProfitLoss(Closes[3][0],PerformanceUnit.Currency);

                        if (totprofloss > reward || totprofloss < (risk * -1))
                        {
                        if (Position.MarketPosition == MarketPosition.Long) ExitLong("ESL");
                        if (Positions[1].MarketPosition == MarketPosition.Long)
                        ExitLong("ZBL");
                        if (Positions[2].MarketPosition == MarketPosition.Long)
                        ExitLong("GCL");
                        if (Positions[3].MarketPosition == MarketPosition.Long)
                        ExitLong("6EL");

                        if (Position.MarketPosition == MarketPosition.Short) ExitShort("ESS");
                        if (Positions[1].MarketPosition == MarketPosition.Short)
                        ExitShort("ZBS");
                        if (Positions[2].MarketPosition == MarketPosition.Short)
                        ExitShort("GCS");
                        if (Positions[3].MarketPosition == MarketPosition.Short)
                        ExitShort("6ES");

                        }
                        }

                        if (BarsInProgress == 1)
                        {
                        if (Position.MarketPosition == MarketPosition.Flat
                        && EMA(Fast)[0] > EMA(Slow)[0])
                        EnterShort(DefaultQuantity, "ZBS");

                        if (Position.MarketPosition == MarketPosition.Flat
                        && EMA(Fast)[0] < EMA(Slow)[0])
                        EnterLong(DefaultQuantity, "ZBL");

                        totprofloss = Positions[0].GetProfitLoss(Closes[0][0],PerformanceUnit.Currency) +
                        Positions[1].GetProfitLoss(Closes[1][0],PerformanceUnit.Currency) +
                        Positions[2].GetProfitLoss(Closes[2][0],PerformanceUnit.Currency) +
                        Positions[3].GetProfitLoss(Closes[3][0],PerformanceUnit.Currency);

                        if (totprofloss > reward || totprofloss < (risk * -1))
                        {
                        if (Position.MarketPosition == MarketPosition.Long) ExitLong("ZBL");
                        if (Positions[0].MarketPosition == MarketPosition.Long)
                        ExitLong("ESL");
                        if (Positions[2].MarketPosition == MarketPosition.Long)
                        ExitLong("GCL");
                        if (Positions[3].MarketPosition == MarketPosition.Long)
                        ExitLong("6EL");

                        if (Position.MarketPosition == MarketPosition.Short) ExitShort("ZBS");
                        if (Positions[0].MarketPosition == MarketPosition.Short)
                        ExitShort("ESS");
                        if (Positions[2].MarketPosition == MarketPosition.Short)
                        ExitShort("GCS");
                        if (Positions[3].MarketPosition == MarketPosition.Short)
                        ExitShort("6ES");

                        }
                        }
                        }

                        Comment


                          #13
                          Which exactly is the part that does not work? What does it do instead?
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            It does not exit all trades at once. It still appears to be treating them individually. It is obviously triggering the if statement that it should encounter when totprofloss is higher than reward or lower than risk, otherwise there would be no exits at all. There are no profit targets or stop losses set in the initialize method. In fact here is all I have under initialize()

                            protected override void Initialize()
                            {
                            Add("ZB 12-09", PeriodType.Minute, 10);
                            Add("GC 10-09", PeriodType.Minute, 10);
                            Add("6E 12-09", PeriodType.Minute, 10);
                            CalculateOnBarClose = true;
                            ExitOnClose = false;
                            TraceOrders = true;
                            }

                            Comment


                              #15
                              Backtesting? It will indeed be the case because it will need to place in the order at the next actionable location for the particular instrument which could be the next bar for that instrument.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,221 views
                              0 likes
                              Last Post xiinteractive  
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post FAQtrader  
                              Working...
                              X