NexusFi: Find Your Edge


Home Menu

 





Variable to know if stop was hit


Discussion in EasyLanguage Programming

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




 
Search this Thread

Variable to know if stop was hit

  #1 (permalink)
 NormFuture 
Paris, France
 
Experience: Intermediate
Platform: Sierra Chart
Broker: EdgeClear
Trading: ES / MES
Frequency: Several times daily
Duration: Minutes
Posts: 21 since Nov 2020
Thanks Given: 18
Thanks Received: 24

Hi Everyone, I am pretty new to EasyLanguage and I am currently trying to test my first strategy. It is a mean-reverting strategy based on a keltner channel, basically entering short at the top of the channel and long at the bottom.

However to avoid hitting multiple stops in a row when there is a trend, I'd like to be able to only enter longs if my last trade was a short and was stopped out. This way I can only have one stop in a row when going long or short and then can only enter the opposite side.

The way I tried to do it is to have 2 variables, one for when I am long and the stop is hit and one when I am short and the stop is hit. However, I can't seem to find a way to make it work as I always have multiple stops in a row.

If anyone has ideas or a solution I'd be happy to hear your thoughts.

Here is the code:

Inputs:
Price( Close ),
Length( 100 ),
NumATRs( 4.6 ),
Cts ( 1 ),
StopATR( 3 );

variables:
Avg( 0 ),
Shift( 0 ),
LowerBand( 0 ),
UpperBand( 0 ),
ticksize( 0 ),
stoploss1 ( 1 ),
NoStopLong ( True ),
NoStopShort ( True );


//Variables conditions
Avg = AverageFC( Price, Length );
Shift = NumATRs * AvgTrueRange( Length );
LowerBand = Avg - Shift;
UpperBand = Avg + Shift;
ticksize= MinMove/PriceScale;
stoploss1 = AvgTrueRange ( 100 ) * StopATR;



//buy and sell

If NoStopLong = True and Price crosses above LowerBand Then Begin
Buy ("Long") Cts contracts next bar at market ;
NoStopShort = True;
end;

If NoStopShort = True and Price crosses below UpperBand Then Begin
SellShort ("Short") Cts contracts next bar at market ;
NoStopLong = True ;
end;



// StopLoss
If ( MarketPosition = 1 ) Then begin
If Price <= (entryprice - Stoploss1) Then NoStopLong = False ;
If ( CurrentContracts = Cts) then sell ("Long_stoploss1") cts contracts next bar at (entryprice - Stoploss1) stop;
If ( CurrentContracts = Cts) and Price crosses above Avg then sell ("tgtB") cts contracts next bar at market ;
end;

If ( MarketPosition = -1 ) Then begin
If Price >= (entryprice + Stoploss1) then NoStopShort = False ;
If ( CurrentContracts = Cts) then buytocover ("short_stoploss1") cts contracts next bar at (entryprice + Stoploss1) stop;
If ( CurrentContracts = Cts) and Price crosses under Avg then buytocover ("tgtS") cts contracts next bar at market ;
end;

Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Are there any eval firms that allow you to sink to your …
Traders Hideout
Exit Strategy
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
56 thanks
Funded Trader platforms
44 thanks
NexusFi site changelog and issues/problem reporting
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
The Program
19 thanks
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Lilgregy,

I would suggest using the print reserved word for checking the values of your two variables. This way you can see if the are correctly set and should prevent another entry.

If you want to make sure that the exit actually was triggered using a StrategyHost object comes to mind as this way you can obtain the names of the orders that were triggered.
However, this will require using Object Oriented EasyLanguage.

Regards,

ABCTG


Lilgregy View Post
Hi Everyone, I am pretty new to EasyLanguage and I am currently trying to test my first strategy. It is a mean-reverting strategy based on a keltner channel, basically entering short at the top of the channel and long at the bottom.

However to avoid hitting multiple stops in a row when there is a trend, I'd like to be able to only enter longs if my last trade was a short and was stopped out. This way I can only have one stop in a row when going long or short and then can only enter the opposite side.

The way I tried to do it is to have 2 variables, one for when I am long and the stop is hit and one when I am short and the stop is hit. However, I can't seem to find a way to make it work as I always have multiple stops in a row.

If anyone has ideas or a solution I'd be happy to hear your thoughts.

Here is the code:

Inputs:
Price( Close ),
Length( 100 ),
NumATRs( 4.6 ),
Cts ( 1 ),
StopATR( 3 );

variables:
Avg( 0 ),
Shift( 0 ),
LowerBand( 0 ),
UpperBand( 0 ),
ticksize( 0 ),
stoploss1 ( 1 ),
NoStopLong ( True ),
NoStopShort ( True );


//Variables conditions
Avg = AverageFC( Price, Length );
Shift = NumATRs * AvgTrueRange( Length );
LowerBand = Avg - Shift;
UpperBand = Avg + Shift;
ticksize= MinMove/PriceScale;
stoploss1 = AvgTrueRange ( 100 ) * StopATR;



//buy and sell

If NoStopLong = True and Price crosses above LowerBand Then Begin
Buy ("Long") Cts contracts next bar at market ;
NoStopShort = True;
end;

If NoStopShort = True and Price crosses below UpperBand Then Begin
SellShort ("Short") Cts contracts next bar at market ;
NoStopLong = True ;
end;



// StopLoss
If ( MarketPosition = 1 ) Then begin
If Price <= (entryprice - Stoploss1) Then NoStopLong = False ;
If ( CurrentContracts = Cts) then sell ("Long_stoploss1") cts contracts next bar at (entryprice - Stoploss1) stop;
If ( CurrentContracts = Cts) and Price crosses above Avg then sell ("tgtB") cts contracts next bar at market ;
end;

If ( MarketPosition = -1 ) Then begin
If Price >= (entryprice + Stoploss1) then NoStopShort = False ;
If ( CurrentContracts = Cts) then buytocover ("short_stoploss1") cts contracts next bar at (entryprice + Stoploss1) stop;
If ( CurrentContracts = Cts) and Price crosses under Avg then buytocover ("tgtS") cts contracts next bar at market ;
end;


Follow me on Twitter Reply With Quote
  #3 (permalink)
 NormFuture 
Paris, France
 
Experience: Intermediate
Platform: Sierra Chart
Broker: EdgeClear
Trading: ES / MES
Frequency: Several times daily
Duration: Minutes
Posts: 21 since Nov 2020
Thanks Given: 18
Thanks Received: 24


Thank you for your answer ABCTG.

I am not familiar with the print function, I wrote the following line at the end of the code:

Print( " MarketPosition " , " NoStopLong" , "NoStopShort")

Is it how I should do it? I can't find how to open the print log and did not find anything online.

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Lilgregy,

you might find the following link helpful:

https://help.tradestation.com:443/10_00/eng/TradeStationHelp/el_procedures/about_el_print_log.htm

What you use in your print statement is up to you - anything that you think would be helpful.
From looking at your print statement you appear to print variable and reserved word names only, but not the value of those, though.
Something along the lines of the below might give you a good start:
 
Code
Print( BarDateTime.ToString(), "; MarketPosition = ", MarketPosition, "; NoStopLong = ", NoStopLong, "; NoStopShort = ", NoStopShort ) ;
Regards,

ABCTG


Lilgregy View Post
Thank you for your answer ABCTG.

I am not familiar with the print function, I wrote the following line at the end of the code:

Print( " MarketPosition " , " NoStopLong" , "NoStopShort")

Is it how I should do it? I can't find how to open the print log and did not find anything online.


Follow me on Twitter Reply With Quote
Thanked by:




Last Updated on January 26, 2023


© 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