Hi there,
I’ve been tinkering on this bit of code for several days and cannot figure out how to eliminate several behaviors:
- It sometimes places a negative stoploss. It should only start to place a stoploss when the position is breakeven yet at times it will place a stoploss at a loss…
- It sometimes does, sometimes does not increase the trailing stoploss. Obviously, it should always increase whenever possible
If not onmarket then
sellshort 1 contract at market
endif
minsteps = 5
shortbreakeven = positionprice - minsteps
Fullshortexit = positionprice - 10
If shortonmarket and Close[1] > shortbreakeven and Close <= shortbreakeven then
ShortStopSignal = 1
Elsif not shortonmarket then
ShortStopSignal = 0
Endif
If ShortStopSignal[1] = 0 and ShortStopSignal = 1 then
ShortProfitTime = BarIndex
elsif not shortonmarket then
ShortProfitTime = 0
Endif
// Short Profit Stoploss management :
If ShortProfitTime <> 0 and close > Fullshortexit and close < positionprice then
ShortStopProfit = PositionPrice - 1
Elsif ShortProfitTime <> 0 and close <= Fullshortexit and abs(Close - PositionPrice)/MinSteps < 4 then
ShortStopProfit = Close + MinSteps
Elsif ShortProfitTime <> 0 and close <= Fullshortexit and abs(Close - PositionPrice)/MinSteps >= 4 then
ShortStopProfit = close + 2*MinSteps
elsif ShortProfitTime = 0 then
ShortStopProfit = 0
Endif
If ShortProfitTime <> 0 then
MyShortStopProfit = lowest[Max(1,BarIndex - ShortProfitTime)](ShortStopProfit)
Endif
if onmarket then
sell at MyshortStopProfit stop
endif
Thanks for the help, I’m really confused about what it is I’m not getting…
I have submitted your problem to I.A, hereinafter the answer:
Incorrect calculations: Calculations for shortbreakeven, Fullshortexitet stop-loss conditions seem to correspond to arbitrary price levels ( minsteps, positionprice). These values should be based on the actual performance of the position.
Initializing Variables: There is no ShortStopProfit, which can cause problems.
Fuzzy logic: The logic to determine when to place a stop-loss or increase it seems complex and possibly subject to
Exit conditions:
Using BarIndex: Using BarIndexpor to measure profit time can be prone to errors as it depends on the number of bars you have loaded and the frequency of your data.
Management of Short Positions: The code seems mainly intended for the management of short positions. Make sure this fits your trading strategy.
To correct these problems, it may be necessary to rethink the logic of position management and adapt the
JSParticipant
Senior
Hi,
To check if your logic is correct, use:
GraphOnPrice ShortBreakEven
GraphOnPrice FullShortExit
GraphOnPrice PositionPrice
Graph ShortStopSignal
Etc.