NexusFi: Find Your Edge


Home Menu

 





Issue with Marketposition


Discussion in EasyLanguage Programming

Updated
    1. trending_up 4,837 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 12 posts
    2. attach_file 4 attachments




 
Search this Thread

Issue with Marketposition

  #1 (permalink)
 bchip 
Torino, Italy
 
Experience: Advanced
Platform: TradeStation
Trading: ES,YM,CL,GC
Posts: 132 since Sep 2017
Thanks Given: 160
Thanks Received: 116

Hi

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;

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Quant vue
Trading Reviews and Vendors
 
  #2 (permalink)
 bchip 
Torino, Italy
 
Experience: Advanced
Platform: TradeStation
Trading: ES,YM,CL,GC
Posts: 132 since Sep 2017
Thanks Given: 160
Thanks Received: 116

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


Started this thread Reply With Quote
  #3 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360


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;

Follow me on Twitter Reply With Quote
  #4 (permalink)
 bchip 
Torino, Italy
 
Experience: Advanced
Platform: TradeStation
Trading: ES,YM,CL,GC
Posts: 132 since Sep 2017
Thanks Given: 160
Thanks Received: 116


kevinkdog View Post
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):

Yip, version1 works perfectly, when I add the code in version 2 is where the problem comes in.

I noticed it with 3 systems all in incubation (in simulation) and all with the "if markposition <> 0 and (some rule) then..."


Thanks for your help, will try this

Started this thread Reply With Quote
  #5 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360

So you are saying sometimes when running just version 1 by itself it messes up?

If so, I'd try the version I gave you, which puts "sell" and "buytocover" first.

Technically, my addition should not be needed at all, but it might be worth trying.

Again, it could be a SIM issue too. I don't use SIM at all. But I have seen real account trading issues (rarely).

Follow me on Twitter Reply With Quote
  #6 (permalink)
 bchip 
Torino, Italy
 
Experience: Advanced
Platform: TradeStation
Trading: ES,YM,CL,GC
Posts: 132 since Sep 2017
Thanks Given: 160
Thanks Received: 116


kevinkdog View Post
So you are saying sometimes when running just version 1 by itself it messes up?

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.

Started this thread Reply With Quote
  #7 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360


bchip View Post
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.

If version 1 works perfectly, why did you add the code for version 2? I think I am missing the reason you added version 2 code.

I don't see how version 2 adds any new conditions or anything. Version 1 covers it all, since "buy" means first "buytocover" then go long.


As far as marketposition=1 in general, remember it is evaluated as of the bar close, so it is easy to see weird things and unexpected behavior.

Follow me on Twitter Reply With Quote
  #8 (permalink)
 bchip 
Torino, Italy
 
Experience: Advanced
Platform: TradeStation
Trading: ES,YM,CL,GC
Posts: 132 since Sep 2017
Thanks Given: 160
Thanks Received: 116


kevinkdog View Post
If version 1 works perfectly, why did you add the code for version 2? I think I am missing the reason you added version 2 code.

I don't see how version 2 adds any new conditions or anything. Version 1 covers it all, since "buy" means first "buytocover" then go long.


As far as marketposition=1 in general, remember it is evaluated as of the bar close, so it is easy to see weird things and unexpected behavior.

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




Previous trade with that system:

Started this thread Reply With Quote
  #9 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360

Yes, it missed a trade, but it is hard to tell why from the info provided.

Follow me on Twitter Reply With Quote
  #10 (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


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;

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on April 12, 2022


© 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