Move Stop to Breakeven

Discussions about the testing and simulation of mechanical trading systems using historical data and other methods. Trading Blox Customers should post Trading Blox specific questions in the Customer Support forum.
Post Reply
irish91001
Contributing Member
Contributing Member
Posts: 9
Joined: Thu Jun 14, 2012 2:35 pm

Move Stop to Breakeven

Post by irish91001 » Thu Oct 10, 2013 5:40 pm

Right now I have a system where I enter a trade and use a fixed stop.

I'm interested in running a simulation where I trade 2 contracts and move my stop to breakeven once a profit reaches a certain a dollar amount (i.e. $1,000). If it's not reasonable to use a profit amount, I could replace it with ticks or points to equal $1,000.

I would then exit 1 contract and keep the other one in place until it gets stopped out at the breakeven stop or reaches the end of a seasonal window.

The other stipulation is that, if after moving the stop to breakeven and the trade is then stopped out, I would re-enter another full position (2 contracts) as long my technical parameters were still aligned and the seasonal window were still open.

Any tips on how to code this?

Roger Rines
Roundtable Knight
Roundtable Knight
Posts: 1946
Joined: Wed Oct 06, 2004 10:52 am
Location: San Jose, CA

Post by Roger Rines » Fri Oct 11, 2013 10:15 am

Your need to reach a predefined gain, exit 1-contract, and adjust the protective price is easily accomplished with a Target Price exit that also includes the script to setup the protective price to the position's fill-price. It would also require this code to be placed in a module that is named so that it is located above the current exit module's displayed location in the System Editor's display. An alternative would be to add the target process to the current exit blox.

Your requirement to re-enter that instrument again after being stopped out would in most cases be controlled by your current entry blox, and in many cases the entry blox wouldn't need any additional coding to re-enter. However, this is a check the code first before making any assumptions about what this second step would need.

irish91001
Contributing Member
Contributing Member
Posts: 9
Joined: Thu Jun 14, 2012 2:35 pm

Post by irish91001 » Fri Oct 11, 2013 7:28 pm

Roger,

Thanks for the help. However, I'm not finding the Target Price exit. I have looked at the AdjustPositionAtLimit price, but that doesn't allow me to move the stop. Where am I going wrong here? Also, is this where I would put the coding, before the technical indicator exit?

If instrument.position == LONG then

'Reduce position once profit taregt is achieved'
broker.AdjustPositionAtLimit (.5, profitTarget)

if adxIndicator < adxIndicator[1]then
broker.exitAllUnitsOnOpen
endif

If instrument.CanTradeLong = 0 then broker.exitAllUnitsOnOpen

Roger Rines
Roundtable Knight
Roundtable Knight
Posts: 1946
Joined: Wed Oct 06, 2004 10:52 am
Location: San Jose, CA

Post by Roger Rines » Fri Oct 11, 2013 8:01 pm

A Target price is a price created through a process of taking the fill-price, and making an estimate of how many points will be possible to capture a reliable gain. With a point projection estimate and a base price put the two values together to obtain the target price level at which you plan on removing some size. Target prices are often not achieved, but when they are reached the order created to make a change and remove some size is almost always an AtLimit type of execution.

Trading Blox provides more than a few AtLimit type executions, but the most popular Broker objet target exit functions are these:

Code: Select all

   broker.ExitAllUnitsAtLimit
   broker.ExitUnitAtLimit
   broker.AdjustPositionAtLimit 
To move the protective price, first capture its current price using:

Code: Select all

   instrument.unitExitStop 

Then determine the new price you want, and then update it with

Code: Select all

   instrument.SetExitStop
With the new exit price in place, the Exit Orders sections in other modules that execute after the module's section where you perform the above can then read the protective new price and honor it with a normal protective Exit-Order.

Keep in mind that scripts with same name execute in the order in which they are sorted and displayed in the System Editor window after the System Editor has been closed so the blox can be sorted. When the System Editor is re-opened again the Blox-name order you see before you add or remove any blox from the system list is the order in which same name scripts will execute.

irish91001
Contributing Member
Contributing Member
Posts: 9
Joined: Thu Jun 14, 2012 2:35 pm

Post by irish91001 » Mon Oct 14, 2013 12:36 am

Well, I gave it a go this weekend and I don't think I have the programming quite right. I created a new Entry/Exit block with the following:

Code: Select all

IF instrument.position = LONG AND
	instrument.high >= targetPrice THEN
		broker.ExitUnitAtLimit (1, targetPrice)AND
		instrument.setexitstop (1, instrument.unitEntryFill)
I just made a generic target price being entry.fill *20% I added this block to my current system (which I left unchanged)

My skills in this stuff are minimal (at best) so I don't want to keep tying up the conversation, but I certainly appreciate the help!![/code]

Roger Rines
Roundtable Knight
Roundtable Knight
Posts: 1946
Joined: Wed Oct 06, 2004 10:52 am
Location: San Jose, CA

Post by Roger Rines » Mon Oct 14, 2013 10:39 am

Code: Select all

   If instrument.position = LONG AND 
      instrument.high >= targetPrice THEN 
   
      '  Leave Broker object Function calls as a stand-alone statement
      broker.ExitUnitAtLimit (1, targetPrice)  
   ENDIF 
Code above is a complete quantity termination of the position. Once you use the IF statement to discover your crossing of the TargetPrice, use a partial OnOpen statement to remove some quantity. Otherwise to remove all of the quantity is to remove the position, and there will not be a position left on which to adjust the protective price.

If you use a partial exit, you will have a position with a partial Qty to protect...

Code: Select all

   '  If you want the previous target as a protective price,...
   myNewExitStop = targetPrice
   '  your solution was giving it its old price.
   instrument.SetExitStop (1, myNewExitStop ) 
If you want another price conjure up what that might be and use that price.

Your previous statement above didn't make logic programming sense because the Broker object doesn't return anything, and even if it did, there wasn't any variable being assigned to create a complete logical assignment.

Post Reply