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

SampleStrategyTemplate help

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

    SampleStrategyTemplate help

    Hi,

    I did some test with the SampleStrategyTemplate and it works fine for what I'm doing..

    I created another Strategy based on this code but I used different entry rules.

    I can't find any information on the order code in the Ninja Script so I need some advice on what to do.

    Here is some of the code from the SampleStrategyTemplate ( the code in red is the part that I modify and some other code but that's working fine)


    if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
    {
    isAtmStrategyCreated = false; // reset atm strategy created check to false
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();

    AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) =>

    {
    //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
    if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
    isAtmStrategyCreated = true;
    });
    }

    I modified this section hoping that I could have a TYPE Double from my own calculations which I would enter into the AtmStrategyCreate() code

    AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId)


    I want to be able to use the double type instead of the Low[0] and also change order type to stoplimit

    For example

    I created a double type name buyEntry and put that in place of the Low[0] thinking that would be ok..
    AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, buyEntry, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId)


    But I get this message when the strategy is running and conditions are met.

    AtmStrategyCreate method error: A stop limit order requires a limit and stop price

    I created a AtmStrategy and named it AtmStrategyTemplate so it has the stop limits in there , and that AtmStrategy is called in the code above.


    Can anyone help with this? Is there more information on this type of order entry.. I've checked in managed and un-managed orders but can't see any similar code.


    Thanks..


    #2
    Hello traderhawk,

    The error you are getting is just because you are not specifying the correct parameters for the type of order you selected. You have OrderType.StopLimit, that would require using two prices or a stop price and limit price. If you wanted to only submit only at a limit price then you need to specify Limit as the type of the order instead. Otherwise you need to calculate a stop price and use that in the parameter instead of the 0 you have currently.

    The variable is also not the problem, you could add a second variable for the stop price if you wanted to continue using a StopLimit order you just need to replace the 0 with that variable or a price.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Ok Thanks..

      I've managed to get it to work..

      Just created a simple test strategy..understand what it is about now..


      namespace NinjaTrader.NinjaScript.Strategies.MyStrategies
      {
      public class SimpleStopLimit : Strategy
      {
      private string atmStrategyId = string.Empty;
      private string orderId = string.Empty;
      private bool isAtmStrategyCreated = false;
      private double limitPrice;
      private double stopPrice;


      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Testing Stop Limit Orders";
      Name = "SimpleStopLimit";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      }
      }

      protected override void OnBarUpdate()
      {

      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Make sure this strategy does not execute against historical data
      if(State == State.Historical)
      return;

      ////////////////////////////////////// Bull Signal ////////////////////////////////////////////////////


      if (orderId.Length == 0 && atmStrategyId.Length == 0 & (Close[0] > Open[0]) & (Close[1] > Open[1]))
      {

      limitPrice = Low[0] + (12 * TickSize);

      stopPrice = Low[0] + (10 * TickSize);

      isAtmStrategyCreated = false; // reset atm strategy created check to false
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();

      AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, limitPrice, stopPrice, TimeInForce.Gtc, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) =>
      {
      //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
      if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
      isAtmStrategyCreated = true;
      }
      );


      }

      ///////////////////////////////////////////////////////////////////////////////////////////////////////////


      ////////////////////////////////////// Bear Signal ////////////////////////////////////////////////////


      if (orderId.Length == 0 && atmStrategyId.Length == 0 & (Close[0] < Open[0]) & (Close[1] < Open[1]))
      {

      limitPrice = High[0] - (12 * TickSize);

      stopPrice = High[0] - (10 * TickSize);

      isAtmStrategyCreated = false; // reset atm strategy created check to false
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(OrderAction.Sell, OrderType.StopLimit, limitPrice, stopPrice, TimeInForce.Gtc, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) =>
      {
      //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
      if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
      isAtmStrategyCreated = true;
      }
      );

      }


      /////////////////////////////////////////////////////////////////////////////////////////////////////////////


      // Check that atm strategy was created before checking other properties
      if (!isAtmStrategyCreated)
      return;

      // Check for a pending entry order
      if (orderId.Length > 0)
      {
      string[] status = GetAtmStrategyEntryOrderStatus(orderId);

      // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
      if (status.GetLength(0) > 0)
      {
      // Print out some information about the order to the output window
      Print("The entry order average fill price is: " + status[0]);
      Print("The entry order filled amount is: " + status[1]);
      Print("The entry order order state is: " + status[2]);

      // If the order state is terminal, reset the order id value
      if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
      orderId = string.Empty;
      }
      } // If the strategy has terminated reset the strategy id
      else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
      atmStrategyId = string.Empty;

      } // End OnBarUpdate
      }
      }





      Thanks
      TraderHawk
      Last edited by traderhawk; 12-02-2021, 07:40 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by GwFutures1988, Today, 02:48 PM
      0 responses
      2 views
      0 likes
      Last Post GwFutures1988  
      Started by mmenigma, Today, 02:22 PM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by frankthearm, Today, 09:08 AM
      9 responses
      35 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by NRITV, Today, 01:15 PM
      2 responses
      9 views
      0 likes
      Last Post NRITV
      by NRITV
       
      Started by maybeimnotrader, Yesterday, 05:46 PM
      5 responses
      28 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X