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

NT8 Can Not Send Orders for Two Intruments Independently Within one Strategy

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

    NT8 Can Not Send Orders for Two Intruments Independently Within one Strategy

    I have a simple long-only strategy that uses EOD bars for one stock in the primary dataseries, and EOD bars for another stock in the secondary dataseries. The order triggers are based on an MACD crossover (signal vs. average) within the respective BarsInProgress environment for each dataseries. As coded it works out that trades are only made for the primary series, and not the secondary series.

    The level of support available in the online Reference is inadequate for accomplishing developing such a simple strategy.

    Code is below:

    Code:
    private int numshares,numshares1;
    private MACD MACDtest;
    private MACD MACD1;
    
    
    (in State.Defaults):
    
    MACDlen=26;
    AmountPerAsset = 2500;
    asset1="INTC";
    
    else if (State == State.DataLoaded)
    {
        MACDtest=MACD(12,26,9);
        AddChartIndicator(MACDtest);
    
        MACD1=MACD(BarsArray[1],12,26,9);
    }
        else if (State == State.Configure)
    {
        AddDataSeries(asset1,BarsPeriodType.Day, 1);
    }
    
    
    if (BarsInProgress == 0)
    {
    
        if (CurrentBar<MACDlen)
            return;
    
        bool buyStop1,buyExit1;
        buyStop1 = false;
        buyExit1 = false;
        buyStop1 = CrossAbove(MACDtest, MACDtest.Avg, 1) ;
        buyExit1 = CrossBelow(MACDtest, MACDtest.Avg, 1);
    
        numshares = Convert.ToInt32(Math.Max(1,AmountPerAsset/Close[1]));
        if ( buyStop1 && Positions[1].MarketPosition == MarketPosition.Flat)
        {
           EnterLong(0,numshares,"s0");
        }
        if (buyExit1 && Positions[1].MarketPosition == MarketPosition.Long)
           ExitLong("s0");
    }
    
    if (BarsInProgress == 1)
    {
        if (CurrentBars[1]<MACDlen)
           return;
        numshares1 = Convert.ToInt32(Math.Max(1,AmountPerAsset/Close[1]));
        bool buyStop2,buyExit2;
        buyStop2 = false;
        buyExit2 = false;
        buyStop2 = CrossAbove(MACD1,MACD1.Avg,1);
        buyExit2 = CrossBelow(MACD1,MACD1.Avg,1);
        if ( buyStop2 && Positions[1].MarketPosition == MarketPosition.Flat)
        {
             EnterLong(1,numshares1,"s1");
        }
        if (buyExit2 && Positions[1].MarketPosition == MarketPosition.Long)
            ExitLong("s1");
     }
    Last edited by pel11; 06-15-2021, 11:38 PM.

    #2
    Hello pel11,

    Thank you for the post.

    Have you tried adding a Print into your condition to see if the condition to trade is becoming true? That would be my first suggestion to make sure the condition to enter is happening as expected. If that is you may also want to enable TraceOrders to check if anything is happening surrounding the managed approach.



    One other item to try would be to hard code the secondary series instead of using the asset1 variable. AddDataSeries is not expecting variables so that may not work in all use cases.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,

      Am trying a few things today, will see how it goes. I think the first order of business is to ensure all series are also caught up to the primary dataseries, before doing anything. It does appear that anything done regarding secondary dataseries really has to be initiated through BarsInProgress==0, after ensuring all the other dataseries have caught up to CurrentBar for the primary dataseries.

      I did print a lot of detail regarding values for numshares, and buy/exit being true/false, and when true, the orders were not sent for secondary series. I believe this is because the dataseries have not been synced.

      Comment


        #4
        The strategy now requires all CurrentBars[] values to be synced, and trades for all dataseries can be posted from within BarsInProgress==0.

        I added a daily chart (default 365 days) and 3 other dataseries, i.e., [0],[1],[2] and [3]. The MACD buy signal was triggered on the fourth dataseries before the other assets, so it went long for 216 days. In the mean time, even though the other series were flat and MACD crossovers occurred, there were no orders sent for the other series, so I am thinking NT8 cannot be e.g. long for more than one asset at a time. The orderSignal values I am using are stored in a string array, and are unique to the dataseries and the specific trade -- as they are appended when going long (only). Thus, if an MACD exit is triggered later for a given dataseries, then the orderSignal used for exiting is the same orderSignal used for entering on that dataseries.

        Overall, something is preventing overlapping orders from being placed for more than one dataseries.


        Code:
        for (int i=0; i < numSymbols; i++)
        {
            macd=MACD(BarsArray[i],12,26,9);
            buyStop=false;
            buyExit=false;
            buyStop = CrossAbove(macd, macd.Avg, 1) ;
            buyExit = CrossBelow(macd, macd.Avg, 1);
        
            numshares = Convert.ToInt32(Math.Max(1,AmountPerAsset/Closes[i][0]));
        
            if ( buyStop && Positions[i].MarketPosition == MarketPosition.Flat)
            {
                orderSignal[i] += "s" + i;
                EnterLong(i,numshares,orderSignal[i]);
            }
            if ( buyExit && Positions[i].MarketPosition == MarketPosition.Long)
                ExitLong(orderSignal[i]);
        
            Print(i + " " + CurrentBars[i] + " " + Positions[i].MarketPosition);
        }

        Comment


          #5
          Hello pel11,

          Thank you for the post.

          Did you enable TraceOrders to check if anything is happening surrounding the managed approach?



          You would also need more than 1 entries per direction if you are entering the same direction for both instruments.

          I would otherwise suggest to remove the loop and simplify what you are trying.


          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hello Jesse,

            I was able to get it working by pasting:

            Code:
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.UniqueEntries;
            in the Set.Defaults environment of the strategy. Looking at the trades made in the historical performance, it does appear that many are overlapping across multiple instruments.

            I believe use of
            Code:
            EntryHandling = EntryHandling.UniqueEntries
            is what allows this. Or do I really need to ramp up
            Code:
            EntriesPerDirection
            ?

            Comment


              #7
              Ramping up EntriesPerDirection, when Unique is set did not change the trades that were made. I know EntriesPerDirection doesn't work unless AllEntries is specified.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by traderqz, Yesterday, 12:06 AM
              11 responses
              27 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by PaulMohn, Today, 03:49 AM
              0 responses
              8 views
              0 likes
              Last Post PaulMohn  
              Started by inanazsocial, Today, 01:15 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Jason  
              Started by rocketman7, Today, 02:12 AM
              0 responses
              11 views
              0 likes
              Last Post rocketman7  
              Started by dustydbayer, Today, 01:59 AM
              0 responses
              4 views
              0 likes
              Last Post dustydbayer  
              Working...
              X