ProRealCode - Trading & Coding with ProRealTime™
I need some help with this code please. First of all i cant run it on pro order because i get an error saying combined stops can not be used with pro order. Is there anyway i can change code so that it places sl first and when trailing conditions become true it cancels SL and continue with trailing? Will this be possible and how must i change the code..
Second I want to add like a buffer to the entry. For example if the close of candle is at 50 000 i want to add maybe 20 points so that my entry is at 50 020 and not exactly 50 000. Same for sell but just -20 points. Can someone please help me to change the code
DEFPARAM CUMULATEORDERS=FALSE
DEFPARAM FLATAFTER = 091500
BeginTime = 090000
LossValue = 15
TraillingValue = 5
TargetValue = 30
IF Time = BeginTime THEN
Condition1 = (Close > Open)
Condition2 = (Close < Open)
IF Condition1 THEN
BUY 6 SHARE AT MARKET
ELSIF Condition2 THEN
SELLSHORT 6 SHARE AT MARKET
ENDIF
ENDIF
SET STOP LOSS LossValue TRAILING TraillingValue
SET TARGET PROFIT TargetValue
Line 20 is not supported you have to choose whether using SET STOP LOSS LossValue or SET STOP TRAILING TraillingValue.
Even if you write them on separate lines, the last line will override the previous one, no matter the order.
I, and almost everybody else on this forum, use SET STOP LOSS LossValue, then add a snippet of code to trail stops in non-native form. Read https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/ for a full working breakeven/trailing stop code.
You may also read:
https://www.prorealcode.com/blog/learning/trailing-stop-max-favorable-excursion-mfe/
Hi
Thank you for the reply.. I will try and change the code. DO you know how i can change my code to not buy exactly on the close high but to add 20 points. Because i need a small buffer. allot of trades just opens a buy immediately but then goes the other way. and if I had a small buffer of like 20 points it would not have taken the buy but the sell instead.
I hope its clear to understand.
Thanks for all the help
The simplest way is to use a pending Long(Short order, your line 13 would be (same for line 16, but using SELLSHORT):
BUY 6 SHARE AT close + 20 * pipsize STOP
but pending orders only last one bar, so if a trade is not triggered and you still want it to be, you’ll need to place it again and again each candle.
Or you can slightly modify your code to save your desired price (the one you had increased, then check when CLOSE is >= and enter a trade).
Hi
Thank you very much for all your help and on the forum.. I changed the whole code with some codes you gave on other topics. I need your help please for the last time.(I hope)
Thank you
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 091500 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 5
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
SET STOP LOSS 150
IF time = starttime THEN
MaxPrice = highest[1](high) //Il massimo/minimo delle ultime 12 barre
MinPrice = lowest[1](low) // (ogni ora = 12 barre da 5 minuti)
ENDIF
//************************************************************************
// trailing stop function
trailingstart = 20 //5 trailing will start @trailinstart points profit
trailingstep = 60 //5 trailing step to move the "stoploss"
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pointsize THEN
newSL = tradeprice(1)+trailingstep*pointsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pointsize THEN
newSL = newSL+trailingstep*pointsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pointsize THEN
newSL = tradeprice(1)-trailingstep*pointsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pointsize THEN
newSL = newSL-trailingstep*pointsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
// LONG
if not onmarket and starttime then
buy Nlots contract at MaxPrice + 20 * pointsize STOP
if countofposition = 1 then
buy 1 contract at tradeprice + 50 stop
endif
sellshort Nlots contract at MinPrice - 20 * pointsize STOP
MaxPrice = 999999
MinPrice = 0
ENDIF
if countofposition = 1 then
buy 1 contract at tradeprice + 50 stop
endif
if countofposition = -1 then
sellshort 1 contract at tradeprice - 50 stop
endif
I already answered this question for you in one of your similar topics. You seem to be asking the same questions in several different places!
You have to use MTF and trade in a faster time frame if you want to cancel a pending order when an opposite order is filled.
HI
Yes but this is a new code that I need help with. So what you are telling me is that its not possible to cancel another pending order if one is activated?
I dont have much coding experience and i need help please I am trying my best to code and learn but somethings i cant get right to work.. Thats why i am here to ask the experts.
Please help me to get this code right.
Thank you
Pending orders CANNOT ba cancelled because they are automatically cancelled by ProOrder at the closure of each candlestick. You simply need not place it again.
Yes it is possible to cancel one order if another one is filled (well within 1 second) but as I showed you in the other topic in order to cancel one order if another one is filled you have to use MTF and trade on the 1 second time frame otherwise the two orders are on the market until the close of whatever time frame candle you are using. So if it s a 15 minute chart you have 15 minutes in which both orders could be filled.
As I also said before using the 1 second chart will limit your available back test data to just about two and a half days if you have 100k bars and double that if you have 200k bars.
So someone could spend some time trying to work out what you are trying to do with the code (which seems a bit confused at the entry and exit part and also seems to be just other bits of code mixed together) but I doubt very much whether you will be happy with the results or that the back test on only two and a half days data would tell you very much at all about the strategies robustness.
Perhaps you should just set out in clear and simple terms what the complete strategy theory is and then maybe someone who has been messing around with strategies for a long time can tell you why your theory might or might not work before someone goes to a lot of effort to convert it to a code that you are not happy with.
I spent five minutes on your last code example and I got it to at least open and close trades. Whether they are what you want I have no idea!
Launch it on 1 second chart.
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 091500 //21:00
DEFPARAM PreLoadBars = 2000
timeframe(1 second)
ONCE nLots = 5
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
timeframe (15 minute, updateonclose)
IF time = starttime THEN
MaxPrice15 = highest[1](high) //Il massimo/minimo delle ultime 12 barre
MinPrice15 = lowest[1](low) // (ogni ora = 12 barre da 5 minuti)
ENDIF
timeframe(1 second)
//************************************************************************
// trailing stop function
trailingstart = 20 //5 trailing will start @trailinstart points profit
trailingstep = 60 //5 trailing step to move the "stoploss"
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pointsize THEN
newSL = tradeprice(1)+trailingstep*pointsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pointsize THEN
newSL = newSL+trailingstep*pointsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pointsize THEN
newSL = tradeprice(1)-trailingstep*pointsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pointsize THEN
newSL = newSL-trailingstep*pointsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
// LONG
minprice = minprice15
maxprice = maxprice15
if not onmarket then//and starttime then
buy Nlots contract at MaxPrice + 20 * pointsize STOP
sellshort Nlots contract at MinPrice - 20 * pointsize STOP
MaxPrice = 999999
MinPrice = 0
ENDIF
if countofposition = 1 then
buy 1 contract at tradeprice + 50 stop
endif
if countofposition = -1 then
sellshort 1 contract at tradeprice - 50 stop
endif
SET STOP LOSS 150
Hi Guys
Thank you for the help.. I know my code is a few codes put together but it sort of does what is needed. I am not a coder and can really find someone who will write a complete code for me so i tried myself I want it to place a trade on the break of the 8:55 candle when market opens at 9:00. So when break up it buys and when breaks down it sell. I wonder of i can just change it somehow not to place pending order buy just buy at market when conditions are met. So not place stop order but just place order at market when price reaches the break, I am running it on South African 40 on 5 min timeframe. If you run it you will see that because of pending order it enters 2 trades on the same candle. Thats why i want to try and change it to just buy when it reaches conditions. If you can just help me to change the code so it buys at market rather then placing pending orders. And if buy is triggered it will not enter sell again if price moves a little down again. Because like you said it wont be possible to cancel pending orders in the same candle. With my exit of course i am trying to trail it as far as possible.
Thank you in advance
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 210000 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 10
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
SET STOP LOSS 150
IF time = starttime THEN
MaxPrice = highest[1](high) //Il massimo/minimo delle ultime 12 barre
MinPrice = lowest[1](low) // (ogni ora = 12 barre da 5 minuti)
ENDIF
//************************************************************************
// trailing stop function
trailingstart = 20 //5 trailing will start @trailinstart points profit
trailingstep = 80 //5 trailing step to move the "stoploss"
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
// LONG
if not onmarket and starttime then
buy Nlots contract at MaxPrice + 20 * pointsize STOP
if not onmarket and starttime then
sellshort Nlots contract at MinPrice - 20 * pointsize STOP
MaxPrice = 999999
MinPrice = 0
endif
endif
In PRT you have two choices pending orders or buy at market. If you place any pending order then it will be on the market from the open of the next candle until the candle closes. There is no way to cancel it mid candle and there is no way to cancel any other orders mid candle if another order is filled. In fact there is absolutely no way to add an order or change an order mid candle.
At market orders will simply be filled or closed at the opening price of the next candle.
So unless you use a very fast time frame such as the 1 second time frame just like I have shown you on what now feels like several occasions then you are trying to do the impossible. It is not possible to buy at market mid candle – it is not possible to cancel orders mid candle – unless you do what I have suggest many times and use MTF!
Yes this i understand maybe you understood me wrong.. but because of this problem i just want it to buy on the high of the candle and sell on the low. It must not place any stop/pending orders but just buy on the high and sell on the low. like you do it manually if price reach the high i just click on buy or when it reaches low i just click on sell.. So if i understand correct it is not possible to do this? I dont want to place any pending order rather just buy or sell on this condition. Can it be done? I hope its clear. I know about the pending orders but cant it just buy and no go into a sell while still in the buy trade?
So when you say ‘buy’ you really mean SELLSHORT and when you say ‘sell’ you really mean EXITSHORT. Trying to decipher what you are actually trying to do is hard enough without you saying buy when you mean sell!
All orders are sent to market at candle open so if you want to set a price to SELLSHORT at then you can use a pending LIMIT order to enter at a better price and if you know what price you want to EXITSHORT at then send another pending LIMIT order to market to exit at at a better price than your entry price.
To do what you want to do which is buy/sellshort at a price or sell/exitshort at a price you HAVE to use pending orders.
So the conclusion is that – no you can’t do what you want to do -without use pending orders.
//-------------------------------------------------------------------------
// Main code : MySystem(13)
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Main code : MySystem(8)
//-------------------------------------------------------------------------
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 210000 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 5
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
SET TARGET PROFIT 120
IF time = starttime THEN
MaxPrice = highest[6](high) //Il massimo/minimo delle ultime 12 barre
MinPrice = lowest[6](low) // (ogni ora = 12 barre da 5 minuti)
ENDIF
//************************************************************************
// LONG
if not onmarket and starttime then
buy Nlots contract at MaxPrice + 20 * pointsize STOP
set stop loss close- Minprice + 20 * pointsize
if not onmarket and starttime then
sellshort Nlots contract at MinPrice - 20 * pointsize STOP
set stop loss MaxPrice - close - 20 * pointsize
MaxPrice = 999999
MinPrice = 0
endif
endif
Hi
Thank you for all the responses.. Can you please tell me whats wrong why the system will place trades on some days and other days not even though conditions were met? You can run it on 5 min chart South african 40.
Thanks
Adjustment in code because combined stops can not be used with pro order
This topic contains 34 replies,
has 4 voices, and was last updated by Vonasi
6 years, 5 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 09/03/2019 |
| Status: | Active |
| Attachments: | 2 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.