Binario_3
Download scriptTrading Script Logic Overview
This document provides a non-technical explanation of the logic used by the provided trading script. The software is designed to automate trade management and execution based on moving average trends and risk management parameters.
1. Initial Requirements & Safety Checks
Before the script executes any trading actions, it performs three preliminary checks:
- Data History: It ensures there are at least 100 bars of historical price data available.
- Profit Targets: It verifies that the user-defined "Take Profit" value is set to a logical minimum (greater than 10).
- Balance Check: Before opening new trades, it calculates if there is sufficient available margin in the account to support the intended position size.
2. Core Indicators and Price Calculations
The script relies on two primary technical indicators:
- Moving Averages (MA): It calculates a 144-period Exponential Moving Average (EMA) based on both the High prices and the Low prices of the candles. These serve as the "ceiling" and "floor" for the current price range.
- Price Dynamics: The script continuously monitors the current market spread (the difference between the Buy and Sell prices) to ensure that entry prices are calculated accurately relative to real-time market conditions.
3. Trade Entry Logic (Pending Orders)
The script identifies a "trading zone" when the current market price is currently trading between the High Moving Average and the Low Moving Average. When this condition is met:
- Buy Stop Orders: It places a "Buy Stop" order above the High Moving Average plus a specific pip buffer.
- Sell Stop Orders: It places a "Sell Stop" order below the Low Moving Average minus a specific pip buffer.
This strategy essentially looks for "breakout" opportunities, placing orders that only trigger if the price moves beyond the current high or low boundaries.
4. Dynamic Trade Management
Once a trade is active or an order is pending, the script actively monitors and updates them:
A. Stop Loss & Take Profit Adjustments
- Pending Orders: If the moving averages shift, the script will automatically move the Stop Loss levels of your pending Buy/Sell stops to ensure they remain aligned with the current market structure.
- Active Trades: The script monitors the Stop Loss of open trades. If a trade moves into a position where the Stop Loss is no longer favorable (e.g., it is too close to the current price or on the wrong side of the moving average), it can modify the order.
B. Trailing Stop Mechanism
The script includes an automated "Trailing Stop" feature. As the market moves in your favor:
- The Stop Loss price will automatically "follow" the price at a set distance (the
TrailingStopvalue). - This helps lock in profits while allowing the trade room to continue moving upward or downward.
C. Automatic Lot Sizing
Instead of using a fixed lot size, the script calculates the trade size based on your Account Free Margin and a user-defined Maximum Risk percentage. This ensures that the volume of trades is proportional to the amount of capital available in the account.
5. Summary of Workflow
- Scan: Check if the current price is between the High and Low moving averages.
- Positioning: Place "Stop" orders above/below those averages.
- Maintenance: Constant monitoring of existing trades to update Stop Losses based on new price movements or moving average shifts.
- Protection: Actively move the Stop Loss as profit grows (Trailing Stop) to secure gains.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Copyright 2005, Gordago Software Corp. |
//| http://www.gordago.com/ |
//+------------------------------------------------------------------+
// I want to thank Michal Rutka, michal1@zonnet.nl, for helping me correct
// the mistakes that I made... Good Job!!
#property copyright "Provided by sencho, coded by don_forex"
#property link "http://www.gordago.com"
extern int TakeProfit = 850;
extern int TrailingStop = 850;
extern int PipDifference = 25;
extern double Lots = 0.1;
extern double MaximumRisk = 10;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){
int cnt, ticket;
if(Bars<100){
Print("bars less than 100");
return(0);
}
if(TakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
string TradeSymbol = Symbol();
double MA144H = MathRound(iMA(NULL,0,144,0,MODE_EMA,PRICE_HIGH,0)/Point)*Point;
double MA144L = MathRound(iMA(NULL,0,144,0,MODE_EMA,PRICE_LOW,0)/Point)*Point;
double Spread = Ask-Bid; // MarketInfo(TradeSymbol,MODE_SPREAD);
double BuyPrice = MA144H + Spread+PipDifference*Point;
double BuyStopLoss = MA144L - Point;
double BuyTakeProfit = MA144H +(PipDifference+TakeProfit)*Point;
double SellPrice = MA144L -(PipDifference)*Point;
double SellStopLoss = MA144H + Spread+Point;
double SellTakeProfit= MA144L - Spread-(PipDifference+TakeProfit)*Point;
double lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/50000,1);
double close=iClose(NULL,0,0);
int total=OrdersTotal();
bool need_long = true;
bool need_short = true;
// First update existing orders.
for(cnt=0;cnt<total;cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber() == 16384){
if(OrderType() == OP_BUYSTOP){
need_long = false;
if (OrderStopLoss()!=BuyStopLoss){
Print(BuyStopLoss, " ",OrderStopLoss());
OrderModify(OrderTicket(),BuyPrice,BuyStopLoss,BuyTakeProfit,0,Green);
}
}
if(OrderType() == OP_SELLSTOP){
need_short = false;
if (OrderStopLoss()!=SellStopLoss){
Print(SellStopLoss, " ",OrderStopLoss());
OrderModify(OrderTicket(),SellPrice,SellStopLoss,SellTakeProfit,0,Green);
}
}
if(OrderType()==OP_BUY){
need_long = false;
if (OrderStopLoss()<BuyStopLoss){
Print(BuyStopLoss, " ",OrderStopLoss());
OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopLoss,BuyTakeProfit,0,Green);
OrderDelete(OrderTicket());
}
if(TrailingStop>0) {
if(Bid-OrderOpenPrice()>Point*TrailingStop) {
if(OrderStopLoss()<Bid-Point*TrailingStop) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
if(OrderType()==OP_SELL){
need_short = false;
if (OrderStopLoss()>SellStopLoss){
Print(SellStopLoss, " ",OrderStopLoss());
OrderModify(OrderTicket(),OrderOpenPrice(),SellStopLoss,SellTakeProfit,0,Green);
OrderDelete(OrderTicket());
}
if(TrailingStop>0) {
if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
if(AccountFreeMargin()<(1000*lot)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (close<MA144H && close>MA144L){
if(need_long)
ticket=OrderSend(Symbol(),OP_BUYSTOP,lot,BuyPrice,3,BuyStopLoss,BuyTakeProfit,"Binario_v3",16384,0,Green);
if(need_short)
ticket=OrderSend(Symbol(),OP_SELLSTOP,lot,SellPrice,3,SellStopLoss,SellTakeProfit,"Binario_v3",16384,0,Red);
}
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments