Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
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 for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I seem to be encountering an issue with markeposition and just wondering if anybody else
has experienced the same thing.
The backtesting results look perfect but when I ran the system in a simulation I find that the
orders are sometimes missing.
The idea of the system is to be stop-and-reverse. In version 1 below it does exactly that,
i.e. 2 orders, when I am long it closes the long and opens a short.
In version 2 it sometimes only closes the long (it only has 1 order not 2 as expected).
I have the "if in marketposition" because I have an exit check and I dont wont it to enter a
position if its not in a position.
Any help would be appreciated.
Example
Version 1:
vars: iAvg(0), ncons(1);
iAvg = Average(Close, 10);
if (Close crosses below iAvg) then Sellshort ("Test_Short") ncons contracts next bar at market;
if (Close crosses above iAvg) then Buy ("Test_Buy") ncons contracts next bar at market;
Version 2: -- added this code
if marketposition=1 and (Close crosses above iAvg) then Sell ncons contracts next bar at market;
if marketposition=-1 and (Close crosses below iAvg) then Buytocover ncons contracts next bar at market;
Can you help answer these questions from other members on NexusFi?
Whats important to add is that it only SOMETIMES doesnt do 2 orders. Its inconsistent.
I do wonder if it has something to do with async orders getting sent,
If I am long, if it finishes the "Enter Short" task before the "Close Long" task, then If Marketposition will then fail / be false
Version 1 should work without a problem. This could be a SIM issue, not a code issue.
Although I have seen on occasion this happen in real trading too. But not enough times for me to actually try to solve it.
You actually do not need the "marketposition" statements. Tradestation knows to ignore "sell" and "buytocover" if you are not in the proper trade.
You could then just try this and see if this works (first 2 if statements should not be needed though):
vars: iAvg(0), ncons(1);
iAvg = Average(Close, 10);
if (Close crosses above iAvg) then Sell ncons contracts next bar at market;
if (Close crosses below iAvg) then Buytocover ncons contracts next bar at market;
if (Close crosses below iAvg) then Sellshort ("Test_Short") ncons contracts next bar at market;
if (Close crosses above iAvg) then Buy ("Test_Buy") ncons contracts next bar at market;
Sorry sometimes I explain poorly. No, with version 1 everthing worked 100% as expected, so when it reversed it closed and opened a new order.
It was only with version 2 (added to version 1s code) that errors creeped in.
I noticed that all 3 in Sim had the same exit strategy (the if markposition and <some rule> then )
Good to know you havent seen it then.
To elaborate a bit more...long story short is that I have 3 systems in sim testing, all of them had the exit condition "if marketposition<>0 and <some rule> then exit"
Its the only common denominator I could find but with all 3 systems I had the problem that sometimes it missed a trade.
It is an assumption that I made that its the exit rules.
So I wrote the system posted above with version 1...ran this for a day on 15min timeframe no issues (does 2 orders on every stop and reverse)
Then I added the code to the version 1, so that the structure replicates my real systems, to see if there are any issues.
As seen in the image posted in excel after running it for a day it eventually missed a trade as well at the end (line highlighted in yellow)
I have the structure in my systems:
if <entry rule> then go long
if [marketposition>1] and <different exit rule> then exit long
I know that a mistake is made because the backtesting shows that it is suppose to be an entry:
(The pics are of the system posted at the top)
In the image below under trades it should show "Trades - Cover & Trades - Buy"
In Strategy it has Cover and Buy, but the Trades only has a cover
I don't think this is an issue with your code (even though there are issues with your code). Check your Trade Manager - Messages log for an error message. That may help figure out why the order may have been rejected. 9/10 this happens to me because my strategy tried to place an order during "pre open" (1 second too early for ES). Only way I know to fix this is to run a custom session. Its very rare that I get these issues in liquid markets so I have not ventured to fix this yet. I just watch the open. Or more likely I check charts out after the close and know that I am expecting trades to happen on market open, then I make sure I receive fill notification if I am not monitoring.
As far as your code is concerned. I think you made a mistake on version 2 additions. Your closing statements are your opening statements, not your reversal statements. Adding this actually does nothing for you since marketposition = -1 when you buy and wont be 1 until the next bar. I believe your closing statements are reversed even though they wont actually matter.
if (Close crosses above iAvg) then Buy ("Test_Buy") ncons contracts next bar at market;
if marketposition=1 and (Close crosses above iAvg) then Sell ncons contracts next bar at market;
if (Close crosses below iAvg) then Sellshort ("Test_Short") ncons contracts next bar at market;
if marketposition=-1 and (Close crosses below iAvg) then Buytocover ncons contracts next bar at market;