|
|
|
Junior Member
      
Group: Forum Members
Last Login: 1/31/2012 10:31:53 AM
Posts: 12,
Visits: 26
|
|
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 от всех позиций >= задаваемой прибыли было...
На деле, закрытие торгов в прибыли происходит, только если количество открытых позиций по первому инструменту равно количеству открытых позиций по второму инструменту. В остальных случаях, я не понимаю, как все закрывается...Эту стратегию скачал с этого форума и гоняю в демо две недели...
Edited: 11/27/2011 6:10:39 PM by dima1977
|
|
|
|
|
Administrator
Group: Administrators
Last Login: Today @ 10:37:18 AM
Posts: 249,
Visits: 1,229
|
|
Dear dima1977,
Check of the NetPL for all opened position has been added to the current strategy (see attach).
Best regards
-ACTFX© Forum Administrator
|
|
|
|
|
Junior Member
      
Group: Forum Members
Last Login: 1/31/2012 10:31:53 AM
Posts: 12,
Visits: 26
|
|
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!
|
|
|
|
|
Junior Member
      
Group: Forum Members
Last Login: 1/31/2012 10:31:53 AM
Posts: 12,
Visits: 26
|
|
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...
|
|
|
|
|
Administrator
Group: Administrators
Last Login: Today @ 10:37:18 AM
Posts: 249,
Visits: 1,229
|
|
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
|
|
|
|
|
Junior Member
      
Group: Forum Members
Last Login: 1/31/2012 10:31:53 AM
Posts: 12,
Visits: 26
|
|
|
|
|
|
Junior Member
      
Group: Forum Members
Last Login: 1/31/2012 10:31:53 AM
Posts: 12,
Visits: 26
|
|
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.
Edited: 1/31/2012 10:39:04 AM by dima1977
|
|
|
|
|
Administrator
Group: Administrators
Last Login: Today @ 10:37:18 AM
Posts: 249,
Visits: 1,229
|
|
Dear Dmitry,
The attached strategy consider positions previously opened by this strategy after restart
Best regards
-ACTFX© Forum Administrator
|
|
|
|