# [R-SIG-Finance] Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel @yh@nyuk@e|78 @end|ng |rom gm@||@com
Sun Nov 8 13:40:59 CET 2020

```Ilya thanks for your reply, your help is much appreciated

Yes I am familiar with ordersets and orderchains, but my question is
related to how stop loss is applied to each trade

Below I put a minimal example of a strategy where we only open long trades
with a size of 100 lots using moving average crossover, and we have a stop
loss with a threshold of 0.001.

After running the strategy on GBPUSD data, please have a look at the 3rd,
4th and 5 th transactions. In these transactions, the strategy first have a
long trade with  100 lot, then another long trade with 100 lot (so now our
position is 200 lots), and then the stop loss rule is executed and the
whole 200 lots will be closed, which means that the stop loss rule is based
on the whole position that we have.

Every time the strategy adds new long positions to the existing ones (like
pyramiding), the stop loss rule is executed on the whole position and
position is always flattened.

My question is that is there a way to apply the stop loss rule
**seperately** for each of the 100 lot trades, which may have different
cost basis, so may have different stop loss prices? In this case, for
instance, each stop loss will be executed independently and each stop loss
transaction will have a size of -100 lots.

Regards
Ayhan

###########################################
# Example strategy

library(quantstrat)

source(paste0(path.package("quantstrat"),"/demo/luxor.include.R"))
.fast = 5
.slow = 10

source(paste0(path.package("quantstrat"),"/demo/luxor.getSymbols.R"))

### blotter

initPortf(portfolio.st, symbols='GBPUSD', currency='USD')
initAcct(account.st, portfolios=portfolio.st, currency='USD')

### quantstrat

initOrders(portfolio.st)

### define strategy

strategy(strategy.st, store=TRUE)

arguments = list(
x = quote(Cl(mktdata)[,1]),
n = .fast
),
label="nFast"
)

arguments = list(
x = quote(Cl(mktdata)[,1]),
n = .slow
),
label="nSlow"
)

### signals

arguments = list(
columns=c("nFast","nSlow"),
relationship="gte"
),
label='long'
)

### rules ############
# normal entry rules

arguments=list(sigcol='long' , sigval=TRUE,
replace=FALSE,
orderside='long' ,
ordertype='stoplimit',
prefer='High',
threshold=.threshold,
TxnFees=0,
orderqty=100,
orderset='ocolong'
),
type='enter',
timespan = .timespan,
label='EnterLONG'
)

# stop-loss

arguments=list(sigcol='long' , sigval=TRUE,
replace=FALSE,
orderside='long',
ordertype='stoplimit', tmult=TRUE, threshold=0.001,
TxnFees=.txnfees,
orderqty='all',
orderset='ocolong'
),
type='chain', parent='EnterLONG',
label='StopLossLONG',
enabled=TRUE
)

applyStrategy(strategy.st, portfolio.st)

View(getOrderBook(portfolio.st)[[portfolio.st]]\$GBPUSD)

View(getTxns(portfolio.st,"GBPUSD"))

On Sun, 8 Nov 2020 at 01:40, Ilya Kipnis <ilya.kipnis using gmail.com> wrote:

> Ayhan,
>
> If I'm understanding correctly:
>
> Are you familiar with order sets?
>
> By defining a stop loss that works on a particular parent rule, that
> should do the trick.
>
> See this post:
>
> -Ilya
>
> On Sat, Nov 7, 2020 at 4:12 PM Ayhan yuksel <ayhanyuksel78 using gmail.com>
> wrote:
>
>> Dear all,
>>
>> In quantstrat, as described in package documentation, orders turn into
>> transactions,  transactions turn into positions, positions turn into
>> trades. And the underlying logic of how you define a position (using
>> netting or hedging system) or a trade (flat-to-flat or FIFO etc)
>> significantly affects the mechanics of money management rules (stop loss
>> etc) and trade statistics (profit factor etc).
>>
>> For defining positions, the netting vs hedging systems are defined as
>> follows:
>>
>>
>> The original implementation of quantstrat is based on a netting system
>> where after having a long position, each additional long trade increases
>> the current position amount (unless you have a position limit) and the
>> stop-loss/take profit is applied to the total position as a whole and not
>> to the individual long trades separately.
>>
>> My question is that is there a way to apply stop loss and take profit to
>> each opened trade independently, regardless of order side (long or short)
>> and previous positions? If yes, would you provide an example of it?
>>
>> Best regards
>> Ayhan
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-SIG-Finance using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions
>> should go.
>>
>

[[alternative HTML version deleted]]

```