Example of how to run an expert advisor in several currency pairs simultaneously

Examples step by step how to make your own automated strategy or user indicator
Message
Author
Lonesome
Posts: 28
Joined: Fri Oct 21, 2016 9:04 am

Example of how to run an expert advisor in several currency pairs simultaneously

#1 Postby Lonesome » Wed Apr 12, 2017 8:46 am

Below is the script for an expert advisor that simultaneously checks in 10 currency pairs.

Code: Select all

library PriceAction2;

//This EA works with 10 currency pairs. Each is optional.
//For each currency pair one order can be placed at one time.
//The EA works with 2 timeframes. The 4 hr and the 8 hr timeframe.
//Other timeframes can be added by adding timeframe specific procedures.
//The EA contains a MessageBox for script testing purposes.
//There are procedures for checking buy and sell conditions and for executing a buy order.
//The strategy can easily be changed by modifying just these 3 procedures.

uses
  SysUtils, StrategyInterfaceUnit, DateUtils, Windows;

var
  // external parameters
  SymbolName1: PAnsiChar = 'AUDJPY';
  SymbolName2: PAnsiChar = 'AUDUSD';
  SymbolName3: PAnsiChar = 'EURCAD';
  SymbolName4: PAnsiChar = 'EURJPY';
  SymbolName5: PAnsiChar = 'EURUSD';
  SymbolName6: PAnsiChar = 'GBPJPY';
  SymbolName7: PAnsiChar = 'GBPUSD';
  SymbolName8: PAnsiChar = 'USDCAD';
  SymbolName9: PAnsiChar = 'USDCHF';
  SymbolName10: PAnsiChar = 'USDJPY';
  SymbolName: PAnsiChar = nil;
  Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8,Name9,Name10: boolean;
  Timeframe: integer;
  LastTime: TDateTime;
  StopLoss: integer;
  TakeProfit: integer;
  Entry: integer;
  Lot: double = 1.0;

  // local variables
  Open0,Open1,Open2,Open3,Open4,Open5,Open6,Open7:double;
  Close0,Close1,Close2,Close3,Close4,Close5,Close6,Close7:double;
  High0,High1,High2,High3,High4,High5,High6,High7:double;
  Low0,Low1,Low2,Low3,Low4,Low5,Low6,Low7:double;
  BuyCondition,SellCondition:boolean;
  Hr4TimeFrame,Hr8TimeFrame:boolean;
  Ti0,Ti1,Ti2,Ti3,Ti3last:double;
  OrdersSymbol1: integer = 0;
  OrdersSymbol2: integer = 0;
  OrdersSymbol3: integer = 0;
  OrdersSymbol4: integer = 0;
  OrdersSymbol5: integer = 0;
  OrdersSymbol6: integer = 0;
  OrdersSymbol7: integer = 0;
  OrdersSymbol8: integer = 0;
  OrdersSymbol9: integer = 0;
  OrdersSymbol10: integer = 0;
  OrdersSymbolTemp: integer = 0;
  Calc0, Calc1:double;

{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
  // set strategy name and description
  StrategyShortName('Price Action #2');
  StrategyDescription('find reversal on high (sell stop)');

  // register parameters
  RegOption('AUDJPY',ot_Boolean, Name1);
  RegOption('AUDUSD',ot_Boolean, Name2);
  RegOption('EURCAD',ot_Boolean, Name3);
  RegOption('EURJPY',ot_Boolean, Name4);
  RegOption('EURUSD',ot_Boolean, Name5);
  RegOption('GBPJPY',ot_Boolean, Name6);
  RegOption('GBPUSD',ot_Boolean, Name7);
  RegOption('USDCAD',ot_Boolean, Name8);
  RegOption('USDCHF',ot_Boolean, Name9);
  RegOption('USDJPY',ot_Boolean, Name10);
  AddSeparator('Trading options');
  RegOption('Timeframe (4 hr or 8 hr only', ot_TimeFrame, Timeframe);
  RegOption('Lot', Lot, 1, 0.1, 10);
  RegOption('Entry (pips below previous low)', Entry, 1, 100000);
  RegOption('Take Profit (pips below previous low)', TakeProfit, 1, 100000);
  RegOption('Stop Loss (pips above previous high)', StopLoss, 1, 100000);
  end;

{-----Done strategy---------------------------------------------------------}
procedure DoneStrategy; stdcall;
begin
  FreeMem(SymbolName1);
  FreeMem(SymbolName2);
  FreeMem(SymbolName3);
  FreeMem(SymbolName4);
  FreeMem(SymbolName5);
  FreeMem(SymbolName6);
  FreeMem(SymbolName7);
  FreeMem(SymbolName8);
  FreeMem(SymbolName9);
  FreeMem(SymbolName10);
  end;

{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
  LastTime := 0;    // reset options before new test
end;

{-----Message Box-----------------------------------------------}
procedure MessageWindow; stdcall;
begin
  MessageBox(0, PChar('Hour:    '+FloatToStr(Ti0)+#13#10+
            'Minute:  '+FloatToStr(Ti1)+#13#10+
            'Second:  '+FloatToStr(Ti2)+#13#10+
            'MilliSecond:  '+FloatToStr(Ti3)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(0):  '+FloatToStr(Open0)+#13#10+
            'High(0):  '+FloatToStr(High0)+#13#10+
            'Low(0):   '+FloatToStr(Low0)+#13#10+
            'Close(0): '+FloatToStr(Close0)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(1):  '+FloatToStr(Open1)+#13#10+
            'High(1):  '+FloatToStr(High1)+#13#10+
            'Low(1):   '+FloatToStr(Low1)+#13#10+
            'Close(1): '+FloatToStr(Close1)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(2):  '+FloatToStr(Open2)+#13#10+
            'High(2):  '+FloatToStr(High2)+#13#10+
            'Low(2):   '+FloatToStr(Low2)+#13#10+
            'Close(2): '+FloatToStr(Close2)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(3):  '+FloatToStr(Open3)+#13#10+
            'High(3):  '+FloatToStr(High3)+#13#10+
            'Low(3):   '+FloatToStr(Low3)+#13#10+
            'Close(3): '+FloatToStr(Close3)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(4):  '+FloatToStr(Open4)+#13#10+
            'High(4):  '+FloatToStr(High4)+#13#10+
            'Low(4):   '+FloatToStr(Low4)+#13#10+
            'Close(4): '+FloatToStr(Close4)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(5):  '+FloatToStr(Open5)+#13#10+
            'High(5):  '+FloatToStr(High5)+#13#10+
            'Low(5):   '+FloatToStr(Low5)+#13#10+
            'Close(5): '+FloatToStr(Close5)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(6):  '+FloatToStr(Open6)+#13#10+
            'High(6):  '+FloatToStr(High6)+#13#10+
            'Low(6):   '+FloatToStr(Low6)+#13#10+
            'Close(6): '+FloatToStr(Close6)+#13#10+
            '-------------------------------------'+#13#10+
            'Open(7):  '+FloatToStr(Open7)+#13#10+
            'High(7):  '+FloatToStr(High7)+#13#10+
            'Low(7)    '+FloatToStr(Low7)+#13#10+
            'Close(7): '+FloatToStr(Close7)),PChar('Last prices'), MB_OK);
end;

{-----Get Open High Low Close-----------------------------------------------}
procedure OHLC; stdcall;
begin
  Open0:=iOpen(SymbolName,Timeframe,0);
  Open1:=iOpen(SymbolName,Timeframe,1);
  Open2:=iOpen(SymbolName,Timeframe,2);
  Open3:=iOpen(SymbolName,Timeframe,3);
  Open4:=iOpen(SymbolName,Timeframe,4);
  Open5:=iOpen(SymbolName,Timeframe,5);
  Open6:=iOpen(SymbolName,Timeframe,6);
  Open7:=iOpen(SymbolName,Timeframe,7);

  Close0:=iClose(SymbolName,Timeframe,0);
  Close1:=iClose(SymbolName,Timeframe,1);
  Close2:=iClose(SymbolName,Timeframe,2);
  Close3:=iClose(SymbolName,Timeframe,3);
  Close4:=iClose(SymbolName,Timeframe,4);
  Close5:=iClose(SymbolName,Timeframe,5);
  Close6:=iClose(SymbolName,Timeframe,6);
  Close7:=iClose(SymbolName,Timeframe,7);

  High0:=iHigh(SymbolName,Timeframe,0);
  High1:=iHigh(SymbolName,Timeframe,1);
  High2:=iHigh(SymbolName,Timeframe,2);
  High3:=iHigh(SymbolName,Timeframe,3);
  High4:=iHigh(SymbolName,Timeframe,4);
  High5:=iHigh(SymbolName,Timeframe,5);
  High6:=iHigh(SymbolName,Timeframe,6);
  High7:=iHigh(SymbolName,Timeframe,7);

  Low0:=iLow(SymbolName,Timeframe,0);
  Low1:=iLow(SymbolName,Timeframe,1);
  Low2:=iLow(SymbolName,Timeframe,2);
  Low3:=iLow(SymbolName,Timeframe,3);
  Low4:=iLow(SymbolName,Timeframe,4);
  Low5:=iLow(SymbolName,Timeframe,5);
  Low6:=iLow(SymbolName,Timeframe,6);
  Low7:=iLow(SymbolName,Timeframe,7);
 end;

{-----4 hr Timeframe Checker-------------------------------------------------}
procedure Hr4Frame; stdcall;
begin
  Hr4TimeFrame := false;
  if (Ti0 = 23) or (Ti0 = 3) or (Ti0 = 7) or (Ti0 = 11) or (Ti0 = 15) or (Ti0 = 19) then
    begin
      if (Ti1 = 59) and (Ti2 = 0) and (Ti3 <> Ti3last) then
        Begin
          Hr4TimeFrame := true;
        End;
    end;
end;

{-----8 hr Timeframe Checker-------------------------------------------------}
procedure Hr8Frame; stdcall;
begin
  Hr8TimeFrame := false;
  if (Ti0 = 23) or (Ti0 = 7) or (Ti0 = 15) then
    begin
      if (Ti1 = 59) and (Ti2 = 0) and (Ti3 <> Ti3last) then
        Begin
          Hr8TimeFrame := true;
        End;
    end;
end;

{-----Buy Condition Checker--------------------------------------------------}
procedure BuyConditionCheck; stdcall;
begin
  BuyCondition := false;

  Calc0 := (Open0 + Close0)/2;
  Calc1 := Close1/Open5;
  if (Calc1 > 1.005) and (Open0>Close0) and (Close1>Close2) and (Close2>Close3) and (Close3>Close4) and (Open1<Close1) and (Open2<Close2) and (Open3<Close3) then
    begin
        BuyCondition := true;
    end;
end;

{-----Sell Condition Checker------------------------------------------------}
procedure SellConditionCheck; stdcall;
Begin
  SellCondition := false;

  if Close0 > Open0 then
    Begin
      SellCondition := true;
    End;
End;

{-----Buy Executor---------------------------------------------------------}
procedure BuyExecutor; stdcall;
var
  Position,Multiplier:integer;
Begin

  Multiplier := 1;
  Position := AnsiPos('JPY', SymbolName);
  if Position <> 0 then
   begin
    Multiplier := 100;
   end;

  SendPendingOrder(SymbolName, op_SellStop, Lot, High0 + StopLoss*Point*Multiplier, Low0 - TakeProfit*Point*Multiplier, Low0 - Entry*Point*Multiplier, '', 0, OrdersSymbolTemp);

End;

{-----Sell Or Delete Order Checker------------------------------------------}
procedure SellOrDeleteOrderChecker; stdcall;
Begin
  if (Name1 = true) and (OrdersSymbol1 <> 0) then        //AUDJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName1;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol1);
              CloseOrder(OrdersSymbol1);
              OrdersSymbol1 := 0;
            End;
        end;
    end;

  if (Name2 = true) and (OrdersSymbol2 <> 0) then        //AUDUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName2;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol2);
              CloseOrder(OrdersSymbol2);
              OrdersSymbol2 := 0;
            End;
        end;
    end;

  if (Name3 = true) and (OrdersSymbol3 <> 0) then        //EURCAD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName3;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol3);
              CloseOrder(OrdersSymbol3);
              OrdersSymbol3 := 0;
            End;
        end;
    end;

  if (Name4 = true) and (OrdersSymbol4 <> 0) then        //EURJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName4;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol4);
              CloseOrder(OrdersSymbol4);
              OrdersSymbol4 := 0;
            End;
        end;
    end;

  if (Name5 = true) and (OrdersSymbol5 <> 0) then        //EURUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName5;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol5);
              CloseOrder(OrdersSymbol5);
              OrdersSymbol5 := 0;
            End;
        end;
    end;

  if (Name6 = true) and (OrdersSymbol6 <> 0) then        //GBPJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName6;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol6);
              CloseOrder(OrdersSymbol6);
              OrdersSymbol6 := 0;
            End;
        end;
    end;

  if (Name7 = true) and (OrdersSymbol7 <> 0) then        //GBPUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName7;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol7);
              CloseOrder(OrdersSymbol7);
              OrdersSymbol7 := 0;
            End;
        end;
    end;

  if (Name8 = true) and (OrdersSymbol8 <> 0) then        //USDCAD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName8;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol8);
              CloseOrder(OrdersSymbol8);
              OrdersSymbol8 := 0;
            End;
        end;
    end;

  if (Name9 = true) and (OrdersSymbol9 <> 0) then        //USDCHF
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName9;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol9);
              CloseOrder(OrdersSymbol9);
              OrdersSymbol9 := 0;
            End;
        end;
    end;

  if (Name10 = true) and (OrdersSymbol10 <> 0) then        //USDJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName10;
          OHLC;
          SellConditionCheck;
          if (SellCondition = true) then
            Begin
              DeleteOrder(OrdersSymbol10);
              CloseOrder(OrdersSymbol10);
              OrdersSymbol10 := 0;
            End;
        end;
    end;

end;

{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
begin

  if (Timeframe = 480) or (Timeframe = 240) then
    begin
    end
  else
    exit;

  Ti0:=HourOf(TimeCurrent);
  Ti1:=MinuteOf(TimeCurrent);
  Ti2:=SecondOf(TimeCurrent);
  Ti3:=MillisecondOf(TimeCurrent);

  SetCurrencyAndTimeFrame(SymbolName1, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName2, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName3, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName4, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName5, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName6, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName7, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName8, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName9, Timeframe);
  SetCurrencyAndTimeFrame(SymbolName10, Timeframe);

  SellOrDeleteOrderChecker;

  if (Name1 = true) and (OrdersSymbol1 = 0) then    //AUDJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName1;
          OrdersSymbolTemp := OrdersSymbol1;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol1 := OrdersSymbolTemp;
          end;
        end;
    end;

  if (Name2 = true) and (OrdersSymbol2 = 0) then     //AUDUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName2;
          OrdersSymbolTemp := OrdersSymbol2;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol2 := OrdersSymbolTemp;
          end;
        end;
    end;

  if (Name3 = true) and (OrdersSymbol3 = 0) then     //EURCAD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName3;
          OrdersSymbolTemp := OrdersSymbol3;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol3 := OrdersSymbolTemp;
          end;
        end;
    end;

   if (Name4 = true) and (OrdersSymbol4 = 0) then    //EURJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName4;
          OrdersSymbolTemp := OrdersSymbol4;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol4 := OrdersSymbolTemp;
          end;
        end;
    end;

  if (Name5 = true) and (OrdersSymbol5 = 0) then     //EURUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName5;
          OrdersSymbolTemp := OrdersSymbol5;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol5 := OrdersSymbolTemp;
          end;
        end;
    end;

   if (Name6 = true) and (OrdersSymbol6 = 0) then    //GBPJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName6;
          OrdersSymbolTemp := OrdersSymbol6;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol6 := OrdersSymbolTemp;
          end;
        end;
    end;

  if (Name7 = true) and (OrdersSymbol7 = 0) then     //GBPUSD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName7;
          OrdersSymbolTemp := OrdersSymbol7;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol7 := OrdersSymbolTemp;
          end;
        end;
    end;

  if (Name8 = true) and (OrdersSymbol8 = 0) then     //USDCAD
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName8;
          OrdersSymbolTemp := OrdersSymbol8;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol8 := OrdersSymbolTemp;
          end;
        end;
    end;

   if (Name9 = true) and (OrdersSymbol9 = 0) then     //USDCHF
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName9;
          OrdersSymbolTemp := OrdersSymbol9;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol9 := OrdersSymbolTemp;
          end;
        end;
    end;

   if (Name10 = true) and (OrdersSymbol10 = 0) then    //USDJPY
    Begin
      if (Timeframe = 240) then Hr4Frame;
      if (Timeframe = 480) then Hr8Frame;
      if (Hr4TimeFrame = true) or (Hr8TimeFrame = true) then
        begin
          SymbolName := SymbolName10;
          OrdersSymbolTemp := OrdersSymbol10;
          OHLC;
          BuyConditionCheck;
          if (BuyCondition = true) then
          begin
            BuyExecutor;
            OrdersSymbol10 := OrdersSymbolTemp;
          end;
        end;
    end;

    Ti3last := Ti3;
end;

exports

  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick;

begin


end.

Lonesome
Posts: 28
Joined: Fri Oct 21, 2016 9:04 am

Re: Example of how to run an expert advisor in several currency pairs simultaneously

#2 Postby Lonesome » Mon Apr 17, 2017 8:23 am

In my previous script (PriceAction2) I fixed buying and selling to the time when candle 0 is competed. That either in the 4 or 8 hr timeframes.
I came to this because I was working with candle 0 as my first candle to determine the price action. Because the close, high and low are always fluctuating in candle 0 I limited buying to the time when candle 0 was one tick before closing.
All that can be avoided when one uses candle 1 as the first candle to determine price action. The outcome is somewhat different but more realistic and the EA can be used in any timeframe.

Code: Select all

library PriceAction3;

//This EA works with 10 currency pairs. Each is optional.
//There are procedures for checking buy and sell conditions and for executing a buy order.
//The strategy can easily be changed by modifying just these 3 procedures.

uses
  SysUtils, StrategyInterfaceUnit, DateUtils, Windows;

var
  // external parameters
  SymbolName1: PAnsiChar = 'AUDJPY';
  SymbolName2: PAnsiChar = 'AUDUSD';
  SymbolName3: PAnsiChar = 'EURCAD';
  SymbolName4: PAnsiChar = 'EURJPY';
  SymbolName5: PAnsiChar = 'EURUSD';
  SymbolName6: PAnsiChar = 'GBPJPY';
  SymbolName7: PAnsiChar = 'GBPUSD';
  SymbolName8: PAnsiChar = 'USDCAD';
  SymbolName9: PAnsiChar = 'USDCHF';
  SymbolName10: PAnsiChar = 'USDJPY';
  SymbolName: PAnsiChar = nil;
  Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8,Name9,Name10: boolean;
  IncreaseMin:double;
  LastTime: TDateTime;
  Timeframe: integer;
  StopLoss: integer;
  TakeProfit: integer;
  Entry: integer;
  Lot: double = 1.0;

  // local variables
  Open1,Open2,Open3,Open4,Open5,Open6,Open7:double;
  Close1,Close2,Close3,Close4,Close5,Close6,Close7:double;
  High1,High2,High3,High4,High5,High6,High7:double;
  Low1,Low2,Low3,Low4,Low5,Low6,Low7:double;
  BuyCondition,SellCondition:boolean;
  OrdersSymbol1: integer;
  OrdersSymbol2: integer;
  OrdersSymbol3: integer;
  OrdersSymbol4: integer;
  OrdersSymbol5: integer;
  OrdersSymbol6: integer;
  OrdersSymbol7: integer;
  OrdersSymbol8: integer;
  OrdersSymbol9: integer;
  OrdersSymbol10: integer;
  OrdersSymbolTemp: integer;
  Calc0, Calc1:double;

{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
  // set strategy name and description
  StrategyShortName('Price Action Temp');
  StrategyDescription('Reversal on high (sell stop)');

  // register parameters
  AddSeparator('Currency pair options');
  RegOption('AUDJPY',ot_Boolean, Name1);
  RegOption('AUDUSD',ot_Boolean, Name2);
  RegOption('EURCAD',ot_Boolean, Name3);
  RegOption('EURJPY',ot_Boolean, Name4);
  RegOption('EURUSD',ot_Boolean, Name5);
  RegOption('GBPJPY',ot_Boolean, Name6);
  RegOption('GBPUSD',ot_Boolean, Name7);
  RegOption('USDCAD',ot_Boolean, Name8);
  RegOption('USDCHF',ot_Boolean, Name9);
  RegOption('USDJPY',ot_Boolean, Name10);
  AddSeparator('Trading options');
  RegOption('Timeframe', ot_TimeFrame, Timeframe);
  RegOption('Lot', Lot, 1, 0.1, 10);
  RegOption('Entry (pips below low)', Entry, 1, 100000);
  RegOption('Take Profit (pips below low)', TakeProfit, 1, 100000);
  RegOption('Stop Loss (pips above high)', StopLoss, 1, 100000);
  RegOption('Price increase (Close2/Close4)', ot_double, IncreaseMin);
  SetOptionDigits('IncreaseMin', 3);
  end;

{-----Done strategy---------------------------------------------------------}
procedure DoneStrategy; stdcall;
begin
  FreeMem(SymbolName1);
  FreeMem(SymbolName2);
  FreeMem(SymbolName3);
  FreeMem(SymbolName4);
  FreeMem(SymbolName5);
  FreeMem(SymbolName6);
  FreeMem(SymbolName7);
  FreeMem(SymbolName8);
  FreeMem(SymbolName9);
  FreeMem(SymbolName10);
  end;

{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
  LastTime := 0;    // reset options before new test
end;

{-----Get Open High Low Close-----------------------------------------------}
procedure OHLC; stdcall;
begin
  Open1:=iOpen(SymbolName,Timeframe,1);
  Open2:=iOpen(SymbolName,Timeframe,2);
  Open3:=iOpen(SymbolName,Timeframe,3);
  Open4:=iOpen(SymbolName,Timeframe,4);
  Open5:=iOpen(SymbolName,Timeframe,5);
  Open6:=iOpen(SymbolName,Timeframe,6);
  Open7:=iOpen(SymbolName,Timeframe,7);

  Close1:=iClose(SymbolName,Timeframe,1);
  Close2:=iClose(SymbolName,Timeframe,2);
  Close3:=iClose(SymbolName,Timeframe,3);
  Close4:=iClose(SymbolName,Timeframe,4);
  Close5:=iClose(SymbolName,Timeframe,5);
  Close6:=iClose(SymbolName,Timeframe,6);
  Close7:=iClose(SymbolName,Timeframe,7);

  High1:=iHigh(SymbolName,Timeframe,1);
  High2:=iHigh(SymbolName,Timeframe,2);
  High3:=iHigh(SymbolName,Timeframe,3);
  High4:=iHigh(SymbolName,Timeframe,4);
  High5:=iHigh(SymbolName,Timeframe,5);
  High6:=iHigh(SymbolName,Timeframe,6);
  High7:=iHigh(SymbolName,Timeframe,7);

  Low1:=iLow(SymbolName,Timeframe,1);
  Low2:=iLow(SymbolName,Timeframe,2);
  Low3:=iLow(SymbolName,Timeframe,3);
  Low4:=iLow(SymbolName,Timeframe,4);
  Low5:=iLow(SymbolName,Timeframe,5);
  Low6:=iLow(SymbolName,Timeframe,6);
  Low7:=iLow(SymbolName,Timeframe,7);
 end;

{-----Buy Condition Checker--------------------------------------------------}
procedure BuyConditionCheck; stdcall;
begin
  BuyCondition := false;

  Calc0 := (Open1 + Close1)/2;
  Calc1 := Close2/Open4;
  if (Calc1 > IncreaseMin) and (Open1>Close1) and (Close2>Close3) and (Close3>Close4) and (Close4>Close5) and (Open2<Close2) and (Open3<Close3) and (Open4<Close4) then
    begin
        BuyCondition := true;
    end;
end;

{-----Sell Condition Checker------------------------------------------------}
procedure SellConditionCheck; stdcall;
Begin
  SellCondition := false;

  if Close1 > Open1 then
    Begin
      SellCondition := true;
    End;

End;

{-----Buy Executor---------------------------------------------------------}
procedure BuyExecutor; stdcall;
var
  Position,Multiplier:integer;
Begin

  Multiplier := 1;
  Position := AnsiPos('JPY', SymbolName);
  if Position <> 0 then
   begin
    Multiplier := 100;
   end;

  SendPendingOrder(SymbolName, op_SellStop, Lot, High1 + StopLoss*Point*Multiplier, Low1 - TakeProfit*Point*Multiplier, Low1 - Entry*Point*Multiplier, '', 0, OrdersSymbolTemp);

End;

{-----Sell Or Delete Order Checker------------------------------------------}
procedure SellOrDeleteOrderChecker; stdcall;
Begin
  if (Name1 = true) and (OrderClosed(OrdersSymbol1) = false) then        //AUDJPY
    Begin
      SymbolName := SymbolName1;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol1);
          CloseOrder(OrdersSymbol1);
        End;
    end;

  if (Name2 = true) and (OrderClosed(OrdersSymbol2) = false) then        //AUDUSD
    Begin
      SymbolName := SymbolName2;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol2);
          CloseOrder(OrdersSymbol2);
        End;
    end;

  if (Name3 = true) and (OrderClosed(OrdersSymbol3) = false) then        //EURCAD
    Begin
      SymbolName := SymbolName3;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol3);
          CloseOrder(OrdersSymbol3);
        End;
    end;

  if (Name4 = true) and (OrderClosed(OrdersSymbol4) = false) then        //EURJPY
    Begin
      SymbolName := SymbolName4;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol4);
          CloseOrder(OrdersSymbol4);
        End;
    end;

  if (Name5 = true) and (OrderClosed(OrdersSymbol5) = false) then        //EURUSD
    Begin
      SymbolName := SymbolName5;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol5);
          CloseOrder(OrdersSymbol5);
        End;
    end;

  if (Name6 = true) and (OrderClosed(OrdersSymbol6) = false) then        //GBPJPY
    Begin
      SymbolName := SymbolName6;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol6);
          CloseOrder(OrdersSymbol6);
        End;
    end;

  if (Name7 = true) and (OrderClosed(OrdersSymbol7) = false) then        //GBPUSD
    Begin
      SymbolName := SymbolName7;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol7);
          CloseOrder(OrdersSymbol7);
        End;
    end;

  if (Name8 = true) and (OrderClosed(OrdersSymbol8) = false) then        //USDCAD
    Begin
      SymbolName := SymbolName8;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol8);
          CloseOrder(OrdersSymbol8);
        End;
    end;

  if (Name9 = true) and (OrderClosed(OrdersSymbol9) = false) then        //USDCHF
    Begin
      SymbolName := SymbolName9;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol9);
          CloseOrder(OrdersSymbol9);
        End;
    end;

  if (Name10 = true) and (OrderClosed(OrdersSymbol10) = false) then        //USDJPY
    Begin
      SymbolName := SymbolName10;
      OHLC;
      SellConditionCheck;
      if (SellCondition = true) then
        Begin
          DeleteOrder(OrdersSymbol10);
          CloseOrder(OrdersSymbol10);
        End;
    end;

