Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Two independent strategies on the same futures contract

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

  • bltdavid
    replied
    Originally posted by bobperez View Post
    On the same line of thought, I need to control the "Market.Position" for both strategy instances from an external "Account Manager" to avoid conflicts such as a short on chart 1 and a long on chart 2. How do you suggest this may be done?
    If both strategies instances are trading the same instrument,
    you should really use two separate accounts.

    -=o=-

    Otherwise, I'd suggest a lock file.

    In order for either strategy to enter a trade, it must
    first acquire the lock.

    If a strategy fails to acquire the lock, then it does not
    enter the trade, and the setup is abandoned (because
    the other strategy is busy trading in this account).

    When a strategy becomes flat, it must release the
    lock file.

    -=o=-

    A lock file is advanced programming, and unless you
    know C# well, it may be a lot easier to just use two
    different accounts.

    Leave a comment:


  • bltdavid
    replied
    Originally posted by bobperez View Post
    May I ask you for a simple example of how you do this with an Ini file and the call to it from the strategies?
    How would the user define the MaxAcctDailyLoss = ??? to suit his/her needs?
    You have some kind of global bool variable, named something like OkToTrade, right?
    Your strategy should only be allowed to submit orders when OkToTrade is true.

    -- Step 1 --

    Create a text file in your 'NinjaTrader 8' folder.
    Let's name the text file 'MySettings.ini'.

    Now, add these lines to the text file,

    [Settings]
    MaxAcctDailyLoss = 300


    -- Step 2 --

    In every strategy, add this using statement,

    using System.Runtime.InteropServices;

    -- Step 3 --

    In every strategy, add ​these lines,

    [DllImport("kernel32.dll", EntryPoint="GetPrivateProfileIntA", CharSet=CharSet.Ansi)]
    public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefVal, string lpFileName);


    ​-- Step 4 --

    And finally, use something like this code when you want to
    check your account's realized PnL against your account
    daily maximum loss value ...

    Code:
    protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
    {
        if (accountItem == AccountItem.GrossRealizedProfitLoss)
        {
            int MaxAcctDailyLoss = GetPrivateProfileInt("Settings", "MaxAcctDailyLoss", 0,
                                               Core.Globals.UserDataDir+"MySettings.ini");
            if (MaxAcctDailyLoss > 0 && Math.Abs(value) >= MaxAcctDailyLoss)
                OkToTrade = false;
        }
    }
    ​
    You'll probably want to stop/start the strategy every day in order to
    reset the realized PnL back to 0, and reset OkToTrade back to true.

    Originally posted by bobperez View Post
    ​How would the Ini file or any other external file recognize if the Market Position is Long/Short/Flat?
    I don't understand why you'd ask that question.

    Why would you think your external configuration file would need
    to know the Market Position of the strategy?

    Your external config file is not code ... it's just a text file, it is
    not possible for your configuration file to 'recognize' anything
    about any aspect of any strategy, running or not.

    -=o=-

    There are lots of ways to read an Ini file.

    Google more about Windows Ini file format here.

    You'll probably want to know more about DllImport and GetPrivateProfileInt.

    Make sense?

    Leave a comment:


  • bobperez
    replied
    Originally posted by bltdavid View Post

    Well, uh, hmm, in my opinion, uh, you don't do this with property
    grid parameters.

    You're building a family of strategies that need to cooperate on
    a common set of some family-based parameters, right?

    I'd say use an external text based configuration file.

    Anything will do, XML, or JSON, or an Ini file, or something home
    grown. The Ini file is pretty darn simple, and is easy to maintain
    and update by hand. That's what I use.

    I'd suggest using an Ini file, then make each strategy read the Ini
    file to get the MaxAcctDailyLoss=300 parameter ... all strats read
    and share the same Ini config file.

    The idea is:
    A single point of control (the text based config file) is the simplest
    way to guarantee all strategies are running with the same set of
    'common' values.
    Bltdavid,

    May I ask you for a simple example of how you do this with an Ini file and the call to it from the strategies? How would the user define the MaxAcctDailyLoss = ??? to suit his/her needs? How would the Ini file or any other external file recognize if the Market Position is Long/Short/Flat?

    Bobperez
    Last edited by bobperez; 07-17-2023, 03:19 PM.

    Leave a comment:


  • bobperez
    replied
    Thank you Bltdavid and Emily.

    On the same line of thought, I need to control the "Market.Position" for both strategy instances from an external "Account Manager" to avoid conflicts such as a short on chart 1 and a long on chart 2. How do you suggest this may be done?
    BobPerez

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Hello bobperez,

    Thank you for your reply.

    The parameters of each strategy instance act independently from one another. That said, you might be able to do something such as have the strategy logic use StreamReader to read a value from a text file; all strategy instances could refer to the same value from the same text file. Here is an example of using StreamReader to read from a text file:


    Please let us know if we may be of further assistance.

    Leave a comment:


  • bltdavid
    replied
    Originally posted by bobperez View Post
    How can I provide the user with one Parameters window to define the account limits only once and not on each strategy instance?
    Well, uh, hmm, in my opinion, uh, you don't do this with property
    grid parameters.

    You're building a family of strategies that need to cooperate on
    a common set of some family-based parameters, right?

    I'd say use an external text based configuration file.

    Anything will do, XML, or JSON, or an Ini file, or something home
    grown. The Ini file is pretty darn simple, and is easy to maintain
    and update by hand. That's what I use.

    I'd suggest using an Ini file, then make each strategy read the Ini
    file to get the MaxAcctDailyLoss=300 parameter ... all strats read
    and share the same Ini config file.

    The idea is:
    A single point of control (the text based config file) is the simplest
    way to guarantee all strategies are running with the same set of
    'common' values.
    Last edited by bltdavid; 07-17-2023, 01:13 PM.

    Leave a comment:


  • bobperez
    replied
    Emily,

    How can I provide the user with one Parameters window to define the account limits only once and not on each strategy instance?

    Leave a comment:


  • bobperez
    replied
    Originally posted by NinjaTrader_Emily View Post

    Hello Bobperez,

    Thank you for your inquiry.

    If you would like information about the account's profit and loss information, you could consider adding the OnAccountItemUpdate() method to your strategy logic. This would allow you to get AccountItem information from the account the strategy instances are running on. They could check, and if an item such as GrossRealizedProfitLoss hits a certain value, you could call CloseStrategy() to full stop and disable the strategy. Otherwise, if you don't want the strategy to be disabled, you could try something like a bool that allows for trading, then when the GrossRealizedProfitLoss hits a certain value you could toggle that bool so trades are not submitted anymore.

    For more information, please see the following pages:Please feel free to reach out with any additional questions or concerns.
    Ok, I'll try that. Thank you! If you have an example, please include the link here.

    BobPerez
    Last edited by bobperez; 07-17-2023, 11:17 AM.

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Originally posted by bobperez View Post

    Hi Chelsea,
    I am running a strategy on different instruments (a unique chart for each) on the same account. How can I control the Daily Loss/Profit for the Account the strategies are running on?

    For example, let's assume I have 2 charts, one for each instrument: RTY, CL. Both run the same strategy on the same APEX account. I want to limit the Account's Daily Loss to $300. Say RTY lost $200 and CL lost $150. How can code into my ninjascript strategy that it need to stop trading on both charts?

    Thanks,

    Bobperez
    Hello Bobperez,

    Thank you for your inquiry.

    If you would like information about the account's profit and loss information, you could consider adding the OnAccountItemUpdate() method to your strategy logic. This would allow you to get AccountItem information from the account the strategy instances are running on. They could check, and if an item such as GrossRealizedProfitLoss hits a certain value, you could call CloseStrategy() to full stop and disable the strategy. Otherwise, if you don't want the strategy to be disabled, you could try something like a bool that allows for trading, then when the GrossRealizedProfitLoss hits a certain value you could toggle that bool so trades are not submitted anymore.

    For more information, please see the following pages:Please feel free to reach out with any additional questions or concerns.

    Leave a comment:


  • bobperez
    replied
    Originally posted by NinjaTrader_ChelseaB View Post
    Hello yDiamond,

    When trading the same instrument on the same account, the performance of each trade on the Account Performance tab of the Control Center would match in the order the orders are received.

    The position for the account and the position for the strategy are separate. It is possible for the position of a strategy to be out of sync with the position of an account.

    From the help guide:
    "Note: Please be aware that these options will only help you sync your Account Position to your Strategy Position once on startup. These options will not guarantee your Account Position remains in sync afterward. Any active orders you may have had on your account prior to strategy start that was not generated by your strategy would not have been cancelled on start and can lead to your Account Position being out of sync from your Strategy Position. Placing manual trades or running multiple strategies on the same instrument can also lead to your Account Position being out of sync from your Strategy Position."

    Below are links to the NinjaTrader 7 and NinjaTrader 8 help guides on this.
    https://ninjatrader.com/support/help..._positions.htm

    https://ninjatrader.com/support/help..._positions.htm


    Using two NinjaScript Strategies that are placing orders in opposite directions would also be affected by this. The Strategy Position is separate from the Account Position. This means that if one strategy has taken a long position and is showing long 1, and the other has taken a short position and is showing short 1, the account position would be flat and neither strategy would be in sync with the account.

    Further, NinjaTrader does not support hedging.

    All links below are publicly available.

    Below is a public link to a video that demonstrates this for NinjaTrader 7.
    https://www.youtube.com/watch?v=US9c...A14C398CA140D7

    And a link to a video that demonstrates this for NinjaTrader 8.
    World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


    Showing an atm strategy with a long position on one DOM or Chart and a short position on another DOM or Chart on the same instrument and account by setting 'ATM Strategy Selection Mode' to 'Display Selected ATM Strategy Only' does not result in two separate account positions on the same instrument. In this situation the account is flat.

    However, this does allow to see the working orders, position, and performance for each active Atm instance separately. These would appear as a virtual long and short position with working stops and targets.

    Below is a public link to a video on this for NinjaTrader 7.


    And a public link to a video on this for NinjaTrader 8.


    As well as links to the help guide.
    https://ninjatrader.com/support/help...ction_mode.htm


    If one order takes the account position long as an entry and places a stop and target with an Atm, then on a second chart a sell order is placed as an entry with an atm and this fills, the account will be flat but the Atm Strategies will work separately (with 'Display Selected ATM Strategy Only' selected). It will appear that the positions are separate as only the selected Atm is visible on that Chart or DOM, however, the account position is flat. Then the target of the first order may fill and this will take the account into a short position with the atm of the second entry remaining. Once the Atm of the second position fills the account is back to flat again.

    In the account performance the trades are matched with the entry to the entry and the exit to the exit (as this is the sequence that NinjaTrader sees as the actual trades were filled in with First In First Out).

    Instead, we visually show you the performance of this one open trade without showing the performance of other trades running different instances of Atm Strategies. Showing this is the equivalent of multiple positions (without accounting for leverage or loss).. Being long 1 and short 1 is the same as being flat. This works the same as when you call your broker on the phone. You can imagine virtual positions taken from different events and try and put the profit and loss between arbitrary orders together if you would like. But with your broker it's First In First out. Meaning any reporting in NinjaTrader is based on when we have indications of when an order was submitted, became working, and was filled.

    We have very sophisticated ways of connecting orders based on signal name to assist with creating the performance reports, but its still based on what is marked as an entry and what is marked as an exit and at what time and what what price..

    In NinjaScript we also try and match these orders based on signal name and from entry signal.

    Below are links to the help guide on the Trade Performance window for account performance.
    https://ninjatrader.com/support/help...rmance_tab.htm
    https://ninjatrader.com/support/help...erformance.htm

    And viewing the Strategy Performance window for the individual instance of the strategy's performance.
    https://ninjatrader.com/support/help...ed_strateg.htm
    https://ninjatrader.com/support/help...egies_tab2.htm
    https://ninjatrader.com/support/help...egyPerformance
    https://ninjatrader.com/support/help...egyPerformance


    To truly have an open long account position and an open short account position on the same instrument (instead of a net flat account position), this would require placing trades to two different accounts. One account to hold a long position, the other account to hold a short position.

    In NinjaTrader it is possible to create addition Sim accounts to use for practice trading this scenario, however, with NinjaTrader 7 these would require a live license key to utilize as it does require Global Simulation Mode to be disabled so that the Account drop-down is not grayed (disabled) on all trade windows.

    With NinjaTrader 8 this would not require a live license key to utilize and can be done with the Free Simulation License. NinjaTrader 8 allows for additional Sim accounts to be selected even when Global Simulation Mode is enabled.

    To add additional Sim accounts in NinjaTrader 7:
    • Disconnect from all connections
    • Ensure a Live license key is entered in Help -> License Key...
    • Click Tools -> Options... -> Simulator -> Accounts...
    • Type a new name for the new account (for example Sim102)
    • Click OK
    • Reconnect to your connection -> disable Global Simulation Mode
    • Select the new account in the Account drop-down of Chart Trader, SuperDOM, or other trade window
    http://ninjatrader.com/support/helpG...n_accounts.htm
    http://ninjatrader.com/support/helpG...ulator_tab.htm

    To add addition Sim accounts in NinjaTrader 8:
    • On the Control Center select the Accounts tab
    • Right-click the grid in the Accounts tab -> select Add Simulation Account
    • Type a name for the new account in the Name field if desired
    • Click OK
    https://ninjatrader.com/support/help...n_accounts.htm
    http://ninjatrader.com/support/helpG...TheAccountsTab

    But with all of that said, most hedging (going long and short at the same time) is based on risk and margin. Sometimes with a broker its more expensive to take short positions than long positions and you can incur larger losses. Some brokerages, such as NinjaTrader Brokerage, do not allow hedging with multiple accounts.

    But many are wanting to just keep track of what would have happened if you had taken the opposite trade at the same time. And in that case, its absolutely helpful to have multiple accounts (sim, live demo, or actual funded brokerage account).
    Hi Chelsea,
    I am running a strategy on different instruments (a unique chart for each) on the same account. How can I control the Daily Loss/Profit for the Account the strategies are running on?

    For example, let's assume I have 2 charts, one for each instrument: RTY, CL. Both run the same strategy on the same APEX account. I want to limit the Account's Daily Loss to $300. Say RTY lost $200 and CL lost $150. How can code into my ninjascript strategy that it need to stop trading on both charts?

    Thanks,

    Bobperez

    Leave a comment:


  • NinjaTrader_Kate
    replied
    Hello crmtrader,

    Thank you for your reply.

    Two separate accounts would be the only way to keep the positions completely independent, that is correct.

    Please let us know if we may be of further assistance to you.

    Leave a comment:


  • crmtrader
    replied
    I'm interested in trading multiple positions on the same contract as independent trades. Each with its profit target, scale outs, and stop loss and independently of the order in which the positions were taken. Is trading each position on a different account the only way to do it or is there any other way to trade multiple independent positions. Since default NT8 trade management is FIFO I can't do it right now.

    Thank you

    Leave a comment:


  • NinjaTrader_Kate
    replied
    Hello Apo84,

    Thank you for your reply.

    Yes, that is correct.

    Please let us know if we may be of further assistance to you.

    Leave a comment:


  • Apo84
    replied
    Thanks for the answers.

    So in summary, with ninjatrader I can create multiple simulation accounts (for example one for long positions and one for short positions).
    To trade instead I need to ask my broker for the possibility of using two separate accounts which I will probably have to finance separately.

    It's correct?
    Thanks

    Leave a comment:


  • bltdavid
    replied
    Originally posted by Apo84 View Post
    It is possible with a real account (real account = real live transactions) to have one account for LONG positions and one for SHORT positions.
    You should re-read post #2 very carefully.

    It says this,
    "To truly have an open long account position and an open short account position on the same instrument (instead of a net flat account position), this would require placing trades to two different accounts. One account to hold a long position, the other account to hold a short position."

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by CarlTrading, 05-11-2026, 05:56 AM
0 responses
52 views
0 likes
Last Post CarlTrading  
Started by CarlTrading, 05-10-2026, 08:12 PM
0 responses
30 views
0 likes
Last Post CarlTrading  
Started by Hwop38, 05-04-2026, 07:02 PM
0 responses
194 views
0 likes
Last Post Hwop38
by Hwop38
 
Started by CaptainJack, 04-24-2026, 11:07 PM
0 responses
355 views
0 likes
Last Post CaptainJack  
Started by Mindset, 04-21-2026, 06:46 AM
0 responses
274 views
0 likes
Last Post Mindset
by Mindset
 
Working...
X