You didn't post a topic yet. Let us show you how to make your first topic and upload your favorite forex robot. Guide me

Skip to forum content

Quick Login / Register with Facebook Account!

Login with Facebook

Login with Username and Password!

     Username:       Password:  Login

Register  |  Forgot Password?

 

The latest topics with attached files (Forex robots, Forex Indicators, etc..)

Forums in this category with details of Replies, Views, last post

Banana Pips

by Nat4n

Big Breakout EA

by Md Billal Hossain

Hide

Recommended by Admin:

Trade with a small deposit, earn a big profit!

Try your Forex Robot on FBS Broker with a small depositleverage 1:3000ZERO Spread account!


Pages 1

You must login or register to post a reply

RSS

Posts: 2
Please rate this topic
Current rating: 0
Votes: 0

Embed topic to your site

PSAR

Topic: PSAR

//+------------------------------------------------------------------+
//|                                                 EA-PSAR_v1-0.mq4 |
//|                                                    Luca Spinello |
//|                                
//+------------------------------------------------------------------+

#property copyright     "Luca Spinello - mql4tradingautomation.com"
#property version       "1.00"
#property strict
#property description   "This Expert Advisor opens orders when the PSAR changes from above to below the price or vice versa"
#property description   " "
#property description   "DISCLAIMER: This code comes with no guarantee, you can use it at your own risk"
#property description   "We recommend to test it first on a Demo Account"

/*
ENTRY BUY: PSAR changes from above the previous bar to below the current bar
ENTRY SELL: PSAR changes from below the previous bar to above the current bar
EXIT: Take profit or trailing stop using PSAR
*/


extern double LotSize=0.1;             //Position size

extern double StopLoss=20;             //Stop loss in pips
extern double TakeProfit=500;          //Take profit in pips

extern int Slippage=2;                 //Slippage in pips

extern bool TradeEnabled=true;         //Enable trade

extern double PSARStep=0.2;            //PSAR Step         
extern double PSARMaxStep=0.02;        //PSAR Max Step

extern int MagicNumber=11223344;       //Magic Number to assign to the orders

//Functional variables
double ePoint;                         //Point normalized

bool CanOrder;                         //Check for risk management
bool CanOpenBuy;                       //Flag if there are buy orders open
bool CanOpenSell;                      //Flag if there are sell orders open

int OrderOpRetry=10;                   //Number of attempts to perform a trade operation
int SleepSecs=3;                       //Seconds to sleep if can't order
int MinBars=60;                        //Minimum bars in the graph to enable trading

//Functional variables to determine prices
double MinSL;
double MaxSL;
double TP;
double SL;
double Spread;
int Slip; 


//Variable initialization function
void Initialize(){          
   RefreshRates();
   ePoint=Point;
   Slip=Slippage;
   if (MathMod(Digits,2)==1){
      ePoint*=10;
      Slip*=10;
   }
   TP=TakeProfit*ePoint;
   SL=StopLoss*ePoint;
   CanOrder=TradeEnabled;
   CanOpenBuy=true;
   CanOpenSell=true;
}


//Check if orders can be submitted
void CheckCanOrder(){            
   if( Bars<MinBars ){
      Print("INFO - Not enough Bars to trade");
      CanOrder=false;
   }
   OrdersOpen();
   return;
}


//Check if there are open orders and what type
void OrdersOpen(){
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      } 
      if( OrderSymbol()==Symbol() && OrderType() == OP_BUY) CanOpenBuy=false;
      if( OrderSymbol()==Symbol() && OrderType() == OP_SELL) CanOpenSell=false;
   }
   return;
}


//Close all the orders of a specific type and current symbol
void CloseAll(int Command){
   double ClosePrice=0;
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      }
      if( OrderSymbol()==Symbol() && OrderType()==Command) {
         if(Command==OP_BUY) ClosePrice=Bid;
         if(Command==OP_SELL) ClosePrice=Ask;
         double Lots=OrderLots();
         int Ticket=OrderTicket();
         for(int j=1; j<OrderOpRetry; j++){
            bool res=OrderClose(Ticket,Lots,ClosePrice,Slip,Red);
            if(res){
               Print("TRADE - CLOSE - Order ",Ticket," closed at price ",ClosePrice);
               break;
            }
            else Print("ERROR - CLOSE - error closing order ",Ticket," return error: ",GetLastError());
         }
      }
   }
   return;
}


//Open new order of a given type
void OpenNew(int Command){
   RefreshRates();
   double OpenPrice=0;
   double SLPrice;
   double TPPrice;
   if(Command==OP_BUY){
      OpenPrice=Ask;
      SLPrice=OpenPrice-SL;
      TPPrice=OpenPrice+TP;
   }
   if(Command==OP_SELL){
      OpenPrice=Bid;
      SLPrice=OpenPrice+SL;
      TPPrice=OpenPrice-TP;
   }
   for(int i=1; i<OrderOpRetry; i++){
      int res=OrderSend(Symbol(),Command,LotSize,OpenPrice,Slip,NormalizeDouble(SLPrice,Digits),NormalizeDouble(TPPrice,Digits),"",MagicNumber,0,Green);
      if(res){
         Print("TRADE - NEW - Order ",res," submitted: Command ",Command," Volume ",LotSize," Open ",OpenPrice," Slippage ",Slip," Stop ",SLPrice," Take ",TPPrice);
         break;
      }
      else Print("ERROR - NEW - error sending order, return error: ",GetLastError());
   }
   return;
}


//Technical analysis of the indicators
bool CrossToOpenBuy=false;
bool CrossToOpenSell=false;


void CheckPSARCross(){
   CrossToOpenBuy=false;
   CrossToOpenSell=false;
   double PSARCurr=iSAR(Symbol(),0,PSARStep,PSARMaxStep,0);
   double PSARPrev=iSAR(Symbol(),0,PSARStep,PSARMaxStep,1);
   if(PSARCurr<Close[0] && PSARPrev>Close[1]){
      CrossToOpenBuy=true;
   }
   if(PSARCurr>Close[0] && PSARPrev<Close[1]){
      CrossToOpenSell=true;
   }
}


//Trailing Stop with PSAR
void UpdatePositions(){

   //Scan the open orders backwards
   for(int i=OrdersTotal()-1; i>=0; i--){
   
      //Select the order, if not selected print the error and continue with the next index
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         continue;
      } 
      
      //Check if the order can be modified matching the criteria, if criteria not matched skip to the next
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderMagicNumber()!=MagicNumber) continue;
      
      //Prepare the prices
      double TakeProfitPrice=OrderTakeProfit();
      double StopLossPrice=OrderStopLoss();
      double OpenPrice=OrderOpenPrice();
      double NewStopLossPrice=StopLossPrice;
      double NewTakeProfitPrice=TakeProfitPrice;
      
      //PSAR Value of the current symbol, current time frame , PSARStep, PSARMaxStep, previous candle
      double PSARPrice=iSAR(Symbol(),0,PSARStep,PSARMaxStep,1);
      
      //Get updated prices and calculate the new Stop Loss and Take Profit levels, these depends on the type of order
      RefreshRates();
      if(OrderType()==OP_BUY){
         if(PSARPrice>StopLossPrice && PSARPrice<Close[1] && PSARPrice<TakeProfitPrice){
            NewStopLossPrice=NormalizeDouble(PSARPrice,Digits);
         }
      } 
      if(OrderType()==OP_SELL){
         if(PSARPrice<StopLossPrice && PSARPrice>Close[1] && PSARPrice>TakeProfitPrice){
            NewStopLossPrice=NormalizeDouble(PSARPrice,Digits);
         }
      }
      
      //If the new Stop Loss and Take Profit Levels are different from the previous then I try to update the order
      if(NewStopLossPrice!=StopLossPrice){
         Print("Modifying order ",OrderTicket()," Open Price ",OpenPrice," New Stop Loss ",NewStopLossPrice," New Take Profit ",NewTakeProfitPrice);
         if(OrderModify(OrderTicket(),OpenPrice,NewStopLossPrice,NewTakeProfitPrice,0,Yellow)){
            Print("Order modified");
         }
         else{
            Print("Order failed to update with error - ",GetLastError());
         }      
      
      }

   
   }
}




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   //Calling initialization, checks and technical analysis
   Initialize();
   CheckCanOrder();
   UpdatePositions();
   CheckPSARCross();
   //Check of Entry/Exit signal with operations to perform
   if(CrossToOpenBuy){
      CloseAll(OP_SELL);
      if(CanOpenBuy && CanOrder) OpenNew(OP_BUY);
   }
   if(CrossToOpenSell){
      CloseAll(OP_BUY);
      if(CanOpenSell && CanOrder) OpenNew(OP_SELL);
   }
  }
//+------------------------------------------------------------------+

Post's attachments

EA-PSAR_v1-0.ex4 15.2 kb, 37 downloads since 2017-12-13 

Message from the MT4talk Admin:

Try your Forex robot on leverage 1:3000  All Forex strategies allowed! Scalping, hedging, martingale, etc... Deposit small money and make a big profit!

