Help edit hedging strategy, please!

Posted By dima1977 Sunday, November 27, 2011
Add to Favorites0
Author Message
dima1977
 Posted Sunday, November 27, 2011
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
const
StrategyName = 'New';

var
Hist1, Hist2: TCandleHistory;
Account: TAccount;
Amount, Rate, // last rate of the open position
RateCur1, // current rate of the first instrument
RateCur2, // current rate of the second instrument
RateFirst1, // rate of the first position opened on the first instrument. When = 0 - no position is opened on this step
RateFirst2, // rate of the first position opened on the second instrument. When = 0 - no position is opened on this step
mult1, //multiplier for calculating PL on positions opened on the first instrument
mult2: Double; //multiplier for calculating PL on positions opened on the second instrument
TraderRange, OnlyBuy, ProfitPlus, ProfitMinus, Profit, Pos, PosCounter : integer;


procedure OnCreate;
begin
AddCandleHistorySetting(@Hist1, 'Candle History 1', 'EURUSD', CI_1_minute, 100);
Hist1.OnNewRateEvent := @OnNewRate;

AddCandleHistorySetting(@Hist2, 'Candle History 2', 'USDCHF', CI_1_minute, 100);

AddIntegerSetting(@OnlyBuy, 'Only Buy 0 - buy, 1 - sell', 1);
AddIntegerSetting(@ProfitPlus, 'Profit Plus', 10);
AddIntegerSetting(@ProfitMinus, 'Profit Minus', -10);
AddIntegerSetting(@Profit, 'Profit', 15);

AddAccountSetting(@Account, 'Account', '');
AddFloatSetting(@Amount, 'Amount(Lots)', 1);
AddIntegerSetting(@Pos, 'Number of positions', 10);
AddIntegerSetting(@TraderRange, 'Trader Range', 0); //setting up the trader range in pips

end;

procedure OnStart;
begin
// the prices are compared; mult - is multiplier for calculating PL of the position
mult1 := 1;
if (OnlyBuy = 1) then mult1 := -1;
mult2 := mult1;
mult1 := mult1*Hist1.Instrument.PointSize;
mult2 := mult2*Hist2.Instrument.PointSize;
Rate := 0;
RateFirst1 := 0;
RateFirst2 := 0;
PosCounter := 0;
end;

procedure OnNewRate;
var
i : integer;
p1, p2 : double;
begin
if (OnlyBuy = 0) then
begin
RateCur1 := Hist1.Instrument.Buy;
RateCur2 := Hist2.Instrument.Buy;
end
else
begin
RateCur1 := Hist1.Instrument.Sell;
RateCur2 := Hist2.Instrument.Sell;
end;
// first event
if Rate = 0 then Rate := RateCur1;
// when position is opened
if (((RateCur1 - Rate)/mult1 >= ProfitPlus) and (PosCounter < Pos)) then
begin
PosCounter := PosCounter +1;
CreateOrder(Hist1.Instrument, Account, Amount, OnlyBuy,
NullRate,
NullRate, TraderRange, 'Hedging Strategy');
Rate := RateCur1;
if RateFirst1 = 0 then RateFirst1 := RateCur1;
end;
// when position is hedged
if (((RateCur1 - Rate)/mult1 <= ProfitMinus) and (PosCounter < Pos)) then
begin
PosCounter := PosCounter +1;
CreateOrder(Hist2.Instrument, Account, Amount, OnlyBuy,
NullRate,
NullRate, TraderRange, 'Hedging Strategy');
Rate := RateCur1;
if RateFirst2 = 0 then RateFirst2 := RateCur2;
end;
//positions can be opened only in one instrument
if ((PosCounter > 0) and (RateFirst1 <> 0)) then
p1 := ((RateCur1 - RateFirst1)/mult1)
else p1 := 0;
if ((PosCounter > 0) and (RateFirst2 <> 0)) then
p2 := ((RateCur2 - RateFirst2)/mult2)
else p2 := 0;
// when position is closed
if (p1 + p2 >= Profit) then
begin
for i:=TradeList.Count-1 downto 0 do
if (TradeList.Get(i).Tag = 'Hedging Strategy') then
CloseTrade(TradeList.Get(i), TradeList.Get(i).Amount, TraderRange, TradeList.Get(i).Tag);
// step is finished
RateFirst1 := 0;
RateFirst2 := 0;
PosCounter := 0;
end;
end;


Help edit the strategy, please! I want all closed when Net Pl from all positions > = Profit was ...
In fact, сlosing of the trades in profit happening only if the number of open positions on the first instrument is = the number of open positions on the second instrument. In other cases, I don't understand how everything is closed.This strategy i'm downloaded from this forum and running in the demo two weeks ...Sorry for my English, I'm from Russia)


