NexusFi: Find Your Edge


Home Menu

 





Exit condition not working properly buying and selling same bar


Discussion in EasyLanguage Programming

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




 
Search this Thread
  #1 (permalink)
 dylant58 
osceola, missouri/united states of america
 
Experience: Beginner
Platform: Ninjatrader
Trading: ES
Posts: 4 since Oct 2018
Thanks Given: 1
Thanks Received: 1

Hello,

I am practicing learning easylanguage. Slowly building my skills. I am coding a very basic strategy for practice. Everything is working correctly until I get to my exit condition. For some reason it keeps buying and selling on the same bar even though the price isn't at the target price and it continues to do this on every bar that meets my entry condition.

Since my entrycondition stuff works fine ill just paste my exit condition logic.


 
Code
If EntryCondition Then
    Buy("Entry") Next Bar at Market;

// Calculate Average True Range (ATR)
ATR = AvgTrueRange(ATRLength);

// Exit condition
TargetPrice = Avgentryprice + testtarget;
stoplossprice = avgentryprice - teststop;

ExitCondition = AvgPrice >= TargetPrice;
stoplosshit = AvgPrice <= stoplossprice; 

If ExitCondition Then
    Sell("Exit") Next Bar at Market;

Originally I was using atr as a stop but I since just switched to a variable that has a static numbers. teststop(15) and testtarget(30) because I was using 1 atr and that was coming out to like 2 points and on a 30 minute chart that is easily hit so i thought I just had my stops/targets to close initially but even when i expanded it to 4 atr it still was buying and selling immediately on every bar that meets my entry conditions.

Emini sp 500 futures is the market for reference.

If anyone could be of assistance that would be great.


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Would a node-based workflow for automated trading be useful?
Traders Hideout
Six Days to Kickoff: World Cup Prediction Markets Hit $1 …
Prediction Markets & Event Contracts
Fabrication or Framework? Irans Denied MOU Explains the …
Prediction Markets & Event Contracts
Penalties in Budapest, Peace Deadline in Tehran: Arsenal …
Prediction Markets & Event Contracts
Thursday May 28: GDP + Core PCE + Jobless Claims All at …
Traders Hideout
 
  #2 (permalink)
 bfulks 
Boston MA
 
Experience: Advanced
Platform: TradeStation
Trading: Stocks and options
Posts: 24 since Aug 2022
Thanks Given: 2
Thanks Received: 4

It is hard to comment since not all the code is there.

But you might try adding code to prevent the sell from occurring until you are long.

Something like:

MP = MarketPosition;
if MP > 0 and ExitCondition Then
Sell("Exit") Next Bar at Market;


Reply With Quote
  #3 (permalink)
abev
seattle washington
 
Posts: 75 since Feb 2019
Thanks Given: 11
Thanks Received: 30


I agree that it is a bit difficult to comment without more or all of the code. Three things strike me: first, are you using IOG? I ask because using IOG during back testing is so convoluted that it is just best not to use IOG in back testing. Second, I question the use of avgentryprice since that assumes you have two or more open positions to use to average their entry price. Finally, I also wonder about the use of avgprice since that is the average of the hloc of the current bar and cannot be known until the bar closes.

And I guess I'd also like to know if the unseen part of the code would demand that any of all of your variables need to be reset to 0 or null before moving to the next bar.


Reply With Quote




Last Updated on May 28, 2023


© 2026 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 - Downloads - Top
no new posts