Good evening everyone,
I am struggling to get my code to open new orders. I have a simple entry rule and a set stop loss and set take profit. I am trying to update the code so that as it approaches the Stop Loss, it enters more trades (Up to 3 additional trades) – all with the same stop loss and take profit as the original trade.
The problem I am getting now is that all my orders are rejected as stop loss tries to enter with price 16,000 and Take Profit 115,000 while the index price is around 66,000. I have tried every combination of PipSize/p/$ and can’t get it to work. In reality take profit and stop loss should only be a few hundred points away from the index price, not tens of thousands!
If anyone could point out where I am going wrong, I would greatly appreciate it!
Kind regards,
Anton
//Code parameters
DEFPARAM CumulateOrders = TRUE
DEFPARAM FLATBEFORE = 083500
DEFPARAM FLATAFTER = 180000
noEntryBeforeTime = 083500
noEntryAfterTime = 180000
timeEnterAfter = time < noEntryAfterTime
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
SSS = 270
TPT = 110
GAP2 = SSS/3
//ONCE Capital = 100000
//ONCE Risk = 0.05
//equity = Capital + StrategyProfit
//maxrisk = round(equity*Risk)
//PositionSize = abs(round((maxrisk/SSS)/2))
PositionSize = 5
//
Positions = abs(CountOfPosition)
IF Positions > Positions[1] THEN
IF LongOnMarket THEN
SSS2 = (Close - SSS)*PipSize
ELSIF ShortOnMarket THEN
SSS2 = (Close + SSS)*PipSize
ENDIF
ENDIF
// Conditions to enter new positions
IF NOT ONMARKET THEN
LONGRULE = (close[0] >= open[0])
SHORTRULE = (open[0] >=close[0])
IF LONGRULE THEN
BUY PositionSize CONTRACT AT MARKET
SSS2 = (close - SSS)*PipSize
TPT2 = (close + TPT)*PipSize
SET TARGET PROFIT TPT2
SET STOP LOSS SSS2
ENDIF
IF SHORTRULE THEN
SELLSHORT PositionSize CONTRACT AT MARKET
SSS2 = (close + SSS)*PipSize
TPT2 = (close - TPT)*PipSize
SET STOP LOSS SSS2
SET TARGET PROFIT TPT2
ENDIF
ENDIF
// Conditions to add addition positions
IF LONGONMARKET THEN
ALR = (close[0] < tradeprice(1)-(GAP2*PipSize))
IF ALR THEN
BUY PositionSize CONTRACT AT MARKET
SSS2 = (TradePrice[Positions] - SSS)*PipSize
TPT2 = (TradePrice[Positions] + TPT)*PipSize
SET STOP LOSS SSS2
SET TARGET PROFIT TPT2
ENDIF
ENDIF
IF SHORTONMARKET THEN
ASR = (close[0] >= tradeprice(1)+(GAP2*PipSize))
IF ASR THEN
SELLSHORT PositionSize CONTRACT AT MARKET
SSS2 = (TradePrice[Positions] + SSS)*PipSize
TPT2 = (TradePrice[Positions] - TPT)*PipSize
SET TARGET PROFIT TPT2
SET STOP LOSS SSS2
ENDIF
ENDIF
IF LongOnMarket THEN
SELL AT SSS2 STOP
ELSIF ShortOnMarket THEN
EXITSHORT AT SSS2 STOP
ENDIF
You are multiplying with PIPSIZE your stoploss/takeprofit calculation, I don’t understand why? Because you already get a price value and use it with SET TARGET PROFIT and SET STOP LOSS which are both using a price distance value.
Thank you Nicolas, I’ve updated and they are still giving the same issue. So below, I changed Take Profit to a set amount (TPT) and Stop loss to (SSS) instead of the variables TPT2 and SSS2.
All orders now seem to use the original Stop Loss level. But the take profits are still all different. I’ve attached a picture as an example. It shows an original Long Trade then as the price dropped it entered another Long. Both have the same Stop loss but different Take profits.
Both Take profits should be where the top one is (the original level)
//Code parameters
DEFPARAM CumulateOrders = TRUE
DEFPARAM FLATBEFORE = 080000 //083500
DEFPARAM FLATAFTER = 180000
noEntryBeforeTime = 080000//083500
noEntryAfterTime = 180000
timeEnterAfter = time < noEntryAfterTime
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
SSS = 270
TPT = 110
GAP2 = SSS/3
//ONCE Capital = 100000
//ONCE Risk = 0.05
//equity = Capital + StrategyProfit
//maxrisk = round(equity*Risk)
//PositionSize = abs(round((maxrisk/SSS)/2))
PositionSize = 5
//
Positions = abs(CountOfPosition)
IF Positions > Positions[1] THEN
IF LongOnMarket THEN
SSS2 = TradePrice - SSS*PipSize
ELSIF ShortOnMarket THEN
SSS2 = TradePrice + SSS*PipSize
ENDIF
ENDIF
// Conditions to enter new positions
IF NOT ONMARKET THEN
LONGRULE = (close[0] >= open[0])
SHORTRULE = (open[0] >=close[0])
IF LONGRULE THEN
BUY PositionSize CONTRACT AT MARKET
SSS2 = close - SSS*PipSize
TPT2 = close + TPT*PipSize
SET TARGET PROFIT TPT //TPT2
SET STOP LOSS SSS //SSS2
ENDIF
IF SHORTRULE THEN
SELLSHORT PositionSize CONTRACT AT MARKET
SSS2 = close + SSS*PipSize
TPT2 = close - TPT*PipSize
SET STOP LOSS SSS//SSS2
SET TARGET PROFIT TPT//TPT2
ENDIF
ENDIF
// Conditions to add addition positions
IF LONGONMARKET THEN
ALR = (close[0] < tradeprice(1)-(GAP2*PipSize))
IF ALR THEN
BUY PositionSize CONTRACT AT MARKET
SSS2 = TradePrice[1] - SSS*PipSize
TPT2 = TradePrice[1] + TPT*PipSize
SET STOP LOSS SSS
SET TARGET PROFIT TPT
ENDIF
ENDIF
IF SHORTONMARKET THEN
ASR = (close[0] >= tradeprice(1)+(GAP2*PipSize))
IF ASR THEN
SELLSHORT PositionSize CONTRACT AT MARKET
SSS2 = TradePrice[1] + SSS*PipSize
TPT2 = TradePrice[1] - TPT*PipSize
SET TARGET PROFIT TPT
SET STOP LOSS SSS
ENDIF
ENDIF
IF LongOnMarket THEN
SELL AT SSS2 STOP
ELSIF ShortOnMarket THEN
EXITSHORT AT SSS2 STOP
ENDIF
Try using TradePrice(1), instead of TradePrice[1].
Always GRAPH your variables while debugging, it will save you precious time.
Thank you Nicolas, please forgive my ignorance. Could you please explain me how to graph when debugging?
JSParticipant
Senior
...
...
...
IF LongOnMarket THEN
SELL AT SSS2 STOP
ELSIF ShortOnMarket THEN
EXITSHORT AT SSS2 STOP
ENDIF
Graph SSS2 as "sss2"
Graph TPT2 as "TPT2"
...
...
Hi @15482847
Just simply add “Graph” at the end of your code… (and BackTest)
This will show you the “Graph” variables in your graph…
This is amazing, thank you JS. I will definitely be using this going forward!