Author: Copyright � 2007, Yury V. Reshetov
Profit factor:
1.51

This script is designed to manage trading positions by balancing the portfolio's "fair value" against current market conditions. It functions as an automated management system that monitors your open trades, calculates your financial standing, and attempts to keep your account balanced between buy and sell positions.

Core Logic Overview

1. Financial Tracking and Portfolio Balancing The script begins by scanning your trading history and current open positions. It calculates a "money" value by looking at previous closed trades (profit or loss) and combining that with the value of your currently active trades. It specifically tracks the total volume of your buys versus your sells to determine your current exposure.

2. Identifying "Fair Value" The system identifies what it considers the "fair value" of your portfolio. It does this by comparing your current financial standing against the total volume of your positions. This calculation helps the script understand if you are currently over-exposed in one direction (too many buys or too many sells) relative to your available capital and previous performance.

3. Automated Trade Adjustment Based on the "fair value" calculation, the script makes real-time decisions to balance the account:

  • Closing Positions: If the script detects that a position is contributing to an imbalance, it will attempt to close part of that position (either a buy or a sell) to bring the account back toward its target state.
  • Opening New Positions: If the account is imbalanced but lacks an opposing position to offset the risk, the script can automatically open new trades in the opposite direction.

4. Safety and Margin Management Before opening any new trades, the script includes a safety check. It calculates how much "free margin" (available cash) you have left. If the capital required for a trade exceeds your available funds, it will automatically adjust the trade size down to the maximum amount your account can safely handle at that moment.

5. Synchronization and Timing To prevent the script from overwhelming the trading server with constant commands, it operates on a timed cycle. It checks the market once per candle (time bar) and incorporates short "sleep" periods between actions. This ensures the execution is orderly and reduces the risk of conflicts during rapid price movements.

Summary of Behavior

Instead of just entering trades based on simple signals, this script acts as a portfolio balancer. It constantly asks: "Is my current balance of buys and sells balanced against my history and available money?" If the answer is no, it automatically trims oversized positions or adds offsetting positions to maintain equilibrium.

Orders Execution
Checks for the total of closed ordersChecks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reached
12 Views
0 Downloads
0 Favorites
Swaper_1.1
//+------------------------------------------------------------------+
//|                                                   Swaper_1.1.mq4 |
//|                               Copyright © 2007, Yury V. Reshetov |
//|                                          http://reshetov.xnet.uz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Yury V. Reshetov"
#property link      "http://reshetov.xnet.uz"
//---- input parameters
extern double experts = 1;
extern double beginPrice = 1.8014;
extern int    magicnumber = 777;
static int    prevtime = 0;
static double bl = 1000;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   if(Time[0] == prevtime) 
       return(0);
   prevtime = Time[0];
   if(!IsTradeAllowed()) 
     {
       prevtime = Time[1];
       return(0);
     }
