NexusFi: Find Your Edge


Home Menu

 





Where is the mistake?


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one SMCJB with 1 posts (2 thanks)
    2. looks_two JBroex with 1 posts (0 thanks)
    3. looks_3 kevinkdog with 1 posts (1 thanks)
    4. looks_4 ShadowFox with 1 posts (1 thanks)
    1. trending_up 4,178 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?
CFTC Opens First COT Report Review in 20 Years -- Asks W …
Traders Hideout
Roland Garros Final Day: $3M on Zverev at 80.5% -- Leban …
Prediction Markets & Event Contracts
Weekend Update: First Qatari LNG Transit Attempted -- IR …
Traders Hideout
Warsh Rate Hike at 40%, Iran June 15 Expires Tonight at …
Prediction Markets & Event Contracts
June 15 Peace Odds Surge From 3.6% to 12.25% After Trump …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Big Mike in Ecuador
196 thanks
Sober Journey With S&P
27 thanks
30 Sessions
20 thanks
BERN ALGOS algo trading journal
8 thanks
Volume Indicators
8 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,241 since Dec 2013
Thanks Given: 4,585
Thanks Received: 10,531

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,737 since Jul 2012
Thanks Given: 1,917
Thanks Received: 7,476

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