Помогите отредактировать стратегию, пожалуйста! Чтобы все закрывалось, когда Net Pl от всех позиций >= задаваемой прибыли было...
На деле, закрытие торгов в прибыли происходит, только если количество открытых позиций по первому инструменту равно количеству открытых позиций по второму инструменту. В остальных случаях, я не понимаю, как все закрывается...Эту стратегию скачал с этого форума и гоняю в демо две недели...
Sunday, November 27, 2011 by dima1977
Admin
 Posted Tuesday, January 24, 2012
Administrator

Administrator - (123,785 reputation)

Group: Administrators
Last Active: Friday, June 02, 2017
Posts: 548, Visits: 3,295
Dear dima1977,

Check of the NetPL for all opened position has been added to the current strategy (see attach).


Best regards

-ACTFX© Forum Administrator
 Hedging Strategy with NetPL check.acts (67 views, 4.59 KB)
dima1977
 Posted Wednesday, January 25, 2012
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
Admin (1/24/2012)
Dear dima1977,

Check of the NetPL for all opened position has been added to the current strategy (see attach).


Thank you very much for help!
dima1977
 Posted Thursday, January 26, 2012
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
Admin (1/24/2012)
Dear dima1977,

Check of the NetPL for all opened position has been added to the current strategy (see attach).


Thanks a lot! Everything works fine!
The last question ...How do I make this strategy to take into account open positions and did not open new after the weekend or lack of communication with the server?


Спасибо большое! Все работает отлично! Последний вопрос! Как сделать, чтобы эта стратегия заново начинала учитывать открытые позиции и не открывала новые после выходных или перебоев со связью с сервером?

Best regards!
Dmitry...
Admin
 Posted Friday, January 27, 2012
Administrator

Administrator - (123,785 reputation)

Group: Administrators
Last Active: Friday, June 02, 2017
Posts: 548, Visits: 3,295
Dear dima1977,

If you want your strategy to be linked with positions opened by it before reconnection, you should add the following code to the OnStart procedure:


procedure OnStart;
var i:integer;
begin
for i:=tradelist.count-1 downto 0 do
begin
if (TradeList.Get(i).Tag = 'Hedging Strategy') then
begin
inc(PosCounter);
end;
end;
….


Best regards

-ACTFX© Forum Administrator
dima1977
 Posted Friday, January 27, 2012
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
Thanks for your help!
dima1977
 Posted Tuesday, January 31, 2012
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
Admin (1/27/2012)
Dear dima1977,

If you want your strategy to be linked with positions opened by it before reconnection, you should add the following code to the OnStart procedure:


procedure OnStart;
var i:integer;
begin
for i:=tradelist.count-1 downto 0 do
begin
if (TradeList.Get(i).Tag = 'Hedging Strategy') then
begin
inc(PosCounter);
end;
end;
….



Dear Admin! I added! But she did not want to link the previously opened positions anyway after reboot.Check please!

const
StrategyName = 'Hedging Strategy with NetPL check';
//StrategyVersion=1.1;

var
Hist1, Hist2: TCandleHistory;
Account: TAccount;
Amount, Rate, // last rate of the open position
RateCur1, // current rate of the first instrument
RateCur2, // current rate of the second instrument
RateFirst1, // rate of the first position opened on the first instrument. When = 0 - no position is opened on this step
RateFirst2, // rate of the first position opened on the second instrument. When = 0 - no position is opened on this step
mult1, //multiplier for calculating PL on positions opened on the first instrument
mult2: Double; //multiplier for calculating PL on positions opened on the second instrument
TraderRange, OnlyBuy, ProfitPlus, ProfitMinus, Profit, Pos, PosCounter : integer;


procedure OnCreate;
begin
AddCandleHistorySetting(@Hist1, 'Candle History 1', 'USDCHF', CI_1_Day, 100);
Hist1.OnNewRateEvent := @OnNewRate;

AddCandleHistorySetting(@Hist2, 'Candle History 2', 'EURUSD', CI_1_Day, 100);

AddIntegerSetting(@OnlyBuy, 'Only Buy 0 - buy, 1 - sell', 1);
AddIntegerSetting(@ProfitPlus, 'Profit Plus', 5);
AddIntegerSetting(@ProfitMinus, 'Profit Minus', -5);
AddIntegerSetting(@Profit, 'Profit', 20);

AddAccountSetting(@Account, 'Account', '');
AddFloatSetting(@Amount, 'Amount(Lots)', 1);
AddIntegerSetting(@Pos, 'Number of positions', 10);
AddIntegerSetting(@TraderRange, 'Trader Range', 0); //setting up the trader range in pips

end;

procedure OnStart;

var i:integer;

