Example, if I go long +1 ES, then go short -10 MES. And enable the indicator to set hedge amount and TP and SL levels for the position.
I tried some on the Script Editor but getting some errors.
Full code:
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.DrawingTools;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class Hedger : Indicator
{
// Define variables for selecting instruments and contract size
private string instrument1 = "ES"; // Enter the symbol of the first instrument
private string instrument2 = "NQ"; // Enter the symbol of the second instrument
private int contractSize = 1; // Enter the contract size for both instruments
// Initialize the opposite trade flag to false
private bool oppositeTrade = false;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "Hedger";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
// Check if a trade has been placed in instrument1
if (Position.MarketPosition != MarketPosition.Flat && Position.Instrument.FullName == Instrument.FullName && !oppositeTrade)
{
// Calculate the opposite trade quantity for instrument2
int oppositeQuantity = -(int)(Position.Quantity * Instrument.MasterInstrument.PointValue / Instrument.MasterInstrument.TickSize * contractSize);
// Place an opposite trade in instrument2
int EnterShort(instrument2, oppositeQuantity, "Opposite Trade");
// Set the opposite trade flag to true
oppositeTrade = true;
}
// Reset the opposite trade flag when the position in instrument1 is closed
else if (Position.MarketPosition == MarketPosition.Flat && oppositeTrade)
{
oppositeTrade = false;
}
}
}
}
region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private Hedger[] cacheHedger;
public Hedger Hedger()
{
return Hedger(Input);
}
public Hedger Hedger(ISeries<double> input)
{
if (cacheHedger != null)
for (int idx = 0; idx < cacheHedger.Length; idx++)
if (cacheHedger[idx] != null && cacheHedger[idx].EqualsInput(input))
return cacheHedger[idx];
return CacheIndicator<Hedger>(new Hedger(), input, ref cacheHedger);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.Hedger Hedger()
{
return indicator.Hedger(Input);
}
public Indicators.Hedger Hedger(ISeries<double> input )
{
return indicator.Hedger(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.Hedger Hedger()
{
return indicator.Hedger(Input);
}
public Indicators.Hedger Hedger(ISeries<double> input )
{
return indicator.Hedger(input);
}
}
}
#endregion

Comment