NexusFi: Find Your Edge


Home Menu

 





thread safe variables in method


Discussion in NinjaTrader

Updated
    1. trending_up 900 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5

Hello community,

I request support for the following problems:

1. I want to use a counter in a method. This does not yet work thread safe.
Sample 1a would be encapsulated but does not return a correct value.
Sample 1b works, but is not thread-safe (==> Sample2)

2. How can I use predecessor values in a method?
Sample 3 shows that no previous value is determined with this procedure.
Analog Sample 1b I can initialize the series outside the method; this solution is also not thread safe.

I gave the examples because I want to simplify my request.

I make 5 calculations with different values, but they always have the same conditions.
This requires a TickCounter and a previous value.

That's why I want to do the calculations in a method, using thread-safe variables.
My programming example shows that reusing a variable does not work if it is initialized outside the method.
I would have to use 5 variables or series for my 5 calculations, so that the method actually makes no sense.
Finally, methods should simplify the code and "save" variables.

There are suggestions on how I can solve the problem?

Thank you!
Best
user10



PS: Are there any examples of function programming in the forum; I did not find any.

 
Code
                            
namespace NinjaTrader.NinjaScript.Strategies

{
    public class 
TestFunktionen Strategy
    
{
        private 
SMA SMA1;
        private 
int barCounter;
        private 
int barCounterFuncIn;
        private 
int barCounterFuncIn1;
        private 
int barCounterFuncIn2;
        
        private 
int funktionsValue;
        private 
int funktionsValue1;
        private 
int funktionsValue2;
        
        protected 
override void OnStateChange()
        {
            
#region notrelevant
            
            
if (State == State.SetDefaults)
            {
                
Calculate    Calculate.OnEachTick;
            }
            else if (
State == State.Configure)
            {
                
AddDataSeries(Data.BarsPeriodType.Minute1);
            }
            else if (
State == State.DataLoaded)
            {                
            }
            
#endregion
        
}
        protected 
override void OnBarUpdate()
        {
            
// OnBarUpdate
            
barCounter++;
            
            
// Funktionen
            
funktionsValue barCounterF();
            
funktionsValue1 barCounterThread();
            
funktionsValue2 barCounterPrevious();
            
            Print (
Time[0]     
            + 
"  barCounterF   " funktionsValue 
            
"  barCounterThread   " funktionsValue1 
            
"  barCounterPrevious   " funktionsValue2    
            
"  barCounter   " barCounter);
        }

            public 
int barCounterF()                    // Sample 1
            
{
//                int counter = 0;                // Sample a    
//                return counter;                // returns 1
                
                
barCounterFuncIn++;            // Sample b
                
return barCounterFuncIn;        // not thread-safe
            
}
    
            public 
int barCounterThread()                // Sample 2
            
{
                
barCounterFuncIn +=2;
                return 
barCounterFuncIn;           // not thread-safe
            
}
            
            public 
int barCounterPrevious()        // Sample 3
            
{
                
Series <intbarCounterSeries = new Series<int> (this);
                
barCounterFuncIn2++;
                
barCounterSeries[0] = barCounterFuncIn2;
                
//                return barCounterFuncIn2;        // correct
                
return barCounterSeries[0];        // correct
//                return barCounterSeries[1];        // ==> 0 ==> no previous Value
            


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Radarscreen
TradeStation
Quantum physics & Trading dynamics
The Elite Circle
STRATEGY on MC crossEMA
The Elite Circle
Elite Trader Funding, Avoid?
Trading Reviews and Vendors
Help re translation of ninjascript to EL
NinjaTrader
 
  #2 (permalink)
 LukePoga 
Sydney Australia
 
Experience: Intermediate
Platform: Rithmic API
Trading: ES CL
Posts: 63 since Jan 2014
Thanks Given: 11
Thanks Received: 24

It’s simple to make a part of your code single threaded.

Add a class member first:

private static object lockObject = new object();


And then in your code where you want to make single threaded do this:

lock(lockObject)
{
// do your work here - threadsafe
}

If you only want to make an increment safe you do not need the above. You can do it in a simple command like this

return Interlocked.Increment(ref COUNTER);

Reply With Quote
Thanked by:
  #3 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5


Hello LukePoga,

Thanks for the hint.
In the meantime, I've learned that threadsafe is probably the wrong name.

I calculate values in periods whose beginning and end are indefinite. At the end of the period I would like to do another calculation;
because the condition is wrong at this time, I need to access the predecessor value.
In real-time operation, this condition is true for one bar, but the condition should only be true for a tick to execute the calculation once.
These 5 calculations are performed with different values and periods and all five should access one method.

I understand that I can initialize variables and series outside the method.
Then both the counter and the return of the previous value will work.

In my example barCounterF () and barCounterThread () I increase the same variable barCounterFuncIn once by 1 and once by 2 and return it.
Although I assign a different variable (functionValue1 and functionValue2) to the respective return value, the methods influence each other.

If I change the barCounterThread () method to another variable e.g. assigning barCounterFuncIn1, both methods provide the correct result.

I added the code for verification.

If I want to simplify my code, this must not happen. It also makes no sense to use exactly the variables and series that I would have to use without any method.

Where is my mistake?

Best
user10

Reply With Quote




Last Updated on December 5, 2018


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts