﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>ActFX Algorithmic Trading » ActFX » Trading Strategy Interface and General Functionality  » place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><generator>InstantForum.NET 2012-1</generator><description>ActFX Algorithmic Trading</description><link>http://forum.actfx.com/</link><webMaster>ActFX Algorithmic Trading</webMaster><lastBuildDate>Sun, 20 May 2012 22:46:19 GMT</lastBuildDate><ttl>20</ttl><item><title>place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost998.aspx</link><description>Hi all,&lt;br&gt;&lt;br&gt;I need help with my strategy.&lt;br&gt;&lt;br&gt;I would like to create a strategy that consists of 3 steps. &lt;br&gt;A strategy that:&lt;br&gt;&lt;br&gt;1- puts 2 entry stop orders (an entry stop buy and an entry stop sell) at a predefined time and at a predefined distance above the market;&lt;br&gt;2- sets a predefined stop loss and a predefined limit under/above the open rate of the trade when the order or the orders are executed;&lt;br&gt;3- when a trade is closed (automatically by the stop or limit of the previous passage) removes the entry stop order (buy or sell) not yet executed, if still existing.&lt;br&gt;&lt;br&gt;I tried to write it down, but it doesn't work properly.&lt;br&gt;&lt;br&gt;const&lt;br&gt;  StrategyName = 'MyFirstStrategy';&lt;br&gt;  &lt;br&gt;var    //declaration of the variables&lt;br&gt;  History: TTickHistory;&lt;br&gt;  Account: TAccount;&lt;br&gt;  Amount, Point,: Double;&lt;br&gt;  ESPrice, AbPrice, ES2Price, Ab2Price: Integer;&lt;br&gt;  Stop: NullRate;&lt;br&gt;  Limit: NullRate;&lt;br&gt;  i,m: Integer;&lt;br&gt;  Rate: Double;&lt;br&gt;  Instrument: TInstrument;&lt;br&gt;  EOTime: String;&lt;br&gt;  LastTime: TDateTime;  &lt;br&gt;  MyTrade: TTrade;&lt;br&gt; &lt;br&gt;procedure OnCreate;&lt;br&gt;begin&lt;br&gt;  AddTickHistorySetting(@History, 'Strumento', 'EURUSD', 1);   //setting up the chart history&lt;br&gt;  History.OnNewRateEvent := @OnNewRate;    //indicating the procedure to run when a new rate is received &lt;br&gt;  AddAccountSetting(@Account, 'Account', '');   //the account number&lt;br&gt;  AddFloatSetting(@Amount, 'Lots', 1);   //the number of lots&lt;br&gt;  AddIntegerSetting(@ESPrice, 'Entry Stop Distance Buy', 20);   //setting up entry stop distance in pips&lt;br&gt;  AddIntegerSetting(@AbPrice, 'Entry Stop Distance Sell', 20);&lt;br&gt;  AddStringSetting(@EOTime, 'Entry Orders Time', '10.00.00');  //setting up the time&lt;br&gt;  AddIntegerSetting(@Stop, 'Stop', 100);&lt;br&gt;  AddIntegerSetting(@Limit, 'Limit', 100); &lt;br&gt;end;&lt;br&gt;&lt;br&gt;&lt;br&gt;// PROCEDURE 1: THIS PROCEDURE PLACES 2 ENTRY STOP ORDER (ONE BUY, ONE SELL) AT 10 O'CLOCK WHEN A NEW RATE IS RECEIVED&lt;br&gt;//(IT WORKS)&lt;br&gt;&lt;br&gt;procedure OnNewRate;&lt;br&gt;begin&lt;br&gt;Point:= History.Instrument.PointSize;&lt;br&gt;&lt;br&gt;if (Time &gt;= StrToTime(EOTime))&lt;br&gt;and  (LastTime &lt; StrToTime(EOTime)) then        &lt;br&gt;&lt;br&gt;begin&lt;br&gt;CreateEntryOrder(History.Instrument, Account, Amount, bsSell,&lt;br&gt;History.Instrument.Sell - AbPrice*Point,&lt;br&gt;NullRate,                   &lt;br&gt;NullRate,&lt;br&gt;otEStop,'EntryStopSell');                  &lt;br&gt;    &lt;br&gt;CreateEntryOrder(History.Instrument, Account, Amount, bsBuy,&lt;br&gt;History.Instrument.Buy + ESPrice*Point,&lt;br&gt;NullRate,                   &lt;br&gt;NullRate,&lt;br&gt;otEStop,'EntryStopBuy');&lt;br&gt;&lt;br&gt;end;    &lt;br&gt;&lt;br&gt;    LastTime:=Time;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;//PROCEDURE 2: THIS PROCEDURE INSERT (or should insert, since I'm not able to compile it) the predefined stop and limit on each trade inserted&lt;br&gt;// I want the stop and limit to be set respectively xPoint under and above the open rate of the trade&lt;br&gt;//(IT DOESN'T WORK!!!)&lt;br&gt;&lt;br&gt;procedure OnTradeChange(const Action: TDataModificationType; const Trade: TTrade);&lt;br&gt;var i: Integer;&lt;br&gt;begin&lt;br&gt;&lt;br&gt;//if the entry stop order "Buy" is executed, then insert the predefined stop and limit on the buy position just opened &lt;br&gt;    &lt;br&gt;    if (Action = dmtInsert) and (Trade.Tag =  'EntryStopBuy') then  //I think this passage is wrong...I have to identify the buy position opened&lt;br&gt;	&lt;br&gt;	       CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Stop*Point, otStop, 'Stop');&lt;br&gt;           CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Limit*Point, otLimit, 'Limit');&lt;br&gt;		   &lt;br&gt;end;&lt;br&gt;&lt;br&gt;begin&lt;br&gt;&lt;br&gt;//if the entry stop order "Sell" is executed, then insert the predefined stop and limit on the sell position just opened &lt;br&gt;   &lt;br&gt;    if (Action = dmtInsert) and (Trade.Tag =  'EntryStopSell') then  //I think this passage is wrong...I have to identify the sell position opened&lt;br&gt;	&lt;br&gt;	       CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Stop*Point, otStop, 'Stop');&lt;br&gt;           CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Limit*Point, otLimit, 'Limit');&lt;br&gt;		   &lt;br&gt;end;&lt;br&gt;&lt;br&gt;&lt;br&gt;//PROCEDURE 3: THIS PROCEDURE SHOULD REMOVE, when a trade is closed (automatically by the stop or limit of the previous passage),     &lt;br&gt;// the entry stop order (buy or sell) not yet executed, if still existing.&lt;br&gt;&lt;br&gt;begin&lt;br&gt;&lt;br&gt;//if the Trade "Buy" is closed, then remove, if still existing, the entry stop order "Sell".&lt;br&gt;&lt;br&gt;    if (Action = dmtDelete) and (Trade.Tag =  'EntryStopBuy') then   //I think this passage is wrong, I have to identify the buy position closed&lt;br&gt;    &lt;br&gt;begin&lt;br&gt;     for i:=OrderList.Count-1 downto 0 do&lt;br&gt;      begin&lt;br&gt;        if OrderList.Count&gt;0 then&lt;br&gt;         if OrderList.Get(i).Tag = 'EntryStopSell' then &lt;br&gt;         try&lt;br&gt;         OrderList.Get(i).Remove;&lt;br&gt;         except&lt;br&gt;         if ExceptionClassName &lt;&gt; 'ETradeNotFound' then RaiseCurrentException;&lt;br&gt;         end;&lt;br&gt;    end; &lt;br&gt;end;&lt;br&gt;&lt;br&gt;begin&lt;br&gt;&lt;br&gt;//if the Trade "Sell" is closed, then remove, if still existing, the entry stop order "Buy".&lt;br&gt;&lt;br&gt;if (Action = dmtDelete) and (Trade.Tag =  'EntryStopSell') then   //I think this passage is wrong, I have to identify the sell position closed&lt;br&gt;    &lt;br&gt;begin&lt;br&gt;     for i:=OrderList.Count-1 downto 0 do&lt;br&gt;      begin&lt;br&gt;        if OrderList.Count&gt;0 then&lt;br&gt;         if OrderList.Get(i).Tag = 'EntryStopBuy'  then &lt;br&gt;         try&lt;br&gt;         OrderList.Get(i).Remove;&lt;br&gt;         except&lt;br&gt;         if ExceptionClassName &lt;&gt; 'ETradeNotFound' then RaiseCurrentException;         &lt;br&gt;&lt;br&gt;end;&lt;br&gt;end; &lt;br&gt;end;</description><pubDate>Fri, 14 May 2010 09:43:29 GMT</pubDate><dc:creator>p.costa</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1512.aspx</link><description>still new around here,any help for starters</description><pubDate>Fri, 14 May 2010 09:43:29 GMT</pubDate><dc:creator>localjuju</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1040.aspx</link><description>Nice strategy! Congratulations, p.costa!</description><pubDate>Mon, 22 Feb 2010 02:50:04 GMT</pubDate><dc:creator>scriptor</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1024.aspx</link><description>Congratulations! ;)</description><pubDate>Fri, 19 Feb 2010 04:14:39 GMT</pubDate><dc:creator>bobby</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1020.aspx</link><description>Thank you very much!&lt;br&gt;It works perfectly.&lt;br&gt;&lt;br&gt;In attachment the corrected strategy.</description><pubDate>Fri, 19 Feb 2010 00:34:27 GMT</pubDate><dc:creator>p.costa</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1015.aspx</link><description>I think you can do it even simpler:&lt;br&gt;&lt;br&gt;Try it this way:&lt;br&gt;&lt;br&gt;if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then&lt;br&gt;&lt;br&gt;begin&lt;br&gt;CreateStopLimitOrder(Trade, Trade.OpenRate+Stop*Point, otStop, 'Stop');&lt;br&gt;CreateStopLimitOrder(Trade, Trade.OpenRate-Limit*Point, otLimit, 'Limit');&lt;br&gt;end;&lt;br&gt;&lt;br&gt;(The Trade variable is already identified in the procedure)</description><pubDate>Thu, 18 Feb 2010 01:11:01 GMT</pubDate><dc:creator>eMoe</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1009.aspx</link><description>Hi eMOe!&lt;br&gt;&lt;br&gt;I've just tested the strategy.&lt;br&gt;&lt;br&gt;It gives me an error in the line 83:11 "Reference to undefined object". &lt;br&gt;&lt;br&gt;This is the wrong passage:&lt;br&gt;------------------------------------------------------------------------------------------&lt;br&gt;if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then &lt;br&gt;&lt;br&gt;     begin&lt;br&gt;          CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Stop*Point, otStop, 'Stop');&lt;br&gt;          CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Limit*Point, otLimit, 'Limit');&lt;br&gt;     end;&lt;br&gt;------------------------------------------------------------------------------------------&lt;br&gt;&lt;br&gt;It doesn't recognize the trade inserted in order to set the stop and limit orders.&lt;br&gt;Maybe do we have to insert the script: MyTrade:=Trade ?&lt;br&gt;&lt;br&gt;I mean like this:&lt;br&gt;&lt;br&gt;if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then [b][i]MyTrade:=Trade;[/i][/b] ??&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;</description><pubDate>Wed, 17 Feb 2010 08:02:35 GMT</pubDate><dc:creator>p.costa</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1005.aspx</link><description>I'm very grateful to you.&lt;br&gt;&lt;br&gt;Now I'm going to test my strategy in order to discover if it works.&lt;br&gt;&lt;br&gt;I'll keep you posted.&lt;br&gt;&lt;br&gt;Bye.&lt;br&gt;&lt;br&gt;Thanks!</description><pubDate>Wed, 17 Feb 2010 00:52:15 GMT</pubDate><dc:creator>p.costa</dc:creator></item><item><title>RE: place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is closed</title><link>http://forum.actfx.com/FindPost1002.aspx</link><description>Hello p.costa!&lt;br&gt;&lt;br&gt;I saw that your strategy does not compile, and tried to correct the syntax. Now it does compile. I am not sure if the logic is correct as I have not checked it yet. Perhaps you will be able to figure it out while testing.&lt;br&gt;I have attached the corrected script.&lt;br&gt;</description><pubDate>Wed, 17 Feb 2010 00:03:29 GMT</pubDate><dc:creator>eMoe</dc:creator></item></channel></rss>
