hi,
anyone know how to code set stop loss in profit region when in profit?
thank you
You need to use pending a STOP order.
Example to set SL at breakeven + 10 pips, after profits have reached 50 pips:
If not OnMarket then
StopLoss = 0
Endif
If StopLoss = 0 and OnMarket then
Profit = PositionPerf * PositionPrice
If Profit > 50 * pipsize then
If LongOnMarket then
StopLoss = PositionPrice + 10 * pipsize
Else
StopLoss = PositionPrice - 10 * pipsize
Endif
Endif
Endif
If StopLoss > 0 then
Sell at StopLoss STOP
Exitshort at StopLoss STOP
Endif
(not tested)
can someone explain why I exit there?
IF c1 and uptrend and not onmarket THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
If OnMarket then
totalProfit = PositionPerf * PositionPrice
if totalprofit > 10*pipsize then
stoploss = positionprice + 5*pipsize
sell at stoploss stop
exitshort at stoploss stop
Endif
Endif
If longonmarket and totalProfit > 10 * pipsize then
if habearcandle5mn then
Sell at market
Endif
endif
I’m not suppose to have a stop loss in the loss region
thanks
sorry problem solved. the reason why it stopped because the backtest period ended where it stopped.
Be warned that pending orders expire each bar and you have to place them again and again, if needed.
Lines 9-10 will only be executed the first time.
You should place them outside that check (like my example), otherwise you SL will placed correctly when 10 pips are reached but, if not hit and the next bar profit decreses to 6 pips, it won’t be placed at all!
hi, i want to put stoploss at 0 when profit is 5 pips above the positionprice and move to 5 pips in profit when it is 10pips above the positionprice. can you check my codes if i have done that? thank you
If longOnMarket then
totalProfit = PositionPerf * PositionPrice
if totalprofit > 5*pipsize then
stoploss = 0
endif
if totalprofit > 10*pipsize then
stoploss = positionprice + 5*pipsize
Endif
Endif
if stoploss=0 then
sell at stoploss stop
endif
if stoploss > 0 then
sell at stoploss stop
endif
if not onmarket then
stoploss = 0
endif
If longOnMarket then
totalProfit = PositionPerf * PositionPrice
if totalprofit > 10*pipsize then
stoploss = positionprice + 5*pipsize
Endif
Endif
if longonmarket and close <10*pipsize then
sell at stoploss stop
endif
If longonmarket and totalProfit > 10 * pipsize then
if habearcandle5mn then
Sell at market
Endif
endif
set target pprofit 100
graph stoploss
does not seem to trigger the stop loss i have on 5 pips above position price.
Tell us then please … stories without endings are not good for sleeping!? 🙂
change line 12 to
if longonmarket and ((close - positionprice )< 10*pipsize) then
but for going short it does not work. it opens and closes immediately and the stoploss variable is always zero.
if not onmarket then
stoploss = 0
endif
If shortOnMarket then
totalProfit = PositionPerf * PositionPrice
if totalprofit > 10*pipsize then
stoploss = positionprice - 5*pipsize
Endif
Endif
if shortonmarket and (( positionprice - close )< 10*pipsize) then
exitshort at stoploss stop
endif
If shortonmarket and totalProfit > 10 * pipsize then
if habullcandle5mn then
exitshort at market
Endif
endif
anyone got any ideas?
seems like it worked when i changed line 2 to stoploss=2 because i have spread =2 when i backtested
Obviously when going short prices and stop losses are reversed. So the calculations in lines 7 and 11 need to be reversed.
stoploss = positionprice + 5*pipsize
if shortonmarket and (( close - positionprice)< 10*pipsize) then
Thanks – very useful to code this to minimise losing any initial capital.