How do I code this second entry filter on C#

Discussion in 'Automated Trading' started by dennis86, Mar 26, 2019.

  1. dennis86


    I have a coded entry filter

    if(sma50.LookBack(1)>sma150.LookBack(1) && Close.LookBack(1) > Highest())

    if(sma50.LookBack(1)<sma150.LookBack(1) && Close.LookBack(1) < Lowest())

    However, this entry filter affects ALL markets selected during backtest. I want to add another entry filter which affects some markets so that the above can affect some markets. So how do i code that??

    Second entry filter
    SMA 25 > SMA 100 = Long....but it fetches some market like GC, SI, ES, TU etc
    SMA 25 < SMA 100 = Short....but it fetches some markets only e.g. PA, NQ, GI etc.

    Response will be highly appreciated
  2. 2rosy


    without further detail try...

    if(Opens()==0 && Symbol in ['GC','SI',...] )
  3. DaveV


    I don't think that C# has an "in" operator.
    Try something like:

    if ( Opens()==0 && new[]{"GS","SI",...}.Contains( Symbol ))
  4. RedDuke


    Very simple rules, you do not need to code this. use NinjaTrader strategy wizard to build it. Ninja is free until you need to trade live.


    Put the symbols you want to pass into a collection before the backtest starts. On each iteration test whether if(tradableSymbolCollection. Contains(currentSymbol)) then move on to next filter else return. Very simple. This is a poor approach if you backtest tick based data that contain millions of data points. In this case use a dictionary and lookup by key.

  6. fan27


    This is really a poor design as your strategy code should not have to make logical decisions based on the symbols passed to it. Try breaking this up into multiple strategies and pass the desired symbols to their corresponding strategy.
    userque likes this.
  7. dennis86


    Thanks all.....

    Will try all of those different tweaks and see which sticks...
  8. DaveV


    Your solution is great, if efficiency was paramount, and you were advising an experienced C# programmer. It seemed obvious from the question that the OP was not an experienced C# programmer, so a simpler solution was probably better.


    I have to actually disagree with this. Take for example a basket that you like to compute an index on which is perused inside the strategy to make trading decisions. And imagine some symbols follow a certain algebraic logic while others follow another (example, fx symbols where USD based symbols are intended to be handled differently than non Usd based ones). In this case you must not only allow the strategy to understand which symbol each quote pertains to but you may need to differentiate inside the strategy by symbol.

  10. fan27


    What you describe makes sense but I could easily see what the OP described spiraling out of control as I am picturing a huge if/else tree containing conditional logic pertaining to only certain groups of symbols.

    Obviously I don't know all of the details of what you described above, but I have a similar issue in that I have symbols that I need to get info for (tick size, margin percent, instument type, etc.) in numerous parts of my app. The solution I came up with is to have an in memory exchange that I populate at application startup and I can access from anywhere in my app. The data does not change during the course of running the application so it works. Of course this approach may not work for your use case.
    #10     Mar 26, 2019