//----
   int total = OrdersHistoryTotal();
   double money = bl * beginPrice;
   int i = 0;
   for (i = 0; i < total; i++) 
     {
       OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
       if(OrderSymbol() == Symbol() && OrderMagicNumber() == magicnumber) 
         {
           if(OrderType() == OP_BUY) 
             {
               money = money + (OrderClosePrice() - 
                       OrderOpenPrice()) * 10 * OrderLots();
             } 
           else 
             {
               money = money - (OrderClosePrice() - 
                       OrderOpenPrice()) * 10 * OrderLots();
             }
         }
     }
   total = OrdersTotal();   
   double com =  bl;
   int tickbuy = -1;
   double buyvolume = 0;
   int ticksell = -1;
   double sellvolume = 0;
   for(i = 0; i < total; i++) 
     {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber() == magicnumber) 
         {
           if(OrderType() == OP_BUY) 
             {
               if(OrderSymbol() == Symbol()) 
                 {
                   money = money - 10 * buyvolume * OrderOpenPrice();
                   if ((MarketInfo(Symbol(), MODE_SWAPLONG) < 0) || (AccountFreeMargin() < 0 && OrderProfit() < 0)) {
                     buyvolume = OrderLots();
                     tickbuy = OrderTicket();
                   } 
                 }
               com = com + 10 * OrderLots(); 
             } 
           else 
             {
               if(OrderSymbol() == Symbol()) 
                 {
                   money = money + 10 * sellvolume * OrderOpenPrice();
                   if ((MarketInfo(Symbol(), MODE_SWAPSHORT) < 0) || (AccountFreeMargin() < 0 && OrderProfit() < 0)) {
                     sellvolume = OrderLots();
                     ticksell = OrderTicket();
                   }
                 }
               com = com - 10 * OrderLots(); 
             }
         }
     }
      
   double fv = money / com;
   Comment("Fair value ", fv);
   
   if(! IsTradeAllowed()) 
     {
       prevtime = Time[1];
       return(0);
     }
   closeby(ticksell, tickbuy);
   if(!IsTradeAllowed()) 
     {
       prevtime = Time[1];
       return(0);
     }
   double lots = 0;
   double price = 0;
   double dt = (money / (MathMax(High[1], High[0]) + MarketInfo(Symbol(), MODE_SPREAD) * Point) - com) * experts / (experts + 1);
   if(dt < 0) 
     {
       dt = (com - money / MathMin(Low[1], Low[0])) * experts / (experts + 1);
       if(dt < 1) 
         {
           closeby(tickbuy, ticksell);
           return(0);
         }
       lots = MathFloor(dt) / 10;
       if(tickbuy >= 0) 
         {
           if(buyvolume > lots) 
             {
               OrderClose(tickbuy, lots, Bid, 3, Blue);
               Sleep(30000);
             } 
           else 
             {
               OrderClose(tickbuy, buyvolume, Bid, 3, Blue);
               tickbuy = -1;
               Sleep(30000);
             }
         } 
       else 
         {
           lots = getLots(lots);
           if(lots > 0) 
             {
               ticksell = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, 
                          0, 0, "Swaper", magicnumber, 0, Red);
               Sleep(30000);
             }
         }
     } 
   else 
     {
       if(dt < 1) 
         {
           closeby(ticksell, tickbuy);
           return(0);
         }
       lots = MathFloor(dt) / 10;
       if(ticksell >= 0) 
         {
           if(sellvolume > lots) 
             {
               OrderClose(ticksell, lots, Ask, 3, Red);
               Sleep(30000);
             } 
           else 
             {
               OrderClose(ticksell, sellvolume, Ask, 3, Red);
               ticksell = -1;
               Sleep(30000);
             }
         } 
       else 
         {
           lots = getLots(lots);
           if(lots > 0) 
             {
               tickbuy = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 
                         0, "Swaper",  magicnumber, 0, Blue);
               Sleep(30000);
             }
         }
     }
//----
   if(!IsTradeAllowed()) 
     {
       prevtime = Time[1];
       return(0);
     }
   closeby(tickbuy, ticksell);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void closeby(int sell, int buy) 
  {
   if(sell >= 0 && buy >= 0) 
     {
       OrderCloseBy(buy, sell, Green);
       Sleep(30000);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getLots(double lt) 
  {
   double marginrequired = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double freemargin = AccountFreeMargin();
   if(freemargin > (marginrequired * lt)) 
     {
       return(lt);
     } 
   double result = freemargin / marginrequired;
   result = MathFloor(result * 10) / 10;
   return(result);
  }
//+------------------------------------------------------------------+

Profitability Reports

PERFORMANCE BY SYMBOL

Expected payoff

Average expected payoff across the available backtests for each symbol.

Positive Negative
30 reports
AUD/USD Jan 2025 - Jul 2025
18.27
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff 6096.90
Gross Profit 12900.00
Gross Loss -706.20
Total Net Profit 12193.80
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
10.57
Total Trades 5
Won Trades 3
Lost trades 2
Win Rate 60.00 %
Expected payoff 241.64
Gross Profit 1334.40
Gross Loss -126.20
Total Net Profit 1208.20
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
7.03
Total Trades 176
Won Trades 72
Lost trades 104
Win Rate 40.91 %
Expected payoff 1022.08
Gross Profit 209723.26
Gross Loss -29836.80
Total Net Profit 179886.46
-100%
-50%
0%
50%
100%
EUR/USD Oct 2025 - Feb 2026
5.92
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff 166.45
Gross Profit 400.50
Gross Loss -67.60
Total Net Profit 332.90
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
1.69
Total Trades 98
Won Trades 54
Lost trades 44
Win Rate 55.10 %
Expected payoff 33.67
Gross Profit 8070.82
Gross Loss -4770.77
Total Net Profit 3300.05
-100%
-50%
0%
50%
100%
GBP/CAD Jan 2025 - Jul 2025
1.09
Total Trades 300
Won Trades 136
Lost trades 164
Win Rate 45.33 %
Expected payoff 15.33
Gross Profit 54034.05
Gross Loss -49436.39
Total Net Profit 4597.66
-100%
-50%
0%
50%
100%
GBP/AUD Jan 2025 - Jul 2025
0.72
Total Trades 88
Won Trades 38
Lost trades 50
Win Rate 43.18 %
Expected payoff -51.06
Gross Profit 11700.64
Gross Loss -16193.48
Total Net Profit -4492.84
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
0.04
Total Trades 15
Won Trades 2
Lost trades 13
Win Rate 13.33 %
Expected payoff -247.44
Gross Profit 136.49
Gross Loss -3848.10
Total Net Profit -3711.61
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.00
Total Trades 2
Won Trades 0
Lost trades 2
Win Rate 0.00 %
Expected payoff -489.90
Gross Profit 0.00
Gross Loss -979.80
Total Net Profit -979.80
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -3140.50
Gross Profit 0.00
Gross Loss -3140.50
Total Net Profit -3140.50
-100%
-50%
0%
50%
100%
EUR/USD Jan 2025 - Jul 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -399.10
Gross Profit 0.00
Gross Loss -399.10
Total Net Profit -399.10
-100%
-50%
0%
50%
100%
NZD/USD Jan 2025 - Jul 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -1027.80
Gross Profit 0.00
Gross Loss -1027.80
Total Net Profit -1027.80
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -353.86
Gross Profit 0.00
Gross Loss -353.86
Total Net Profit -353.86
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -382.17
Gross Profit 0.00
Gross Loss -382.17
Total Net Profit -382.17
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -750.00
Gross Profit 0.00
Gross Loss -750.00
Total Net Profit -750.00
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -1452.00
Gross Profit 0.00
Gross Loss -1452.00
Total Net Profit -1452.00
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.00
Total Trades 5
Won Trades 0
Lost trades 5
Win Rate 0.00 %
Expected payoff -1011.32
Gross Profit 0.00
Gross Loss -5056.59
Total Net Profit -5056.59
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -8336.60
Gross Profit 0.00
Gross Loss -8336.60
Total Net Profit -8336.60
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.00
Total Trades 9
Won Trades 0
Lost trades 9
Win Rate 0.00 %
Expected payoff -217.83
Gross Profit 0.00
Gross Loss -1960.46
Total Net Profit -1960.46
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -479.22
Gross Profit 0.00
Gross Loss -479.22
Total Net Profit -479.22
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -444.49
Gross Profit 0.00
Gross Loss -444.49
Total Net Profit -444.49
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -310.80
Gross Profit 0.00
Gross Loss -310.80
Total Net Profit -310.80
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -539.60
Gross Profit 0.00
Gross Loss -539.60
Total Net Profit -539.60
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.00
Total Trades 3
Won Trades 0
Lost trades 3
Win Rate 0.00 %
Expected payoff -333.66
Gross Profit 0.00
Gross Loss -1000.97
Total Net Profit -1000.97
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -12.50
Gross Profit 0.00
Gross Loss -12.50
Total Net Profit -12.50
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -1775.00
Gross Profit 0.00
Gross Loss -1775.00
Total Net Profit -1775.00
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.00
Total Trades 9
Won Trades 0
Lost trades 9
Win Rate 0.00 %
Expected payoff -218.82
Gross Profit 0.00
Gross Loss -1969.42
Total Net Profit -1969.42
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -479.24
Gross Profit 0.00
Gross Loss -479.24
Total Net Profit -479.24
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -361.15
Gross Profit 0.00
Gross Loss -361.15
Total Net Profit -361.15
-100%
-50%
0%
50%
100%
GBP/AUD Oct 2025 - Feb 2026
0.00
Total Trades 9
Won Trades 0
Lost trades 9
Win Rate 0.00 %
Expected payoff -637.88
Gross Profit 0.00
Gross Loss -5740.88
Total Net Profit -5740.88
-100%
-50%
0%
50%
100%

Comments