-4
public bool BreakOvHigh()
{
        if(_OpeningRange.PreSessionHigh[0] < _RegularSessionRange.OpeningRangeHigh[0])
        {
        return true;
        }
        return false;

}

if(ToTime(Time[0]) == 161500) {BreakOvHigh();}

I was to run an if statment (above code) inside File.AppendAllText function. How can I do that?

        File.AppendAllText(path, Time[0] + ";" 
    + High[0] + ";" 
    + Low[0] + ";"  
    + Open[0] + ";" 
    + Close[0] + ";" 
    + Volume[0] + ";"
     /*+ _CurrentDay.CurrentHigh[0] + ";" + _CurrentDay.CurrentLow[0] + ";" + _CurrentDay.CurrentOpen[0] + ";" + PriorDayOHLC().PriorHigh[0].ToString() + ";" + PriorDayOHLC().PriorLow[0].ToString() + ";" + PriorDayOHLC().PriorOpen[0].ToString() + ";" + PriorDayOHLC().PriorClose[0].ToString() + ";" */
    + _ASIARegularSessionRange.PreSessionHigh[0].ToString() 
        + ";" + _ASIARegularSessionRange.PreSessionMid[0].ToString() + ";" 
        + _ASIARegularSessionRange.PreSessionLow[0].ToString() + ";" 
        + _OpeningRange.PreSessionHigh[0].ToString() + ";" + _OpeningRange.PreSessionMid[0].ToString() + ";" 
        + _OpeningRange.PreSessionLow[0].ToString() + ";" + _OpeningRange.OpeningRangeHigh[0].ToString() + ";" 
        + _OpeningRange.OpeningRangeMid[0].ToString() + ";" 
        + _OpeningRange.OpeningRangeLow[0].ToString() + ";" 
        + _RegularSessionRange.OpeningRangeHigh[0].ToString() + ";" 
        + _RegularSessionRange.OpeningRangeMid[0].ToString() + ";" 
        + _RegularSessionRange.OpeningRangeLow[0].ToString() + ";" 
+ _VWAPValue.VWAP[0].ToString() + ";" 
+ if(ToTime(Time[0]) == 161500) {BreakOvHigh();} /*+ ";" 
+ PlotsIndi1_Values + ";" 
+ PlotsIndi2_Values */ + Environment.NewLine);
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
Isak La Fleur
  • 4,428
  • 7
  • 34
  • 50
  • What should happen to your file `if(ToTime(Time[0]) == 161500)`? – Dour High Arch May 26 '20 at 17:02
  • @DourHighArch I want to run the method BreakOvHigh() if the if statment is true.. but it looks like I can't run a if statment when I construct this string. Do you have any idea how I can add this if statment there? – Isak La Fleur May 26 '20 at 17:09
  • @IsakLaFleur - Does `BreakOvHigh` return a string that you're trying to append, or is it meant to do other "stuff"? – Broots Waymb May 26 '20 at 17:10
  • 1
    Use the [StringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=netcore-3.1) to create/generate/format the output and never concatenate strings like that. Remember that `string` is immutable object. Please read [this](https://stackoverflow.com/q/2365272/10216583) –  May 26 '20 at 17:12
  • 2
    Is this something you can do with the conditional operator, like ToTime() == 161500 ? BreakOvHigh() : BreakIvLow() – Brent Ellingson May 26 '20 at 17:13
  • BreakOvHigh, return a boolean. I have tested the code and it works fine if I just use the BreakOvHigh in the function, but when I try to add a if statment it breaks. – Isak La Fleur May 26 '20 at 17:13
  • 1
    You didn't answer my question; “I want to run the method BreakOvHigh()” does not tell us what happens to your file. Does `BreakOvHigh` do something to your file? You need to tell us what it does. – Dour High Arch May 26 '20 at 17:19
  • @DourHighArch I added that part in the code. – Isak La Fleur May 26 '20 at 17:29
  • `BreakOvHigh()` returns a `bool`? *That's it?* `if (true) {true;}`? What? – gravity May 26 '20 at 17:33
  • @gravity, it return a bool IF current price breaks a previous price level and I only want to check this at a certain time, 16:15:00.. – Isak La Fleur May 26 '20 at 17:38
  • That doesn't dismiss the fact that your `if` statement, fundamentally, didn't really *do* anything - it ran `BreakOvHigh` but that was it. – gravity May 26 '20 at 17:44
  • 1
    There's seems to be a fundamental misunderstanding of the language here. This monstrosity of code is not running "inside" of `File.AppendAllText()`; it is constructing the parameter being _passed to_ `File.AppendAllText()`. There's no reason this needs to be done within the `()` of `File.AppendAllText()`, let alone all on one line. And an `if` statement simply cannot be part of another expression. Note the operator listed in the ["See also" section of the documentation](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/if-else#see-also). – Lance U. Matthews May 26 '20 at 17:51
  • @gravity, it only run the BreakOvHigh IF the time is 16:45:00. – Isak La Fleur May 26 '20 at 18:16
  • @BACON, ok, I understand now I cant run a if statment inside File.AppendAllText(). What is the best solution to solve this then? – Isak La Fleur May 26 '20 at 18:21
  • But... that does nothing except return `true` or `false` - either way, *nothing substantial happens* (within your original code). @BACON has made several points as to why this isn't a good Q&A - as it's not likely to help anyone else, and in fact may confuse people into bad coding practices. Your answer further confuses the *idea* you're trying to accomplish, as well. – gravity May 26 '20 at 18:34

1 Answers1

-3

To get the code running I had to change the bool to string and not run a if statment inside the File.AppendAllText... It not beautiful, but works..

public string BreakOvHigh()
{
        if(_OpeningRange.PreSessionHigh[0] < _RegularSessionRange.OpeningRangeHigh[0])
        {
        return "1";
        }
        return "0";

}


if(ToTime(Time[0]) == 161500) {
                breakovHigh = BreakOvHigh();
            } else {
                breakovHigh = "";
            }

File.AppendAllText(path, Time[0] + ";" + High[0] + ";" + Low[0] + ";"  + Open[0] + ";" + Close[0] + ";" + Volume[0] 
                + ";" /*+ _CurrentDay.CurrentHigh[0] + ";" + _CurrentDay.CurrentLow[0] + ";" + _CurrentDay.CurrentOpen[0] + ";" + PriorDayOHLC().PriorHigh[0].ToString() + ";" + PriorDayOHLC().PriorLow[0].ToString() + ";" + PriorDayOHLC().PriorOpen[0].ToString() + ";" + PriorDayOHLC().PriorClose[0].ToString() + ";" */+ _ASIARegularSessionRange.PreSessionHigh[0].ToString() + ";" + _ASIARegularSessionRange.PreSessionMid[0].ToString() + ";" + _ASIARegularSessionRange.PreSessionLow[0].ToString() + ";" + _OpeningRange.PreSessionHigh[0].ToString() + ";" + _OpeningRange.PreSessionMid[0].ToString() + ";" + _OpeningRange.PreSessionLow[0].ToString() + ";" + _OpeningRange.OpeningRangeHigh[0].ToString() + ";" + _OpeningRange.OpeningRangeMid[0].ToString() + ";" + _OpeningRange.OpeningRangeLow[0].ToString() + ";" + _RegularSessionRange.OpeningRangeHigh[0].ToString() + ";" + _RegularSessionRange.OpeningRangeMid[0].ToString() + ";" + _RegularSessionRange.OpeningRangeLow[0].ToString() + ";" + _VWAPValue.VWAP[0].ToString() + ";" + breakovHigh /*+ ";" + PlotsIndi1_Values + ";" + PlotsIndi2_Values */ + Environment.NewLine);
Isak La Fleur
  • 4,428
  • 7
  • 34
  • 50