- ago
Is there a block that will give a Boolean condition? The code will be something like this in C#. (See below) I tried the Time of Day block in PowerPack but it does not let me work it this way.
Of course I can create a C# code snippet and insert it in my strategy, but I was wondering if a block was available. Thanks.

CODE:
if Close > EMA(Indicator) and Time = 9.20AM(Time) { PreMarketCond = True }
0
608
Solved
22 Replies

Reply

Bookmark

Sort
Glitch8
 ( 12.83% )
- ago
#1
If attach 2 Conditions like this below, they are ANDed together.

0
Best Answer
- ago
#2
Thanks, I am not getting any trades when I add the Time of Day block.
I do get trades if I have only the Close Grater than EMA block enabled though.
0
Glitch8
 ( 12.83% )
- ago
#3
Are you running on an intraday scale? Did you enable pre-post market data? Did you open a chart and verify that your selected provider is returning the expected data?
0
- ago
#4
Thanks, It was a data download issue I solved it.
In reality what I was looking for is to sets of conditions to occur at different times of day to be true. See code below. Is it possible to do it with two "Multi-Condition" Blocks maybe?

CODE:
public override void Initialize(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new SMA(bars.Close,20);          PlotIndicator(indicator2,new WLColor(0,0,0)); } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool preMktCondition;          if (foundPosition0 == null)          {             preMktCondition = false;             {                if(bars.DateTimes[index].GetTime() >= 928)                {                   if(bars.DateTimes[index].GetTime() <= 929)                   {                      if (index - 0 >= 0 && indicator1[index] < indicator2[index - 0])                      {                         preMktCondition = true;                      }                   }                }             } MktHrsCondition = false;             {                if(bars.DateTimes[index].GetTime() >= 935)                {                   if(bars.DateTimes[index].GetTime() <= 1045)                   {                      if (index - 0 >= 0 && indicator1[index] < indicator2[index - 0])                      {                         MktHrsCondition = true;                      }                   }                }             }             if (preMktCondition) && (MktHrsCondition)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }
0
Glitch8
 ( 12.83% )
- ago
#5
yes, it sounds like that’s what would do it.
0
- ago
#6
At the very least you need to promote preMktCondition and MktHrsCondition from the method level to the class level. And, you need to consider resetting both of them prior to 9:28.
0
- ago
#7
Thanks, I am trying the concept in Building Blocks. This is what I came up (C# Code below) With Building Blocks will not give me any entries because both Multiple Conditions cancel each other. Any suggestions welcomed. Thank you.



CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { StartIndex = 20; } public override void Initialize(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new SMA(bars.Close,20);          PlotIndicator(indicator2,new WLColor(0,0,0));          indicator12 = bars.Close;          indicator22 = new SMA(bars.Close,20); } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                count = 0;                if(bars.DateTimes[index].GetTime() >= 928)                {                   count++;                }                if(bars.DateTimes[index].GetTime() <= 929)                {                   count++;                }                if (index - 0 >= 0 && indicator1[index] < indicator2[index - 0])                {                   count++;                }                if (count >= 3)                {                   count2 = 0;                   if(bars.DateTimes[index].GetTime() >= 935)                   {                      count2++;                   }                   if(bars.DateTimes[index].GetTime() <= 1200)                   {                      count2++;                   }                   if (indicator12.CrossesOver(indicator22, index))                   {                      count2++;                   }                   if (count2 >= 3)                   {                      condition0 = true;                   }                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                Backtester.CancelationCode = 149;                if (idx - foundPosition0.EntryBar + 1 >= 5)                {                   ClosePosition(foundPosition0, OrderType.Market, 0,"Sell after 5 bars");                }             }          } } public override void NewWFOInterval(BarHistory bars) { }       private int count;       private bool mc;       private TimeSeries indicator1;       private IndicatorBase indicator2;       private int count2;       private bool mc2;       private TimeSeries indicator12;       private IndicatorBase indicator22;       private Transaction _transaction; } }
0
Cone8
 ( 22.68% )
- ago
#8
First, if you stack Multi-Condition blocks, then they both have to be true, which is not what you want. You need to put each one under its own BuyAtMarket signal. However, both could create a position on the same day.

Recently we discussed the need for an "OR Divider" but I'm not sure if that we turned into a Feature Request or not.

Also, the time condition is VERY restrictive. e.g., If there's no premarket bar at 9:28, then you'll never get a signal.
0
- ago
#9
QUOTE:
Recently we discussed the need for an "OR Divider" but I'm not sure if that we turned into a Feature Request or not.

I just turned it into one: Or condition with multiple condition blocks.
0
Cone8
 ( 22.68% )
