NexusFi: Find Your Edge


Home Menu

 





Coding a simple entry and exit


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one johni with 4 posts (0 thanks)
    2. looks_two Jura with 3 posts (3 thanks)
    3. looks_3 RM99 with 3 posts (2 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 14,648 views
    2. thumb_up 5 thanks given
    3. group 2 followers
    1. forum 10 posts
    2. attach_file 0 attachments




 
Search this Thread

Coding a simple entry and exit

(login for full post details)
  #1 (permalink)
johni
canada
 
Posts: 29 since Jun 2011
Thanks Given: 0
Thanks Received: 0

Entry : Pattern 1 setup
Entry at highest high for buy and lowest low for sell
Must be filled within 2 to 3 bars

Exit : 1) Profit target calculated using the entry setup highest high - lowest low add to the highest high for buy or subtract from the lowest low for sell
2) Or exit on the lowest low for buy and highest high for sell

How to code the above?

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Request for MACD with option to use different MAs for fa …
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Trader Dale [email protected] or Scammer Dale
Trading Reviews and Vendors
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Tao te Trade: way of the WLD
30 thanks
GFIs1 1 DAX trade per day journal
27 thanks
Supertradersams Thread Journal on NQ/MNQ
26 thanks
Deaddogs Stock Trading
24 thanks
Leaving CFD trading and learning futures
11 thanks

(login for full post details)
  #3 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690



johni View Post
Entry : Pattern 1 setup
Entry at highest high for buy and lowest low for sell
Must be filled within 2 to 3 bars

Exit : 1) Profit target calculated using the entry setup highest high - lowest low add to the highest high for buy or subtract from the lowest low for sell
2) Or exit on the lowest low for buy and highest high for sell

How to code the above?

On the top of my head, not compiled but it should get you started:
 
Code
                            
Vars:
    
myEntrySetupLong(false),
    
myEntrySetupShort(false),
    
barNumber(0),
    
limitPrice(0),
    
profitTarget(0),
    
exitPrice(0);
    
// Enter long setup
if ....pattern1.... then begin
    myEntrySetupLong 
true;
    
barNumber CurrentBar;            // store bar number
end else
    
myEnterSetupLong false;

// Enter short setup
if ....pattern1.... then begin
    myEntrySetupShort 
true;
    
barNumber CurrentBar;            // store bar number
end else
    
myEnterSetupShort false;
    
condition2 = (CurrentBar barNumber) < 4;            // enter within 4 bars

// Enter Long
if myEntrySetupLong true and condition2 true then begin
    limitPrice 
Highest(High10);                    // note: dynamic limit price!
    
exitPrice Lowest(Low10);
    
profitTarget limitPrice exitPrice;
    
