Search | User Listing Vidsite | Calendars | Quotes
Home Page ->  OLang, OScript & OPilot -> Writing OLang Stops -> View ThreadLogon (or Register, or Join TradeTight)

You are logged in as a limited-access Guest.To join TradeTight, first read the info in the Organization & Content room, then click the link above. 

1 Timothy 6:17 (ESV) ... As for the rich in this present age, charge them not to be haughty, not to set their hopes on the uncertainty of riches, but on God, who richly provides us with everything to enjoy.


Conditional Entry at Close, using Close
Jump to page : 1
Now viewing page 1 [50 msgs/pg]
Jump to room :
JimDean
Posted 9/27/2014 6:30 AM (#5995)
Subject: Conditional Entry at Close, using Close



Owner/Admin

Posts: 3925
2000100050010010010010025
Location:
USA: GA, Lawrenceville
Click here to see a request that was made by Vinay ... I want to delay Entry by 1 bar (EOD) for which one of your suggestion was to use N-bar Stop as Entry condition in the Trade Plan. It works but works erratically. Many times instead of next bar the Trade Plan enters into trade after 2-3 bars. However I have found (from my limited testing) that if I put "0" as value of the Nbar then it enters the trade on the next bar. But my problem is that I want to take the long trade next day on MOC basis only if the prices are lower than previous (Signal Bar) close and vice versa for short trade. I would appreciate if you can help me out in coding this Stop. Thanks in advance.

The solution requires a "compromise" - you want to EXIT at Close, using an MOC order, but you also want to make that Entry DEPENDENT on what the Close price is for the very same day. The two things can't happen simultaneously, so you have to "give a little" on one or the other.

The solution is different for EOD feed versus RT feed. Which are you using? I'll assume EOD for now ...

One EOD (or RT) solution is to enter at the next bar's Open, in which case you can check the true, final Close of the day in question the evening before entry day, and use an MOO order. If that is acceptable, do your Download AFTER the exchange closes, look for an exit signal, and fire off an MOO order to your broker. The code is simple:

#Stop
#param "DelayDays", 1, 0, 3
dim BarCnt, ChkBar as integer
dim SigC as string
' save the the Signal bar's Close & set up constants
if BarCnt = 0 then
SigC = C
ChkBar = 1 + DelayDays
elseif bar=ChkBar then
if Signal = LongSignal then
if C < SigC then Signal = ExitSignal
else ' ShortSignal
if C > SigC then Signal = ExitSignal
endif
end if
' increment the BarCnt
BarCnt += 1

This code executes ONLY on the day you specify (not before or after) ... if the condition is not met on that day, you need to structure your TradePlan to jump to the end without ever executing the trade. So, in your first TradePlan Step, you must have two conditions ... the first references this stop routine, and has your MOC order attached (yes I know it says "Stop" & "Exit" and you want to "go" & "Enter" but hey there is no other syntax that N has created yet), and it Jumps to the next Step (presumably your Exit control step). The second condition of your first (entry) step must have a condition Nbar=DelayDays (fill in the value), and NO order, and on the far right, jump to "End". This should cause your TradePlan to close without creating an order, if the order was not filled on the desired day before the Close.

That's all it "should" require, if you're using EOD, and if you are OK with "giving a little" re your logic, and entering on the next bar's Open.

But, that's not what you requested, so the next post will give another possible solution ...
Top of the page Bottom of the page
JimDean
Posted 9/27/2014 6:48 AM (#5997 - in reply to #5995)
Subject: Conditional Entry at Close, using Close



Owner/Admin

Posts: 3925
2000100050010010010010025
Location:
USA: GA, Lawrenceville
Another solution solves the EOD need by "giving" a little on the other side ... instead of using the "true Close", you use a price that occurs just before the Close as a surrogate for the true Close. If you do it this way, then you can use the MOC order to exit at the Close on the same day.

To do this, you use the SAME code as the prior post, but you execute it differently. This only works for EOD ... and it's important to keep in mind that EOD's data feed has approx 20 min delay ... so the prices that you "see" from an EOD download at 3:50pm are actually the prices from 3:30pm.

This method uses the same TradePlan as described in the prior post, BUT, you manually (or via OPilot) wait to do your download until approximately ten minutes before the exchange session ends (4pmEST, for example => do the download at 3:50pmEST). You might need to do it earlier than that, say at 3:40pm, if you have a very long focus list to process, or a very complex strategy that takes a while to calc, or a very slow computer, or if you use manual order entry via a phone call. So, adapt the time accordingly but remember that there is a 20-min delay in the prices.

The "trick" here is that for Exit (or pending Entry Signal) purposes, OT treats the most recent bar as if it was a complete bar for that period. Signals don't appear as solid triangles until the next bar has Opened ... but Exits can occur "during" the actual bar. The trick is that if you do several EOD downloads during the course of the day, then EVERY TIME you download, the Stop code will run, THINKING that it's the end of the day, and therefore potentially firing an Exit order.

Of course if you are manually entering the orders then you can do earlier downloads with impunity - just ignore any signalled exits until your final just-before-4pm download and recalc. But if you use OPilot you'll have to restrict it (for EOD) to just the one final download.

So ... download a few min before the end of the session, let the recalc complete, and if you see an exit, send a MOC order to the Broker.

Hopefully that's clear enough. If you are using RT feed, the next post provides a solution ...
Top of the page Bottom of the page
VinayJain
Posted 9/27/2014 6:52 AM (#5998 - in reply to #5995)
Subject: Conditional Entry at Close, using Close


Regular

Posts: 59
2525
Location:
India: , Bangalore
Thanks Jim for your valuable time and efforts in trying to help me.

I think a little background info will help you better understand what I want and why?

I am using a RTM strat which enters on MOC basis. So I download EOD data just before market close, run analysis and enter the trade. However I have noticed that many times it would have been better if I had waited for one more day and taken the trade on next day's MOC. I want to test (PortSim) whether waiting for one more day gives me sustainable advantage. This is for this reason I want this entry condition to be coded.

Top of the page Bottom of the page
JimDean
Posted 9/27/2014 7:03 AM (#5999 - in reply to #5997)
Subject: Conditional Entry at Close, using Close



Owner/Admin

Posts: 3925
2000100050010010010010025
Location:
USA: GA, Lawrenceville
Finally ... if you are using RT and not EOD, you can "shave things closer" in a couple of different ways. From reading your original post, I'm guessing you are on EOD, so I'll just provide a sketch of the RT solution paths here.

FIRST solution: if you are using DAILY bars in RT (that is, your Strategy logic uses Daily bars throughout), then you set up code similar in structure to the earlier post, but include some extra code for "system time checking". I've written about this in several other places in the forum ... do a search on the Phrase "system time" to find them ... here is the main reference:
http://tradetight.org/forums/thread-view.asp?tid=1053&posts=8#M5746

Using those local-computer-time calls, plus appropriate input parameters where you set the time-window that the exit-tests are allowed, you test the bar's "current Close" (ie last, most recent print that the feed delivered) just before the session ends, again allowing for calc time and time to contact your broker with an MOC order

The big difference here is that you don't have the 20min delay ... for some symbols, that may matter a lot, and for others it will produce no meaningful difference.

ANOTHER solution for RT: if you are using INTRADAY bars for your strategy logic, then the code structure is "generally" similar but you have to account for the fact that the prior day's Close is not the close of the Signal bar ... it's a bunch of bars earlier ... and you need to account for the actual bar duration when you pick your time window for execution near the end of the day. As I recall, the Search I suggested turns up a post from TomH where he worked on something like this ... maybe that will give you some clues.

Suffice to say that an RT solution to your request, when intraday bars are used by the Strat, is doable, and elegant, but not in the "freebie" category (at least not today ... I've got a huge amount of stuff on my plate).

So, I hope that this fairly lengthy response is enough to get you on the right track.
Top of the page Bottom of the page
VinayJain
Posted 9/27/2014 7:42 AM (#6000 - in reply to #5995)
Subject: Conditional Entry at Close, using Close


Regular

Posts: 59
2525
Location:
India: , Bangalore
I tried the code and followed all the steps you mentioned in the first post but unfortunately it is not working. I am only getting solid triangles on the voteline with the message in the Advisor saying "Not Filled". Thanks nonetheless for your efforts.
Top of the page Bottom of the page
JimDean
Posted 9/27/2014 8:09 AM (#6001 - in reply to #6000)
Subject: Conditional Entry at Close, using Close



Owner/Admin

Posts: 3925
2000100050010010010010025
Location:
USA: GA, Lawrenceville
If you'd like further help then you'll need to attach snapshots and execution-related files so that I can dup your situation ... sometimes the entry conditions can be made to work but it takes more than just a casual attempt - experimentation is key.

Hopefully Nirvana will find time to do a major overhaul of TradePlan related stuff, including conditional entries. It will however be a big project so we will probably have a significant wait. That's why, if you really *need* this solution, it calls for try and try again.
Top of the page Bottom of the page
Jump to page : 1
Now viewing page 1 [50 msgs/pg]
( E-mail a Link | Printer Version | Search Room )

Owner of site: Jim Dean -- Forum content is confidential, and may not be distributed without written permission.