Hello,
What i would like help with is if my system opens up X amounts of positions i want them all to count as 1. Meaning when all of the positions is = to X amount of + pips/$ i want it to close all open positions. The problem i have atm is that my system closes one of the positions as soon as it hits TP and dont care if there is more then 1 position open.
this is what is what im using atm to close my open positions if there is more then 1 open;
IF countoflongshares=>2 AND close>(positionprice+extrapips*pipsize) THEN
SELL AT MARKET
ENDIF
All open positions are always closed at one time. Whenever you place a SELL order ProOrder closes all positions in live trading, while ProBackTest allows closing partial positions.
In case of multiple open positions there can only be one SL and one TP.
marcuscn – Please use the ‘Insert PRT Code’ button when posting code to make it more readable. I have tidied up your post 🙂
Your code will close all positions at the close of a candle if price is extrapips above your average entry price. To close mid candle you need to calculate your desired exit prices for TP and SL and use LIMIT and STOP orders. Your code cannot know what your average entry price is until the close of the following candle which might lead to some issues however. You need to send these STOP and LIMIT orders every candle as they only last for one candle before being cancelled.
Example for long positions:
tp = positionprice + extrapips
sl = positionprice - extrapips
sell at tp limit
sell at sl stop
thanks. this should work?
IF not daysForbiddenEntry and MAXSHARES and onmarket = 0 then
if diff1 > gransvarde1 and n1 and n2 and c1 and c2 and c3 and c4 and onmarket = 0 then
BUY 1 SHARES AT MARKET
ENDIF
ENDIF
if MAXSHARES and countofposition = 1 and positionprice - close > 20 then
buy 2 shares at market
endif
extrapips=5
IF LONGONMARKET AND countoflongshares=>3 AND close>(positionprice+extrapips*pipsize) THEN
SELL AT MARKET
endif
That will work as long as you are happy to close any long position only at the close of a candle. If you want to close mid candle then use my previous suggestion.
Okey, well i dont have a tp only trailing. But when i use ur code to close it dont work at all, maybe u can help me. I use this;
//trailing stop function
trailingstart = 14
trailingstep = 5
//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
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
extrapips1 = 20
tp = positionprice + extrapips1
sell at tp limit
How you have coded it will not work in the candle that the position is opened on as there is no value for POSITIONPRICE when the decision to enter is made and if you add to a position then the new value for POSITIONPRICE is not known until the next candle is closed. To overcome this you have to assume that the open of the next candle is the same as the close of the candle where the decision to buy is made and place a pending limit order at the same time as you enter based on this value. Then use the actual POSITIONPRICE once it is known at the close of a candle when you are actually on the market and not looking to buy again.
Something like this (not tested):
if longonmarket then
sell at positionprice + 20 limit
endif
if (your long conditions) then
buy 1 contract at market
if longonmarket then
sell at (((positionprice*countofposition) + close)/(countofposition+1)) + 20 limit
else
if not longonmarket
sell at close + 20 limit
endif
endif
endif