Buy("EnterLong"1 contracts next bar at limitPrice limit;
end;

// Enter short
if myEntrySetupShort true and condition2 true then begin
    limitPrice 
Lowest(Low10);                    // note: dynamic limit price!
    
exitPrice Highest(High10);
    
profitTarget limitPrice exitPrice;
    
SellShort("EnterShort"1 contracts next bar at limitPrice limit;
end;

// Exit conditions for long
if MarketPosition 1 then begin
    Sell
("ExitLong"next bar at exitPrice stop;
    
Sell("TakeProfit"next bar at profitTarget limit;
end;

// Exit conditions for short
if MarketPosition = -1 then begin
    BuyToCover
("ExitShort"next bar at exitPrice stop;
    
BuyToCover("TakeProfit"next bar at profitTarget limit;
end
You need to check this code for yourself - I find your point 1 not very clear so I don't know if it's coded correctly.

Regards,

Reply With Quote
The following 2 users say Thank You to Jura for this post:
(login for full post details)
  #4 (permalink)
 RM99 
Austin, TX
 
Experience: Advanced
Platform: TradeStation
Trading: Futures
Posts: 839 since Mar 2011
Thanks Given: 124
Thanks Received: 704


johni View Post
Entry : Pattern 1 setup
Entry at highest high for buy and lowest low for sell
Must be filled within 2 to 3 bars

Exit : 1) Profit target calculated using the entry setup highest high - lowest low add to the highest high for buy or subtract from the lowest low for sell
2) Or exit on the lowest low for buy and highest high for sell

How to code the above?

He did a decent job above, but he's right. You're rules have logic "challenges." Spotting the Highest H and the Lowest L is only possible in retrospect, even if you set a time interval, you don't know it's the highest high until after it's come and gone. (same with LL).

I guess if you're interval holds (2-3 bars) you would seek 3 consecutive HH's and enter a long position or 3 consecutive lower lows and enter a short position.

Again, for your exits, you cannot declare "Highest" or "Lowest" in real time...only in retrospect. You can evaluate a 10 bar period, but again, if for instance, you have a spike on bar #3, how do you know that was the highest high? You simply know it's higher, you cannot enter or exit based on "est."

On a side note, even if you could, wouldn't you want to sell at the high"est" point and buy at the low"est" point?

Reply With Quote
The following user says Thank You to RM99 for this post:
(login for full post details)
  #5 (permalink)
johni
canada
 
Posts: 29 since Jun 2011
Thanks Given: 0
Thanks Received: 0

The code uses dynamic price for the exits and profit targets but the pattern uses a fix profit target and exit

The pattern requires H>H[1] and indicator 1 crosses 0 and next bar open < H

Once satisfied place a buy order on the current bar high

Once filled, calculate the profit target using current bar H-L, not dynamic, add to the H for the profit target

The exit price is the current bar low

The trade once filled would either profit or exit on the current bar low using fix profit target, H-L, add to H or exit on the current bar low

Both the exit using orders place on the profit target calculated or the current bar low and the code should not use next bar exit

Currently using ts2000i

Reply With Quote
(login for full post details)
  #6 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690

Can you show us your code with which you tried to tackle this problem? Then we can build on that going forward. An image would also be helpful.

Reply With Quote
(login for full post details)
  #7 (permalink)
johni
canada
 
Posts: 29 since Jun 2011
Thanks Given: 0
Thanks Received: 0

Hi Jura,

Did not have any code because the pattern is derived browsing and considering there may be an advantage

The trade is simple a pattern with a fix profit target calculated using the current bar high and low and a exit on the current bar low but only the high must be more compare to preivous high

Your code is correct except for the dynamic profit target and exit

Would you change the code to a fix proft target and exit using the current bar high and low?

Tks

Reply With Quote
(login for full post details)
  #8 (permalink)
 RM99 
Austin, TX
 
Experience: Advanced
Platform: TradeStation
Trading: Futures
Posts: 839 since Mar 2011
Thanks Given: 124
Thanks Received: 704


johni View Post
Hi Jura,

Did not have any code because the pattern is derived browsing and considering there may be an advantage

The trade is simple a pattern with a fix profit target calculated using the current bar high and low and a exit on the current bar low but only the high must be more compare to preivous high

Your code is correct except for the dynamic profit target and exit

Would you change the code to a fix proft target and exit using the current bar high and low?

Tks

That's what I was trying to tell you, you cannot determine the current bar's high/low until it's historical (in the past).

If your exit is based on this bar close, it may or may not be at the high/low. If you set the exit on the open of the next bar, again, it may or may not be at this bar's high low.

If you set the exit to run intrabar, every tick is evaluated and so if a bar opens at 100.00 and moves up to 100.01, then that is the current "high" and would trigger an exit.

Reply With Quote
(login for full post details)
  #9 (permalink)
johni
canada
 
Posts: 29 since Jun 2011
Thanks Given: 0
Thanks Received: 0


RM99 View Post
That's what I was trying to tell you, you cannot determine the current bar's high/low until it's historical (in the past).

If your exit is based on this bar close, it may or may not be at the high/low. If you set the exit on the open of the next bar, again, it may or may not be at this bar's high low.

If you set the exit to run intrabar, every tick is evaluated and so if a bar opens at 100.00 and moves up to 100.01, then that is the current "high" and would trigger an exit.

There is a misunderstanding

Understand what you are explaining

But the pattern is a historical pattern and the high and low are determined and orders are placed on these levels

Indicator 1 cross 0 and the bar high > previous bar high and next bar open < bar high place a buy order on the bar high
THE BAR HIGH IS DETERMINED BECAUSE THE NEXT BAR OPEN IS THE CURRENT BAR
No order is placed with next bar open > bar high must be < bar high

Should not say current bar high but rather previous bar high for order placement

Sell is the opposite

Reply With Quote
(login for full post details)
  #10 (permalink)
 RM99 
Austin, TX
 
Experience: Advanced
Platform: TradeStation
Trading: Futures
Posts: 839 since Mar 2011
Thanks Given: 124
Thanks Received: 704



johni View Post
There is a misunderstanding

Understand what you are explaining

But the pattern is a historical pattern and the high and low are determined and orders are placed on these levels

Indicator 1 cross 0 and the bar high > previous bar high and next bar open < bar high place a buy order on the bar high
THE BAR HIGH IS DETERMINED BECAUSE THE NEXT BAR OPEN IS THE CURRENT BAR
No order is placed with next bar open > bar high must be < bar high

Should not say current bar high but rather previous bar high for order placement

Sell is the opposite

////////////////////////////////////////////////////////////////////////////////////////////
Variables: Indicatorx(0);

if marketposition = 0 then begin

If Indicatorx crosses over 0 and H[1] > H[2] and O < H[1] then buy next bar at market;

if Indicatorx crosses under 0 and L[1] < L[2] and O > L[1] then sellshort next bar at market;

end;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


If you want it it tick/tick, then you'll have to turn IOG on [IntraBarOrderGeneration=True] and you'll have to hold the value for the indicator...........

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[IntraBarOrderGeneration=True]

Variables: Intrabarpersist Indicatorx(0);

indicatatorx = indicatorx;

if marketposition = 0 then begin

If Indicatorx crosses over 0 and H[1] > H[2] and O < H[1] then buy next bar at market;

if Indicatorx crosses under 0 and L[1] < L[2] and O > L[1] then sellshort next bar at market;

end;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Reply With Quote
The following user says Thank You to RM99 for this post:





Last Updated on July 1, 2011


© 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