end;

{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
begin

  if (Name1 = true) then SetCurrencyAndTimeFrame(SymbolName1, Timeframe);
  if (Name2 = true) then SetCurrencyAndTimeFrame(SymbolName2, Timeframe);
  if (Name3 = true) then SetCurrencyAndTimeFrame(SymbolName3, Timeframe);
  if (Name4 = true) then SetCurrencyAndTimeFrame(SymbolName4, Timeframe);
  if (Name5 = true) then SetCurrencyAndTimeFrame(SymbolName5, Timeframe);
  if (Name6 = true) then SetCurrencyAndTimeFrame(SymbolName6, Timeframe);
  if (Name7 = true) then SetCurrencyAndTimeFrame(SymbolName7, Timeframe);
  if (Name8 = true) then SetCurrencyAndTimeFrame(SymbolName8, Timeframe);
  if (Name9 = true) then SetCurrencyAndTimeFrame(SymbolName9, Timeframe);
  if (Name10 = true) then SetCurrencyAndTimeFrame(SymbolName10, Timeframe);

  SellOrDeleteOrderChecker;

  if (Name1 = true) and (OrderClosed(OrdersSymbol1) = true) then    //AUDJPY
    Begin
      SymbolName := SymbolName1;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol1 := OrdersSymbolTemp;
      end;
    end;

  if (Name2 = true) and (OrderClosed(OrdersSymbol2) = true) then     //AUDUSD
    Begin
      SymbolName := SymbolName2;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol2 := OrdersSymbolTemp;
      end;
    end;

  if (Name3 = true) and (OrderClosed(OrdersSymbol3) = true) then     //EURCAD
    Begin
      SymbolName := SymbolName3;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol3 := OrdersSymbolTemp;
      end;
    end;

   if (Name4 = true) and (OrderClosed(OrdersSymbol4) = true) then    //EURJPY
    Begin
      SymbolName := SymbolName4;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol4 := OrdersSymbolTemp;
      end;
    end;

  if (Name5 = true) and (OrderClosed(OrdersSymbol5) = true) then     //EURUSD
    Begin
      SymbolName := SymbolName5;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol5 := OrdersSymbolTemp;
      end;
    end;

   if (Name6 = true) and (OrderClosed(OrdersSymbol6) = true) then    //GBPJPY
    Begin
      SymbolName := SymbolName6;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol6 := OrdersSymbolTemp;
      end;
    end;

  if (Name7 = true) and (OrderClosed(OrdersSymbol7) = true) then     //GBPUSD
    Begin
      SymbolName := SymbolName7;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol7 := OrdersSymbolTemp;
      end;
    end;

  if (Name8 = true) and (OrderClosed(OrdersSymbol8) = true) then     //USDCAD
    Begin
      SymbolName := SymbolName8;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol8 := OrdersSymbolTemp;
      end;
    end;

   if (Name9 = true) and (OrderClosed(OrdersSymbol9) = true) then     //USDCHF
    Begin
      SymbolName := SymbolName9;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol9 := OrdersSymbolTemp;
      end;
    end;

   if (Name10 = true) and (OrderClosed(OrdersSymbol10) = true) then    //USDJPY
    Begin
      SymbolName := SymbolName10;
      OHLC;
      BuyConditionCheck;
      if (BuyCondition = true) then
      begin
        BuyExecutor;
        OrdersSymbol10 := OrdersSymbolTemp;
      end;
    end;
end;

exports

  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick;

begin


end.


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 1 guest