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

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 AveryFlynn, Today, 04:57 AM
      0 responses
      4 views
      0 likes
      Last Post AveryFlynn  
      Started by RubenCazorla, 08-30-2022, 06:36 AM
      3 responses
      79 views
      0 likes
      Last Post PaulMohn  
      Started by f.saeidi, Yesterday, 12:14 PM
      9 responses
      25 views
      0 likes
      Last Post f.saeidi  
      Started by Tim-c, Today, 03:54 AM
      0 responses
      3 views
      0 likes
      Last Post Tim-c
      by Tim-c
       
      Started by FrancisMorro, Today, 03:24 AM
      0 responses
      5 views
      0 likes
      Last Post FrancisMorro  
      Working...
      X