Hello
I am looking for a code that means that if I am +50 points, another position is opened with 1 contract.
But it should not be repeated more than once so max position size = 2.
Like this maybe?
defparam cumulateorders = true
Positionsize = 1
maxpositions = 2
if ”my conditions ” and not maxpositions and not not shortonmarket then
buy positionsize contract at market
IF OnMarket and PositionPrice + (50 * PositionPrice) then
Buy positionsize contract at market
ENDIF
defparam cumulateorders = true
Positionsize = 1
maxpositions = 2
if ”my conditions ” and (not onmarket) then
buy positionsize contracts at market
endif
If longonmarket and positionprice * positionperf >= 50 and countofposition < maxpositions then
Buy positionsize contracts at market
ENDIF
This will buy 1 contract, when no position is open, and buy one further contract when the closing price of the first position is at least 50 points in plus. Not tested, however. Will work for indices, but I’m not sure about forex because they work with pips there.
Have previously asked about a new position be opened.
But it does not work.
It opens new positions all the time.
I want only 1 more position to be opened when my position is +40 points.
And the new position will be closed at the same time as the first
Does anyone know what such a code looks like?
JSParticipant
Senior
Try: Defparam cumulateorders = false (instead of true)
JSParticipant
Senior
You can also try: abs(countofposition)
Try this.
At current prices, positionprice * 1.0025 on the DAX is approx a 40 point gain. You can change the multiplier depending on what instrument you want to use.
It should make your backtest more accurate, as it keeps your gain relational to the historical index value.
DEFPARAM CUMULATEORDERS = TRUE
if not onmarket then
flag = 1
ENDIF
// Conditions to enter long positions
IF not longonmarket and CONDITIONS THEN
BUY positionsize CONTRACT AT MARKET
elsif longonmarket and CONDITIONS and flag and HIGH >= (positionprice * 1.0025) then
BUY positionsize CONTRACT AT MARKET
flag = 0
ENDIF
Thank you n.
The code works.
A maximum of 2 positions are taken 🙂
But sometimes the second position is taken at +40 points and sometimes at + 120-130 points.
How is it that..
What instrument are you using?
you could try
G = (40/close) + 1
HIGH >= (positionprice * G)
or if you don’t mind if it’s not proportional you could just use
HIGH >= (positionprice + 40)
if not onmarket then
flag = 1
ENDIF
IF not shortonmarket and condsell THEN
sellshort positionsize CONTRACT AT MARKET
elsif shortonmarket and condsell and flag and HIGH >= (positionprice - 40) then
sellshort positionsize CONTRACT AT MARKET
flag = 0
ENDIF
And short looks like this?