IMPORTANT:

If you are using a Forex robot, you must use a very fast Forex VPS! Try this super fast Forex VPS!

Hot Forum Topics:

Forex robot with a total net profit $1,945.43 >> Go to the forum topic.

Forex robot with a total net profit $194,459.50 >> Go to the forum topic.

Share

Thanked for the post: jozmik

Re: PSAR

li will check this, thank you very much, i have some eas, i will post here, thank you for all information, see you later, thank you very much

Share

Posts: 2

Pages 1

You must login or register to post a reply


NOTE: Hacked or illegal versions of copyrighted Forex robots are highly prohibited on MT4talk.com. If you upload any illegal version of any copyrighted Forex robot, your forum account will be banned. No exceptions.

 

Forex Risk Warning: Due to leverage, Forex comes with a high risk of losing money. Eighty to 90 percent of Forex traders lose money. You should not invest money that you cannot afford to lose.

Forum statistics

  • Total number of registered users: 23,945
  • Newest registered user: Sven Aria
  • Total number of topics: 11,311
  • Total number of posts: 37,108

Online users: 2,098 guests, 196 Members:

2trade2win, 777legrand, addat, adilloadam6, admin, ahmed.gooh.1, AL Dossary, Albert Bleyler, alejo.2511, alizidouri, Allan Rubini, amala3247, amdpc93, amwforex, Andrea Englaro, Andrew Kennedy, Aneilton Morais, Anjum Khan, AnthonyT, axel7969, badriaz918, ball2goal, BearBull7, BENEX, billymaggot, Boonchoke Intarapanya, bubelembizeni6, caballoblanc, chinapei, chris1961GR, chrislind, christiaan.heuer, ciprofar, Cretu Vasile, crootster, cruelzhang, d3nix111, Daniel Olokun, Darshana Samaraweera, david_lem57, deprofiters, dhayatsuzukiyk, dillansym, dino82, dlaminqk, doctorsnfw, DomCount12, Donovan Sissing, dopeyslydawg, doyathink, dsavse, e440e48d00, Eakapun, EliteOne Sport, eng.ahmed.gh009, Enoch Ngwenya, etiennehogue77, fcaillou, flsekme, Forex Stock Trading, Forum Admin, freezyabe, frettad, fridgefx, gaebler79, giodel, Gizmo79, gofire, h.sadeghiyan, Hamad Ali, Hans-Peter Aebi, hiltomclaudemoore, hiraokii, hktsui, hmaunoir, hondacbrr6, ian.kungu, igolfishing, iharmanbajwa, israelz74, jailson925, janich921, jaysonajax, jlhtrader, jmuntjan, Joe0013, José Luis Pérez, jozmik, jp1_imports, judeekatherinexx, Julio Alfredo Sandoval Zambrana, Kamiel Blouw, kava93, kcskavi, Khaled Azaiez, khalili1989, kiko, kindofblue79, kno, Kojo Bame, kozura, krisapong, Krzysztof Błaszczyk, lastwariors, latifi98, lawrence.jrt, leitzb, leonardi31, llight08, lori, machs_nochmal, mag00, Makari, marbre, marcial03, martinesleonardo, matoseyems, matuskotira1, mayiselak, Md Billal Hossain, miguefx, mikeb4, mikedyke, morkosa, Mostafa Essam Nassar, Mustafa Naci Temiz, mwachii, Nakhoda AndiQa, Nat4n, netprofit, nhawan429, NPleyer, Nur Hidayat, octavianus, omarchipsahoy, Oxogen Basudan, ozsah98, Pablo Dario Martinez, paolocroci69, Paso, Paul Ene, pellingrini45, Peral, pipcard3, pitbull0525, Polop, Rain Forest, ramialam95, Raymond Makuvatsine, reynier.punisher.barnard, Richard Spong, rochellebrown30, Roytall, rthousan, Sai Srikar, Sam Kimball, scorpionfx69, sebastianvillenadubiel, serctes, shahbaz.star333, shahin, shailey1s, smookey, sporty, ssehrat, Stans2037, Sue+254, Sven Aria, Švihák Lázeňský, tahucrispyku2, tai, tfeller, thetradersway, thiaw99, Timswood, Tom Kuhn, topcat275, tuvix2, uilues, uzobusy11, Valie, Verapan Ruampatanakit, verdeamanecer1983, Victoriano, webuser14, Whale, why, wisemansibusiso454, Wllie Bester, Wynand Nel, Xgar, yang, yansu75, yclow_1995, younes700, วรท ชอบทำดี

 


Copyright MT4talk.com Forum Rules - Privacy Policy.