Where is the mistake? - futures io
futures io futures trading

Where is the mistake?

Discussion in EasyLanguage Programming

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

Welcome to futures io: the largest futures trading community on the planet, with well over 125,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

Where is the mistake?

(login for full post details)
  #1 (permalink)
Posts: 35 since Sep 2020
Thanks: 22 given, 17 received

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?


//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 futures io?
ZigZag with Depth and Backstep
EasyLanguage Programming
Can the Magnet Tool Affect Chart Orders In Overcharts Ha …
Platforms and Indicators
Short Interest Ratio indicator?
Automatic pattern research with NT
Elite Algorithmic NinjaTrader Trading
What blockvolume is visible? - order processing
(login for full post details)
  #2 (permalink)
Legendary Market Wizard
Houston, TX
Experience: Advanced
Platform: Trading Technologies
Broker: Primary Advantage Futures. Also ED&F and Tradestation
Trading: Primarily Energy but also a little GE, GC, SI & Bitcoin
Posts: 3,972 since Dec 2013
Thanks: 3,273 given, 7,790 received

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.

//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

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

Reply With Quote
The following 2 users say Thank You to SMCJB for this post:
(login for full post details)
  #3 (permalink)
Experience: Intermediate
Platform: TradeStation
Trading: Stocks, Futures
Posts: 38 since Jun 2020
Thanks: 28 given, 25 received

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.

Reply With Quote
(login for full post details)
  #4 (permalink)
Posts: 2,979 since Jul 2012
Thanks: 1,576 given, 5,900 received

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 Twitter Reply With Quote

futures io Trading Community Platforms and Indicators EasyLanguage Programming > Where is the mistake?

Last Updated on February 22, 2021

Upcoming Webinars and Events

NinjaTrader Indicator Challenge!


Battlestations! Show us your trading desk - $1,500 in prizes!


Importance of Finding Your Own Way w/Adam Grimes

Elite only

Journal Challenge w/Jigsaw


Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, 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