Hi Experts, I need some help to figure out a hedging coding with a snippet stated below. I reckon the code below should exit with a win, or it’ll never exit the positions “forever…”, but the a backtesting showed losing trades which I couldn’t figure out why. The closest lead that I suspect is the “positionprice” with respect to the long AND short position has messed thing up? if so, any suggestion to fix the code? Thanks!! // Define a positivegain value, of course this value can be a small loss in real life. Sensibility aside 🙂 just making it positive here for validating a hedging if longonmarket and not shortonmarket then positivegain = close – positionprice > pointvalue * 10 elsif shortonmarket and not longonmarket then positivegain = positionprice – close > pointvalue * 10 elsif shortonmarket and longonmarket then positivegain = abs(close – positionprice) > pointsize // Tried a variation of: positionperf > pointsize, didn’t work endif // A branch of entry criteria that can buy and sell the position which are omitted here as it doesn’t matter to the inquiry // Exit condition if longonmarket and shortonmarket then // — this backtest generated losing trades… 🙁 if positivegain then sell at market if shortonmarket then exitshort at market endif endif endif // This variation #1 did not work either, same backtest result as above if longonmarket and shortonmarket then if positivegain then sell at market exitshort at market endif endif // This variation #2 did not work either, same backtest result as above if longonmarket and shortonmarket then if positivegain then sell at market endif exitshort at market endif // some more codes to show a long Or a short position exit strategy for completeness. Don’t think these codes affected the long And short positions criteria above. if longonmarket and not shortonmarket then if positivegain then sell at market endif endif if not longonmarket and shortonmarket then if positivegain then exitshort at market endif endif