place 2 entry stop orders; insert stop and limit on orders executed; remove orders when a trade is... Expand / Collapse
Author
Message
Posted 2/16/2010 10:59:07 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 12/2/2010 9:08:34 AM
Posts: 36, Visits: 246
Hi all,

I need help with my strategy.

I would like to create a strategy that consists of 3 steps.
A strategy that:

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;
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;
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.

I tried to write it down, but it doesn't work properly.

const
StrategyName = 'MyFirstStrategy';

var //declaration of the variables
History: TTickHistory;
Account: TAccount;
Amount, Point,: Double;
ESPrice, AbPrice, ES2Price, Ab2Price: Integer;
Stop: NullRate;
Limit: NullRate;
i,m: Integer;
Rate: Double;
Instrument: TInstrument;
EOTime: String;
LastTime: TDateTime;
MyTrade: TTrade;

procedure OnCreate;
begin
AddTickHistorySetting(@History, 'Strumento', 'EURUSD', 1); //setting up the chart history
History.OnNewRateEvent := @OnNewRate; //indicating the procedure to run when a new rate is received
AddAccountSetting(@Account, 'Account', ''); //the account number
AddFloatSetting(@Amount, 'Lots', 1); //the number of lots
AddIntegerSetting(@ESPrice, 'Entry Stop Distance Buy', 20); //setting up entry stop distance in pips
AddIntegerSetting(@AbPrice, 'Entry Stop Distance Sell', 20);
AddStringSetting(@EOTime, 'Entry Orders Time', '10.00.00'); //setting up the time
AddIntegerSetting(@Stop, 'Stop', 100);
AddIntegerSetting(@Limit, 'Limit', 100);
end;


// PROCEDURE 1: THIS PROCEDURE PLACES 2 ENTRY STOP ORDER (ONE BUY, ONE SELL) AT 10 O'CLOCK WHEN A NEW RATE IS RECEIVED
//(IT WORKS)

procedure OnNewRate;
begin
Point:= History.Instrument.PointSize;

if (Time >= StrToTime(EOTime))
and (LastTime < StrToTime(EOTime)) then

begin
CreateEntryOrder(History.Instrument, Account, Amount, bsSell,
History.Instrument.Sell - AbPrice*Point,
NullRate,
NullRate,
otEStop,'EntryStopSell');

CreateEntryOrder(History.Instrument, Account, Amount, bsBuy,
History.Instrument.Buy + ESPrice*Point,
NullRate,
NullRate,
otEStop,'EntryStopBuy');

end;

LastTime:=Time;
end;

//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
// I want the stop and limit to be set respectively xPoint under and above the open rate of the trade
//(IT DOESN'T WORK!!!)

procedure OnTradeChange(const Action: TDataModificationType; const Trade: TTrade);
var i: Integer;
begin

//if the entry stop order "Buy" is executed, then insert the predefined stop and limit on the buy position just opened

if (Action = dmtInsert) and (Trade.Tag = 'EntryStopBuy') then //I think this passage is wrong...I have to identify the buy position opened

CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Stop*Point, otStop, 'Stop');
CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Limit*Point, otLimit, 'Limit');

end;

begin

//if the entry stop order "Sell" is executed, then insert the predefined stop and limit on the sell position just opened

if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then //I think this passage is wrong...I have to identify the sell position opened

CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Stop*Point, otStop, 'Stop');
CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Limit*Point, otLimit, 'Limit');

end;


//PROCEDURE 3: THIS PROCEDURE SHOULD REMOVE, when a trade is closed (automatically by the stop or limit of the previous passage),
// the entry stop order (buy or sell) not yet executed, if still existing.

begin

//if the Trade "Buy" is closed, then remove, if still existing, the entry stop order "Sell".

if (Action = dmtDelete) and (Trade.Tag = 'EntryStopBuy') then //I think this passage is wrong, I have to identify the buy position closed

begin
for i:=OrderList.Count-1 downto 0 do
begin
if OrderList.Count>0 then
if OrderList.Get(i).Tag = 'EntryStopSell' then
try
OrderList.Get(i).Remove;
except
if ExceptionClassName <> 'ETradeNotFound' then RaiseCurrentException;
end;
end;
end;

