Hans123Trader_v1

Author: hans123
Profit factor:
0.81
Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
8 Views
0 Downloads
0 Favorites
Hans123Trader_v1
//+------------------------------------------------------------------+
//|                                                Hans123Trader v1  |
//+------------------------------------------------------------------+
#include <stdlib.mqh>

#property copyright   "hans123"
#property link        "http://www.strategybuilderfx.com/forums/showthread.php?t=15439"
// programmed by fukinagashi

extern int BeginSession1=6;
extern int EndSession1=10;
extern int BeginSession2=10;
extern int EndSession2=14;

extern double TrailingStop = 0;
extern double TakeProfit = 0;
extern double InitialStopLoss=40;

double Lots = 0.1;
datetime bartime = 0;
double Slippage=3;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
   {
   int cnt, ticket, err, i, j, cmd;
   int MagicNumber;
   double ts, tp, LowestPrice, HighestPrice, Price;
   bool Order[5];
   string setup;
   datetime Validity=0;

   
   if(IsTesting() && Bars<100) return(0);  
   
	MagicNumber = 50000 + func_Symbol2Val(Symbol())*100; 

   setup="H123_" + Symbol();

   if (bartime == Time[0]) {
      return(0);
   } else {
      bartime = Time[0]; 
   }



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// MODIFICATIONS ON OPEN ORDERS   ////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   for(cnt=OrdersTotal();cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()==OP_BUY && (OrderMagicNumber()==MagicNumber+1 || OrderMagicNumber()==MagicNumber+3)) {
      	if(TimeDay(OrderOpenTime())!=TimeDay(Time[0])) {
      		Print(".");
      		OrderClose(OrderTicket(), Lots, Bid, 3, Red);
      		if (err>1) { Print("Error closing buy order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
			} else if (OrderStopLoss()==0) {
				if (TakeProfit>0) {  tp=OrderOpenPrice()+TakeProfit*Point;
				} else { 				tp=0; }
				if (InitialStopLoss>0) { 	ts=OrderOpenPrice()-InitialStopLoss*Point;
				} else { 						ts=0; }
				OrderModify(OrderTicket(),OrderOpenPrice(),ts,tp,0,White);
				if (err>1) { Print("Error modifying Buy order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
			} else if (TrailingStop>0) {	
				ts = Bid-Point*TrailingStop;
				if (OrderStopLoss()<ts && OrderProfit()>0) OrderModify(OrderTicket(),OrderOpenPrice(),ts,OrderTakeProfit(),0,White);
			}
      } else if(OrderType()==OP_SELL && (OrderMagicNumber()==MagicNumber+2 || OrderMagicNumber()==MagicNumber+4)) {
      	if(TimeDay(OrderOpenTime())!=TimeDay(Time[0])) {
      		Print(".");
      		OrderClose(OrderTicket(), Lots, Ask, 3, Red);
      		if (err>1) { Print("Error closing Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
			} else if (OrderStopLoss()==0) {
				if (TakeProfit>0) {  tp=OrderOpenPrice()-TakeProfit*Point;
				} else { 				tp=0; }
				if (InitialStopLoss>0) { 	ts=OrderOpenPrice()+InitialStopLoss*Point;
				} else { 						ts=0; }
				OrderModify(OrderTicket(),OrderOpenPrice(),ts,tp,0,White);
				if (err>1) { Print("Error modifying Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
			} else if (TrailingStop>0) {	
				ts = Ask+Point*TrailingStop;
				if (OrderStopLoss()>ts && OrderProfit()>0) OrderModify(OrderTicket(),OrderOpenPrice(),ts,OrderTakeProfit(),0,White);
			}
		}
	}
				

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// SETTING ORDERS                 ////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   if(AccountFreeMargin()<(1000*Lots)) return(0);  
   
	Validity=StrToTime(TimeYear(Time[0]) + "." + TimeMonth(Time[0]) + "." + TimeDay(Time[0]) + " 23:59");
	
	if (TimeHour(Time[0])==EndSession1 && TimeMinute(Time[0])==0) {
		
		LowestPrice=Low[Lowest(NULL, PERIOD_M5, MODE_LOW, 80, 0)];
		HighestPrice=High[Highest(NULL, PERIOD_M5, MODE_HIGH, 80, 0)];
		
		//// the following is necessary, to avoid a BUYSTOP/SELLSTOP Price which is too close to Bid/Ask, 
		//// in which case we get a 130 invalid stops. 
		//// I experimented to change to proper OP_BUY and OP_SELL, but the results where not satisfying
		
		//if (HighestPrice+5*Point<Ask+Spread*Point) {
		//	cmd=OP_BUY;
		//	Price=Ask;
		//} else {
			cmd=OP_BUYSTOP;
			Price=HighestPrice+5*Point;
		//}

		ticket=OrderSendExtended(Symbol(),cmd,Lots,Price,Slippage,0,0,setup,MagicNumber+1,Validity,Green);
      	   
		err = GetLastError();
		if (err>1) { Print("Error modifying Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
		
		//if (LowestPrice-5*Point>Bid-Spread*Point) {
		//	cmd=OP_SELL;
		//	Price=Bid;
		//} else {
			cmd=OP_SELLSTOP;
			Price=LowestPrice-5*Point;
		//}

		ticket=OrderSendExtended(Symbol(),OP_SELLSTOP,Lots,Price,Slippage,0,0,setup,MagicNumber+2,Validity,Green); 
		err = GetLastError();
		if (err>1) { Print("Error modifying Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
	}
	
	if (TimeHour(Time[0])==EndSession2 && TimeMinute(Time[0])==0) {

		LowestPrice=Low[Lowest(NULL, PERIOD_M5, MODE_LOW, 80, 0)];
		HighestPrice=High[Highest(NULL, PERIOD_M5, MODE_HIGH, 80, 0)];

		//if (HighestPrice+5*Point<Ask+Spread*Point) {
		//	cmd=OP_BUY;
		//	Price=Ask;
		//} else {
			cmd=OP_BUYSTOP;
			Price=HighestPrice+5*Point;
		//}

		ticket=OrderSendExtended(Symbol(),cmd,Lots,Price,Slippage,0,0,setup,MagicNumber+3,Validity,Green); 
		err = GetLastError();
		if (err>1) { Print("Error modifying Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }

		//if (LowestPrice-5*Point>Bid-Spread*Point) {
		//	cmd=OP_SELL;
		//	Price=Bid;
		//} else {
			cmd=OP_SELLSTOP;
			Price=LowestPrice-5*Point;
		//}
					
		ticket=OrderSendExtended(Symbol(),cmd,Lots,Price,Slippage,0,0,setup,MagicNumber+4,Validity,Green);
		err = GetLastError();
		if (err>1) { Print("Error modifying Sell order [" + setup + "]: (" + err + ") " + ErrorDescription(err)); }
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// DIVERSE SUBROUTINES   /////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int func_Symbol2Val(string symbol) {
	if(symbol=="AUDUSD") {	return(01);

	} else if(symbol=="CHFJPY") {	return(10);

	} else if(symbol=="EURAUD") {	return(10);
	} else if(symbol=="EURCAD") {	return(11);
	} else if(symbol=="EURCHF") {	return(12);
	} else if(symbol=="EURGBP") {	return(13);
	} else if(symbol=="EURJPY") {	return(14);
	} else if(symbol=="EURUSD") {	return(15);

	} else if(symbol=="GBPCHF") {	return(20);
	} else if(symbol=="GBPJPY") {	return(21);
	} else if(symbol=="GBPUSD") { return(22);


	} else if(symbol=="USDCAD") {	return(40);
	} else if(symbol=="USDCHF") {	return(41);
	} else if(symbol=="USDJPY") {	return(42);


	} else if(symbol=="GOLD") {	return(90);
	} else {	Comment("unexpected Symbol"); return(0);
	}
}

int OrderSendExtended(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic, datetime expiration=0, color arrow_color=CLR_NONE) {
   datetime OldCurTime;
   int timeout=30;
   int ticket;

   OldCurTime=CurTime();
   while (GlobalVariableCheck("InTrade") && !IsTradeAllowed()) {
      if(OldCurTime+timeout<=CurTime()) {
         Print("Error in OrderSendExtended(): Timeout encountered");
         return(0); 
      }
      Sleep(1000);
   }
     
   GlobalVariableSet("InTrade", CurTime());  // set lock indicator
   ticket = OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);
   GlobalVariableDel("InTrade");   // clear lock indicator
   return(ticket);
}

Profitability Reports

USD/CAD Jul 2025 - Sep 2025
0.61
Total Trades 103
Won Trades 11
Lost trades 92
Win Rate 10.68 %
Expected payoff -1.90
Gross Profit 301.12
Gross Loss -496.48
Total Net Profit -195.36
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
1.44
Total Trades 40
Won Trades 8
Lost trades 32
Win Rate 20.00 %
Expected payoff 1.45
Gross Profit 189.70
Gross Loss -131.90
Total Net Profit 57.80
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
1.43
Total Trades 104
Won Trades 18
Lost trades 86
Win Rate 17.31 %
Expected payoff 2.75
Gross Profit 954.90
Gross Loss -668.40
Total Net Profit 286.50
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.22
Total Trades 57
Won Trades 8
Lost trades 49
Win Rate 14.04 %
Expected payoff -9.39
Gross Profit 148.31
Gross Loss -683.57
Total Net Profit -535.26
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.82
Total Trades 53
Won Trades 9
Lost trades 44
Win Rate 16.98 %
Expected payoff -0.86
Gross Profit 206.51
Gross Loss -252.13
Total Net Profit -45.62
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
1.58
Total Trades 56
Won Trades 16
Lost trades 40
Win Rate 28.57 %
Expected payoff 4.12
Gross Profit 630.10
Gross Loss -399.60
Total Net Profit 230.50
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
0.54
Total Trades 56
Won Trades 11
Lost trades 45
Win Rate 19.64 %
Expected payoff -2.73
Gross Profit 176.28
Gross Loss -329.40
Total Net Profit -153.12
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.23
Total Trades 55
Won Trades 6
Lost trades 49
Win Rate 10.91 %
Expected payoff -4.98
Gross Profit 81.50
Gross Loss -355.60
Total Net Profit -274.10
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.79
Total Trades 68
Won Trades 8
Lost trades 60
Win Rate 11.76 %
Expected payoff -1.85
Gross Profit 485.20
Gross Loss -611.00
Total Net Profit -125.80
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
0.40
Total Trades 54
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -3.28
Gross Profit 120.30
Gross Loss -297.40
Total Net Profit -177.10
-100%
-50%
0%
50%
100%

Comments