Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
How do I submit an order in NinjaScript to use a selected ATM Strategy?
Collapse
X
-
How do I submit an order in NinjaScript to use a selected ATM Strategy?
I have an ATM Strategy that has a target, stop loss and moves the stop loss after a certain amount of ticks in profit. I want to be able to use a strategy created with NinjaScript to run that ATM Strategy when the strategy triggers an order. How do I go about doing that?Tags: None
-
Actually, it seems like the "Algorithmic Trading with NinjaTrader" course on Udemy has a section on this. It seems to be working so far.
-
Here is a template in case anyone is interested.
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.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 _ATMStrat : Strategy { #region Variables private string atmStrategyId; private string atmStrategyOrderId; private bool isAtmStrategyCreated = false; private bool entryLong; private bool entryShort; #endregion #region Properties [NinjaScriptProperty] [Display(Name ="ATM Template Name", Description="The name of the custom ATM Strategy.", Order = 1, GroupName = "ATM Strat")] public string ATMTemplateName {get; set;} #endregion protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "_ATMStrat"; Calculate = Calculate.OnEachTick; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; 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.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; ATMTemplateName = "ATMStratName"; } else if (State == State.Configure) { } } protected override void OnBarUpdate() { if (CurrentBar < BarsRequiredToTrade) return; if (State == State.Historical) return; // Checking for ATM Strategy will fail if not in real time. if (State != State.Realtime) return; entryLong = Close[0] > Close[1]; entryShort = Close[0] < Close[1]; if (AtmIsFlat()) { if (entryLong) { atmStrategyId = GetAtmStrategyUniqueId(); atmStrategyOrderId = GetAtmStrategyUniqueId(); AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, ATMTemplateName, atmStrategyId, (atmCallbackErrorCode, atmCallbackId) => { if (atmCallbackId == atmStrategyId) { if (atmCallbackErrorCode == Cbi.ErrorCode.NoError) { isAtmStrategyCreated = true; } } }); } else if (entryShort) { atmStrategyId = GetAtmStrategyUniqueId(); atmStrategyOrderId = GetAtmStrategyUniqueId(); AtmStrategyCreate(OrderAction.SellShort, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, ATMTemplateName, atmStrategyId, (atmCallbackErrorCode, atmCallbackId) => { if (atmCallbackId == atmStrategyId) { if (atmCallbackErrorCode == Cbi.ErrorCode.NoError) { isAtmStrategyCreated = true; } } }); } } if (isAtmStrategyCreated) { // Do stuff... } if (!isAtmStrategyCreated) { // Do stuff for failed ATM Strategy } } private bool AtmIsFlat() { if (atmStrategyId == null) return true; return GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat; } } }
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by algospoke, Yesterday, 06:40 PM
|
2 responses
19 views
0 likes
|
Last Post
by algospoke
Today, 07:04 PM
|
||
Started by ghoul, Today, 06:02 PM
|
3 responses
14 views
0 likes
|
Last Post Today, 06:43 PM | ||
Started by jeronymite, 04-12-2024, 04:26 PM
|
3 responses
45 views
0 likes
|
Last Post
by jeronymite
Yesterday, 10:10 PM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
7 responses
20 views
0 likes
|
Last Post Today, 06:16 PM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
10 responses
181 views
0 likes
|
Last Post
by jeronymite
Today, 06:11 PM
|
Comment