Is there a way to auto-align parts of code within a line of code? - formatting

Let's say I have to write code = 0, on 100 lines of code, and the code varies in character length, but you want all the = 0 to be at the same character length.
So to go from this:
one = 1,
onetwo = 12,
onetwothree = 123
To this
one = 1,
onetwo = 12,
onetwothree = 123

Related

In pine script code, how can I draw line between 2 time ranges (9:20PM to 3:20PM) between 2 values

I am writing some pine script code to draw line between 2 time ranges (starting line from 9:20PM to end line at 3:20PM) also between 2 values (ex: 38000 to 38200). How can I code it? we can achieve using line tool in tradingview, but I want it in pine script.
I tried the below code, but it is not working as expected. The line should be like trendline not horizontal line.
indicator(title="Line", shorttitle="Line", overlay=true)
starttime = (hour(time) == 09 and minute(time) == 25)
endtime = (hour(time) == 15 and minute(time) == 25)
s_closeup200 := 38000
s_closedown200 := 38200
line_x1 = ta.valuewhen(time==starttime,time, 0)
line_x2 = ta.valuewhen((hour(time) == 09 and minute(time) == 25), s_closeup200,0)
line_x3 = ta.valuewhen(time==endtime, time, 0)
line_x4 = ta.valuewhen((hour(time) == 15 and minute(time) == 25), s_closedown200, 0)
line.new(x1 = line_x1, y1 = line_x2, x2 = line_x3, y2 = line_x4, xloc = xloc.bar_time)

Pine script setting timestamp for last quarter not working

I wrote a simple strategy in pine scrip, which is based on crossover/crossunder for two different SMAs.
It is important for me to test my strategy in some time frames, especially in the last quarter. It doesn't work. I got only a few results. I should get results for whole period (colored on green), when SMAs crossed.
Unwanted result
My script is working very well, when I don't use time range or when I set dateCond on true value.
Below I present source code:
//#version=4
strategy("Moving Average Cross 1", initial_capital=1000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)
strategy.close("long", when = short)
strategy.close("short", when = long)
I tried with different time ranges (when I set start date on the 1 January 2020 it works very well). I additionally colored a background to check condition, but coloring it also work good. I haven't more ideas why for the last quarter it isn't working properly. I will appreciate any help.
I tested script mainly for pair ETH/USDT (Binance)
Here we use larger capital and close positions before entering a new one. This way the whole position is closed before a new one is opened:
//#version=4
strategy("Moving Average Cross 1", initial_capital=100000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
strategy.close("long", when = short)
strategy.close("short", when = long)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)

Is there a way to sort a list of numbers whilst maintaining their original indexes in VB.net?

I need to sort a list of numbers but I need to keep their initial indexes.
I had previously just created an array of the numbers and then another array of the indexes which I sorted at the same time like so:
Dim AverageSuccess(23) As Decimal
Dim intervalList() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
x = 2
Do
Sorted = True
For i = 23 To x Step -1
If AverageSuccess(i) < AverageSuccess(i - 1) Then
TempNum = AverageSuccess(i)
AverageSuccess(i) = AverageSuccess(i - 1)
AverageSuccess(i - 1) = TempNum
TempIndex = intervalList(i)
intervalList(i) = intervalList(i - 1)
intervalList(i - 1) = TempIndex
Sorted = False
End If
Next
x += 1
Loop Until Sorted
however this is for a project and my teacher informed me that this is bad programming practise and I should be using a list instead.
I have struggled to find a simple example of how to use a list in VB.net for this purpose, so if someone could give me an example I would appreciate it.
I don't know how much you have covered about lists in class...
Let us create a list with some integers in:
Dim nums As New List(Of Integer) From {9, 8, 4, 5}
Now, we want to store the original indices of those numbers. We can do that with the Select method, which has an optional parameter that will give the index of the current item, and create a new entity with items which we can give names to, say "Num" and "Idx":
Dim numsWithIndex = nums.Select(Function(n, i) New With {.Num = n, .Idx = i})
Then we can use the LINQ method OrderBy to get those entities in the desired order:
Dim sortedNums = numsWithIndex.OrderBy(Function(nwi) nwi.Num)
And we can have a look at what we have constructed with
Console.WriteLine(String.Join(vbCrLf, sortedNums))
which outputs:
{ Num = 4, Idx = 2 }
{ Num = 5, Idx = 3 }
{ Num = 8, Idx = 1 }
{ Num = 9, Idx = 0 }
(It shows the names we gave to the properties of the anonymous type created with New earlier.)
Here is the whole thing as a console app you can copy-and-paste to investigate with on your computer:
Module Module1
Sub Main()
Dim nums As New List(Of Integer) From {9, 8, 4, 5}
Dim numsWithIndex = nums.Select(Function(n, i) New With {.Num = n, .Idx = i})
Dim sortedNums = numsWithIndex.OrderBy(Function(nwi) nwi.Num)
Console.WriteLine(String.Join(vbCrLf, sortedNums))
Console.ReadLine()
End Sub
End Module

vb.net chart with week numbers as X Axis

I know a similar question was asked before, but it wasn't answered.
I have a chart where I use week numbers as its X-axis, that are retrieved as part of the SQL query.
It works fine up until a new year starts. In such a case, even though the weeks are ordered correctly when retrieved (for example, 49, 50, 51, 52, 1, 2, 3), they appear on the axis numerically ordered: 1, 2, 3, 49, 50, 51, 52.
Is there a way to fix that?
The relevant sql query part is:
SELECT DATEPART(year, start_charge_time),
DATEPART(week, start_charge_time) week_num,
COUNT(*) num_sessions
FROM parking_log
GROUP BY DATEPART(year, start_charge_time),
DATEPART(week, start_charge_time)
ORDER BY DATEPART(year, start_charge_time),
DATEPART(week, start_charge_time)
The Chart is:
Dim SeriesParkingRev As Series
SeriesParkingRev = ChartParkingSummery.Series("SeriesParkingRev")
' Set series chart type
SeriesParkingRev.ChartType = SeriesChartType.Line
SeriesParkingRev.MarkerStyle = MarkerStyle.Square
SeriesParkingRev.MarkerSize = 10
SeriesParkingRev.BorderWidth = 3
SeriesParkingRev.Color = Color.Red
SeriesParkingRev.IsValueShownAsLabel = True
SeriesParkingRev.IsVisibleInLegend = True
'' Set series members names for the X and Y values
SeriesParkingRev.XValueMember = "week_num"
SeriesParkingRev.YValueMembers = "total_charge"
SeriesParkingRev.LegendText = "Parking Revenue"
'' --------------------------
' Create the destination series and add it to the chart
'Dim SeriesParkingTime As New Series("SeriesParkingTime")
'ChartParkingSummery.Series.Add(SeriesParkingTime)
Dim SeriesParkingTime As Series
SeriesParkingTime = ChartParkingSummery.Series("SeriesParkingTime")
' Ensure the destination series is a Line or Spline chart type
SeriesParkingTime.ChartType = SeriesChartType.Line
SeriesParkingTime.MarkerStyle = MarkerStyle.Diamond
SeriesParkingTime.MarkerSize = 12
SeriesParkingTime.BorderWidth = 3
SeriesParkingTime.IsValueShownAsLabel = True
SeriesParkingTime.Color = Color.Blue
' Assign the series to the same chart area as the column chart
SeriesParkingTime.ChartArea = ChartParkingSummery.Series("SeriesParkingRev").ChartArea
' Assign this series to use the secondary axis and set its maximum to be 100%
SeriesParkingTime.YAxisType = AxisType.Secondary
ChartParkingSummery.Series("SeriesParkingTime").XValueMember = "week_num"
ChartParkingSummery.Series("SeriesParkingTime").YValueMembers = "avg_time"
ChartParkingSummery.Series("SeriesParkingTime").LegendText = "Average Time"
'' ----------------------------------------
'Dim SeriesCars As New Series("SeriesCars")
'ChartParkingSummery.Series.Add(SeriesCars)
Dim SeriesCars As Series
SeriesCars = ChartParkingSummery.Series("SeriesCars")
SeriesCars.ChartType = SeriesChartType.Point
SeriesCars.MarkerStyle = MarkerStyle.Triangle
SeriesCars.MarkerSize = 8
SeriesCars.BorderWidth = 3
SeriesCars.IsValueShownAsLabel = True
SeriesCars.MarkerStyle = MarkerStyle.Triangle
SeriesCars.MarkerSize = 15
SeriesCars.MarkerColor = Color.Green
ChartParkingSummery.Series("SeriesCars").XValueMember = "week_num"
ChartParkingSummery.Series("SeriesCars").YValueMembers = "num_cars"
ChartParkingSummery.Series("SeriesCars").LegendText = "Cars"
'' ----------------------------------------
ChartParkingSummery.Legends.Add(New Legend("Default"))
' Set legend style
ChartParkingSummery.Legends("Default").LegendStyle = LegendStyle.Table
' Set table style if legend style is Table
' Set legend docking
ChartParkingSummery.Legends("Default").Docking = Docking.Right
' Set legend alignment
ChartParkingSummery.Legends("Default").Alignment = StringAlignment.Center
ChartParkingSummery.Titles.Add("Revenue, Avg Time & Cars")

Looping through a specified array?

I am trying to match the row height of specific rows from one sheet to another, This works if I just remove all lines with rowlist and do For i = 1 to 200, but this takes too long. I only want to match a few row heights and not go through all between 1 and 200. My code is below:
Dim y As Double
Dim i As Long
Dim rowlist() As Variant
rowlist = Array(3, 5, 23, 30)
For i = LBound(rowlist) To UBound(rowlist)
y = Worksheets("Development").Rows(i).RowHeight
Worksheets("Final").Rows(i).RowHeight = y
Next i
When you're setting and using y, use .Rows(rowlist(i)) rather than .Rows(i).
i simply stores the index of the array, not the value, i.e.
i = 0 ; rowlist(i) =3
i = 1 ; rowlist(i)= 5
i = 2 ; rowlist(i)= 23
i = 3 ; rowlist(i)= 30
So you're correct in looping from LBound(rowlist) to UBound(rowlist), you just need to make sure that you're using the values stored in the array within that loop.