- ago
#10
Thanks Eugene. Add your vote for first post on that topic.. I added mine!
0
- ago
#11
Both multiple conditions have to be true though. It is a reference to a previous event in time. So if previous event = true and current event = true then... Enter.
It might work if I split the premarket session and the regular session into 2 different sessions?
0
Cone8
 ( 22.68% )
- ago
#12
I see what you mean - you want an enabling premarket condition that stays true all day AND when the other block is true, then place the trade.

There are some things you'll probably never be able to do with blocks, but maybe we can envision adding a Power Pack "Setup" Qualifier. We'll have to study it!
0
- ago
#13
Yes thank you. This will be useful also for near past events of any sort.
0
- ago
#14
Cone was this feature implemented? Or could it be?
QUOTE:
but maybe we can envision adding a Power Pack "Setup" Qualifier.

Thank you.
0
Cone8
 ( 22.68% )
- ago
#15
No, I didn't mean to imply that it was an actionable remark. If there's a specific request that needs to be a feature, then we need to make a specific feature request and garner votes for it. Another route is Concierge support.
0
- ago
#16
Thank you. I was able to code the same concept in NinjaTrader and works ok there (see code below) but then I tried to translate the NT code into WL8 (see code below) I get no entries. I must be missing something. Please advise. Thank you.

NT CODE
CODE:
private bool Condition01; private SMA SMA1; Condition01   = false; if (State == State.DataLoaded)          {                         SMA1= SMA(Close, 20);          } protected override void OnBarUpdate()       {          if (BarsInProgress != 0)             return;          if (CurrentBars[0] < 1)             return;           // Condition01 TRUE          if ((Close[0] < SMA1[0])              && (Times[0][0].TimeOfDay > new TimeSpan(9, 28, 0))              && (Times[0][0].TimeOfDay < new TimeSpan(9, 30, 0)))          {             Condition01 = true;          }                     // Condition01 FALSE          if ((Close[0] < SMA1[0])              && (Times[0][0].TimeOfDay > new TimeSpan(9, 28, 0))              && (Times[0][0].TimeOfDay < new TimeSpan(9, 30, 0)))          {             Condition01 = false;          }                     // Condition02 ENTRY          if ((Close[0] > SMA1[0])              && (Times[0][0].TimeOfDay > new TimeSpan(10, 0, 0))              && (Times[0][0].TimeOfDay < new TimeSpan(12, 30, 0))              && (Condition01 == true))          {             EnterLong(100, "");          }                 }


WL8
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.MyIndicators; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          StartIndex = 20; } public override void Initialize(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new CustomSMA(bars.Close,20);          PlotIndicator(indicator2,new WLColor(0,0,0));       } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool Condition01;          bool Condition02;          Condition01 = false;                    if (foundPosition0 == null)          { if ((bars.DateTimes[index].GetTime() > 928)                && (bars.DateTimes[index].GetTime() < 930)                && (index - 0 >= 0 && indicator1[index] < indicator2[index - 0]))             {                Condition01 = true;             }             if ((bars.DateTimes[index].GetTime() < 928)                && (bars.DateTimes[index].GetTime() > 930)                && (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]))             {                Condition01 = false;             }                                    if (Condition01 == true)             {                if ((bars.DateTimes[index].GetTime() > 1000)                   && (bars.DateTimes[index].GetTime() < 1250)                   && (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]))                {                   _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");                }             }                       }          else          {             Condition01 = false;             {                Condition01 = true;             }             Condition02 = false;             {                Condition02 = true;             }             if ((Condition01) && (Condition02))             {                Backtester.CancelationCode = 88;                if (idx - foundPosition0.EntryBar + 1 >= 5)                {                   ClosePosition(foundPosition0, OrderType.Market, 0,"Sell after 5 bars");                }             }          } } public override void NewWFOInterval(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new CustomSMA(bars.Close,20); }       private TimeSeries indicator1;       private IndicatorBase indicator2; private Transaction _transaction; } }
0
Cone8
 ( 22.68% )
- ago
#17
You have to replace with your CustomSMA (what's that?), but here's what you're trying to do without all the verbosity of Building Block code -

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.MyIndicators; namespace WealthScript9183 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {          StartIndex = 20;       }       public override void Initialize(BarHistory bars)       {          _indicator1 = bars.Close;          _indicator2 = new SMA(bars.Close, 20);          PlotIndicator(_indicator2, WLColor.NeonGreen);       }       public override void Execute(BarHistory bars, int idx)       {          // reset to false at the end of the day          if (bars.IsLastBarOfDay(idx))             _premarketCondition = false;                    if (!HasOpenPosition(bars, PositionType.Long))          {                         if (bars.DateTimes[idx].GetTime() > 928                && bars.DateTimes[idx].GetTime() < 930                 && _indicator1[idx] < _indicator2[idx])             {                _premarketCondition = true;             }             if (_premarketCondition)             {                if (bars.DateTimes[idx].GetTime() > 1000                 && bars.DateTimes[idx].GetTime() < 1250                 && _indicator1[idx] > _indicator2[idx])                {                   _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");                }             }          }          else          {             Position p = LastOpenPosition;             Backtester.CancelationCode = 88;             if (idx - p.EntryBar + 1 >= 5)             {                ClosePosition(p, OrderType.Market, 0, "Sell after 5 bars");             }          }       }       public override void NewWFOInterval(BarHistory bars)       {          _indicator1 = bars.Close;          _indicator2 = new SMA(bars.Close, 20);       }       // values for variables declared here at the class level are 'remembered' for every new bar       private TimeSeries _indicator1;       private IndicatorBase _indicator2;       private Transaction _transaction;       private bool _premarketCondition = false;    } }
0
- ago
#18
Cone Thanks so much. I was able to make modifications to my code based on your solution to see what I was missing. I was able to see why it didn't work before. Here is the corrected version. Works good now.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.MyIndicators; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          StartIndex = 20; } public override void Initialize(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new CustomSMA(bars.Close,20);          PlotIndicator(indicator2,new WLColor(0,0,0));       } public override void Execute(BarHistory bars, int idx) {          // reset to false at the end of the day          if (bars.IsLastBarOfDay(idx))             Condition01 = false;          int index = idx;          //Position foundPosition0 = FindOpenPosition(0);          //bool Condition01;          //bool Condition02;          //Condition01 = false;                    if (!HasOpenPosition(bars, PositionType.Long))//= in TS if marketPosition = 0 then enter Long          { if ((bars.DateTimes[index].GetTime() > 928)                && (bars.DateTimes[index].GetTime() < 930)                && (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]))             {                Condition01 = true;             }             //if ((bars.DateTimes[index].GetTime() < 928)                //&& (bars.DateTimes[index].GetTime() > 930)                //&& (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]))             //{                //Condition01 = false;             //}                                    //if (Condition01 == true)             if (Condition01)             {                if ((bars.DateTimes[index].GetTime() > 1000)                   && (bars.DateTimes[index].GetTime() < 1100)                   && (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]))                {                   _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");                }             }                       }          else          {             //Condition01 = false;             //{                //Condition01 = true;             //}             //Condition02 = false;             //{                //Condition02 = true;             //}             //if ((Condition01) && (Condition02))             //{             Position foundPosition0 = LastOpenPosition;                Backtester.CancelationCode = 88;                if (idx - foundPosition0.EntryBar + 1 >= 5)                {                   ClosePosition(foundPosition0, OrderType.Market, 0,"Sell after 5 bars");                }             //}          } } public override void NewWFOInterval(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = new CustomSMA(bars.Close,20); }       private TimeSeries indicator1;       private IndicatorBase indicator2; private Transaction _transaction;       private bool Condition01 = false; } }
0
- ago
#19
BTW I still haven't figured out what is the difference between customSMA and SMA? When you open a Block strategy in C# that contains an SMA indicator it always comes as customSMA. Seems to be the same thing. I could not find any reference to customSMA in the WL documents.
0
Glitch8
 ( 12.83% )
- ago
#20
CustomSMA is what you get when you click the SMA Example button in the Custom Indicator Builder tool. At some point you must have done that and created a CustomSMA using the Indicator Builder. You can delete it by finding it in the My Indicators folder and right clicking it.
0
- ago
#21
Thanks. Yes I did click the SMA Example button a few times. If I delete it, then in the future when I click the SMA Example again will it recreate it again? Also when I include an SMA in a block strategy the C# code shows the CustomSMA as default rather than the SMA.
0
Cone8
 ( 22.68% )
- ago
#22
QUOTE:
If I delete it, then in the future when I click the SMA Example again will it recreate it again?
It will remove the extra "SMA" indicator from your MyIndicators folder. There's no need for it.

QUOTE:
Also when I include an SMA in a block strategy the C# code shows the CustomSMA as default rather than the SMA.
It won't if you delete it.

CustomSMA is just a code example of how to create a custom indicator. Unfortunately, the abbreviation in the code is "SMA", which as you'll notice, causes 2 "SMA" indicators to show up in the B.Block's list of Indicators.
0

Reply

Bookmark

Sort