NexusFi: Find Your Edge


Home Menu

 





EasyLanguage: Changing the StopLoss after Reaching a Target


Discussion in EasyLanguage Programming

Updated
    1. trending_up 5,671 views
    2. thumb_up 5 thanks given
    3. group 5 followers
    1. forum 4 posts
    2. attach_file 2 attachments




 
Search this Thread

EasyLanguage: Changing the StopLoss after Reaching a Target

  #1 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
Posts: 17 since Aug 2018
Thanks Given: 19
Thanks Received: 23

I'm trying to create my own stop loss strategy where an initial take profit order and stop loss order are set upon entering the trade and triggered when either "ProfitTarget" or "StopLoss" are reached.

However, I want my stop loss to move to a pre-determined level ("StopLossAdj") once the price reaches a certain threshold ("RatchetAmt") above the EntryPrice.

The code below works fine for most trades but not for others and I don't understand why. I join two screenshots taken from the exact same 2000 tick chart. All trades have the same signals applies to them.

On the first screenshot, we see the code working as expecte: the stoploss on the left trade is moved to 1 below entry price after ratchetAmt 3 is reached. On the right side, the ratchetAmt is never reached so the price stops out at StopLoss 6 points below EntryPrice.





Yet, on the following trade (second screenshot), the price does reach ratchetAmt 3 but the stop is apparently not moved to the level 1 point below EntryPrice. It should have triggered there.



I'm relatively new to learning EasyLanguage. I've spent two days trying to work it out. Thank you for your help.




 
Code
Input: StopLoss(6), ProfitTarget(6), RatchetAmt(3), StopLossAdj(1);


SetStopContract;
	SetProfitTarget(ProfitTarget * BigPointValue);
	SetStopLoss(StopLoss* BigPointValue);
		
		
If marketposition <> 0 AND High >= (EntryPrice + RatchetAmt )

Then  begin 

SetStopLoss(StopLossAdj * BigPointValue);

	End;
	
	[IntrabarOrderGeneration = False]
			
		
SetExitOnClose;

Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
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
34 thanks
Just another trading journal: PA, Wyckoff & Trends
30 thanks
Tao te Trade: way of the WLD
23 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
21 thanks
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628

samasthiti,

you are using "SetStopLoss" twice in your code. The first one is valid on every bar and the second one would only be used on bars where "High >= (EntryPrice + RatchetAmt )", but not on later bars where High < (EntryPrice + RatchetAmt ), even if the condition was met on a prior bar. On those bars only the first stop would be used according to your code, which appears to be what you are seeing on the chart, too.
You could consider using the reserved word SetStopLoss once only at the end of the code and using a variable that holds the stop amount.

If you are flat you would reset the variable (let's call it myStopAmount for this example) to myStopAmount = StopLoss* BigPointValue and in case High >= (EntryPrice + RatchetAmt ) while you are in a position you would set it to myStopAmount = StopLossAdj * BigPointValue.
At the end of the code you would issue the stop using SetStopLoss( myStopAmount ).

Regards,

ABCTG


samasthiti View Post
I'm trying to create my own stop loss strategy where an initial take profit order and stop loss order are set upon entering the trade and triggered when either "ProfitTarget" or "StopLoss" are reached.

However, I want my stop loss to move to a pre-determined level ("StopLossAdj") once the price reaches a certain threshold ("RatchetAmt") above the EntryPrice.

The code below works fine for most trades but not for others and I don't understand why. I join two screenshots taken from the exact same 2000 tick chart. All trades have the same signals applies to them.

On the first screenshot, we see the code working as expecte: the stoploss on the left trade is moved to 1 below entry price after ratchetAmt 3 is reached. On the right side, the ratchetAmt is never reached so the price stops out at StopLoss 6 points below EntryPrice.

Attachment 307024



Yet, on the following trade (second screenshot), the price does reach ratchetAmt 3 but the stop is apparently not moved to the level 1 point below EntryPrice. It should have triggered there.

Attachment 307025

I'm relatively new to learning EasyLanguage. I've spent two days trying to work it out. Thank you for your help.




 
Code
Input: StopLoss(6), ProfitTarget(6), RatchetAmt(3), StopLossAdj(1);


SetStopContract;
	SetProfitTarget(ProfitTarget * BigPointValue);
	SetStopLoss(StopLoss* BigPointValue);
		
		
If marketposition <> 0 AND High >= (EntryPrice + RatchetAmt )

Then  begin 

SetStopLoss(StopLossAdj * BigPointValue);

	End;
	
	[IntrabarOrderGeneration = False]
			
		
SetExitOnClose;


Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
Posts: 17 since Aug 2018
Thanks Given: 19
Thanks Received: 23


Very clear explanation.

The code works flawlessly with the modifications you suggested.

Thank you ABCTG

Started this thread Reply With Quote
  #4 (permalink)
 
GoldenRatio's Avatar
 GoldenRatio 
Philadelphia, PA
 
Experience: Advanced
Platform: Matlab, TradeStation
Trading: Stocks
Posts: 211 since Aug 2012
Thanks Given: 5,192
Thanks Received: 296

@samasthiti It would be helpful to future readers, facing a similar issue, to post your final working code.

Reply With Quote
  #5 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
Posts: 17 since Aug 2018
Thanks Given: 19
Thanks Received: 23

Here we go:


 
Code
Input: StopLoss(1), ProfitTarget(1), RatchetAmt(1), StopLossAdj(1);
Vars: myStopAmount(0);

SetStopContract;
	SetProfitTarget(ProfitTarget * BigPointValue);
			
If marketposition = 0 then myStopAmount = StopLoss * BigPointValue;
 
If marketposition <> 0 AND High >= (EntryPrice + RatchetAmt )
then myStopAmount = StopLossAdj * BigPointValue;

SetStopLoss( myStopAmount );
	
	[IntrabarOrderGeneration = False]
			
		
SetExitOnClose;

Started this thread Reply With Quote
Thanked by:




Last Updated on December 5, 2020


© 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