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

Simple SMA cross

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

    Simple SMA cross

    Hi, I am trying to create a strategy for a SMA cross. When the fast moving crosses above the slow moving then long with a set TP and SL on the next bar open. When the fast moving crosses below the slow moving then short on the next bar open with a set TP and SL. Right now when I try it, may enter the position many times. Like if the SL or TP is hit in the same bar it will re enter.

    #2
    Hello mikeset,

    Thanks for your post and welcome to the NinjaTrader forums!

    So that we can best assist you with your inquiry we will need further information about your strategy and application.

    What is the Calculate setting used? (Calculate.OnBarClose, Calculate.OnPriceChange, or Calculate.OnEachTick).

    Are you testing on live data, historical data on the chart, historical data on the strategy analyzer, or playback with market replay data?

    Does your script access additional data series? (if so please identify)

    Are you using the Strategy Builder or the Ninjascript editor?

    Can you provide an example of the code you are using for your stop loss and profit target?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul, thanks for the response!

      Oh it is OnEachTick. So this should be OnBarClose?

      Live data is when the error occurs. Doing a historical back test it works fine from what I see.

      Only one data series.

      Strategy Builder.

      SetProfitTarget("", CalculationMode.Ticks, Tp);
      SetStopLoss("", CalculationMode.Ticks, Sl, false);

      for inputs and variables I created
      Sma1
      Sma2
      Tp
      Sl

      Then in Conditions and Actions I made two Sets.

      Set 1:

      So if all of the following conditions are met:
      CrossAbove(SMA(Convert.ToInt32(Sma1)), SMA(Convert.ToInt32(Sma2)), 1)

      Do the following
      EnterLong(DefaultQuantity, @"L");

      Set 2:

      So if all of the following conditions are met:
      CrossBelow(SMA(Convert.ToInt32(Sma1)), SMA(Convert.ToInt32(Sma2)), 1)

      Do the following
      EnterShort(DefaultQuantity, @"S");

      Comment


        #4
        Hello mikeset,

        Thanks for your reply.

        Based on the information provided, thank-you, the issue is caused by using Calculate.OneachTick with live data. What happens is that your script is executed on every tick and during a crossover event , as price oscillates, so does the actual crossover event (crossing then uncrossing then crossing again, etc). Each time the crossover occurs the strategy is sending a new order. There are protections in place to prevent multiple entries per direction however these depend on the transmission of the order to the exchange, the placement of the order at the exchange, and then the update to the position from the exchange, and all of this happens asynchronously to the execution of your code. Calculate.OnEachTick means your script is running far faster than the order information can be updated.

        This does not happen in backtesting because the strategy will run as if Calculate.OnBarClose = true, so it only executes once per bar with finished (unchanging) data so there is no multiple entry opportunities. backtesting uses only completed bars so it can only run once per bar.

        You have a couple of options, use Calculate.OnBarClose and your strategy will perform the same in live as it does in backtesting

        If you want to maintain using Calculate.OnEachTick, then you can modify your code so that it prevents the multiple orders. The way to do this is to create an int variable (call it EntryBar, or whatever you like). In your entry set, add the condition Left: Misc>Current bar, Not Equal, right User Variables>EntryBar. In the "do the following" of the same set, add the action Misc>Set EntryBar, click "set" in the field, and select Misc>CurrentBar. What this will do is to let the first cross event occur and the first order to occur and then the current bar number is saved inEntryBar, on the next crossevent the action cannot occur because the current bar IS equal to the EntryBar. It is only when the next bar starts that a new order could be placed but by then the cross event should be finished. You would need to do this for each entry set.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you, I will try this right away! Is it possible to enter x amount of lots as a % of the balance? Like risk 2% of the balance each trade?

          Comment


            #6
            Hello mikeset,

            Thanks for your reply.

            "Is it possible to enter x amount of lots as a % of the balance? Like risk 2% of the balance each trade?" The strategy builder is limited in its functionality and there is no means to pull account balances in the Strategy Builder.

            If after your strategy is working as you wish, you could make a copy of it, unlock its code and work in Ninjascript to add that functionality.

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Ok understood, thanks for the help, let me make sure the basic functionality is working

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jon17, Today, 04:33 PM
              0 responses
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              4 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Started by alifarahani, Today, 09:40 AM
              6 responses
              40 views
              0 likes
              Last Post alifarahani  
              Started by Waxavi, Today, 02:10 AM
              1 response
              19 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Working...
              X