EasyLanguage: Changing the StopLoss after Reaching a Target - futures io
futures io



EasyLanguage: Changing the StopLoss after Reaching a Target


Discussion in EasyLanguage Programming

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




Welcome to futures io: the largest futures trading community on the planet, with well over 150,000 members
  • 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 and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

EasyLanguage: Changing the StopLoss after Reaching a Target

(login for full post details)
  #1 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
 
Posts: 16 since Aug 2018
Thanks: 17 given, 23 received

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
The following user says Thank You to samasthiti for this post:

Can you help answer these questions
from other members on futures io?
Multi Time Frame High/Low/Median levels
NinjaTrader
help with take profit several targets
TradeStation
Dissect open interest data
Crypto Futures
NT8 Order Management Strategy
The Elite Circle
Market on Close data within IB
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Is anyone actually making money?
123 thanks
ApexTraderFunding.com experience and review
11 thanks
Artificial Intelligence (AI) and Chat GPT
7 thanks
Dax market profile (FDAX, TPO)
7 thanks
Want your NinjaTrader indicator created, free?
7 thanks
 
(login for full post details)
  #2 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,360 since Apr 2013
Thanks: 426 given, 1,578 received

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
The following 3 users say Thank You to ABCTG for this post:
 
(login for full post details)
  #3 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
 
Posts: 16 since Aug 2018
Thanks: 17 given, 23 received


Very clear explanation.

The code works flawlessly with the modifications you suggested.

Thank you ABCTG

Started this thread Reply With Quote
 
(login for full post details)
  #4 (permalink)
 GoldenRatio 
Philadelphia, PA
 
Experience: Advanced
Platform: Matlab, TradeStation
Trading: Stocks
 
GoldenRatio's Avatar
 
Posts: 210 since Aug 2012
Thanks: 5,164 given, 295 received

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

Reply With Quote
 
(login for full post details)
  #5 (permalink)
 samasthiti 
Montreal QC
 
Experience: Intermediate
Platform: MultiCharts
Broker: AMP Futures, CQG
Trading: Emini ES
 
Posts: 16 since Aug 2018
Thanks: 17 given, 23 received

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
The following user says Thank You to samasthiti for this post:


futures io Trading Community Platforms and Indicators EasyLanguage Programming > EasyLanguage: Changing the StopLoss after Reaching a Target


Last Updated on December 5, 2020


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
     



Copyright © 2023 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
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.
no new posts