Passing on tick variable into OnChartEvent....??????

 

I'm a newbie to programming, so please forgive this as probably a very stupid question....

I'm trying to pass a variable ("SL_sell" - see below) into an OnChartEvent - so that when a Sell Button is pressed, the Stop Loss is defined by the ATR value (which is being constantly updated in OnTick)...

but have no idea how to make this work... I'm not even sure if I'm asking the right question!

Any help much appreciated. 


//+------------------------------------------------------------------+

//|                                Adam-Paul-Scalping-TradeEntry.mq4 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

input double TP_Multiplier = 2;

input double SL_Multiplier = 2;

input bool Add_Spread = true;



double TP_buy, SL_buy, TP_sell, SL_sell;



//double CalculateNormalizedDigits()

//{

//   //If there are 3 or less digits (JPY for example) then return 0.01 which is the pip value

//   if(Digits<=3){

//      return(0.01);

//   }

//   //If there are 4 or more digits then return 0.0001 which is the pip value

//   else if(Digits>=4){

//      return(0.0001);

//   }

//   //In all other cases (there shouldn't be any) return 0

//   else return(1);

}







//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

   

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {





double ATR_value =   NormalizeDouble(iATR(NULL,0,14,0),5 );

double StopLoss = ATR_value*SL_Multiplier;

double TakeProfit = ATR_value*TP_Multiplier;



double PipsMultiplier = CalculateNormalizedDigits();



double TP_buy = Ask+TakeProfit;

double SL_buy = Ask-StopLoss;



double TP_sell = Bid-TakeProfit;

double SL_sell = Bid+StopLoss;







ObjectCreate(_Symbol,"SellButton",OBJ_BUTTON,0,0,0);



ObjectSetInteger(_Symbol,"SellButton",OBJPROP_XDISTANCE,20);

ObjectSetInteger(_Symbol,"SellButton",OBJPROP_XSIZE,100);

ObjectSetInteger(_Symbol,"SellButton",OBJPROP_YDISTANCE,60);

ObjectSetInteger(_Symbol,"SellButton",OBJPROP_YSIZE,25);

ObjectSetInteger(_Symbol,"SellButton",OBJPROP_CORNER,2);

ObjectSetString(_Symbol,"SellButton",OBJPROP_TEXT,"SELL");





ObjectCreate(_Symbol,"BuyButton",OBJ_BUTTON,0,0,0);



ObjectSetInteger(_Symbol,"BuyButton",OBJPROP_XDISTANCE,20);

ObjectSetInteger(_Symbol,"BuyButton",OBJPROP_XSIZE,100);

ObjectSetInteger(_Symbol,"BuyButton",OBJPROP_YDISTANCE,30);

ObjectSetInteger(_Symbol,"BuyButton",OBJPROP_YSIZE,24);

ObjectSetInteger(_Symbol,"BuyButton",OBJPROP_CORNER,2);

ObjectSetString(_Symbol,"BuyButton",OBJPROP_TEXT,"BUY");





   Comment("\n"

           "ATR: ",ATR_value,"\n"

           "Stop Loss: ",StopLoss,"\n"

           "Take Profit: ",TakeProfit,"\n"

           //"Pips Multiplier: ",PipsMultiplier,"\n"

   

   

   );





}



void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

               

{





if(id==CHARTEVENT_OBJECT_CLICK)

   {

   if(sparam=="SellButton")

      {Alert("YESSSSSS!!!");

      

      }  



   }





if(id==CHARTEVENT_OBJECT_CLICK)

   {

   if(sparam=="BuyButton")

      {Alert("NOOOOO!!!");

      OrderSend(NULL,OP_BUY,1,Ask,3,SL_sell,TP_buy,NULL,100001,0,clrYellow);

      

      }  



   }







}













   

  

//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
One Click Close The script allows users to easily close positions if their profit/loss reaches or exceeds a value specified in pips. Please set slippage value first. Sometimes some positions do not close due to high volatility of the market. Please set larger slippage or restart the script. The free demo version is: ...
 
Please edit your post and

use the code button (Alt+S) when pasting code

You also might consider making your code easier to read by removing all the unnecessary empty lines.

I don't believe that I am the only one, but I will not bother to read code with excessive white space.

 
double TP_buy, SL_buy, TP_sell, SL_sell;

double TP_sell = Bid-TakeProfit;

You are redefining TP_sell and the other variables on entrance of OnTick, so that when OnTick exits its value is lost. Just don't do that - you already have it defined in the global scope, that's ok, keep it there.

Reason: