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 3,732 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

Where is the mistake?

  #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?
What broker to use for trading palladium futures
Commodities
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
Cheap historycal L1 data for stocks
Stocks and ETFs
MC PL editor upgrade
MultiCharts
 
  #2 (permalink)
 
SMCJB's Avatar
 SMCJB 
Houston TX
Legendary Market Wizard
 
Experience: Advanced
Platform: TT and Stellar
Broker: Advantage Futures
Trading: Primarily Energy but also a little Equities, Fixed Income, Metals and Crypto.
Frequency: Many times daily
Duration: Never
Posts: 5,049 since Dec 2013
Thanks Given: 4,388
Thanks Received: 10,207

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: 157


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,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360

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
Thanked by:




Last Updated on February 22, 2021


© 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