ProRealCode - Trading & Coding with ProRealTime™
Is there av version of R. Gozzi- trail with % and diffrent values for short and long?
//--------------------------------------------------------------------------------------------
DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //TrP Exit (Gozzi
IF Not OnMarket OR DirectionSwitch THEN
TrailStart = TS//40 // Start trailing profits
PointToKeep = PK//0.2 // 20% Profit percentage to keep when setting BreakEven
StepSize = SS//5 // Point to increase Percentage
PerCentInc = PC//0.2 // 20% PerCent increment after each StepSize Chunk
RoundTO = -0.5 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviour
PriceDistance = 10* pipsize //6minimun distance from current price
maxProfitL = 0 //0 eller 2050
maxProfitS = 0
ProfitPerCent = PointToKeep //reset to desired default value
SellPriceX = 0
SellPrice = 0
ExitPriceX = 9999999
ExitPrice = 9999999
ELSE
IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
IF LongOnMarket THEN
newSlL = PositionPrice + ((close - PositionPrice) * ProfitPerCent) //calculate new SL
SellPriceX = max(max(SellPriceX,SellPrice),newSlL)
SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (maxProfitL * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ELSIF ShortOnMarket THEN
newSlS = PositionPrice - ((PositionPrice - close) * ProfitPerCent)
ExitPriceX = min(min(ExitPriceX,ExitPrice),newSlS)
ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (maxProfitS * pipsize))
ENDIF
ENDIF
ENDIF
//---------------------------------------------------------------------------------------------------------------------------------------------------
IF LongOnMarket AND close > (PositionPrice + (maxProfitL * pipsize)) THEN //LONG positions
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
profitL = (close - PositionPrice) / pipsize //convert price to pips
IF profitL >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - profitL) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
maxProfitL = max(profitL * ProfitPerCent, maxProfitL)
ENDIF
ELSIF ShortOnMarket AND close < (PositionPrice - (maxProfitS * pipsize)) THEN //SHORT positions
profitS = (PositionPrice - close) / pipsize
IF profitS >= TrailStart THEN
Diff2 = abs(TrailStart - profitS)
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
maxProfitS = max(profitS * ProfitPerCent, maxProfitL)
ENDIF
ENDIF
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
IF maxProfitL THEN //LONG positions - Place pending STOP order when maxProftiL > 0 (LONG positions)
SellPrice = max(SellPrice,PositionPrice + (maxProfitL * pipsize)) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF maxProfitS THEN
ExitPrice = min(ExitPrice,PositionPrice - (maxProfitS * pipsize)) //SHORT positions
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
There you go:
MA = 50
IF close crosses over average[MA,0] AND Not OnMarket THEN
BUY at market
ELSIF close crosses UNDer average[MA,0] AND Not OnMarket THEN
SELLSHORT at market
endif
SET TARGET pPROFIT 300
SET STOP pLOSS 150
//--------------------------------------------------------------------------------------------
DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //TrP Exit (Gozzi
//--------------------------------------------------------------------------------------------
// MyEquity must be defined somewhere in your code (even to ZERO if its use is not planned)
ONCE MyEquity = 10000//initial Capital (only if UseEquity = 1)
ONCE UsePerCentage = 0 //0=use Pips (default), 1=use Percentages
ONCE UseEquity = 0 //0=use price (default), 1=use Equity (initial Capital+StrategyProfit, defined by MyEquity)
//
IF Not OnMarket OR DirectionSwitch THEN
StartPerCentL = 0.25 //0.25% to start triggering Trailing Stop (when UsePerCentage=1)
StepPerCentL = 50 //50% (of the 0.25% above) as a Trqiling Step (when UsePerCentage=1) (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)
StartPerCentS = 0.25 //0.25% to start triggering Trailing Stop (when UsePerCentage=1)
StepPerCentS = 50 //50% (of the 0.25% above) as a Trqiling Step (when UsePerCentage=1) (set to 100 to make StepSize=TrailStart, set to 200 to make it twice TrailStart)
//
TrailStartL = TS//40 //Start trailing profits from this point (when UsePerCentage=0)
TrailStartS = TS//40 //Start trailing profits from this point (when UsePerCentage=0)
MinStartL = 10 //10 Minimum value for TrailStart (when UseEquity=1, to prevent TrailStart from
// dropping below ZERO when Equity turns negative)
MinStartS = 10 //10 Minimum value for TrailStart (when UseEquity=1, to prevent TrailStart fromMinStartL = 10 //10 Minimum value for TrailStart (when UseEquity=1, to prevent TrailStart from
IF UsePerCentage THEN
TrailStartL= (close / PipSize) * StartPerCentL / 100 //use current price (CLOSE) for calculations
TrailStartS= (close / PipSize) * StartPerCentS / 100 //use current price (CLOSE) for calculations
IF UseEquity THEN //alternative calculations using EQUITY
TrailStartL= Max(MinStartL,(MyEquity / PipValue) * StartPerCentL / 100) //MyEquity is the variable (feel free to use a different name) retaining your current equity
TrailStartS= Max(MinStartS,(MyEquity / PipValue) * StartPerCentS / 100) //MyEquity is the variable (feel free to use a different name) retaining your current equity
ENDIF
ENDIF
PointToKeep = PK//0.2 // 20% Profit percentage to keep when setting BreakEven
StepSizeL = SS//5 // Point to increase Percentage
StepSizeS = SS//5 // Point to increase Percentage
//
IF UsePerCentage THEN
StepSizeL = TrailStartL * StepPerCentL / 100
StepSizeS = TrailStartS * StepPerCentS / 100
ENDIF
//
PerCentInc = PC//0.2 // 20% PerCent increment after each StepSize Chunk
RoundTO = -0.5 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviour
PriceDistance = 10* pipsize //6minimun distance from current price
maxProfitL = 0 //0 eller 2050
maxProfitS = 0
ProfitPerCent = PointToKeep //reset to desired default value
SellPriceX = 0
SellPrice = 0
ExitPriceX = 9999999
ExitPrice = 9999999
ELSE
IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
IF LongOnMarket THEN
newSlL = PositionPrice + ((close - PositionPrice) * ProfitPerCent) //calculate new SL
SellPriceX = max(max(SellPriceX,SellPrice),newSlL)
SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (maxProfitL * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ELSIF ShortOnMarket THEN
newSlS = PositionPrice - ((PositionPrice - close) * ProfitPerCent)
ExitPriceX = min(min(ExitPriceX,ExitPrice),newSlS)
ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (maxProfitS * pipsize))
ENDIF
ENDIF
ENDIF
//---------------------------------------------------------------------------------------------------------------------------------------------------
IF LongOnMarket AND close > (PositionPrice + (maxProfitL * pipsize)) THEN //LONG positions
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
profitL = (close - PositionPrice) / pipsize //convert price to pips
IF profitL >= TrailStartL THEN // go ahead only if N+ pips
Diff1 = abs(TrailStartL - profitL) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSizeL) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
maxProfitL = max(profitL * ProfitPerCent, maxProfitL)
ENDIF
ELSIF ShortOnMarket AND close < (PositionPrice - (maxProfitS * pipsize)) THEN //SHORT positions
profitS = (PositionPrice - close) / pipsize
IF profitS >= TrailStartS THEN
Diff2 = abs(TrailStartS - profitS)
Chunks2 = max(0,round((Diff2 / StepSizeS) + RoundTO))
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
maxProfitS = max(profitS * ProfitPerCent, maxProfitL)
ENDIF
ENDIF
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
IF maxProfitL THEN //LONG positions - Place pending STOP order when maxProftiL > 0 (LONG positions)
SellPrice = max(SellPrice,PositionPrice + (maxProfitL * pipsize)) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF maxProfitS THEN
ExitPrice = min(ExitPrice,PositionPrice - (maxProfitS * pipsize)) //SHORT positions
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
Thanks alot Roberto!
Roberto. I got this error: “stoped by broker due to many atempts to place order”
Nasdaq 1EUR
Timeframe 15M
Do u know whats wrong?
See attached picture
//--------------------------------------------------------------------------------------------
DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //TrP Exit (Gozzi
IF Not OnMarket OR DirectionSwitch THEN
TrailStart = 0.9 // Start trailing profits
PointToKeep = 0.81 // PK20% Profit percentage to keep when setting BreakEven
StepSize = 1 // Point to increase Percentage
PerCentInc = 0.9 // 20% PerCent increment after each StepSize Chunk
RoundTO = -0.8 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviour
PriceDistance = 10* pipsize //6minimun distance from current price
maxProfitL = 0 //0 eller 2050
maxProfitS = 0
ProfitPerCent = PointToKeep //reset to desired default value
SellPriceX = 0
SellPrice = 0
ExitPriceX = 9999999
ExitPrice = 9999999
ELSE
IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
IF LongOnMarket THEN
newSlL = PositionPrice + ((close - PositionPrice) * ProfitPerCent) //calculate new SL
SellPriceX = max(max(SellPriceX,SellPrice),newSlL)
SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (maxProfitL * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ELSIF ShortOnMarket THEN
newSlS = PositionPrice - ((PositionPrice - close) * ProfitPerCent)
ExitPriceX = min(min(ExitPriceX,ExitPrice),newSlS)
ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (maxProfitS * pipsize))
ENDIF
ENDIF
ENDIF
//---------------------------------------------------------------------------------------------------------------------------------------------------
IF LongOnMarket AND close > (PositionPrice + (maxProfitL * pipsize)) THEN //LONG positions
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
profitL = (close - PositionPrice) / pipsize //convert price to pips
IF profitL >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - profitL) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
maxProfitL = max(profitL * ProfitPerCent, maxProfitL)
ENDIF
ELSIF ShortOnMarket AND close < (PositionPrice - (maxProfitS * pipsize)) THEN //SHORT positions
profitS = (PositionPrice - close) / pipsize
IF profitS >= TrailStart THEN
Diff2 = abs(TrailStart - profitS)
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
maxProfitS = max(profitS * ProfitPerCent, maxProfitL)
ENDIF
ENDIF
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
IF maxProfitL THEN //LONG positions - Place pending STOP order when maxProftiL > 0 (LONG positions)
SellPrice = max(SellPrice,PositionPrice + (maxProfitL * pipsize)) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF maxProfitS THEN
ExitPrice = min(ExitPrice,PositionPrice - (maxProfitS * pipsize)) //SHORT positions
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
It seems you are using a 1-second TF, so every second a pending order is placed when trailing is triggered. It seems that IG cannot process further oreders because in a single bar could not adress the prior request.
It often occurs to me, as well. But I am using a 5-minute TF, which should be a TF large enough not to make this happen, but it does, however.
So far I couldn’t get any clue how to solve it, despite some email messages.
Im using 15min timeframe
Is there a combiantion of these vakkues thats not possible to use in live-trading?
TrailStart = 0.9 // Start trailing profits
PointToKeep = 0.81 // PK20% Profit percentage to keep when setting BreakEven
StepSize = 1 // Point to increase Percentage
PerCentInc = 0.9 // 20% PerCent increment after each StepSize Chunk
RoundTO = -0.8 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviour
PriceDistance = 10* pipsize //6minimun distance from current price
No, they seem all correct.
Try GRAPHing the variables involved to spot any weird value.
R. Gozzi- trail
This topic contains 7 replies,
has 2 voices, and was last updated by
robertogozzi
3 years, 10 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 04/13/2022 |
| Status: | Active |
| Attachments: | 2 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.