begin

//if the Trade "Sell" is closed, then remove, if still existing, the entry stop order "Buy".

if (Action = dmtDelete) and (Trade.Tag = 'EntryStopSell') then //I think this passage is wrong, I have to identify the sell position closed

begin
for i:=OrderList.Count-1 downto 0 do
begin
if OrderList.Count>0 then
if OrderList.Get(i).Tag = 'EntryStopBuy' then
try
OrderList.Get(i).Remove;
except
if ExceptionClassName <> 'ETradeNotFound' then RaiseCurrentException;

end;
end;
end;

Edited: 2/16/2010 12:09:28 PM by p.costa

  Post Attachments 
MyFirstStrategy.txt (172 views, 4.58 KB)
Post #998
Posted 2/17/2010 5:03:29 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 1/27/2012 12:53:25 PM
Posts: 201, Visits: 295
Hello p.costa!

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.
I have attached the corrected script.


Best regards!

  Post Attachments 
pcosta.act (180 views, 4.40 KB)
Post #1002
Posted 2/17/2010 5:52:15 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 12/2/2010 9:08:34 AM
Posts: 36, Visits: 246
I'm very grateful to you.

Now I'm going to test my strategy in order to discover if it works.

I'll keep you posted.

Bye.

Thanks!
Post #1005
Posted 2/17/2010 1:02:35 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 12/2/2010 9:08:34 AM
Posts: 36, Visits: 246
Hi eMOe!

I've just tested the strategy.

It gives me an error in the line 83:11 "Reference to undefined object".

This is the wrong passage:
------------------------------------------------------------------------------------------
if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then

begin
CreateStopLimitOrder(MyTrade, MyTrade.OpenRate+Stop*Point, otStop, 'Stop');
CreateStopLimitOrder(MyTrade, MyTrade.OpenRate-Limit*Point, otLimit, 'Limit');
end;
------------------------------------------------------------------------------------------

It doesn't recognize the trade inserted in order to set the stop and limit orders.
Maybe do we have to insert the script: MyTrade:=Trade ?

I mean like this:

if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then MyTrade:=Trade; ??





Edited: 2/17/2010 1:04:55 PM by p.costa
Post #1009
Posted 2/18/2010 6:11:01 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 1/27/2012 12:53:25 PM
Posts: 201, Visits: 295
I think you can do it even simpler:

Try it this way:

if (Action = dmtInsert) and (Trade.Tag = 'EntryStopSell') then

begin
CreateStopLimitOrder(Trade, Trade.OpenRate+Stop*Point, otStop, 'Stop');
CreateStopLimitOrder(Trade, Trade.OpenRate-Limit*Point, otLimit, 'Limit');
end;

(The Trade variable is already identified in the procedure)

Best regards!
Post #1015
Posted 2/19/2010 5:34:27 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 12/2/2010 9:08:34 AM
Posts: 36, Visits: 246
Thank you very much!
It works perfectly.

In attachment the corrected strategy.

  Post Attachments 
pcosta IT WORKS.act (402 views, 4.10 KB)
Post #1020
Posted 2/19/2010 9:14:39 AM


Forum Guru

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru

Group: Forum Members
Last Login: 3/9/2010 8:13:40 AM
Posts: 58, Visits: 114
Congratulations!

---------------
Bob Farlow
Post #1024
Posted 2/22/2010 7:50:04 AM


Forum Guru

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru

Group: Forum Members
Last Login: 10/4/2010 6:19:12 AM
Posts: 67, Visits: 186
Nice strategy! Congratulations, p.costa!




Sincerely,

Benjamin Hoover
Post #1040
Posted 5/14/2010 2:43:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 5/14/2010 4:16:19 PM
Posts: 1, Visits: 6
still new around here,any help for starters

http://expertforextraders.co.uk
Post #1512
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin

All times are GMT -5:00, Time now is 11:29am


© 2009 Actforex.com Terms of Use