Now that multi-timeframe support is available for ProOrder, there are many new possibilities for us, including position management in a more refined and faster way than before.
Indeed, it is now possible to trade a strategy that makes trading decisions in a timeframe different from the one we will use to manage its positions.
In this article, I’m going to expose a rather simple function of partial closure of position when the price retraces to your disadvantage.
Entering position is easy! But how to get out? It is often much more difficult to manage a position exit than to find the best way to enter it.
Should we close on a contrary signal? On a support or a resistance? Or through a thousand other arithmetic tests based on empirical statistics? Hard to say. In fact there would be only one good way, it is based on the behavior of the price, it is the purpose of the function below.
The idea of the function is simple:
If the price was in profit of X points, then one starts the function of partial closure. At this point, if the price retrace Y points from the highest price (for a long order), then we close Z percent of the remaining position. The highest price reached is then reset to the current price. And so on until exhaustion of the position (its total closure).
That’s the function code, which incorporates a simple SuperTrend indicator-based strategy on a 15-minutes timeframe:
defparam cumulateorders=false
// --- Partial Close settings ---
size = 10
mincontracts = 1
trigger = 20
pointback = 5
closepercent = 20
// ------------------------------
timeframe(15 minutes,updateonclose)
st = Supertrend[3,10]
if not longonmarket and close crosses over st then
buy size contracts at market
set stop ploss 15
endif
if not shortonmarket and close crosses under st then
sellshort size contracts at market
set stop ploss 15
endif
timeframe(1 minute)
if not onmarket then
AllowPartialClose=0
endif
//## -- PARTIAL CLOSE PRICE BACK FUNCTION -- ##
//## - BUY ORDERS
if longonmarket then
//trigger for the partial closure function to start
if close-tradeprice>=trigger*pointsize then
AllowPartialClose = 1
endif
if AllowPartialClose then
//compute the maxprice reached
maxprice = max(maxprice,close)
//check to trigger a partial closure or not
if maxprice-close>=pointback*pointsize then
//close partially
sell max(mincontracts,size*(closepercent/100)) contracts at market
//reset the maxprice to the current price
maxprice = close
endif
endif
endif
//## - SELLSHORT ORDERS
if shortonmarket then
//trigger for the partial closure function to start
if tradeprice-close>=trigger*pointsize then
AllowPartialClose = 1
endif
if AllowPartialClose then
//compute the maxprice reached
minprice = min(minprice,close)
//check to trigger a partial closure or not
if close-minprice>=pointback*pointsize then
//close partially
exitshort max(mincontracts,size*(closepercent/100)) contracts at market
//reset the maxprice to the current price
minprice = close
endif
endif
endif
graph st coloured(200,100,200) as "Supertrend"
graph Close
The parameters are as follows:
Size of the initial position opened by the strategy.
Minimum contract size to close (corresponds to the minimum size possible for the instrument), so to set according to the instrument on which you will perform this function.
From how many points of profit the function will start
Step to close a position, in points, since the highest price (BUY) or lowest (SELLSHORT) ever reached
Closure size at each level, in this example, 20% of “size” = 10*0.2 = 2 contracts
Many ideas could be added to this way of managing closures, for example: