How to use the value of a variable from one function into another? - page 2

 
Look at the variable names. No human coder would use this type of naming because it makes it hard to read your code.
 
Enrique Dangeroux:
Look at the variable names. No human coder would use this type of naming because it makes it hard to read your code.

You would be surprised...loool

 
Alain Verleyen:

You would be surprised...loool

This exactly what I thought and I am so frustrated now to realize that the code I had been working with wasnt intentionally poorly written. I am going to ask for the REAL source.

 
Can anyone still help me? I am trying to pass the value of a variable from one function to another after it has been calculated in the first function.
 
Basically I am trying to make a local variable global if that makes sense...
 
QuantCoder:
Basically I am trying to make a local variable global if that makes sense...

Not really... if you want a variable to retain its state between calls then you need to make it static. If you need to pass a variable to function and have that function modify it in-place then you need to pass it by reference. If you just need to the second function to know a significant value for operation then pass it by value. 


enum SIGNAL_FLAGS
{
   NONE           = 0,
   LONG_WEAK      = (1<<0),
   LONG_STRONG    = (1<<1),
   SHORT_WEAK     = (1<<2),
   SHORT_STRONG   = (1<<3)
};

void in_place(int &by_reference)
{
   if(true)
      by_reference|= LONG_STRONG;
   else
      by_reference|= SHORT_STRONG;
}

SIGNAL_FLAGS by_return_value()
{
   if(true)
      return LONG_STRONG;
   else
      return SHORT_STRONG;
}

bool scheck(const int signal_flags, SIGNAL_FLAGS signal_to_check)
{
   return bool(signal_flags & signal_to_check);
}
void OnTick()
{
   int signal_flags = by_return_value();
   in_place(signal_flags);
   if(scheck(signal_flags, LONG_STRONG) && !scheck(signal_flags, SHORT_STRONG))
      Print("Go LONG!");
}
 
nicholi shen:

Not really... if you want a variable to retain its state between calls then you need to make it static. If you need to pass a variable to function and have that function modify it in-place then you need to pass it by reference. If you just need to the second function to know a significant value for operation then pass it by value. 


Ok so say my first function is the one below (onTick) and I am trying to use the returned value of switch_function in my second function

1. first function

void OnTick()
  {
   int switch_action;
   int ticket=-1;
   double price;
   double TradeSize;
   double SL;

//---

//Open Buy Order, instant signal is tested first
   RefreshRates();
   if(Cross(0,iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_SIGNAL,0)) //Stochastic Oscillator crosses above Stochastic Oscillator
      && iStochastic(NULL,PERIOD_CURRENT,34,3, 3, MODE_SMA, 0, MODE_MAIN, 0) < 20 //Stochastic Oscillator < fixed value
      && Bid < iBands(NULL, PERIOD_CURRENT, 34, 2, 0, PRICE_CLOSE, MODE_LOWER, 0) //Price < Bollinger Bands
      && iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)<32 //Relative Strength Index < fixed value
      )
     {
      RefreshRates();
      price=Ask;
      if(IsTradeAllowed())
        {
         switch_action=1;
         if(ticket <= 0) return;
        }
     }

//Open Sell Order, instant signal is tested first
   RefreshRates();
   if(Cross(1,iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_MAIN,0)<iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_SIGNAL,0)) //Stochastic Oscillator crosses below Stochastic Oscillator
      && iStochastic(NULL,PERIOD_CURRENT,34,3, 3, MODE_SMA, 0, MODE_MAIN, 0) > 80 //Stochastic Oscillator > fixed value
      && Bid > iBands(NULL, PERIOD_CURRENT, 34, 2, 0, PRICE_CLOSE, MODE_UPPER, 0) //Price > Bollinger Bands
      && iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)>58 //Relative Strength Index > fixed value
      )
     {
      RefreshRates();
      price=Bid;
      if(IsTradeAllowed())
        {
         switch_action=2;
         if(ticket <= 0) return;
        }

      //myOrderModifyRel(ticket, SL, 0);
     }


  }

2. second function

int secondfunction() 
  {
   int switch_action;
   switch_action = 0;
   if(DayOfWeek()<1) 
     {
         if(switch_action=1)
            checktrade=TRUE;

         else
            checktrade=FALSE;

         }
   return (checktrade);
  }

How could I make sure that the value of the switch_action carries over from function 1 to function 2?

@nicholi shen

 
QuantCoder:
What on earth?!? This isnt decompiled code?! who deleted my code snippet?

I deleted it. Anybody with a little more than basic knowledge of coding can recognise decompiled code immediately.

Posting decompiled code is against the forum rules and doing so can get you banned.

You have been given the benefit of the doubt this time as often people who have no coding experience do not realise that the code is decompiled.

 
QuantCoder:

Ok so say my first function is the one below (onTick) and I am trying to use the returned value of switch_function in my second function

1. first function

2. second function

How could I make sure that the value of the switch_action carries over from function 1 to function 2?

@nicholi shen

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

7.3 — Passing arguments by reference
7.3 — Passing arguments by reference
  • www.learncpp.com
While pass by value is suitable in many cases, it has a couple of limitations. First, when passing a large struct or class to a function, pass by value will make a copy of the argument into the fun…
 
nicholi shen:

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Ok so this is what I did:

First function,

int OnTick(int &switch_action)
  {
   //int switch_action;
   int ticket=-1;
   double price;
   double TradeSize;
   double SL;

//---

//Open Buy Order, instant signal is tested first
   RefreshRates();
   if(Cross(0,iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_SIGNAL,0)) //Stochastic Oscillator crosses above Stochastic Oscillator
      && iStochastic(NULL,PERIOD_CURRENT,34,3, 3, MODE_SMA, 0, MODE_MAIN, 0) < 20 //Stochastic Oscillator < fixed value
      && Bid < iBands(NULL, PERIOD_CURRENT, 34, 2, 0, PRICE_CLOSE, MODE_LOWER, 0) //Price < Bollinger Bands
      && iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)<32 //Relative Strength Index < fixed value
      )
     {
      RefreshRates();
      price=Ask;
      //SL = 10 * myPoint; //Stop Loss = value in points (relative to price)   
      if(IsTradeAllowed())
        {
         switch_action=1;
        }
     }

//Open Sell Order, instant signal is tested first
   RefreshRates();
   if(Cross(1,iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_MAIN,0)<iStochastic(NULL,PERIOD_CURRENT,34,3,3,MODE_SMA,0,MODE_SIGNAL,0)) //Stochastic Oscillator crosses below Stochastic Oscillator
      && iStochastic(NULL,PERIOD_CURRENT,34,3, 3, MODE_SMA, 0, MODE_MAIN, 0) > 80 //Stochastic Oscillator > fixed value
      && Bid > iBands(NULL, PERIOD_CURRENT, 34, 2, 0, PRICE_CLOSE, MODE_UPPER, 0) //Price > Bollinger Bands
      && iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,0)>58 //Relative Strength Index > fixed value
      )
     {
      RefreshRates();
      price=Bid;
      //SL = 10 * myPoint; //Stop Loss = value in points (relative to price)   
      if(IsTradeAllowed())
        {
         switch_action=2;
        }

     }
return(switch_action);

  }

Second function,

int secondfunction() 
  {
   int switch_action;
   OnTick(switch_action);
   if(DayOfWeek()<1) 
     {
         if(switch_action=1)
            checktrade=TRUE;

         else
            checktrade=FALSE;

         }
   return (checktrade);
  }

doing "if(switch_action==1)" doesn't work here either. If I use "if(switch_action=1)" it will only buy. Any ideas?

Reason: