Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding ATM to my Strategy

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

    Adding ATM to my Strategy

    I am new to trading and using strategy builder and I’m having a few issues with a strategy. Below is what I added to my stategy from the SampleATMStrategy in Ninja Script. Can you please look at it and tell me what I have to do to make it right and working correctly. The original Strategy before I added ATM works flawlessly, I have no issues with my original strategy but below are the issues I have once I add ATM to it.

    My Issues:
    1. The ATM strategy works in playback mode & on simulated accounts. When I try to trade with my Apex or TP Accounts it will not enter a trade. I was testing it today on simulated accounts and it just stopped working. I can run the original strategy on a separate chart beside the ATM Added Strategy and the Original executes properly and the ATM does nothing. The ATM Added Strategy worked last week and made me money and then quit.I didn't change anything for it to quit working on active accounts.
    2. Once the ATM Strategy is working properly, I want it to use the Max Profit & Max Loss so it will not take new trades once it reaches either limit. How would I add that to work off Realized PNL.
    Here is what I added:
    public class SampleAtmStrategy : Strategy

    {




    private string atmStrategyId = string.Empty;

    private string orderId = string.Empty;

    private bool isAtmStrategyCreated = false;

    Then:
    IsInstantiatedOnEachOptimizationIteration = true;

    ATMStrategy = @"ATM1";

    ​Then:
    protected override void OnBarUpdate()
    ​{
    if(State == State.Historical)

    return;
    ​}

    Then: in red is what I added to the first line of my If All for my long entry
    if (orderId.Length == 0 && atmStrategyId.Length == 0 && (NBarsUp1[0] == 1)

    Then: In red below are the changes made to the SampleATM Strategy
    {

    BackBrush = Brushes.CornflowerBlue;

    isAtmStrategyCreated = false; // reset atm strategy created check to false

    atmStrategyId = GetAtmStrategyUniqueId();

    orderId = GetAtmStrategyUniqueId();

    AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Gtc, orderId, ATMStrategy, 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;




    if (atmStrategyId.Length > 0)

    {

    // You can change the stop price

    if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)

    AtmStrategyChangeStopTarget(DefaultQuantity, 0, "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)) ;



    }

    Please let me know how to get this working and what looks not correct.
    Stephen​​

    #2
    Hello Bigtime77,

    If you are trying to use ATM strategies the strategy builder cannot be used, that uses the managed approach for orders which is a different system.

    The best way to start using ATM's is to make a full copy of the SampleATMStrategy by opening it, right click and choose Save As. The way that code is structured is what would be required to launch and later manage an ATM from a NinjaScript strategy so that is a good starting place for custom scripts. You can basically use that script as is, the only points of interest would be the entry condition which is currently: && Close[0] > Open[0]. The other part would be the template name being used.

    Comment


      #3
      I have added the SampleATMStrategy to my strategy. It will work properly on playback & simulation. It just will not enter a trade when I have one of my APEX or TakeProfit Accounts on the strategy. Any ideas why the is?

      Comment


        #4
        Hello Bigtime77,

        With only that amount of detail I wouldn't be able to say what may be happening, you can try using Print statements to verify the logic you made is working as expected.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        50 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X