protected override void Initialize() { SetStopLoss( CalculationMode.Percent, 0.08); SetProfitTarget( CalculationMode.Percent, 0.03); } //Example code protected override void OnBarUpdate() { if( RSI( slowSMA, 3)[0] > 30 && RSI( slowSMA,3)[0]<70) { if ( SMA(fastSMA)[0] > SMA(slowSMA)[0] ) { EnterLong( 1); } if(SMA(fastSMA)[0] < SMA(slowSMA)[0] ) { EnterShort( 1); } } }
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
SetProfitTarget Not working
Collapse
X
-
SetProfitTarget Not working
I can not make it work. this is my code:
Code:
Tags: None
-
Hello hectorzx,
Thank you for writing in and welcome to the NinjaTrader Support Forum!
When using CalculationMode.Percent, this will be placing the stop loss/profit target at a price that is the specified percentage of your entry.
For example, let's say the strategy enters at 2038.
3% of 2038 is 61.14.
2038 + 61.14 is 2099.14. Rounding up to the ticksize, the profit target would be placed at 2099.25.
To place the profit target as you have intended, you will want to use the CalculationMode of Price instead and set it to Position.AvgPrice * 1.0003.
You will be unable to use Position.AvgPrice in Initialize(), so you will have to do this in OnBarUpdate() and ensure that you are in a position before doing so.
Example:
Code:protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Long) SetProfitTarget(CalculationMode.Price, Position.AvgPrice * 1.0003); }
Please, let us know if we may be of further assistance.Last edited by NinjaTrader_ZacharyG; 06-24-2016, 11:48 AM.Zachary G.NinjaTrader Customer Service
-
answer + issues on exiting on profit targets
OP, for one, try using different code. Rather than calculate, set the profit target as a double:
private double profit = 0; // setting it to $0
private double profitTarget = 0.03; // that's 3% you're looking for
THEN:
profit = Position.AveragePrice + (profitTarget * Position.AveragePrice); // if your entry price was $10, profit will become $10.30
However, if someone can then help me, as this code below should exit positions... The way I'm understanding... IF I'm long, AND most recent High is Above profit, it should exit. I understand I don't really need to check if it's above my average, if it's above profit, but I kept it in there, since it will exit the trade if it's above average price, but won't if it's above profit... So then I see the hypothetical price go above $10.40 for most recent high, and no exit. What's going on here?
if (Position.MarketPosition == MarketPosition.Long && High[0] > profit )
{
Print(" PROFIT HIT, EXIT");
ExitLong();
}
THE TRAIL STOP DOES work, and position DOES EXIT
else if (Low[0] <= trailStop && Position.MarketPosition == MarketPosition.Long && Position.AveragePrice < trailStop)
{
Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName);
ExitLong();
}
profit and trailStop are both updated on every bar count
Comment
-
good point. I've added print everywhere, and I do see that High[0] or Close[0] simply is usually below profit level... However, then I'm confused as to why it doesn't seem to update on every bar.
I'm noticing that I'm only getting these print outs, whenever an entry OR and exit of the position took place, but I feel like I should get a data dump on EVERY tick, no? Here is the bar update script
protected override void OnBarUpdate()
{
// Condition set 1 - Upward Price Breakout
if (CurrentBar < BarsRequiredToTrade)
return;
// if not entered yet, first time's a charm
if (PriceBreakoutRectangularBase(upwardBreakout,Volum eMultiple).Plot0[0] == 1 &&
Position.MarketPosition == MarketPosition.Flat && smaFast[0] > smaSlow[0])
{
EnterLong();
justEntered = true;
}
// now the position is long, and should update on bar update, every bar, based on time?
if (Position.MarketPosition == MarketPosition.Long && justEntered == true)
{
if (trailingStopPct == 0)
{
trailStop = Position.AveragePrice - (chandelierRangeMultiple * ATR(atrPeriod)[0]);
}
else
{
trailStop = Position.AveragePrice - (trailingStopPct * Position.AveragePrice);
}
StopLoss = Position.AveragePrice - (stopLossPct * Position.AveragePrice);
profit = Position.AveragePrice + (profitTarget * Position.AveragePrice);
Print("profit target is" + profit);
Print("close is" + Close[0]);
Print("high is" + High[0]);
Print("Low is" + Low[0]);
Print("trail is" + trailStop);
Print("stop is" + StopLoss);
prevValue = Position.AveragePrice;
justEntered = false;
//Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName);
}
In one week of PCLN I only got one entry, and only got two output prints.
Comment
-
Hello lmatiukas,
Your prints are within the if (Position.MarketPosition == MarketPosition.Long && justEntered == true) statement.
The prints will not occur again unless both conditions are true; I see that you change justEntered to false within this if statement. The prints will not print again until justEntered becomes true again.Zachary G.NinjaTrader Customer Service
Comment
-
I see what you mean, took me a while to realize it. It only re-calculates that one single time, and then after it closes the position, as it satisfies the original condition. VALID POINT!
I'll take re-calculation of trailstop, profit stop outside of this loop, at the beginning of onBarUpdate function, so I can get them new numbers every time!
Thanks a bunch.. Still such a nightmare learning this fresh with no C coding, and trying to figure out how to incorporate custom indicators into strategies.
Comment
-
Hello lmatiukas,
I would suggest taking a look at the Educational Resources page of the help guide for additional information for learning basic through intermediate skills in building custom indicators and strategies within NinjaTrader: https://ninjatrader.com/support/help..._resources.htmLast edited by NinjaTrader_ChelseaB; 02-28-2018, 02:19 PM.Zachary G.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by fx.practic, 10-15-2013, 12:53 AM
|
5 responses
5,403 views
0 likes
|
Last Post
by Bidder
Today, 12:22 AM
|
||
Started by Shai Samuel, 07-02-2022, 02:46 PM
|
4 responses
94 views
0 likes
|
Last Post
by Bidder
Today, 12:11 AM
|
||
Started by DJ888, Yesterday, 10:57 PM
|
0 responses
6 views
0 likes
|
Last Post
by DJ888
Yesterday, 10:57 PM
|
||
Started by MacDad, 02-25-2024, 11:48 PM
|
7 responses
158 views
0 likes
|
Last Post Yesterday, 10:23 PM | ||
Started by Belfortbucks, Yesterday, 09:29 PM
|
0 responses
8 views
0 likes
|
Last Post
by Belfortbucks
Yesterday, 09:29 PM
|
Comment