Tradematic Support Center
Guides, articles, videos and links for Tradematic users and developers.
How to code stop-loss and take-profit for trading inside the bar?
87199ARTICLE CODE EDITOR BREAKOUT BREAK TOUCH INSIDE BAR BUYATSTOP SELLATLIMIT RUN EVERYStop-loss/take-profit inside the bar triggers when High and Low prices of the current bar break stop-loss/take-profit level. Use High and Low prices to save this signal at the performance chart of strategy. The pre class='prettyprint' looks the following way:
//conditions for closing long position //stop-loss for long position if (Low[bar]<=LastPosition.EntryPrice*(1-X/100)) SellAtPrice(bar, LastPosition.EntryPrice*(1-X/100), LastPosition,"Stop-loss"); //take-profit for long position else if (High[bar]>=LastPosition.EntryPrice*(1+X/100)) SellAtPrice(bar, LastPosition.EntryPrice*(1+X/100), LastPosition,"Take-profit"); //conditions for closing short position //stop-loss for short position if (High[bar]>=LastPosition.EntryPrice*(1+X/100)) CoverAtPrice(bar, LastPosition.EntryPrice*(1+X/100), LastPosition,"Stop-loss"); //take-profit for short position else if (Low[bar]<=LastPosition.EntryPrice*(1-X/100)) CoverAtPrice(bar, LastPosition.EntryPrice*(1-X/100), LastPosition,"Take-profit");
Please take into account, that the result can be imprecise due to lack of information what occurred first – condition for take-profit or condition for stop-loss.
Second variant of stop-loss and take-profit for trading inside the bar:
if (MarketPosition == 1) { // Close long position // Take-profit if entry price increases by 5% SellAtLimit(bar,LastPosition.EntryPrice*1.05, LastPosition, "TP (LX)"); // Stop-loss if entry price decreased by 3% SellAtStop(bar,LastPosition.EntryPrice*0.97, LastPosition, "SL (LX)"); // Other conditions for closing ... }
We have added take-profit for price increase by 5%, and stop-loss for price decrease by 3%. Add such pre class='prettyprint' to the block of position closing.