﻿<?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 Strategies  » Help edit hedging strategy, please!</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:49:40 GMT</lastBuildDate><ttl>20</ttl><item><title>Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3658.aspx</link><description>const&lt;br&gt;StrategyName = 'New';&lt;br&gt;&lt;br&gt;var&lt;br&gt;Hist1, Hist2: TCandleHistory;&lt;br&gt;Account: TAccount;&lt;br&gt;Amount, Rate, // last rate of the open position&lt;br&gt;RateCur1, // current rate of the first instrument&lt;br&gt;RateCur2, // current rate of the second instrument&lt;br&gt;RateFirst1, // rate of the first position opened on the first instrument. When = 0 - no position is opened on this step&lt;br&gt;RateFirst2, // rate of the first position opened on the second instrument. When = 0 - no position is opened on this step&lt;br&gt;mult1, //multiplier for calculating PL on positions opened on the first instrument&lt;br&gt;mult2: Double; //multiplier for calculating PL on positions opened on the second instrument&lt;br&gt;TraderRange, OnlyBuy, ProfitPlus, ProfitMinus, Profit, Pos, PosCounter : integer;&lt;br&gt;&lt;br&gt;&lt;br&gt;procedure OnCreate;&lt;br&gt;begin&lt;br&gt;AddCandleHistorySetting(@Hist1, 'Candle History 1', 'EURUSD', CI_1_minute, 100);&lt;br&gt;Hist1.OnNewRateEvent := @OnNewRate;&lt;br&gt;&lt;br&gt;AddCandleHistorySetting(@Hist2, 'Candle History 2', 'USDCHF', CI_1_minute, 100);&lt;br&gt;&lt;br&gt;AddIntegerSetting(@OnlyBuy, 'Only Buy 0 - buy, 1 - sell', 1);&lt;br&gt;AddIntegerSetting(@ProfitPlus, 'Profit Plus', 10);&lt;br&gt;AddIntegerSetting(@ProfitMinus, 'Profit Minus', -10);&lt;br&gt;AddIntegerSetting(@Profit, 'Profit', 15);&lt;br&gt;&lt;br&gt;AddAccountSetting(@Account, 'Account', '');&lt;br&gt;AddFloatSetting(@Amount, 'Amount(Lots)', 1);&lt;br&gt;AddIntegerSetting(@Pos, 'Number of positions', 10);&lt;br&gt;AddIntegerSetting(@TraderRange, 'Trader Range', 0); //setting up the trader range in pips&lt;br&gt;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;procedure OnStart;&lt;br&gt;begin&lt;br&gt;// the prices are compared; mult - is multiplier for calculating PL of the position&lt;br&gt;mult1 := 1;&lt;br&gt;if (OnlyBuy = 1) then mult1 := -1;&lt;br&gt;mult2 := mult1;&lt;br&gt;mult1 := mult1*Hist1.Instrument.PointSize;&lt;br&gt;mult2 := mult2*Hist2.Instrument.PointSize;&lt;br&gt;Rate := 0;&lt;br&gt;RateFirst1 := 0;&lt;br&gt;RateFirst2 := 0;&lt;br&gt;PosCounter := 0;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;procedure OnNewRate;&lt;br&gt;var&lt;br&gt;i : integer;&lt;br&gt;p1, p2 : double;&lt;br&gt;begin&lt;br&gt;if (OnlyBuy = 0) then&lt;br&gt;begin&lt;br&gt;RateCur1 := Hist1.Instrument.Buy;&lt;br&gt;RateCur2 := Hist2.Instrument.Buy;&lt;br&gt;end&lt;br&gt;else&lt;br&gt;begin&lt;br&gt;RateCur1 := Hist1.Instrument.Sell;&lt;br&gt;RateCur2 := Hist2.Instrument.Sell;&lt;br&gt;end;&lt;br&gt;// first event&lt;br&gt;if Rate = 0 then Rate := RateCur1;&lt;br&gt;// when position is opened&lt;br&gt;if (((RateCur1 - Rate)/mult1 &gt;= ProfitPlus) and (PosCounter &lt; Pos)) then&lt;br&gt;begin&lt;br&gt;PosCounter := PosCounter +1;&lt;br&gt;CreateOrder(Hist1.Instrument, Account, Amount, OnlyBuy,&lt;br&gt;NullRate,&lt;br&gt;NullRate, TraderRange, 'Hedging Strategy');&lt;br&gt;Rate := RateCur1;&lt;br&gt;if RateFirst1 = 0 then RateFirst1 := RateCur1;&lt;br&gt;end;&lt;br&gt;// when position is hedged&lt;br&gt;if (((RateCur1 - Rate)/mult1 &lt;= ProfitMinus) and (PosCounter &lt; Pos)) then&lt;br&gt;begin&lt;br&gt;PosCounter := PosCounter +1;&lt;br&gt;CreateOrder(Hist2.Instrument, Account, Amount, OnlyBuy,&lt;br&gt;NullRate,&lt;br&gt;NullRate, TraderRange, 'Hedging Strategy');&lt;br&gt;Rate := RateCur1;&lt;br&gt;if RateFirst2 = 0 then RateFirst2 := RateCur2;&lt;br&gt;end;&lt;br&gt;//positions can be opened only in one instrument&lt;br&gt;if ((PosCounter &gt; 0) and (RateFirst1 &lt;&gt; 0)) then&lt;br&gt;p1 := ((RateCur1 - RateFirst1)/mult1)&lt;br&gt;else p1 := 0;&lt;br&gt;if ((PosCounter &gt; 0) and (RateFirst2 &lt;&gt; 0)) then&lt;br&gt;p2 := ((RateCur2 - RateFirst2)/mult2)&lt;br&gt;else p2 := 0;&lt;br&gt;// when position is closed&lt;br&gt;if (p1 + p2 &gt;= Profit) then&lt;br&gt;begin&lt;br&gt;for i:=TradeList.Count-1 downto 0 do&lt;br&gt;if (TradeList.Get(i).Tag = 'Hedging Strategy') then&lt;br&gt;CloseTrade(TradeList.Get(i), TradeList.Get(i).Amount, TraderRange, TradeList.Get(i).Tag);&lt;br&gt;// step is finished&lt;br&gt;RateFirst1 := 0;&lt;br&gt;RateFirst2 := 0;&lt;br&gt;PosCounter := 0;&lt;br&gt;end;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;&lt;br&gt;[b]Help edit the strategy, please! I want all closed when Net Pl from all positions &gt; =  Profit was ...&lt;br&gt;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)[/b]&lt;br&gt;&lt;br&gt;[b]Помогите отредактировать стратегию, пожалуйста! Чтобы все закрывалось, когда Net Pl от всех позиций &gt;= задаваемой прибыли было...&lt;br&gt;На деле, закрытие торгов в прибыли происходит, только если количество открытых позиций по первому инструменту равно количеству открытых позиций по второму инструменту. В остальных случаях, я не понимаю, как все закрывается...Эту стратегию скачал с этого форума и гоняю в демо две недели...[/b]</description><pubDate>Wed, 14 Mar 2012 11:26:10 GMT</pubDate><dc:creator>dima1977</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3828.aspx</link><description>Hello&lt;br&gt;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&lt;br&gt;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.&lt;br&gt;greetings&lt;br&gt;Richard</description><pubDate>Wed, 14 Mar 2012 11:26:10 GMT</pubDate><dc:creator>ryko</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3789.aspx</link><description>Dear Dmitry,&lt;br&gt;&lt;br&gt;The attached strategy consider positions previously opened by this strategy after restart</description><pubDate>Wed, 01 Feb 2012 06:16:42 GMT</pubDate><dc:creator>Admin</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3788.aspx</link><description>[quote][b]Admin (1/27/2012)[/b][hr]Dear dima1977,&lt;br&gt;&lt;br&gt;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:&lt;br&gt;&lt;br&gt;[code]&lt;br&gt;procedure OnStart;&lt;br&gt;var i:integer;&lt;br&gt;begin&lt;br&gt;   for i:=tradelist.count-1 downto 0 do   &lt;br&gt;   begin&lt;br&gt;       if (TradeList.Get(i).Tag = 'Hedging Strategy') then&lt;br&gt;         begin         &lt;br&gt;           inc(PosCounter);&lt;br&gt;         end;    &lt;br&gt;   end;&lt;br&gt;….&lt;br&gt;[/code][/quote]&lt;br&gt;&lt;br&gt;&lt;br&gt;[b]Dear Admin! I added! But she did not want to link the previously opened positions anyway after reboot.Check please![u][/u][/b]&lt;br&gt;&lt;br&gt;const&lt;br&gt;  StrategyName = 'Hedging Strategy with NetPL check';&lt;br&gt;//StrategyVersion=1.1; &lt;br&gt;&lt;br&gt;var&lt;br&gt;  Hist1, Hist2: TCandleHistory;&lt;br&gt;  Account: TAccount;&lt;br&gt;  Amount, Rate, //  last rate of the open position&lt;br&gt;          RateCur1, // current rate of the first instrument&lt;br&gt;          RateCur2, // current rate of the second instrument&lt;br&gt;          RateFirst1, // rate of the first position opened on the first instrument. When = 0 - no position is opened on this step&lt;br&gt;          RateFirst2, // rate of the first position opened on the second instrument. When = 0 - no position is opened on this step      &lt;br&gt;          mult1, //multiplier for calculating PL on positions opened on the first instrument&lt;br&gt;          mult2: Double; //multiplier for calculating PL on positions opened on the second instrument   &lt;br&gt;  TraderRange, OnlyBuy, ProfitPlus, ProfitMinus, Profit, Pos, PosCounter : integer;&lt;br&gt;&lt;br&gt;&lt;br&gt;procedure OnCreate;&lt;br&gt;begin&lt;br&gt;  AddCandleHistorySetting(@Hist1, 'Candle History 1', 'USDCHF', CI_1_Day, 100);&lt;br&gt;  Hist1.OnNewRateEvent := @OnNewRate;&lt;br&gt;&lt;br&gt;  AddCandleHistorySetting(@Hist2, 'Candle History 2', 'EURUSD', CI_1_Day, 100);      &lt;br&gt;&lt;br&gt;  AddIntegerSetting(@OnlyBuy, 'Only Buy 0 - buy, 1 - sell', 1);  &lt;br&gt;  AddIntegerSetting(@ProfitPlus, 'Profit Plus', 5);  &lt;br&gt;  AddIntegerSetting(@ProfitMinus, 'Profit Minus', -5);  &lt;br&gt;  AddIntegerSetting(@Profit, 'Profit', 20);&lt;br&gt;&lt;br&gt;  AddAccountSetting(@Account, 'Account', '');&lt;br&gt;  AddFloatSetting(@Amount, 'Amount(Lots)', 1);&lt;br&gt;  AddIntegerSetting(@Pos, 'Number of positions', 10);              &lt;br&gt;  AddIntegerSetting(@TraderRange, 'Trader Range', 0);   //setting up the trader range in pips      &lt;br&gt;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;procedure OnStart;&lt;br&gt;&lt;br&gt;[b]var i:integer;[/b]&lt;br&gt;&lt;br&gt;begin&lt;br&gt;  // the prices are compared; mult - is multiplier for calculating PL of the position &lt;br&gt;   mult1 := 1; &lt;br&gt;   if  (OnlyBuy = 1) then mult1 := -1;   &lt;br&gt;   mult2 := mult1;  &lt;br&gt;   mult1 := mult1*Hist1.Instrument.PointSize;      &lt;br&gt;   mult2 := mult2*Hist2.Instrument.PointSize;&lt;br&gt;   Rate :=  0;&lt;br&gt;   RateFirst1 := 0;&lt;br&gt;   RateFirst2 := 0;                     &lt;br&gt;   PosCounter := 0;      &lt;br&gt;   for i:=tradelist.count-1 downto 0 do   &lt;br&gt;&lt;br&gt;   [b]begin&lt;br&gt;&lt;br&gt;       if (TradeList.Get(i).Tag = 'Hedging Strategy') then&lt;br&gt;&lt;br&gt;         begin         &lt;br&gt;&lt;br&gt;           inc(PosCounter);&lt;br&gt;&lt;br&gt;         end;    &lt;br&gt;&lt;br&gt;   end;[/b]&lt;br&gt;end;&lt;br&gt;&lt;br&gt;procedure OnNewRate;  &lt;br&gt;var &lt;br&gt;  i : integer;    &lt;br&gt;  totalPL, p1, p2 : double;                                                                                        &lt;br&gt;&lt;br&gt;begin&lt;br&gt;    if  (OnlyBuy = 0) then &lt;br&gt;    begin&lt;br&gt;       RateCur1 := Hist1.Instrument.Buy;       &lt;br&gt;       RateCur2 := Hist2.Instrument.Buy;&lt;br&gt;    end  &lt;br&gt;    else &lt;br&gt;    begin&lt;br&gt;       RateCur1 := Hist1.Instrument.Sell;&lt;br&gt;       RateCur2 := Hist2.Instrument.Sell;          &lt;br&gt;    end;&lt;br&gt;    // first event        &lt;br&gt;    if Rate = 0 then Rate := RateCur1;&lt;br&gt;    //  when position is opened&lt;br&gt;    if (((RateCur1 - Rate)/mult1 &gt;= ProfitPlus) and (PosCounter &lt; Pos)) then       &lt;br&gt;    begin                &lt;br&gt;        PosCounter := PosCounter +1;&lt;br&gt;        CreateOrder(Hist1.Instrument, Account, Amount, OnlyBuy,&lt;br&gt;                    NullRate,&lt;br&gt;                    NullRate, TraderRange, 'Hedging Strategy');&lt;br&gt;        Rate := RateCur1;            &lt;br&gt;        if RateFirst1 = 0 then RateFirst1 := RateCur1;             &lt;br&gt;    end;                  &lt;br&gt;    //  when position is hedged&lt;br&gt;    if (((RateCur1 - Rate)/mult1 &lt;= ProfitMinus) and (PosCounter &lt; Pos))  then       &lt;br&gt;    begin                   &lt;br&gt;        PosCounter := PosCounter +1;&lt;br&gt;        CreateOrder(Hist2.Instrument, Account, Amount, OnlyBuy,&lt;br&gt;                    NullRate,&lt;br&gt;                    NullRate, TraderRange, 'Hedging Strategy');                      &lt;br&gt;        Rate := RateCur1; &lt;br&gt;        if RateFirst2 = 0 then RateFirst2 := RateCur2;                   &lt;br&gt;    end;                   &lt;br&gt;    //positions can be opened only in one instrument &lt;br&gt;    if ((PosCounter &gt; 0) and (RateFirst1 &lt;&gt; 0)) then     &lt;br&gt;        p1 := ((RateCur1 - RateFirst1)/mult1)        &lt;br&gt;    else p1 := 0;          &lt;br&gt;    if ((PosCounter &gt; 0) and (RateFirst2 &lt;&gt; 0)) then     &lt;br&gt;        p2 := ((RateCur2 - RateFirst2)/mult2)        &lt;br&gt;    else p2 := 0;                 &lt;br&gt;   &lt;br&gt;    //  when position is closed&lt;br&gt;    totalPL:=0;                                          &lt;br&gt;        for i:=TradeList.Count-1 downto 0 do&lt;br&gt;         if (TradeList.Get(i).Tag = 'Hedging Strategy') then            &lt;br&gt;            begin&lt;br&gt;               totalPL:=totalPL+TradeList.Get(i).netPL;&lt;br&gt;            end;                           &lt;br&gt;&lt;br&gt;        if totalPL&gt;=Profit then&lt;br&gt;        begin    &lt;br&gt;           for i:=TradeList.Count-1 downto 0 do&lt;br&gt;             if (TradeList.Get(i).Tag = 'Hedging Strategy') then            &lt;br&gt;               begin&lt;br&gt;                  CloseTrade(TradeList.Get(i), TradeList.Get(i).Amount, TraderRange, TradeList.Get(i).Tag);&lt;br&gt;               end;    &lt;br&gt;                &lt;br&gt;           RateFirst1 := 0;&lt;br&gt;           RateFirst2 := 0;     &lt;br&gt;           PosCounter := 0;              &lt;br&gt;        end;          &lt;br&gt;&lt;br&gt;end;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;[b]With respect, Dmitry.[/b]</description><pubDate>Tue, 31 Jan 2012 05:36:12 GMT</pubDate><dc:creator>dima1977</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3782.aspx</link><description>Thanks for your help!</description><pubDate>Fri, 27 Jan 2012 11:28:37 GMT</pubDate><dc:creator>dima1977</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3779.aspx</link><description>Dear dima1977,&lt;br&gt;&lt;br&gt;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:&lt;br&gt;&lt;br&gt;[code]&lt;br&gt;procedure OnStart;&lt;br&gt;var i:integer;&lt;br&gt;begin&lt;br&gt;   for i:=tradelist.count-1 downto 0 do   &lt;br&gt;   begin&lt;br&gt;       if (TradeList.Get(i).Tag = 'Hedging Strategy') then&lt;br&gt;         begin         &lt;br&gt;           inc(PosCounter);&lt;br&gt;         end;    &lt;br&gt;   end;&lt;br&gt;….&lt;br&gt;[/code]</description><pubDate>Fri, 27 Jan 2012 06:01:23 GMT</pubDate><dc:creator>Admin</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3778.aspx</link><description>[quote][b]Admin (1/24/2012)[/b][hr]Dear dima1977,&lt;br&gt;&lt;br&gt;Check of the NetPL for all opened position has been added to the current strategy (see attach).&lt;br&gt;[/quote]&lt;br&gt;&lt;br&gt;Thanks a lot! Everything works fine!&lt;br&gt;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?&lt;br&gt;&lt;br&gt;&lt;br&gt;Спасибо большое! Все работает отлично! Последний вопрос! Как сделать, чтобы эта стратегия заново начинала учитывать открытые позиции и не открывала новые после выходных или перебоев со связью с сервером?&lt;br&gt;&lt;br&gt;Best regards!&lt;br&gt;Dmitry...</description><pubDate>Thu, 26 Jan 2012 23:55:24 GMT</pubDate><dc:creator>dima1977</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3777.aspx</link><description>[quote][b]Admin (1/24/2012)[/b][hr]Dear dima1977,&lt;br&gt;&lt;br&gt;Check of the NetPL for all opened position has been added to the current strategy (see attach).&lt;br&gt;[/quote]&lt;br&gt;&lt;br&gt;Thank you very much for help!</description><pubDate>Wed, 25 Jan 2012 02:09:47 GMT</pubDate><dc:creator>dima1977</dc:creator></item><item><title>RE: Help edit hedging strategy, please!</title><link>http://forum.actfx.com/FindPost3776.aspx</link><description>Dear dima1977,&lt;br&gt;&lt;br&gt;Check of the NetPL for all opened position has been added to the current strategy (see attach).&lt;br&gt;</description><pubDate>Tue, 24 Jan 2012 05:31:42 GMT</pubDate><dc:creator>Admin</dc:creator></item></channel></rss>
