1/2 POSITION ON TARGET
04/07/2020 at 5:38 AM #124950
Within the last couple weeks, i have downloaded ProRealTime and have started to learn how to programme a simple idea i have had. Ive learn alot since starting and looking through the library of code on here and really helped but I am currently stuck on 2 points and wondered if anyone would be able to help me with these please.
These are quite basic questions but i just cant figure out how you would write these scenarios in code:
- If my code opens a position (either long or short) with 2 contracts on an action, how can i set the code to close 1 of the contracts on a profit target and leave the remaining running until another action closes it? Is that possible? This would really help to maximise profits but limit risk.
- Also, how can I get my code to close a position (long or short) if there are say 4 candles in the row that have only moved a maximum of say 2 points in either direction?
Thanks in advance04/07/2020 at 7:59 AM #124957
Use LIMIT orders. You have to place one at the time you enter the trade (you can only use the closing price to calculate the exit at this point and hope there is no big gap!) so that it is on the market from the moment the 2 positions are opened. After the first bar closes you can calculate the exit price based on the actual price you bought at. Limit orders last only one bar so you then have to keep sending them to the market on every candle.
You should be aware that at the current time it is not possible to run a strategy either live in demo or in real live trading that uses partial position closure. You can back test ideas though.
In this example the first position is closed out at 1% profit.1234567891011121314151617181920212223242526272829if not onmarket and (your long entry conditions) thenbuy 2 contracts at marketprofitprice = close * 1.01sell 1 contract at profitprice limitendifif longonmarket and countofposition = 2 thenprofitprice = tradeprice * 1.01sell 1 contract at profitprice limitendifif longonmarket and countofposition = 1 and (your long exit conditions) thensell at marketendifif not onmarket and (your short entry conditions) thensellshort 2 contracts at marketprofitprice = close * 0.99exitshort 1 contract at profitprice limitendifif shortonmarket and countofposition = -2 thenprofitprice = tradeprice * 0.99exitshort 1 contract at profitprice limitendifif shortonmarket and countofposition = -1 and (your short exit conditions) thenexitshort at marketendif04/07/2020 at 8:04 AM #12495804/07/2020 at 12:08 PM #125005
Thanks very much for coming back to me with the above, much appreciated!
Oh right, is there a reason that we are not currently able to do partial position closure? Thats a shame….so the code you’ve sent me above for the limit order wouldnt actually work on the live or live demo system, only on back test?
Thanks again04/07/2020 at 12:27 PM #125009
The PRT platform has the ability to do partial position closure but the broker IG doesn’t allow it via automated trading at the moment. The closest we can get is to use SET STOP and SET PROFIT instructions that allow positions to be closed separately. All positions however have exactly the same stoploss and takeprofit distances from their opening price set to whatever the last SET instruction values were.04/07/2020 at 12:45 PM #125019
Thanks again, i would never have worked all this out without speaking to you! 🙂
I know this is going to be a dumb question but i just want to make sure i understand it correctly….how exactly do we use the SET STOP and SET PROFIT instructions to close positions separately?04/07/2020 at 1:05 PM #12502212345678set target pprofit 2set stop ploss 2set set target %profit 2set stop %loss 2set target profit 2set stop loss 2
Whenever one of the above instructions is read at the close of a candle then the stop loss and target orders are sent to the market and effect all open positions. The orders stay on the market until the position they are attached to are closed or until a new SET order is sent. If you change the 2 to a 3 then all positions will have their exit orders changed to 3 pips or 3%.04/07/2020 at 1:33 PM #125035
Thanks again for explaining, this makes it alot easier to understand.
One last question i promise, then i’ll leave you alone 🙂
Within my code, my buy instructions are looking at when exponential lines cross but also to then count 2 candles after the lines cross to ensure that the market is still heading that way at that point….the code i have used is below:
Example of buy order:12345c1 = (indicator1 CROSSES OVER indicator2)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND open < close AND open < close THENBUY 1 CONTRACT AT MARKETENDIF
But i’m still getting buys (or shorts) in my backtesting when the candles arn’t as i’ve set in the code, i think i’ve set it so it doesn’t buy before the indicators cross and there are 2 subsequent candles that are green before buying? Or have i got the code wrong? I have done the same but in reverse for shorts.
Thanks again04/07/2020 at 1:41 PM #125043
There are some simple rules that everyone using the forums is expected to follow. Your post has broken one or more of these rules.
The forum rules are as follows. I have highlighted in bold the rule/rules that you have not followed:
Post your topic in the correct forum.
ProRealTime Platform Support only platform related issues.
ProOrder only strategy topics.
ProBuilder only indicator topics.
ProScreener only screener topics
General Discussion any other topics.
Welcome New Members for new forum members to introduce themselves.
Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
Do not double post. Ask your question only once and only in one forum. All double posts will be deleted anyway so posting the same question multiple times will just be wasting your own time and will not get you an answer any quicker. Double posting just creates confusion in the forums.
Be careful when quoting others in your posts. Only use the quote option when you need to highlight a particular bit of text that you are referring to or to highlight that you are replying to a particular member if there are several involved in a conversation. Do not include large amounts of code in your quotes. Just highlight the text you want to quote and then click on ‘Quote’.
Give your topic a meaningful title. Describe your question or your subject in your title. Do not use meaningless titles such as ‘Coding Help Needed’.
Do not include personal information such as email addresses or telephone numbers in your posts. If you would like to contact another forum member directly outside of the forums then contact the forums administrator via ‘Contact Us’ and they will pass your details on to the member that you wish to contact.
Always be polite and courteous to others.
I have edited your post where required. Please ensure that your future posts meet these few simple forum rules. 🙂04/07/2020 at 1:42 PM #125044
You need to check C1 two candles back.12345c1 = (indicator1 CROSSES OVER indicator2)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND open < close AND open < close THENBUY 1 CONTRACT AT MARKETENDIF
So this will now need a cross followed by two green candles.
1 user thanked author for this post.04/07/2020 at 1:44 PM #12504704/07/2020 at 1:52 PM #125052