I am trying to code the Trailing stop strategy seen in the photo and cannot seem to get it to work. Any have an idea how I would code this?
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
Complex Stop Strategy
Collapse
X
-
Complex Stop Strategy
Hey guys,
I am trying to code the Trailing stop strategy seen in the photo and cannot seem to get it to work. Any have an idea how I would code this?
1 PhotoTags: None
-
Hello Jonathan.Lee,
Thanks for your post.
The screenshot shows an initial stop of 20 ticks, so you would set the initial stoploss at 20 ticks.
When the current price is 10 ticks greater than the entry price, you would begin trailing the current price by 20 ticks. For each tick of profit after the 10 ticks, the trailing stop will be moved 1 tick closer.
In Ninjascript, prior to the entry, you would use SetStopLoss(CalculationMode.Ticks, 20);
Then you would need to create logic to compare the current price to the Position.AveragePrice (this would be the actual entry price if using single contract). When the current price is greater than or equal to the Position.Average+ 10 * TickSize you would then move the SetStopLoss using CalculationMode.Price to 20 ticks behind the current price. You would continue monitoring price to the stop level and continue adjusting as the trade continues into profit. You would need to use additional logic so that you do not move the stop backward (unless you want to).
In order for the stop to be moved correctly, your strategy would need to run with Calculate.OnPriceChange or Calculate.OnEachTick.
Here are references I used above that you should review if you are not familiar with them:
Paul H.NinjaTrader Customer Service
-
Sorry I am still a little confused. Where can I find an example of using Calculate.OnEachTick with an ATM strategy.
Comment
-
Hello Jonathan.Lee,
Thanks for your reply.
In your initial post you wrote, "I am trying to code..." and " Any have an idea how I would code this?"
My reply was based on that you were writing code in Ninjascript and wanted to replicate the ATM stop strategy displayed.
Can you clarify exactly what you are trying to accomplish?
Paul H.NinjaTrader Customer Service
Comment
-
I am trying to create that ATM Strategy shown in the photo in this 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 ATO : Strategy
{
private int HighestBarsAgo;
private int LowestBarsAgo;
private double HighestPrice;
private double LowestPrice;
private bool AtoTraded;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "ATO";
Calculate = Calculate.OnBarClose;
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 = 7;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
LookBackPeriod = 15;
TraceOrders = false;
HighestBarsAgo = 1;
LowestBarsAgo = 1;
HighestPrice = 1;
LowestPrice = 1;
AtoTraded = false;
}
else if (State == State.Configure)
{
SetStopLoss(CalculationMode.Ticks, 20);
//SetProfitTarget(CalculationMode.Ticks, 12);
//SetTrailStop(CalculationMode.Ticks, 20);
AddChartIndicator(ATR(4));
}
}
protected override void OnBarUpdate()
{
//Add your custom strategy logic here.
if (BarsInProgress != 0)
return;
// set initial highest/lowest price to first bar of session so we have a baseline
if(Bars.IsFirstBarOfSession)
{
HighestPrice = High[0];
LowestPrice = Low[0];
AtoTraded = false;
Print("AtoTraded value is now" + " " + AtoTraded + " " + Time[0].ToString() );
}
if (CurrentBars[0] < LookBackPeriod)
return;
if (ToTime(Time[0]) >= 62000 && ToTime(Time[0]) <= 65000)
{
HighestPrice = MAX(High, 7)[0];
Print("The current MAX value is " + HighestPrice.ToString() + " " + Time[0].ToString());
LowestPrice = MIN(Low, 7)[0];
Print("The current MIN value is " + LowestPrice.ToString() + " " + Time[0].ToString());
Print("ATR is" + ATR(4)[0]);
}
// Set 2
if (ToTime(Time[0]) >= 65001 && ToTime(Time[0]) <= 82500 && Close[0] > HighestPrice && AtoTraded == false)
{
EnterLongLimit(HighestPrice, "Enter ATO Long");
Print("ATO Long " + HighestPrice.ToString() + " " + Time[0].ToString());
AtoTraded = true;
Print("AtoTraded value is now" + " " + AtoTraded + " " + Time[0].ToString());
Print("The strategy has taken " + SystemPerformance.LongTrades.Count + " Long trades.");
//Print("Gross profit is: " + SystemPerformance.AllTrades.TradesPerformance.Gros sProfit);
Print("ATR is" + ATR(4)[0]);
}
if (ToTime(Time[0]) >= 65001 && ToTime(Time[0]) <= 82500 && Close[0] < LowestPrice && AtoTraded == false)
{
EnterShortLimit(LowestPrice, "Enter ATO Short");
Print("ATO Short " + LowestPrice.ToString() + " " + Time[0].ToString());
AtoTraded = true;
Print("AtoTraded value is now" + " " + AtoTraded + " " + Time[0].ToString());
Print("The strategy has taken " + SystemPerformance.ShortTrades.Count + " short trades.");
//Print("Gross profit is: " + SystemPerformance.AllTrades.TradesPerformance.Gros sProfit);
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="LookBackPeriod", Order=1, GroupName="Parameters")]
public int LookBackPeriod
{ get; set; }
#endregion
}
}
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by brunoviveiros, 02-17-2020, 05:31 AM
|
4 responses
59 views
0 likes
|
Last Post Today, 06:25 AM | ||
Started by volIQ, 03-27-2024, 04:28 PM
|
3 responses
21 views
0 likes
|
Last Post
by RJay
Today, 05:41 AM
|
||
Started by Nickgiaco, 02-26-2024, 10:23 AM
|
2 responses
72 views
0 likes
|
Last Post Today, 05:12 AM | ||
Started by Johndc, Today, 04:39 AM
|
0 responses
7 views
0 likes
|
Last Post
by Johndc
Today, 04:39 AM
|
||
Started by johntraderuser2, Today, 02:34 AM
|
0 responses
10 views
0 likes
|
Last Post Today, 02:34 AM |
Comment