NexusFi: Find Your Edge


Home Menu

 





Anyone who needs a flat stop set after a certain amount of profit, I have an answer


Discussion in NinjaTrader

Updated
    1. trending_up 1,028 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 0 posts
    2. attach_file 0 attachments




 
Search this Thread

Anyone who needs a flat stop set after a certain amount of profit, I have an answer

  #1 (permalink)
sra18376
kansas city, ks
 
Posts: 17 since Jun 2014
Thanks Given: 0
Thanks Received: 5

I have gone from being completely new to ninjatrader, ninjascript and programming in general to finishing programming my own strategy in about a month and a half, and I have to give a lot of credit to this forum, because browsing through threads here taught me 80% of what I learned.

So to pay it forward here is a little code that I have seen a lot of people have questions about. Setting a flat stop once you are a few points in profit. The great thing about this code is that you can still use SetTrailStop() if you want because this code doesn't use SetStopLoss(). So here it is. Enjoy


This will set a flat stop once you are 2 points in profit.

Under the Variables region:

 
Code
private bool stopSet = false;

Under the OnBarUpdate() section:

 
Code
if (Position.MarketPosition == MarketPosition.Long)
{
				if (Close [0] >= Position.AvgPrice + 2)
				{
				stopSet = true;
				}

				if (stopSet && Close [0] <= Position.AvgPrice)
				{
				ExitLong();
				stopSet = false;
				}
}

Reply With Quote
Thanked by:




Last Updated on July 17, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts