Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Join our fan page
You liked the script? Try it in the MetaTrader 5 terminal
- Views:
- 1798
- Rating:
- Published:
- 2024.05.28 21:04
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
I. Main function
1. Auto caculate lot size
2. Auto trailling ATR
3. Using volume indicator and moving average indicator
4. Condition buy or sell
2. Backtest.
II. Main function code
1. Volume indicator declaration
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { if(!m_symbol.Name(_Symbol)) return INIT_FAILED; // Set Trade parameter trade.SetTypeFillingBySymbol(m_symbol.Name()); trade.SetExpertMagicNumber(m_magicnumber); trade.SetDeviationInPoints(Slippage); // Turning 3 or 5 Digit int adjustdigit=1; if(m_symbol.Digits()==3 || m_symbol.Digits()==5) adjustdigit=10; m_adjustpoint=adjustdigit*m_symbol.Point(); // Indicator RSI declaration Handle_MA= iMA(_Symbol,timeframe,MA_period,MA_shift,MODE_EMA,PRICE_CLOSE); if(Handle_MA==INVALID_HANDLE) return INIT_FAILED; // Indicator RSI declaration hand_atr= iATR(_Symbol,timeframe,atr_period); if(hand_atr==INVALID_HANDLE) return INIT_FAILED; // Indicator volume handle_volume= iVolumes(_Symbol,timeframe,VOLUME_TICK); if(handle_volume==INVALID_HANDLE) return INIT_FAILED; // Indicator Moving average volume handle_MA_volume= iMA(_Symbol,timeframe,MA_volume_period,MA_volume_shift,MODE_EMA,handle_volume); if(handle_MA_volume==INVALID_HANDLE) return INIT_FAILED; //--- return(INIT_SUCCEEDED); }
2. Tick function
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(OpenBar(Symbol())) { // Candle declaration double High[],Low[],open[],close[]; ArraySetAsSeries(High,true);ArraySetAsSeries(Low,true);ArraySetAsSeries(close,true);ArraySetAsSeries(open,true); CopyHigh(Symbol(),timeframe,0,1000,High); CopyLow(Symbol(),timeframe,0,1000,Low); CopyOpen(_Symbol,timeframe,0,100,open); CopyClose(_Symbol,timeframe,0,100,close); // Highest high and lowest low declaration int highest= ArrayMaximum(High,HL_shift,HL_period); int lowest= ArrayMinimum(Low,HL_shift,HL_period); double HH= High[highest]; Drawline(" Kháng Cự ", clrRed,HH); double LL= Low[lowest]; Drawline(" hỗ trợ ", clrBlue,LL); // Moving average declaration CopyBuffer(Handle_MA,0,0,100,MA_Filter); ArraySetAsSeries(MA_Filter,true); // Atr declaration ArraySetAsSeries(atr,true); CopyBuffer(hand_atr,0,0,50,atr); // Volume and MA_volume array buffe ArraySetAsSeries(Volume,true); ArraySetAsSeries(MA_volume,true); CopyBuffer(handle_volume,0,0,50,Volume); CopyBuffer(handle_MA_volume,0,0,50,MA_volume); //+------------------------------------------------------------------+ //| Broker parameter | //+------------------------------------------------------------------+ double point = SymbolInfoDouble(_Symbol,SYMBOL_POINT); double ask= SymbolInfoDouble(_Symbol,SYMBOL_ASK); double bid= SymbolInfoDouble(_Symbol,SYMBOL_BID); double spread=ask-bid; double stoplevel= (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL); int freezerlevel= (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL); // Count bjuy and count sell int count_buy=0; int count_sell=0; count_position(count_buy,count_sell,atr); // Main condition for buy and sell if(count_buy==0) { if(ask>(HH) && High[highest] > MA_Filter[highest] && Volume[1]>MA_volume[1] ) { double entryprice= ask; double sl = LL; double tp = entryprice +TP_factor*atr[1]; double lotsize = calculate_lotsize(sl,entryprice); if( bid-sl>stoplevel && tp-bid>stoplevel&& CheckVolumeValue(lotsize) ) { trade.Buy(lotsize,_Symbol,entryprice,sl,tp, " Buy Mr Tan "); } } } if(count_sell==0) { if(bid<(LL) && Low[lowest] < MA_Filter[lowest]&& Volume[1]>MA_volume[1]) { double entryprice= bid; double sl = HH; double tp = entryprice -TP_factor*atr[1]; double lotsize = calculate_lotsize(sl,entryprice); if( sl-ask>stoplevel && ask-tp>stoplevel&& CheckVolumeValue(lotsize) ) { trade.Sell(lotsize,_Symbol,entryprice,sl,tp, " Sell Mr Tan "); } } } } }
![Trailing stop tutorial using ATR indicator](https://c.mql5.com/i/code/expert.png)
Trailing stop tutorial using ATR indicator
![Calculate Lot Size and Trailling Stop](https://c.mql5.com/i/code/expert.png)
This is a lesson on how to: 1. calculate the lot size in each order corresponding to the percentage of balance loss. 2. Trailling and count position buy and sell. The condition buy or sell base on Price action and moving average filter
![Lesson 9 Buy sell stop Order](https://c.mql5.com/i/code/expert.png)
This is a lesson on buying or selling with pending orders stop
![Beginner Programming: Moving Average Crossover with and without Martingale functionality](https://c.mql5.com/i/code/expert.png)
Moving averages are useless. In fact, some argue that the best way to lose money for a beginner are MA-Price crossover strategies. But is it possible to make it work?