Triangular Arbitration

Author: Copyright © 2021, Khlistov Vladimir
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Triangular Arbitration
ÿþ//+--------------------------------------------------------------------------------+

//| !>25B=8:, 2KAB02;O5B 3 ?>78F88 ?> 3< 20;NB0< 5A;8 ?> =8< =5B >B:@KBKE ?>78F89. |

//|                                       Copyright © 2013-2021, Khlistov Vladimir |

//|                                                       http://cmillion.narod.ru |

//+--------------------------------------------------------------------------------+

#property copyright "Copyright © 2021, Khlistov Vladimir"

#property link      "http://cmillion.ru"

#property version   "1.0"

#property strict

#property description "!>25B=8: B>@3C5B ?> 3< 8=AB@C<5=B0< ?> A8AB5<5 B@5C3>;L=8:. !B@0EC5B @8A: >4=>9 ?>78F88 70 AG5B 42CE 4@C38E. https://cmillion.ru/sovetnik-cm_ea_hedge/"

#property description "The Expert Advisor trades on 3 instruments using the triangle system. Insures the risk of one position at the expense of the other two."

//+------------------------------------------------------------------+

extern string  SYMBOL1        = "GBPUSD";

extern string  SYMBOL2        = "USDJPY";

extern string  SYMBOL3        = "GBPJPY";

extern double  LOT1           = 0.1;

extern double  LOT2           = 0.1;

extern double  LOT3           = 0.1;

extern int     TYPE1          = OP_BUY;

extern int     TYPE2          = OP_BUY;

extern int     TYPE3          = OP_SELL;

extern int     Magic          = 777888;

extern double  ProfitClose    = 1.0;

//+------------------------------------------------------------------+

string AC;

int OnInit()

{ 

   EventSetTimer(1);

   AC=AccountCurrency();

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

void OnTick() {OnTimer();}

void OnTimer()

{

   if (IsTesting())

   {

      if (OrdersTotal()==0) if (OrderSend(Symbol(),OP_BUY,SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),Ask,1000,0,0,"Hedge 3",Magic,0,clrNONE)==-1) 

         Print("Error open ",LOT1," ", SYMBOL1);

      return;

   }

   string Sym;

   int    i;

   double profit=0;

   double profit1=0;

   double profit2=0;

   double profit3=0;

   bool open1=false;

   bool open2=false;

   bool open3=false;

   for (i=0; i<OrdersTotal(); i++)

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      { 

         if (Magic!=OrderMagicNumber()) continue;

         Sym = OrderSymbol();

         if (Sym!=SYMBOL1 && Sym!=SYMBOL2 && Sym!=SYMBOL3) continue;

         profit=OrderProfit()+OrderSwap()+OrderCommission();

         if (Sym==SYMBOL1) {open1=true;profit1+=profit;}

         if (Sym==SYMBOL2) {open2=true;profit2+=profit;}

         if (Sym==SYMBOL3) {open3=true;profit3+=profit;}

      }

   } 

   

   //---

   

   profit=profit1+profit2+profit3;

   

   //---

   

   if (profit>=ProfitClose)

   {

      for (i = OrdersTotal()-1; i >= 0; i--)

      {

         if (OrderSelect(i, SELECT_BY_POS))

         {

            if (Magic!=OrderMagicNumber()) continue;

            Sym = OrderSymbol();

            if (Sym!=SYMBOL1 && Sym!=SYMBOL2 && Sym!=SYMBOL3) continue;

            if (!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(Sym,OrderType()==OP_BUY?MODE_BID:MODE_ASK),(int)MarketInfo(Sym,MODE_DIGITS)),1000,clrNONE)) 

            Print("Error close ",Sym);

         }

      }

   }

   

   Comment(TimeCurrent(),"\n",SYMBOL1," ",DoubleToString(profit1,2)," ",AC,

                         "\n",SYMBOL2," ",DoubleToString(profit2,2)," ",AC,

                         "\n",SYMBOL3," ",DoubleToString(profit3,2)," ",AC,

                         "\nprofit =",DoubleToString(profit,2)," ",AC);

   //---

   

   if (!open1)

   {

      if (OrderSend(SYMBOL1,TYPE1,LOT1,NormalizeDouble(MarketInfo(SYMBOL1,TYPE1==OP_BUY?MODE_BID:MODE_ASK),

      (int)MarketInfo(SYMBOL1,MODE_DIGITS)),1000,0,0,"Hedge 3",Magic,0,clrNONE)==-1) 

         Print("Error open ",LOT1," ", SYMBOL1);

   }

   if (!open2)

   {

      if (OrderSend(SYMBOL2,TYPE2,LOT2,NormalizeDouble(MarketInfo(SYMBOL2,TYPE2==OP_BUY?MODE_BID:MODE_ASK),

      (int)MarketInfo(SYMBOL2,MODE_DIGITS)),1000,0,0,"Hedge 3",Magic,0,clrNONE)==-1) 

         Print("Error open ",LOT2," ", SYMBOL2);

   }

   if (!open3)

   {

      if (OrderSend(SYMBOL3,TYPE3,LOT3,NormalizeDouble(MarketInfo(SYMBOL3,TYPE3==OP_BUY?MODE_BID:MODE_ASK),

      (int)MarketInfo(SYMBOL3,MODE_DIGITS)),1000,0,0,"Hedge 3",Magic,0,clrNONE)==-1) 

         Print("Error open ",LOT3," ", SYMBOL3);

   }

} 

//--------------------------------------------------------------------



Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---