Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

i would like to create a test strategy that alternated between long-short positions.

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

    i would like to create a test strategy that alternated between long-short positions.



    people with nt,



    regards.


    i have had several issues trying to get nt to trade my strategies automated. nt insists that the strategy and the position in my account are in synchronization but that's just not the case.


    in another inferior platform (ts), i was able to create a very simple strategy that would alternate between long and short positions if the bar number of the current bar could be exactly divided by 2 or not. i imagine such a strategy must also be easily achievable in nt but i have searched through the help guide and tried to use the strategy build tool without success. this kind of strategy is quite helpful when trying to determine whether orders are being correctly processed and sent or not.


    the following fragment is an amalgamation of ninjascript and the structure of this strategy in the other platform:

    Code:
    if mod(currentbar,2)=0 and lastbaronchart and barstatus( 1 ) = 2 then
    {
    EnterShort(2, Convert.ToInt32(Psize), @"sp01");
    }
    if mod(currentbar,2)=1 and lastbaronchart and barstatus( 1 ) = 2 then
    {
    EnterLong(2, Convert.ToInt32(Psize), @"lp01");
    }

    ¿how would it be possible to get nt to generate long or short orders at the close of each bar depending on whether its bar number is divisible by 2 or not?

    very well, thanks, regards.
    Last edited by rtwave; 10-03-2019, 12:14 AM.

    #2
    Hello,

    To find if the bar number is divisible by 2 in C#:

    if (CurrentBar % 2 == 0)
    {
    // trigger action
    }

    Below is a link to a forum post about synchronizing and start behavior.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3


      Chelsea,



      thanks.


      the code you suggested works to perfection.


      i share this strategy in case it could be of use to anyone, if this strategy on a 10 second bar chart doesn't generate endless alternating positions then there's something wrong with the platform, internet connection, account, etc. if this strategy does generate a torrent of alternating positions but other strategies you are working on do not generate valid orders then the problem lies with your other strategies.


      Code:
      
      if (CurrentBars[0] <= BarsRequiredToTrade) return;
      
      // Set 1
      if (CurrentBar % 2 == 0)
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), "");
      }
      
      // Set 2
      if (CurrentBar % 2 == 1)
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), "");
      }
      Last edited by rtwave; 10-03-2019, 12:15 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      45 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      21 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      31 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X