Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can Ninja do the opposite that i try to do?

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

    Can Ninja do the opposite that i try to do?

    After years of studying and trading, I realized that I am not able to be profitable. I need to set up NinjaTrader so that when I go long, it does so in simulation mode, and at the same time, on the real account, it places a short order, opposite to mine. Same size, same stop loss, and take profit but reversed. Is it possible to do this?"

    #2
    Hello scalper09092024,

    Welcome to the NinjaTrader forums!

    This would be possible to do, but I would recommend testing it out on a Sim account first.

    Sim accounts can be added on the Accounts tab of the Control Center. (Multi-provider mode must be enabled)


    To send the order to the account, you will need the account object which can be found by name.

    private Account myAccount;

    In OnStateChange when State is State.DataLoaded:

    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");

    The order would be created and submitted with <Account>.CreateOrder() and <Account>.Submit().

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you, I tried to insert all the values but I get an error on line 40

      protected override void OnExecutionUpdate(Execution execution, Order order, double quantity, double price, (ERROR CS0115)



      FULL CODE

      region Using declarations
      using System;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using System.Linq;
      #endregion

      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class OppositeTradeStrategy : Strategy
      {
      private Account simAccount; // L'account SIM
      private Account realAccount; // L'account Reale

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Name = "OppositeTradeStrategy";
      Calculate = Calculate.OnEachTick;
      }
      else if (State == State.DataLoaded)
      {
      // Ottieni l'account SIM e l'account reale tramite il nome
      lock (Account.All)
      {
      simAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101"); // Modifica con il nome del tuo account SIM
      realAccount = Account.All.FirstOrDefault(a => a.Name == "RealAccount"); // Modifica con il nome del tuo account reale
      }

      if (simAccount == null)
      throw new Exception("Account SIM non trovato");
      if (realAccount == null)
      throw new Exception("Account reale non trovato");
      }
      }

      // Metodo corretto per OnExecutionUpdate
      protected override void OnExecutionUpdate(Execution execution, Order order, double quantity, double price,
      MarketPosition position, string executionId)
      {
      // Controlla se l'esecuzione è avvenuta sull'account SIM
      if (execution.Account == simAccount)
      {
      Order oppositeOrder = null;

      // Se è un ordine long sul conto SIM, crea un ordine short sul conto reale
      if (position == MarketPosition.Long)
      {
      oppositeOrder = realAccount.CreateOrder(
      Instrument,
      OrderAction.SellShort,
      OrderType.Market,
      OrderEntry.Automated,
      TimeInForce.Day,
      (int)quantity,
      0, // No limit price for Market order
      0, // No stop price for Market order
      null, // OCO ID (non usato in questo caso)
      "ShortOrder", // Nome dell'ordine
      Core.Globals.MaxDate, // GTD, non utilizzato
      null); // Custom order, null in questo caso
      }
      // Se è un ordine short sul conto SIM, crea un ordine long sul conto reale
      else if (position == MarketPosition.Short)
      {
      oppositeOrder = realAccount.CreateOrder(
      Instrument,
      OrderAction.Buy,
      OrderType.Market,
      OrderEntry.Automated,
      TimeInForce.Day,
      (int)quantity,
      0, // No limit price for Market order
      0, // No stop price for Market order
      null, // OCO ID (non usato in questo caso)
      "BuyOrder", // Nome dell'ordine
      Core.Globals.MaxDate, // GTD, non utilizzato
      null); // Custom order, null in questo caso
      }

      // Invia l'ordine se è stato creato
      if (oppositeOrder != null)
      {
      realAccount.Submit(new[] { oppositeOrder });
      }
      }
      }
      }
      }

      Comment


        #4
        Hello scalper09092024,

        The override method signature for OnExecutionUpdate() is:

        protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)

        This must match the help guide exactly.


        Where did you get this incorrect overload?
        protected override void OnExecutionUpdate(Execution execution, Order order, double quantity, double price, MarketPosition position, string executionId)​
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 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
        65 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