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've got the ATM Strategy working with coded Strategy. One last problem.

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

    I've got the ATM Strategy working with coded Strategy. One last problem.

    The Markers and Texts aren't showing up when trades are executed. I have them selected in the candle properties. Don't know what to do about it. Here is the code:

    private string atm_id;
    private string atmStrategyId = string.Empty;
    private bool isAtmStrategyCreated = false;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyCustomStrategyTEST";
    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 (CurrentBar < BarsRequiredToTrade)
    return;

    // HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies" under NinjaScript--> Educational Resources--> http://ninjatrader.com/support/helpG...strategies.htm

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



    if(Close[1] <= SMA(5)[1] && Close[0] > SMA(5)[0])
    {
    if(PositionAccount.MarketPosition != MarketPosition.Flat)
    {
    return;
    }

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

    AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, atm_id, "NQ Wave", 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 == atm_id)
    isAtmStrategyCreated = true;
    });

    if (!isAtmStrategyCreated)
    return;

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

    // 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")
    atm_id = string.Empty;
    }
    } // If the strategy has terminated reset the strategy id
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;

    if (atmStrategyId.Length > 0)
    {
    // You can change the stop price
    if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
    AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

    // Print some information about the strategy to the output window, please note you access the ATM strategy specific position object here
    // the ATM would run self contained and would not have an impact on your NinjaScript strategy position and PnL
    Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
    Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
    Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
    Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
    }
    }
    }



    else if(Close[1] >= SMA(5)[1] && Close[0] < SMA(5)[0])
    {
    if(PositionAccount.MarketPosition != MarketPosition.Flat)
    {
    return;
    }

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

    AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, atm_id, "NQ Wave", 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 == atm_id)
    isAtmStrategyCreated = true;
    });

    if (!isAtmStrategyCreated)
    return;

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

    // 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")
    atm_id = string.Empty;
    }
    } // If the strategy has terminated reset the strategy id
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;

    if (atmStrategyId.Length > 0)
    {
    // You can change the stop price
    if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
    AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

    // Print some information about the strategy to the output window, please note you access the ATM strategy specific position object here
    // the ATM would run self contained and would not have an impact on your NinjaScript strategy position and PnL
    Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
    Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
    Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
    Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
    }
    }
    }


    }

    #2
    Hello jamestrader21x,

    Thank you for your post.

    This would be expected. From our help guide:

    "Executions resulting from an ATM Strategy that is created from within a NinjaScript automated strategy will not plot on a chart during real-time operation"
    https://ninjatrader.com/support/help...strategies.htm

    This is due to the fact that when a strategy is running on a chart, only executions managed by the strategy will be shown. Since submitting ATMs from a NinjaScript Strategy means that the strategy is not managing those orders, the ATM is, the executions will not be seen on the chart the strategy is running on. Opening a second chart without the strategy applied to it will allow you to see the executions.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Okay. I appreciate all of the help.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by AttiM, 02-14-2024, 05:20 PM
      11 responses
      184 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by fernandobr, Today, 09:11 AM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by timmbbo, Today, 08:59 AM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by KennyK, 05-29-2017, 02:02 AM
      2 responses
      1,281 views
      0 likes
      Last Post marcus2300  
      Started by itrader46, Today, 09:04 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X