begin
// the prices are compared; mult - is multiplier for calculating PL of the position
mult1 := 1;
if (OnlyBuy = 1) then mult1 := -1;
mult2 := mult1;
mult1 := mult1*Hist1.Instrument.PointSize;
mult2 := mult2*Hist2.Instrument.PointSize;
Rate := 0;
RateFirst1 := 0;
RateFirst2 := 0;
PosCounter := 0;
for i:=tradelist.count-1 downto 0 do

begin

if (TradeList.Get(i).Tag = 'Hedging Strategy') then

begin

inc(PosCounter);

end;

end;

end;

procedure OnNewRate;
var
i : integer;
totalPL, p1, p2 : double;

begin
if (OnlyBuy = 0) then
begin
RateCur1 := Hist1.Instrument.Buy;
RateCur2 := Hist2.Instrument.Buy;
end
else
begin
RateCur1 := Hist1.Instrument.Sell;
RateCur2 := Hist2.Instrument.Sell;
end;
// first event
if Rate = 0 then Rate := RateCur1;
// when position is opened
if (((RateCur1 - Rate)/mult1 >= ProfitPlus) and (PosCounter < Pos)) then
begin
PosCounter := PosCounter +1;
CreateOrder(Hist1.Instrument, Account, Amount, OnlyBuy,
NullRate,
NullRate, TraderRange, 'Hedging Strategy');
Rate := RateCur1;
if RateFirst1 = 0 then RateFirst1 := RateCur1;
end;
// when position is hedged
if (((RateCur1 - Rate)/mult1 <= ProfitMinus) and (PosCounter < Pos)) then
begin
PosCounter := PosCounter +1;
CreateOrder(Hist2.Instrument, Account, Amount, OnlyBuy,
NullRate,
NullRate, TraderRange, 'Hedging Strategy');
Rate := RateCur1;
if RateFirst2 = 0 then RateFirst2 := RateCur2;
end;
//positions can be opened only in one instrument
if ((PosCounter > 0) and (RateFirst1 <> 0)) then
p1 := ((RateCur1 - RateFirst1)/mult1)
else p1 := 0;
if ((PosCounter > 0) and (RateFirst2 <> 0)) then
p2 := ((RateCur2 - RateFirst2)/mult2)
else p2 := 0;

// when position is closed
totalPL:=0;
for i:=TradeList.Count-1 downto 0 do
if (TradeList.Get(i).Tag = 'Hedging Strategy') then
begin
totalPL:=totalPL+TradeList.Get(i).netPL;
end;

if totalPL>=Profit then
begin
for i:=TradeList.Count-1 downto 0 do
if (TradeList.Get(i).Tag = 'Hedging Strategy') then
begin
CloseTrade(TradeList.Get(i), TradeList.Get(i).Amount, TraderRange, TradeList.Get(i).Tag);
end;

RateFirst1 := 0;
RateFirst2 := 0;
PosCounter := 0;
end;

end;



With respect, Dmitry.
Tuesday, January 31, 2012 by dima1977
Admin
 Posted Wednesday, February 01, 2012
Administrator

Administrator - (123,785 reputation)

Group: Administrators
Last Active: Friday, June 02, 2017
Posts: 548, Visits: 3,295
Dear Dmitry,

The attached strategy consider positions previously opened by this strategy after restart


Best regards

-ACTFX© Forum Administrator
 Hedging Strategy with NetPL check.acts (70 views, 4.88 KB)
ryko
 Posted Wednesday, March 14, 2012
Supreme Being

Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)Supreme Being - (323 reputation)

Group: Forum Members
Last Active: Thursday, March 15, 2012
Posts: 1, Visits: 18
323
Hello
Checking strategy for real account. In the test behaves reasonably well. Realtime results in incorrect values ​​of the account stats . Balance, equity and netprofit are wrongly reported. For example, the net profit
111.24 is a result of open positions is -145.67. I am using the latest version, which after restarting should include previously open positions. This did not happen. Previously, open positions are visible only in the position log and are not Supported by the strategy. If there are 10 this strategy does nothing. Unfortunately I can not find the cause of these errors. Can someone fluent in programming could find a solution.
greetings
Richard
dima1977
 Posted Wednesday, June 27, 2012
Supreme Being

Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)Supreme Being - (3,273 reputation)

Group: Forum Members
Last Active: Sunday, December 23, 2012
Posts: 16, Visits: 58
Hello!

Dear Admin!
You can adjust this strategy? I want to get the number of open positions for the first pair = to the number of open positions on the second pair?
Or in an extreme case could be separately set the number of open positions for each pair.
Thank you in advance...

Здравствуйте, дорогой Админ!
Можете немного подправить эту стратегию? Чтобы количество открываемых позиций по первому инструменту равнялось количеству открываемых позиций по второму инструменту. Или, в крайнем случае, можно было бы отдельно для каждой пары задавать количество открытых позиций.
Спасибо заранее...

Similar Topics

Expand / Collapse

Reading This Topic

Expand / Collapse

Back To Top