View Full Version : Plotting my trailstop on chart
kcsystemtrader
01-08-2009, 08:19 PM
Been reading through old forum posts and reference samples and can't seem to find a good example of how to plot the price of my trailing stop loss on my chart.
It doesn't have to adjust the stop as it gets adjusted...I'd be happy with just a dot or line at the intial stop level. I've looked at the "SampleMonitorStopProfit" Reference sample and hoping I don't have to go that route.
Would a simple DrawDot or DrawLine statement work if I place it right next to my entry code? I have a calculated variable "TrailStop" that holds a value in ticks.
I tried the following, but nothing showed up on the chart. Basically I was trying to plot a horizontal line at a level of the high of the most recent bar minus my Trailstop value, and plot the line for 15 bars.
DrawLine("Buy", true, 1, High[0] - TrailStop, 15, High[0] - TrailStop, Color.Red, DashStyle.Dot, 2);
Syntax for reference from helpfile:
DrawLine(string tag, bool autoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Color color, DashStyle dashStyle, int width)
I guess it would be nice if it would plot dots on the chart as the trail stop order is dynamically updated as the market moves. But for starters, just drawing horizontal line at a specific y value on the chart is all I'm trying to accomplish. Is there is a better way to do this? Any help with the code for this is appreciated. Thanks,
kc
NinjaTrader_Bertrand
01-09-2009, 06:05 AM
Hi kcsystemtrader, you have to consider, if you want a separate line to show up for all your trailing stop levels, please use a unique tag for the DrawText statement, like "Buy" + CurrentBar.
You could also create a variable like myStopValue that tracks your stop modifications and then you could plot this from the strategy with the help of this tip - http://www.ninjatrader-support2.com/vb/showthread.php?t=6651
kcsystemtrader
01-09-2009, 08:41 AM
Hi kcsystemtrader, you have to consider, if you want a separate line to show up for all your trailing stop levels, please use a unique tag for the DrawText statement, like "Buy" + CurrentBar.
You could also create a variable like myStopValue that tracks your stop modifications and then you could plot this from the strategy with the help of this tip - http://www.ninjatrader-support2.com/vb/showthread.php?t=6651
I tried...
Initialize:
Add(StrategyPlot(0));
StrategyPlot(0).Plots[0].Pen.Color = Color.Red;
StrategyPlot(0).PanelUI = 1;
OnBarUpdate:
StrategyPlot(0).Value.Set(TrailStop);
And nothing happened. Also tried the following in OnBarUpdate with no luck:
DrawLine("Test" + CurrentBar, true, 1, High[0] - TrailStop, 15, High[0] - TrailStop, Color.Red, DashStyle.Dot, 2);
What am I missing? Thanks
NinjaTrader_Bertrand
01-09-2009, 08:56 AM
Hi kcsystemtrader, this looks good if your 'TrailStop' variable holds the needed values for the adjusted trailing stop.
Do you get any errors in the Log tab of the Control Center when running your code?
kcsystemtrader
01-09-2009, 09:11 AM
Hi kcsystemtrader, this looks good if your 'TrailStop' variable holds the needed values for the adjusted trailing stop.
Do you get any errors in the Log tab of the Control Center when running your code?
Which one looks good, the StrategyPlot code or the DrawLine code? I am not running a multi-time frame strategy so I modified the StrategyPlot code that was in the reference sample. TrailStop is a double and it is calculated continuously until right before entry. Once a position is entered it holds the TrailStop value in Ticks. Upon exit, it resets the TrailStop and dynamic calculations for new TrailStop values begin again. I had the StrategyPlot code outside the entry condition code thinking it would always be plotting my TrailStop value, whether in a position or not. Only difference would be once in a position, the TrailStop line that would draw on the chart would become static until the position is exited and the calculation logic resumes.
Did not see any errors in the Log window. I'll keep fooling with the StrategyPlot code because I think that would be better than just a DrawLine. I had the DrawLine code within the entry condition statement so that it would only draw a horizontal line once for a length of 15 bars upon entry. Thanks,
kc
NinjaTrader_Bertrand
01-09-2009, 09:37 AM
Hi kcsystemtrader, it would be great if you could post more code for us, I think the StrategyPlot would be best here for the trail stop display...do you use SetStopLoss and adjust it, or do you use SetTrailStop?
Please also take a look at this thread, where a trailing stop code was pusblished - http://www.ninjatrader-support2.com/vb/showthread.php?t=10344
kcsystemtrader
01-09-2009, 10:42 AM
Here is the strategy I am using for testing on 1 minute bars:
publicclass TestPlotStrategy : Strategy
{
#region Variables
int ATRPeriod = 20;
double ATRvalue;
double TrailStop;
int ATRMultiple = 1;
bool definedTrailStop = false;
#endregion
protectedoverridevoid Initialize()
{
Add(ATR(ATRPeriod));
Add(StrategyPlot(0));
StrategyPlot(0).Plots[0].Pen.Color = Color.Red;
StrategyPlot(0).PanelUI = 1;
TraceOrders = true;
CalculateOnBarClose = false;
}
protectedoverridevoid OnBarUpdate()
{
//Reset TrailStop calculation upon closing out a position
if (Position.MarketPosition == MarketPosition.Flat)
definedTrailStop = false;
{
// Calculations For Stop Loss
if (definedTrailStop == false)
{
ATRvalue = ATR(ATRPeriod)[0] * 100; //convert to ticks
Print("The current ATR value is " + ATRvalue.ToString());
TrailStop = ATRvalue * ATRMultiple;
Print("The current TrailStop value is " + TrailStop.ToString());
definedTrailStop = true;
}
Print("The current value for BarsSinceExit is " + BarsSinceExit().ToString());
StrategyPlot(0).Value.Set(TrailStop);
//Entry Conditions
// Long Entry Condition
if ((BarsSinceExit() >= 1 || BarsSinceExit() == -1)
&& Position.MarketPosition != MarketPosition.Short)
{
if (Close[0] < Close[1])
{
SetTrailStop("Buy", CalculationMode.Ticks, TrailStop, false);
EnterLong(100,"Buy");
//The next DrawLine statement was also tested without luck
//DrawLine("Buy" + CurrentBar, true, 1, High[0] - TrailStop, 15, High[0] - TrailStop, Color.Red, DashStyle.Dot, 2);
}
}
// Short Entry Condition
if ((BarsSinceExit() >= 1 || BarsSinceExit() == -1)
&& Position.MarketPosition != MarketPosition.Long)
{
if (Close[0] > Close[1])
{
SetTrailStop("Sell", CalculationMode.Ticks, TrailStop, false);
EnterShort(100,"Sell");
}
}
}
}
#region Properties
#endregion
}
NinjaTrader_Bertrand
01-09-2009, 11:26 AM
Hi kcsystemtrader, thanks for posting the code. You calculate the trailing stop value in ticks, which is fine for usage in the SetTrailStop method. To display it on the chart however alongside your strategy, you would need to get the trailing stop price value with the correct scaling. You could do this by calculating it (Position.AvgPrice +/- Trailstop in ticks), plot this value and use to update your SetStopLoss order.
kcsystemtrader
01-09-2009, 12:03 PM
Hi kcsystemtrader, thanks for posting the code. You calculate the trailing stop value in ticks, which is fine for usage in the SetTrailStop method. To display it on the chart however alongside your strategy, you would need to get the trailing stop price value with the correct scaling. You could do this by calculating it (Position.AvgPrice +/- Trailstop in ticks), plot this value and use to update your SetStopLoss order.
Maybe I'm not adding the indicator to the chart correctly. I attached a screenshot of the settings for the Indicator StrategyPlot.
I changed this line:
StrategyPlot(0).Value.Set(TrailStop);
to:
StrategyPlot(0).Value.Set(Position.AvgPrice + TrailStop);
and I'm still not seeing anything on the chart. Any other ideas? Thanks,
kc
NinjaTrader_Josh
01-09-2009, 12:09 PM
kcsystemtrader,
I have not been following this thread intimiately, but have you checked your Control Center logs for errors?
Alternatively, a lot of people trying to do what you are doing just do something like DrawDot() right from the strategy instead of worrying about trying to use StrategyPlot(). StrategyPlot is a limited functionality plotting designed for use with multi-series strategies. It is a workaround for multi-series plotting and has its limitations. For what you are doing I suggest you just use draw objects straight from the strategy. Or you can just create yourself an indicator that uses the same logic as the strategy to properly plot it instead of StrategyPlot.
kcsystemtrader
01-09-2009, 12:42 PM
I can't even get DrawDot to work. I have yet to see any custom markers/lines drawn on a chart.
Is there a strategy or code snippet that is tested that I could import to see what I should be looking for?
kcsystemtrader
01-09-2009, 12:43 PM
Is there anything else I need to add besides just the DrawDot statement in onbarupdate?
Below I'm just trying to draw a red dot 20 ticks below the close
DrawDot("stop", true, 0, Close[0] - (20 * TickSize), Color.Red);
kcsystemtrader
01-09-2009, 12:55 PM
I just tried the following, and still no luck. Any ideas?
#region Variables
double TrailStop = 20;
double TrailStopPlot = .2;
#endregion
protectedoverridevoid Initialize()
{
SetTrailStop("Buy",CalculationMode.Ticks,TrailStop,false);
TraceOrders = true;
CalculateOnBarClose = false;
}
protectedoverridevoid OnBarUpdate()
{
if (Close[0] > Close[1])
EnterLong(100,"Buy");
DrawDot("test dot" + CurrentBar,0,Position.AvgPrice - TrailStopPlot,Color.Red);
}
NinjaTrader_Josh
01-09-2009, 01:20 PM
kcsystemtrader,
It is almost evident that none of your scripts even work. Please try running SampleMACrossOver.
kcsystemtrader
01-09-2009, 02:34 PM
kcsystemtrader,
It is almost evident that none of your scripts even work. Please try running SampleMACrossOver.
Still no luck. Please see attached screenshot of the chart. I ran a modified SampleMACrossOver, the only changes being I added the DrawDot statements to draw dots 20 ticks above and below the Avg Position price. I don't see any dots once a position is established.
Here's the modified code for the OnBarUpdate section of the sample strategy (no changes elsewhere in the strategy). Please see if it works for you. I ran the test on a Market Replay Connection from 1/7 data on AAPL. Thanks
protectedoverridevoid OnBarUpdate()
{
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
EnterLong();
elseif (CrossBelow(SMA(Fast), SMA(Slow), 1))
EnterShort();
//These are the only changes to strategy
DrawDot("test dot",true,0,Position.AvgPrice - 20*TickSize,Color.Red);
DrawDot("test dot",true,0,Position.AvgPrice + 20*TickSize,Color.Red);
}
NinjaTrader_Josh
01-09-2009, 02:41 PM
It does work for me. Hmm. Perhaps your .NET install got corrupted. Please try this.
Backup anything you want to save from NT6.5
Uninstall NT6.5
Uninstall .NET 2.0
Clear internet browser cache
Reinstall .NET 2.0
Reinstall NT6.5
kcsystemtrader
01-09-2009, 05:02 PM
So you ran that exact same strategy and you saw red dots?
I just ran the same strategy on a completely different computer with separate installs of NT and with a separate DB.
Still no dots. Is it possible there is a default setting I am missing here or something I need to change in order for user defined chart objects to show up? I highly doubt that the .NET framework is corrupted on both machines, especially considering everything else in NT appears to be functioning properly. I'd really like to not mess with the install of .NET or NT unless I absolutely have to.
It is not a make or break feature, but I am curious why I can't seem to get it to work. It is just to help visualize the strategy, functionally it has no value as far as the trading strategy is concerned. I'm about to give up on this one...
NinjaTrader_Bertrand
01-10-2009, 10:53 AM
Hi kcsystemtrader, please check the attached file - do you see dots displayed now? Have a good weekend!
kcsystemtrader
01-10-2009, 03:26 PM
Hi kcsystemtrader, please check the attached file - do you see dots displayed now? Have a good weekend!
Unfortunately, same result...see attached Screenshot. When you ran it you saw dots?
Have a good weekend yourself.
NinjaTrader_Bertrand
01-12-2009, 05:14 AM
Your chart shows only the two SMA's applied, did you apply the imported strategy to your AAPL 1min chart? Please reimport it from my post from the weekend and select it with those instructions here - http://www.ninjatrader-support.com/HelpGuideV6/RunningANinjaScriptStrategyFromAChart.html
kcsystemtrader
01-12-2009, 07:58 AM
Your chart shows only the two SMA's applied, did you apply the imported strategy to your AAPL 1min chart? Please reimport it from my post from the weekend and select it with those instructions here - http://www.ninjatrader-support.com/HelpGuideV6/RunningANinjaScriptStrategyFromAChart.html
I knew it was something stupid. It helps when you know that strategy generated objects will only show up on the chart if you run the strategy from the chart. Up until now I have always started strategies from the strategy tab. Thanks for your help...now I can go back and apply it to my strategy and see if I can get it to to work there. Thanks,
kc
NinjaTrader_Bertrand
01-12-2009, 08:07 AM
Hi kc, great you got it working now!
xtrender
02-10-2009, 07:28 AM
Hello,
I am deeeeply confused....
I can not display any drawings (like up arrow) from any strategy on the chart. I have tried to create a simple SMA cross or display any sample strategy. I have strategy applied to the chart with SMA ploted but no arrows or entries? Any ideas?
Thank you.
NinjaTrader_Bertrand
02-10-2009, 07:36 AM
Hi xtrender, do the sample ones provided in this thread work?
Are you using unique drawing tag ID's in your code?
Please post the code you used in your test.
xtrender
02-10-2009, 07:48 AM
Hi Bertrand,
I have orders coming to control center from the chart, but all I want is to see it visualy on the chart?
Thank you.
NinjaTrader_Bertrand
02-10-2009, 08:13 AM
Have you set the Plot executions to 'TextAndMarker'?
xtrender
02-10-2009, 08:40 AM
Since I have no idea of what 'TextAndMarker' is, I have probably not set it.
How should I set this?
And I have one more problem. I have enabled debugging mode in strategies to edit code. I dont know how to disable this feature?
Thank you.
xtrender
02-10-2009, 08:46 AM
OK, I went to chart properties and:D it works! Thank you!
On my second question how to disable debugging mode?
NinjaTrader_Bertrand
02-10-2009, 08:50 AM
Great! Just open your code up in the editor and right click in it. Then uncheck 'Debug mode'.
xtrender
02-10-2009, 11:37 AM
Hello Bertrand,
Thank you for Help!
Is it possible to call custom indicator from another custom indicator?
NinjaTrader_Bertrand
02-10-2009, 12:00 PM
Sure, please check out the second tutorial - http://www.ninjatrader-support.com/HelpGuideV6/Overview18.html
When you create a custom indicator, NinjaScript automatically prepares what other platforms call a function (method in NT), for example SMA is an indicator by itself but you can also call the calculation by using
double mySMA = SMA(Close, 20)[0];
Same goes for your custom coded indicators.
xtrender
02-10-2009, 12:09 PM
My question is how to call custom indicator from custom indicator.
I need to call BBsquese indicator from another indiator.
SMA,MACD,RSI....built in indicators, I know how to reference thouse, but isn't there a dot notation way to reference any indicator imported to ninjatrader? Something like customindicator1.custind2?
NinjaTrader_Bertrand
02-10-2009, 12:15 PM
Just type in your custom indicator name followed by the bracket ' ( ' and then follow the Intellisense guide that pops up on the parameters....
http://www.ninjatrader-support.com/HelpGuideV6/Intellisense.html
xtrender
02-11-2009, 06:14 AM
Thank you Bertrand, that helped a lot.
I am still not sure how to call indicator plots?
I have RSI indicator with Bands and I want to call UpperBand and LowerBand from another indicator
CustomRSI(RSI,14,7).UpperBand
Intellisense detects this indicator as CustomRSI(Dataseries,period,smooth)
This would not compile. What am I doing wrong
NinjaTrader_Bertrand
02-11-2009, 06:21 AM
It expects a DataSeries input like Close or Input.
Try this -
CustomRSI(Close, 14, 7).UpperBand
Does it compile now?
xtrender
02-11-2009, 06:48 AM
Silly me...
Thank you, it woieks!
mattster
08-24-2010, 02:20 PM
I can use DrawDot in order to plot my trail stop on my chart and that works just fine. I would like to however have a smooth line through those dots so that it's easier to read. The dots are too large and not very elegant looking in the display. Any help would be appreciated.
Thanks
NinjaTrader_Josh
08-24-2010, 04:25 PM
mattster,
If you want a plot line I suggest using an indicator to actually have a plot. Then just set the plot values for that indicator to the same values as your trailing stop. This would mean copying over the logic used to generate those trailing stop values to the indicator itself.
Alternatively you could try concepts shown here to get plotting done from the strategy: http://www.ninjatrader.com/support/forum/showthread.php?t=6651
mattster
08-30-2010, 12:33 PM
I can successfully plot out my stop on my backtest charts but they do not show up on my regular trading charts. Any reason why they might not be displaying?
if (Position.MarketPosition == MarketPosition.Long)
DrawText("LongStopDot" + CurrentBar,"_", 0, TrailStopLong, Color.Black);
else if (Position.MarketPosition == MarketPosition.Short)
DrawText("ShortStopDot" + CurrentBar,"_", 0, TrailStopShort, Color.Black);
Works great in backtesting.
NinjaTrader_RyanM
08-30-2010, 01:43 PM
Hello Mattster,
Would you please clarify what you mean by regular trading charts?
The market position information you're checking will only apply to strategy placed trades. You would have to apply the strategy to a chart and then should see the text whenever the strategy position is long or short.
mattster
08-30-2010, 01:54 PM
That is correct. I have an active strategy that performs trades throughout the day. Currently short for example, but my ticks are not displaying in the chart that I have open. My entry points are displaying as well as my indicators. The above code is in the OnBarUpdate section.
NinjaTrader_RyanM
08-30-2010, 02:38 PM
Thanks, mattster.
Some additional things to check:
How you are calculating TrailStopLong and TrailStopShort?
Are you applying to a black background chart?
What is your CalculateOnBarClose setting?
It's working OK here. Check those values with Print() statements or plug in a value based off market prices.
Example:
if (Position.MarketPosition == MarketPosition.Long)
DrawText("LongStopDot" + CurrentBar,"_", 0, Low[0] - TickSize * 15, Color.Red);
If you continue to see issues, post the complete snippet you're using and we can take a look.
daven
10-02-2010, 02:24 PM
I tried using the referenced sample code "SampleStrategyPlot" to plot from within an existing strategy which runs fine. I tried to incorporate the add statement as well as the id variable but I immediately got error codes 1502 and 1503 and it appears that I am trying to mix indicator and strategy classes and it is generating errors. Was it your intent with this sample to allow us to lift code and incorporate it into existing strategies or is this a separate indicator which runs on the same chart as the strategy and somehow magically picks up the traililng stop value from the stategy? If it is the latter, how specifically do I pass that information? If it is the former, why am I getting class conflicts when I try to incorporate the code and how do I fix that?
Thanks
DaveN
NinjaTrader_Austin
10-02-2010, 07:52 PM
Dave, how exactly are you coding this out? Can you please post the full code so we can see what's going on?
daven
10-03-2010, 04:53 PM
It isn't working yet, but once I have it running I will post the relevant code.