NexusFi: Find Your Edge


Home Menu

 





Order Entry, Target, Trailing Stop, and Exit


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one DrRon with 9 posts (0 thanks)
    2. looks_two ABCTG with 8 posts (9 thanks)
    3. looks_3 Jura with 2 posts (3 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 7,351 views
    2. thumb_up 12 thanks given
    3. group 2 followers
    1. forum 18 posts
    2. attach_file 0 attachments




 
Search this Thread

Order Entry, Target, Trailing Stop, and Exit

  #1 (permalink)
 DrRon 
Irvine CA
 
Experience: Advanced
Platform: NinjaTrader, TadeStation, ThinkOrSwim, and StreetSmartEdge
Trading: CL
Posts: 9 since Aug 2015
Thanks Given: 11
Thanks Received: 1

Hello fellow coders,

I consider myself to be a moderate Easy Language coder.

I have coded for a Strategy that if its conditions are met, I can then place an order (long or short depends on the direction that has been determined) with an entry, a target, and a trailing stop at the opening of the new bar.

However, there is a caveat. If the order is not filled within the next 2 bars, then I want the order to cancel. Also, if the order is filled and the target or the stop is not filled within the next 4 bars, then I want to exit the trade at market.

I also want the trailing stop to trail after it has moved into the profitable territory a bit, so that at minimum it makes a 1-tick profit and continues trailing as certain percentages of profit has reached.

Thank you

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
REcommedations for programming help
Sierra Chart
Exit Strategy
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
48 thanks
Just another trading journal: PA, Wyckoff & Trends
35 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628


Hi DrRon,

you can store the bar number when you send the orders and then cancel the orders after X bars. As a hint, the order will be cancelled when a condition for the order is not present anymore.
The same can be done for exiting the trade at market. Store the bar number of your entry and after Y bars send a market order to close your position.

Regards,
ABCTG


DrRon View Post
Hello fellow coders,

I consider myself to be a moderate Easy Language coder.

I have coded for a Strategy that if its conditions are met, I can then place an order (long or short depends on the direction that has been determined) with an entry, a target, and a trailing stop at the opening of the new bar.

However, there is a caveat. If the order is not filled within the next 2 bars, then I want the order to cancel. Also, if the order is filled and the target or the stop is not filled within the next 4 bars, then I want to exit the trade at market.

I also want the trailing stop to trail after it has moved into the profitable territory a bit, so that at minimum it makes a 1-tick profit and continues trailing as certain percentages of profit has reached.

Thank you


Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
 DrRon 
Irvine CA
 
Experience: Advanced
Platform: NinjaTrader, TadeStation, ThinkOrSwim, and StreetSmartEdge
Trading: CL
Posts: 9 since Aug 2015
Thanks Given: 11
Thanks Received: 1

Thank you ABCTG. However, would you in fact show me the code the way you would write it please?

Started this thread Reply With Quote
  #5 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690

By the way, for closing a position you can also use PosTradeEntryBar(), which you then only need to subtract from the current bar number ( Symbol_CurrentBar).


DrRon View Post
Thank you ABCTG. However, would you in fact show me the code the way you would write it please?

Can you share your code (attempt) till now? That way we can give more focused feedback and then you'll learn the most, as opposed to being given a copy/paste solution.

Reply With Quote
Thanked by:
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628

I agree with Jura, posting your code attempt and using feedback to improve it will in the end help you much more in climbing the next step up from being a "moderate Easy Language coder".

PosTradeEntryBar will only work in Multicharts though, for Tradestation you'd have to use OOEL and this requires a bit more advanced skills. You can however use BarsSinceEntry() and it should work fine when not dealing with multi leg entries.

Regards,
ABCTG


Jura View Post
By the way, for closing a position you can also use PosTradeEntryBar(), which you then only need to subtract from the current bar number ( Symbol_CurrentBar).


Can you share your code (attempt) till now? That way we can give more focused feedback and then you'll learn the most, as opposed to being given a copy/paste solution.


Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690


ABCTG View Post
PosTradeEntryBar will only work in Multicharts though, for Tradestation you'd have to use OOEL and this requires a bit more advanced skills. You can however use BarsSinceEntry() and it should work fine when not dealing with multi leg entries.

Oops. Good catch.

Reply With Quote
Thanked by:
  #8 (permalink)
 DrRon 
Irvine CA
 
Experience: Advanced
Platform: NinjaTrader, TadeStation, ThinkOrSwim, and StreetSmartEdge
Trading: CL
Posts: 9 since Aug 2015
Thanks Given: 11
Thanks Received: 1


ABCTG View Post
I agree with Jura, posting your code attempt and using feedback to improve it will in the end help you much more in climbing the next step up from being a "moderate Easy Language coder".

PosTradeEntryBar will only work in Multicharts though, for Tradestation you'd have to use OOEL and this requires a bit more advanced skills. You can however use BarsSinceEntry() and it should work fine when not dealing with multi leg entries.

Regards,
ABCTG

Thank you ABCTG & Jura. I wrote some simple code to get the order execution portion of the coding running correctly. Here is how far I've gotten so far and I haven't been successful at the order execution portion of the coding yet! I haven't even gotten into canceling an order yet either.

 
Code
Inputs:
Numb_Shares(10), //[DisplayName="Number of Shares",ToolTip="Enter the number of shares per trade."],
Min_Range(1), //[DisplayName="Minimum Range",ToolTip="Minimum range to trade."],
Max_Range(10), //[DisplayName="Maximum Range",ToolTip="Maximum range to trade."],
Bullish(1), //[DisplayName="Bullish Trades",ToolTip="Do you want to take bullish trades?(0=No/1=Yes)"],
Bearish(1), //[DisplayName="Bearish Trades",ToolTip="Do you want to take bearish trades?(0=No/1=Yes)"],
x(.2); //[DisplayName="Threshold",ToolTip="What threshold do wish to trade with?"],
Variables:
Bull(False),
Bear(False),
BuyTarget(0),
SellTarget(0),
BuyStop(0),
SellStop(0);
If BarStatus(1)=2 And Range>=Min_Range And Range<=Max_Range Then
	Begin
	Bull=Open<Close;
	Bear=Open>Close;
	If MarketPosition(0)=0 Then
		Begin
		If Bullish=1 And Bull
		Then
			Begin
			Buy Numb_Shares Shares Next Bar At High Stop;
			BuyTarget=High+(Range*x);
			BuyStop=Low+(Range*x);
			SetProfitTarget(BuyTarget);
			SetStopLoss(BuyStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bullish",High,BuyTarget,BuyStop);
			End
		Else
		If Bearish=1 And Bear
		Then
			Begin
			Sell Short Numb_Shares Shares Next Bar At Low Stop;
			SellTarget=Low-(Range*x);
			SellStop=High-(Range*x);
			SetProfitTarget(SellTarget);
			SetStopLoss(SellStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bearish",Low,SellTarget,SellStop);
			End;
		End;
	End;

Started this thread Reply With Quote
  #9 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628

DrRon,

you place your stop and target reserved words inside conditional checks. This means these will only be active when the conditions are true, too. Whenever your conditions are false you'd be left without a stop and target.

Please elaborate what the exact issues are that you are seeing with your code.

Regards,
ABCTG


DrRon View Post
Thank you ABCTG & Jura. I wrote some simple code to get the order execution portion of the coding running correctly. Here is how far I've gotten so far and I haven't been successful at the order execution portion of the coding yet! I haven't even gotten into canceling an order yet either.

 
Code
Inputs:
Numb_Shares(10), //[DisplayName="Number of Shares",ToolTip="Enter the number of shares per trade."],
Min_Range(1), //[DisplayName="Minimum Range",ToolTip="Minimum range to trade."],
Max_Range(10), //[DisplayName="Maximum Range",ToolTip="Maximum range to trade."],
Bullish(1), //[DisplayName="Bullish Trades",ToolTip="Do you want to take bullish trades?(0=No/1=Yes)"],
Bearish(1), //[DisplayName="Bearish Trades",ToolTip="Do you want to take bearish trades?(0=No/1=Yes)"],
x(.2); //[DisplayName="Threshold",ToolTip="What threshold do wish to trade with?"],
Variables:
Bull(False),
Bear(False),
BuyTarget(0),
SellTarget(0),
BuyStop(0),
SellStop(0);
If BarStatus(1)=2 And Range>=Min_Range And Range<=Max_Range Then
	Begin
	Bull=Open<Close;
	Bear=Open>Close;
	If MarketPosition(0)=0 Then
		Begin
		If Bullish=1 And Bull
		Then
			Begin
			Buy Numb_Shares Shares Next Bar At High Stop;
			BuyTarget=High+(Range*x);
			BuyStop=Low+(Range*x);
			SetProfitTarget(BuyTarget);
			SetStopLoss(BuyStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bullish",High,BuyTarget,BuyStop);
			End
		Else
		If Bearish=1 And Bear
		Then
			Begin
			Sell Short Numb_Shares Shares Next Bar At Low Stop;
			SellTarget=Low-(Range*x);
			SellStop=High-(Range*x);
			SetProfitTarget(SellTarget);
			SetStopLoss(SellStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bearish",Low,SellTarget,SellStop);
			End;
		End;
	End;


Follow me on Twitter Reply With Quote
Thanked by:
  #10 (permalink)
 DrRon 
Irvine CA
 
Experience: Advanced
Platform: NinjaTrader, TadeStation, ThinkOrSwim, and StreetSmartEdge
Trading: CL
Posts: 9 since Aug 2015
Thanks Given: 11
Thanks Received: 1



ABCTG View Post
DrRon,

you place your stop and target reserved words inside conditional checks. This means these will only be active when the conditions are true, too. Whenever your conditions are false you'd be left without a stop and target.

Please elaborate what the exact issues are that you are seeing with your code.

Regards,
ABCTG

Thank you ABCTG. I cleaned up my code a bit more as shown below:

 
Code
If BarStatus(1)=2 And Range>=Min_Range And Range<=Max_Range Then
	Begin
	Bull=Open<Close;
	Bear=Open>Close;
	If MarketPosition(0)=0 Then
		Begin
		If Bullish=1 And Bull
		Then
			Begin
			Buy Numb_Shares Shares Next Bar At High Stop;
			SetStopShare;
			BuyTarget=(Range*x);
			BuyStop=Range-(Range*x);
			SetProfitTarget(BuyTarget);
			SetStopLoss(BuyStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bullish",High,BuyTarget,BuyStop);
			End
		Else
		If Bearish=1 And Bear
		Then
			Begin
			SellShort Numb_Shares Shares Next Bar At Low Stop;
			SetStopShare;
			SellTarget=(Range*x);
			SellStop=Range-(Range*x);
			SetProfitTarget(SellTarget);
			SetStopLoss(SellStop);
			Print(File("c:\testOrderExe.txt"),Date,Time,Close,"Bearish",Low,SellTarget,SellStop);
			End;
		End;
	End;
I want to be able to enter a trade when the conditions, noted above, are met and at the same time, my target and stop are set once and only once until the trade is complete. Ultimately, also, if my order is not filled, I also want to be able to cancel that order, if the price goes past the Stop first before coming back to fill me!

Right now, what happens is that only two trades are taken place a few bars apart. No further trades before or after these two trades take place, even though the conditions are appropriate for more order entries.

Started this thread Reply With Quote




Last Updated on November 24, 2015


© 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