NexusFi: Find Your Edge


Home Menu

 





Where is the mistake?


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one kevinkdog with 1 posts (1 thanks)
    2. looks_two ShadowFox with 1 posts (1 thanks)
    3. looks_3 SMCJB with 1 posts (2 thanks)
    4. looks_4 JBroex with 1 posts (0 thanks)
    1. trending_up 4,243 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
JBroex
Istanbul
 
Posts: 35 since Sep 2020
Thanks Given: 24
Thanks Received: 19

Hi everyone,

I create the following code (see below), but my stop loss doesnt seem to work at some instances (both for the "stop l" and the "Stop&rev 1". Any idea why?

Thanks!

 
Code
//Determine current and target profit
TargetProfit = highest(close,MAlen) - lowest(close,MALen);
CurrentProfit = Close - Entryprice(1);

//Define entry
If ATR < ATRlowest[1] and MADiff < EntryAmount then  buy next bar at market;

//Define exit - if initilal is buy
If barssinceentry < 100 AND MarketPosition = 1 AND CurrentProfit < -stopamount then sellshort ("Stop&rev l") next bar at market;
If Currentprofit > TargetProfit then sell ("Long target hit 1") next bar at market;

If barssinceentry > 100 AND MarketPosition = 1 AND CurrentProfit < -stopamount then sell ("Stop l") next bar at market;
If Currentprofit > TargetProfit then sell ("Long target hit 2") next bar at market;

//Define exit - if intial is sell
If MarketPosition = -1 AND CurrentProfit < -stopamount then buy ("Stop&rev  s") next bar at market;
If Currentprofit > TargetProfit then buytocover ("Short targe hit") next bar at market;


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trump Lands in Beijing on CPI Day: Iran Peace Expires To …
Prediction Markets & Event Contracts
$12M Ceasefire Contract Goes Disputed as Bandar Abbas St …
Prediction Markets & Event Contracts
GDP Day: The First Economic Reckoning -- Pahlavi at 6.55 …
Prediction Markets & Event Contracts
Czechia Live at 52.5% as England Rides 4-2 Wave to Co-Fa …
Prediction Markets & Event Contracts
Pakistan Mediator in Tehran as Hormuz Normalization Coll …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
13 thanks
Darmok and Jalad at Tanagra
3 thanks
Big Mike in Ecuador
1 thanks
30 Sessions
1 thanks
  #2 (permalink)
 
SMCJB's Avatar
 SMCJB 
Houston TX
Legendary Market Wizard
 
Experience: Advanced
Platform: TT Stellar & Tradestation
Broker: Primarily Advantage Futures
Trading: Primarily Energy but also a little Equities, Fixed Income, Metals, U308 and Crypto.
Frequency: Many times daily
Duration: Never
Posts: 5,242 since Dec 2013
Thanks Given: 4,586
Thanks Received: 10,533

Not an expert, but I will point out that Tradestation runs code at the end of the bar, for action on the next bar. Since your stops are dependent upon you having a position (you use marketposition to check this) the bar that you enter your position, you will have no stop for that bar as marketposition was different at end of the previous bar. You can get around this by creating your own tracker variable to monitor your position and when you create a new order, update the variable.

ie
 
Code
//Update Variable
If marketposition > 0 then WeAreLong = 1 else WeAreLong = 0

//Define entry
If ATR < ATRlowest[1] and MADiff < EntryAmount then begin
 buy next bar at market;
 WeAreLong = 1 // This tells the code, you will be long next bar, hence make sure you have a stop
End;

//Define exit - if initilal is buy
If barssinceentry < 100 AND WeAreLong = 1 AND ..... // replace marketposition = 1 with WeAreLong = 1


Reply With Quote
Thanked by:
  #3 (permalink)
 
ShadowFox's Avatar
 ShadowFox 
CO/USA
 
Experience: Intermediate
Platform: TradeStation, Multicharts
Trading: Stocks, Futures
Posts: 129 since Jun 2020
Thanks Given: 70
Thanks Received: 159


It's possible the Entryprice(1) is the culprit. Entryprice(0) is your current position entry. Entryprice(1) is your previous position. Looks like most of the logic is expecting more than one bar to hit stops or targets so you may not need intrabar order generation but that may be a good thing to check if you are expecting some fills in the same bar. I can see that being detrimental to this strategy though.


Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,738 since Jul 2012
Thanks Given: 1,918
Thanks Received: 7,485

Instead of CurrentProfit, I'd use OpenPositionProfit, which is a reserved word.

There may be cases where your calculation of CurrentProfit is incorrect, due to flipping of positions, etc.


Follow me on X Reply With Quote
Thanked by:




Last Updated on February 22, 2021


© 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