The above code is 1/4 of 100 lines!
Is it still too much much?
There you go with just 4 lines:
If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
Sell at tradeprice + 10*pipsize STOP
Exitshort at tradeprice - 10*pipsize STOP
Endif
That’s pretty short!
Add this to your code and you will soon report that it will not always exit at the expected entry price + 10 pips.
That’s because pending orders expire each bar, so when your profit is 30+ pips they will be placed, but if they are not triggered and the next bar your profit is, say, 29 pips, they will not be placed again and you may suffer an unexpected big loss! How can this be dealt with? By adding a few lines to RECORD that your trigger level has been reached, so the stop orders are to be placed again, next bar, even if by that time your profit is lower than that level:
Once NewSL = 0
If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
If LongOnMarket then
NewSL = tradeprice + 10*pipsize
ElsIf ShortOnMarket then
NewSL = tradeprice - 10*pipsize
Endif
If NewSL > 0 then
Sell at NeWSL STOP
Exitshort at NewSL STOP
Endif
as you can see, a few more lines have been added, but they are still not so many!
Well… there’s still a problem. Next trade NewSL will still retain the previous exit price and it may exit at an incorrect price,
We need to add a few more lines to prevent this from happening:
Once NewSL = 0
If not OnMarket then
NewSL = 0
Endif
If (positionprice * PositionPerf / PipSize) >= 30 * PipSize then
If LongOnMarket then
NewSL = tradeprice + 10*pipsize
ElsIf ShortOnMarket then
NewSL = tradeprice - 10*pipsize
Endif
If NewSL > 0 then
Sell at NeWSL STOP
Exitshort at NewSL STOP
Endif
it’s no more just a few lines (and I did not add comments to make code easier to read), it it can’t be done with less than that!
100+ lines can easily be reached to add more sophisticated features.
It’s like starting a car, you don’t need too much engineering, but then you have to add support for speeding up, then slowing down, then braking, then steering, then accommodating people, etc…