Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Flatten everything once unrealized P/L reaches a certain number
Is it possible to automatically set NT8 to flatten all of my account positions once the unrealized P/L reaches a certain $ value? And if so, does the platform need to continue running on the computer?
Thank you!
Can you help answer these questions from other members on NexusFi?
It's possible in creating a strategy where its only goal would be to check the PnL of all your open positions and close them if the $ value is reached.
Could you please clarify on how this would be done? When I looked into it, it was for one particular instrument. I have more than one instrument running at the same time. It seems like a simple solution - I'd appreciate the help.
You can use Account.positions. With this you can see the positions for all instruments, not only the one you're running the "fake" strategy on.
I'm not sure we can say it's a simple solution but that's the only one I have in mind.
I'm not sure I'm doing this correctly. Could you assist please?
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class accpositions : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "accpositions";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
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.ByStrategyPosition;
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)
{
AddDataSeries(Data.BarsPeriodType.Minute, 1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 100)
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}
I found "unrealized P/L" - greater or equal to "numeric value", and if met, "exit long pos" "exit short pos". All of this running on "default instrument" at the start.
It's not that simple, a strategy will only see its own instrument and its own PnL.
It should be something like this:
Account myAccount; In OnStateChange() -> configure:
lock (Account.All)
myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
In OnBarUpdate():
double PnL = 0;
foreach (Position position in myAccount.Positions) {
PnL+= position.GetUnrealizedProfitLoss(PerformanceUnit.Currency);
}
if (PnL > 1000) ....find the instruments/quantity/side of the open positions then close them with CreateOrder()
For this specific need it's going to be too long to explain step by step, and if you're not experienced this kind of strategy is a bit dangerous.
If I have some free time I'll put a functional example in this thread.