Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
highest bar close occuring within the last X number of bars . . . .
Hi Forrestang,
I have an issue this Stop Loss and take Profit Target and I want your thoughts on it. I have applied stop loss(SL) and take profit(TP) with CalculationMode.Percent and at OnBarUpdate() a dynamic SL when I am in a trade. The problem is arizing when I am running back test. I believe the worst loss could be iccure should be the ST at CalculationMode.Percent because is lower. However, I had losses and profits much more than my orders. How is this possible? a part of my code is below.
if (Position.MarketPosition== MarketPosition.Long)
{
if( Close[0] < EMA (30)[0] )
{
ExitLong();
Print(Time[0]+"Exit Long Close below EMA");
}
else if (Close[0]>= Position.AvgPrice+BreakEvenTarget * TickSize)
{
NewStopTarget = Position.AvgPrice + SetNewTarget * TickSize;
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
Print(Time[0]+" Exit Long due to BreakEven Ticks");
}
}
if (Position.MarketPosition== MarketPosition.Short)
{
if( Close[0] > EMA (30)[0] )
{
ExitShort();
Print(Time[0]+" Exit Short because of Close cross above EMA");
}
else if (Close[0]<= Position.AvgPrice-BreakEvenTarget * TickSize)
{
NewStopTarget = Position.AvgPrice- SetNewTarget * TickSize;
SetStopLoss(CalculationMode.Price, NewStopTarget);
Print(Time[0]+" Exit Short due to BreakEven Ticks");
}
}
Not sure exactly what your error is based on your explanation. Here is a general structure of how you should program your strategy. This is just for a long. Please take note of all the comments in there.
Basically, you have some setup in the initialize block with setting your target and stop.
Next in your update block, you want to:
Check if flat, if so set your stoploss to your hard stop.
Next, I've added in the OPTION to use a Breakeven adjust stop, you see that there. If you don't need it, you can just delete that statement. Also done at this point while checking if we are long, you'll see there is an option there to EXIT immediately if some condition happens. For example if you wanted to put oscillator crossing below zero in there.
Lastly, we check if we are NOT flat, if we are IN a trade, then exit the loop and start over. But this is basically how you do it.
Many thanks forrestang, my losses have been reduced significanlty! I didnt know that it is so important to put at the beging of the of strategy the SL and TP orders. I will always following this structure. Once again thanks for your imput. I would like to ask you also why the following code execute the same number of trades if I have 3 or 5 condition ?
if ( EMA(5)[0]>EMA(40)[0]
// && EMA(90)[0]>EMA(100)[0]/// If I add those two conditions
// && RSI(14,3)[0]> 60////// the number of the executed trades stay the same.
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(150)[0])/// Different time frame
{
EnterLong()
}
I had made the following changes and the code was working well but I cannot understand why I need to make this
if ( EMA(5)[0]>EMA(40)[0]
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(150)[0])/// Different time frame
{
if ( EMA(90)[0]>EMA(100)[0]/
&& RSI(14,3)[0]> 60)
{
EnterLong()
}
}
Well if you are adding more conditions and getting the same number of trades, it's either because you have some formatting issues, which kinda is what that first code block looks like?
Or you have some conditions that are too highly correlated.
So the check is easy, just run the strategy either in a sim or backtest, put the indicators you are using on a chart, and check and see if your system is plotting the proper trade based on whatever indicators you are using. If need be isolate each condition and check again.
The code "if ( EMA(5)[0]>EMA(40)[0] && EMA(90)[0]>EMA(100)[0]) "execute the same number of trades as the code if ( EMA(5)[0]>EMA(40)[0]
&& EMA(90)[0]>EMA(100)[0]
&& RSI(14,3)[0]> 60
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(BarsArray[1],10)[0])
{
EnterLong(DefaultQuantity);
Print(Time[0] +" Enter Long");
}
However, when I am adding an extra if statment is working well
if ( EMA(5)[0]>EMA(40)[0]
&& EMA(90)[0]>EMA(100)[0])
// && RSI(14,3)[0]> 60
// && Rising(ATR(Close,14))
// && Closes[1][0]>EMA(BarsArray[1],10)[0])
{
if ( RSI(14,3)[0]> 60
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(BarsArray[1],10)[0])
I just wanted to say thank you on behalf of most of us traders who glean the little nuggets of gold dust
from folks like you who assist those traders in need. You did a STELLAR job assisting this guy and folks
like us, iitting on the cloud want to send you a big ray of sunshine!