
1. Introduction Every smart trading program has a section of code that controls the cashback forex/www.fxtraderebate.com">bestforexrebateing of positions It constantly searches through all the orders, selects positions by information, Whatisforexrebate then makes changes and closes Th forexrebatenetwork code looks pretty much the same and often has the same function Thats why this often-repeated code can be extracted from the program as a function, thus making First of all, lets divide the task into three steps by function These three steps are actually three kinds of smart trading programs: [*] Smart trading programs can only create one new position at the same time [*] Smart trading programs can create one new position of each type at the same time (for example, long and short positions) [*] Smart trading programs can create multiple positions at the same time 2. There are many strategies for creating a new position. This control code block is very simple, but it can take time and effort to write it; intstart(){//---- remember the indicator value to do analysis data doubleMACD_1=iMACD(Symbol(), What is forex rebate,12,26,9,PRICE_CLOSE,MODE_MAIN,1);doubleMACD_2=iMACD(Symbol(),0,12,26,9, PRICE_CLOSE,MODE_MAIN,2);int_GetLastError=0,_OrdersTotal=OrdersTotal();//---- search in the open position for(intz=_OrdersTotal-1;z>=0;z--){//If in the search generate error, go to next position if(!OrderSelect(z,SELECT_BY_POS)){_GetLastError=GetLastError();Print(/OrderSelect(/,z,/,SELECT_BY_POS)-error#/,_ GetLastError);continue;}///If the current currency pair is not open,//ignore over if(OrderSymbol()! =Symbol())continue;//If MagicNumber is not equal to _MagicNumber,//ignore the position if(OrderMagicNumber()! =_MagicNumber)continue;//---- if BUY hatch open position,if(OrderType()==OP_BUY){//---- if MACD encounters falling zero line,if(NormalizeDouble(MACD_1,Digits+1)<0.0& &NormalizeDouble(MACD_2,Digits+1)>=0.0){//---- close position if(!OrderClose(OrderTicket(),OrderLots(),Bid,5,Green)){_GetLastError= GetLastError();Alert(/ErrorOrderClose? /,_GetLastError);return(-1);}}///If the signal line has not changed, exit://open a new position too earlyelse{return(0);}}//----If the SELL position is open,if(OrderType()==OP_SELL){//----If the MACD encounters a rising Zero line if(NormalizeDouble(MACD_1,Digits+1)>0.0&&NormalizeDouble(MACD_2,Digits+1)<=0.0){//---- close the position if(!OrderClose(OrderTicket (),OrderLots(),Ask,5,Red)){_GetLastError=GetLastError();Alert(/ErrorOrderClose? /,_GetLastError);return(-1);}}///If the signal does not give poo, exit://open new positions too early elsereturn(0);}}//+----------------------------------------------------------- -------+//|If this point is reached, it means there is no open position|//|Detect possible open position|//+------------------------------------------------------------------+//----If MACD meets rising zero line,if( NormalizeDouble(MACD_1,Digits+1)>0.0&&NormalizeDouble(MACD_2,Digits+1)<=0.0){//---- open BUY position if(OrderSend(Symbol(),OP_ BUY,0.1,Ask,5,0.0,0.0,/MACD_test/,_MagicNumber,0,Green)<0){_GetLastError=GetLastError();Alert(/ErrorOrderSend? /,_GetLastError);return(-1);}return(0);}//---- if MACD encounters a falling zero line,if(NormalizeDouble(MACD_1,Digits+1)<0.0&&NormalizeDouble( MACD_2,Digits+1)>=0.0){//----openaSELLpositionif(OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0.0,0.0,/MACD_test/,_MagicNumber,0,Red)& lt;0){_GetLastError=GetLastError();Alert(/ErrorOrderSend? /,_GetLastError);return(-1);}return(0);}return(0);}Now we write the block of code as a function This function is able to search through all the orders and record their information in a global variable, the program is as follows:Copy the code int_Ticket=0,_Type=0; double_Lots=0.0,_OpenPrice=0.0,_StopLoss=0.0;double_TakeProfit=0.0;datetime_OpenTime=-1;double_Profit=0.0,_Swap=0.0;double_ Commission=0.0;string_Comment=//;datetime_Expiration=-1;voidOneOrderInit(intmagic){int_GetLastError,_OrdersTotal=OrdersTotal();_ Ticket=0;_Type=0;_Lots=0.0;_OpenPrice=0.0;_StopLoss=0.0;_TakeProfit=0.0;_OpenTime=-1;_Profit=0.0;_Swap=0.0;_Commission=0.0;_ Comment=//;_Expiration=-1;for(intz=_OrdersTotal-1;z>=0;z--){if(!OrderSelect(z,SELECT_BY_POS)){_GetLastError=GetLastError(); Print(/OrderSelect(/,z,/,SELECT_BY_POS)-error#/,_GetLastError);continue;}if(OrderMagicNumber()==magic&&OrderSymbol()==Symbol( )){_Ticket=OrderTicket();_Type=OrderType();_Lots=NormalizeDouble(OrderLots(),1);_OpenPrice=NormalizeDouble(OrderOpenPrice(),Digits );_StopLoss=NormalizeDouble(OrderStopLoss(),Digits);_TakeProfit=NormalizeDouble(OrderTakeProfit(),Digits);_OpenTime=OrderOpenTime( );_Profit=NormalizeDouble(OrderProfit(),2);_Swap=NormalizeDouble(OrderSwap(),2);_Commission=NormalizeDouble(OrderCommission(),2);_ Comment=OrderComment();_Expiration=OrderExpiration();return;}}}As you can see, its very simple: there are 11 variables, each storing information about the position (ticket#, type, lotsize, etc.). When the function starts to run, these variables are zeroed as global variables. This is necessary when the function is called, but we dont need the previous information, we need the most recent one and then all positions are searched in the standard way. trading program: copy the code externint_MagicNumber=1122;#include<OneOrderControl.mq4>intstart(){int_GetLastError=0;//remember the parameters for opening a position (if available)OneOrderInit( _MagicNumber);//---- remember the indicator value used for analysis doubleMACD_1=iMACD(Symbol(),0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);doubleMACD_2=iMACD(Symbol(),0,12,26,9, PRICE_CLOSE,MODE_MAIN,2);//Now,instead of searching in the position //there is an open position:if(_Ticket>0){/---- if BUY position is open,if(_Type==OP_BUY){//---- if MACD encounters a falling zero line,if( NormalizeDouble(MACD_1,Digits+1)<0.0&&NormalizeDouble(MACD_2,Digits+1)>=0.0){//---- close the position if(!OrderClose(_Ticket,_Lots, Bid,5,Green)){_GetLastError=GetLastError();Alert(/error OrderClose? /,_GetLastError);return(-1);}}//If the signal does not change, exit://open a new position too early elsereturn(0);}//----If the SELL position is opened, if(_Type==OP_SELL){//----If the MACD encounters a rising zero line if( NormalizeDouble(MACD_1,Digits+1)>0.0&&NormalizeDouble(MACD_2,Digits+1)<=0.0){//---- close the position if(!OrderClose(_Ticket,_Lots, Ask,5,Red)){_GetLastError=GetLastError();Alert(/error OrderClose? /,_GetLastError);return(-1);}}//If the signal does not change, exit://open a new position too early elsereturn(0);}}//If the smart trade is not open //(_Ticket==0)//If the MACD meets the rising zero line if(NormalizeDouble( MACD_1,Digits+1)>0.0&&NormalizeDouble(MACD_2,Digits+1)<=0.0){//---- open BUY position if(OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0.0, 0.0,/CrossMACD/,_MagicNumber,0,Green)<0){_GetLastError=GetLastError();Alert(/ErrorOrderSend? /,_GetLastError);return(-1);}return(0);}//---- if MACD encounters a falling zero line if(NormalizeDouble(MACD_1,Digits+1)<0.0&&NormalizeDouble( MACD_2,Digits+1)>=0.0){//---- open SELL position if(OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0.0,0.0,/CrossMACD/,_MagicNumber,0,Red)<0){_ GetLastError=GetLastError();Alert(/ErrorOrderSend? /,_GetLastError);return(-1);}return(0);}return(0);}As you can see, this smart trading program appears to be more compact and easier to read This is a simple example Now lets tackle the next task 3. This program is able to create many different types of positions and perform operations The following are the rules of this program: [*] The program will run with two pending orders: a buy stop at the sell price + 20 pips and a sell stop at the buy price + 20 pips; [*] When one order is initiated, the other must be cancelled; [*] The opening of a position must be accompanied by a trailing stop; [*] When a position is closed due to a stop loss or profit, it will be activated again. closed, it will be started again, meaning that two pending orders will be set up with the following procedure: copy code externint_MagicNumber=1123;externdoubleLot=0.1;externintStopLoss=60;//spacing of stop loss points (0-none) externintTakeProfit=100;//spacing of win points (0-none) externintTrailingStop=50;//trailing stops (0-none) externintLuft=20;//spacing of pending order transaction placement levels intstart(){//remember each ticket in the order intBuyStopOrder=0,SellStopOrder=0,BuyOrder=0,SellOrder=0;int_GetLastError=0,_OrdersTotal=OrdersTotal();//search in all open positions and remember //open position already exists type :for(intz=_OrdersTotal-1;z>=0;z--){//If an error is generated in the search, // go to the next position if(!OrderSelect(z,SELECT_BY_POS)){_GetLastError=GetLastError();Print(/ OrderSelect(/,z,/,SELECT_BY_POS)-Error#/,_GetLastError);continue;}//If there is no open position in the current currency pair, ignore it if(OrderSymbol()! =Symbol())continue;//If MagicNumber is not equal to _MagicNumber,//ignore this position if(OrderMagicNumber()! =_MagicNumber)continue;//depends on the position type,//change the variable value:switch(OrderType()){caseOP_BUY:BuyOrder=OrderTicket();break;caseOP_SELL:SellOrder= OrderTicket();break;caseOP_BUYSTOP:BuyStopOrder=OrderTicket();break;caseOP_SELLSTOP:SellStopOrder=OrderTicket();break;}}//---- if we have two pending trades, exit //---- wait for them to open if(BuyStopOrder>0&&SellStopOrder>0) return(0);// search for a second time in all orders // run them now:_OrdersTotal=OrdersTotal(); for(z=_OrdersTotal-1;z>=0;z--){//If an error is generated in the position search, // go to the next position if(!OrderSelect(z,SELECT_BY_POS)){_GetLastError=GetLastError();Print(/ OrderSelect(/,z,/,SELECT_BY_POS)-error#/,_GetLastError);continue;}//If for the current currency pair there is no open position // ignore it if(OrderSymbol()! =Symbol())continue;//If MagicNumber is not equal to _MagicNumber,//ignore the position if(OrderMagicNumber()! =_MagicNumber)continue;//depends on the type of position,//change the value of the variable:switch(OrderType()){//---- if BUY position is open,caseOP_BUY:{//If SellStop order has not been deleted,//delete:if( SellStopOrder>0){if(!OrderDelete(SellStopOrder)){Alert(OrderDeleteError#/,GetLastError());return(-1);}}//Detect stop loss is moved://If the size of the trailing stop is not very small,if(TrailingStop>MarketInfo(Symbol(),MODE_STOPLEVEL)){//If the winning point exceeds the trailing stop, if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)> NormalizeDouble(TrailingStop*Point,Digits)){//If the new stop level exceeds the level of the current position//(or if the position has no stop),if(NormalizeDouble(Bid-TrailingStop*Point,Digits)> ;OrderStopLoss()||OrderStopLoss()<=0.0){//---- modify the order if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid- TrailingStop*Point,Digits),OrderTakeProfit(),OrderExpiration())){Alert(/OrderModify error#/,GetLastError());return(-1);}}}}//If there is no open position exit // nothing to do return(0);}// the next cell and BUY position of the same cell // this is the reason we can not be marked on the cell ... .caseOP_SELL:{if(BuyStopOrder>0){if(!OrderDelete(BuyStopOrder)){Alert(/OrderDelete error#/,GetLastError());return(-1);}}if( TrailingStop>MarketInfo(Symbol(),MODE_STOPLEVEL)){if(NormalizeDouble(OrderOpenPrice()-Ask,Digits)>NormalizeDouble( TrailingStop*Point,Digits)){if(NormalizeDouble(Ask+TrailingStop*Point,Digits)<OrderStopLoss()||OrderStopLoss()<=0.0){if(! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*Point,Digits),OrderTakeProfit(),OrderExpiration())){ Alert(/OrderModifyError#/,GetLastError());return(-1);}}}}return(0);}}}//+--------------------------------------------------------- ---------+//|If execution reaches this point, |//| it means there are no pending orders and open positions|//+------------------------------------------------------------------+//---- places BuyStop and SellStop :double_OpenPriceLevel,_StopLossLevel,_TakeProfitLevel;_OpenPriceLevel=NormalizeDouble(Ask+Luft*Point,Digits);if(StopLoss>0){_ StopLossLevel=NormalizeDouble(_OpenPriceLevel-StopLoss*Point,Digits);}else{_StopLossLevel=0.0;}if(TakeProfit>0){_ TakeProfitLevel=NormalizeDouble(_OpenPriceLevel+TakeProfit*Point,Digits);}else{_TakeProfitLevel=0.0;}if(OrderSend(Symbol(),OP_ BUYSTOP,Lot,_OpenPriceLevel,5,_StopLossLevel,_TakeProfitLevel,//,_MagicNumber)<0){Alert(/OrderSendError#/,GetLastError()); return(-1);}_OpenPriceLevel=NormalizeDouble(Bid-Luft*Point,Digits);if(StopLoss>0){_StopLossLevel=NormalizeDouble(_ OpenPriceLevel+StopLoss*Point,Digits);}else{_StopLossLevel=0.0;}if(TakeProfit>0){_TakeProfitLevel=NormalizeDouble(_ OpenPriceLevel-TakeProfit*Point,Digits);}else{_TakeProfitLevel=0.0;}if(OrderSend(Symbol(),OP_SELLSTOP,Lot,_OpenPriceLevel,5,_ StopLossLevel,_TakeProfitLevel,//,_MagicNumber)<0){Alert(/OrderSendError#/,GetLastError());return(-1);}return(0);}