Close_Basket_Profit_Loss

Download script
Author: Ron Thompson
Close_Basket_Profit_Loss

This automated trading script, known as FrameTrader, is designed to act as a high-level portfolio manager for your trading account. Rather than focusing on specific entry signals or complex technical indicators to decide when to buy or sell, the script monitors the overall health of your active positions across all currency pairs simultaneously.

Core Logic Overview

The primary objective of this tool is "Basket Management." It treats all your currently open trades as a single collective group (a "basket"). Instead of looking at the profit or loss of individual trades in isolation, it evaluates the total floating profit or loss of your entire account equity relative to your balance.

How It Works

The script operates based on two primary user-defined thresholds: a Profit Target and a Loss Limit.

  • Continuous Monitoring: The script constantly scans your account in real-time. It calculates the difference between your Account Equity (the current value of your account including open trades) and your Account Balance (your actual資金).
  • Profit Taking: If the total profit of all your open positions reaches a pre-defined positive goal (the "BasketProfit"), the script automatically identifies every open buy and sell order and closes them instantly to lock in the gains.
  • Loss Mitigation: Conversely, if the collective loss of your open positions reaches a pre-defined negative limit (the "BasketLoss"), the script will automatically close all positions to prevent the account from experiencing further significant drawdown.

Key Features

  • Universal Application: The script is designed to work across all currency pairs on your platform, making it a global safety net for your portfolio.
  • Automated Execution: By handling the closing of trades automatically, it removes the emotional element of "waiting" for a trade to hit a goal or "hesitating" to cut a loss.
  • Dynamic Feedback: The tool provides a real-time visual update on your trading screen, displaying your current total floating profit or loss so you can monitor your progress at a glance.

In essence, FrameTrader serves as an automated "circuit breaker." It ensures that your trading results stay within the boundaries you have defined, automatically securing your profits and protecting your capital when specific global account milestones are hit.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
9 Views
0 Downloads
0 Favorites
Close_Basket_Profit_Loss
/*
+------------+
|FrameTrader |
+------------+

Theory of operation


TIME FRAME
==========
Close at equity level with a profit :)


PAIRS
=====
ALL

ENTRY LONG
==========


ENTRY SHORT
===========


EXIT
====


MONEY MANAGEMENT
================
-none-


RISK MANAGEMENT
===============
-none-


FAILURE MANAGEMENT
==================
-none-


VERSION HISTORY
===============
00    - initial concept

*/


#property copyright "Ron Thompson"
#property link      "http://www.lightpatch.com/forex"

// user input
extern double BasketLoss=50;
extern double BasketProfit=50;
extern double Slippage=3;

//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled

int init()
  {
   Print("Init happened ",CurTime());
   Comment(" ");
  }


//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart

int deinit()
  {
   Print("DE-Init happened ",CurTime());
   Comment(" ");
  }


//+-----------+
//| Main      |
//+-----------+
// Called EACH TICK and each Bar[]

int start()
  {
   
   int      cnt=0;
   int      gle=0;
   int      OrdersOpenCount=0;

   string mySymbol;
   double myAsk;
   double myBid;   

   double   currBasket=0;

   //Basket profit or loss
   currBasket=AccountEquity()-AccountBalance();
   Comment("Profit/Loss = ",currBasket);

   if(currBasket>=BasketProfit || currBasket<=(BasketLoss*(-1)) )
     {
      // CLOSE order if profit target made
      for(cnt=OrdersTotal(); cnt>=0; cnt--)
        {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if( OrderType()==OP_BUY || OrderType()==OP_SELL )
           {
            mySymbol=OrderSymbol();
 
            if(OrderType()==OP_BUY)
              {
               myBid=MarketInfo(mySymbol,MODE_BID);            
               OrderClose(OrderTicket(),OrderLots(),myBid,Slippage,White);
               gle=GetLastError();
               if(gle==0)
                 {
                  Print("CLOSE BUY  Bid=",myBid); 
                 }
                  else 
                 {
                  Print("-----ERROR----- CLOSE BUY  Bid=",myBid," error=",gle);
                 }
              } // if BUY


            if(OrderType()==OP_SELL)
              {
               myAsk=MarketInfo(mySymbol,MODE_ASK);            
               OrderClose(OrderTicket(),OrderLots(),myAsk,Slippage,Red);
               gle=GetLastError();
               if(gle==0)
                 {
                  Print("CLOSE SELL Ask=",myAsk);
                 }
                  else 
                 {
                  Print("-----ERROR----- CLOSE SELL Ask=",myAsk," error=",gle);
                 }
              } //if SELL
           
           } // if(OrderSymbol)
        
        } // for

     } //currBasket

  } // start()




Comments