1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 | import clr
clr.AddReference("cAlgo.API")
from cAlgo.API import *
from System import Action, TimeSpan, Array, DayOfWeek
import random
class Test():
InitialDescription = "This is custom symbol initial description"
ChangedDescription = "This is custom symbol changed description"
def on_start(self):
self.isLoadMoreEnabled = True
self.customSymbol = None
panel = StackPanel()
addCustomSymbolWithBaseButton = Button()
addCustomSymbolWithBaseButton.Text = "Add custom symbol with Base Symbol"
addCustomSymbolWithBaseButton.Click += self.on_add_custom_symbol_with_base_button_click
panel.AddChild(addCustomSymbolWithBaseButton)
addCustomSymbolWithoutBaseButton = Button()
addCustomSymbolWithoutBaseButton.Text = "Add custom symbol without Base Symbol"
addCustomSymbolWithoutBaseButton.Click += self.on_add_custom_symbol_without_base_button_click
panel.AddChild(addCustomSymbolWithoutBaseButton)
removeCustomSymbolButton = Button()
removeCustomSymbolButton.Text = "Remove custom symbol"
removeCustomSymbolButton.Click += self.on_remove_custom_symbol_button_click
panel.AddChild(removeCustomSymbolButton)
displayChartInfoButton = Button()
displayChartInfoButton.Text = "Display Charts Info"
displayChartInfoButton.Click += self.on_display_chart_info_button_click
panel.AddChild(displayChartInfoButton)
changeDescriptionButton = Button()
changeDescriptionButton.Text = "Change Description"
changeDescriptionButton.Click += self.on_change_description_button_click
panel.AddChild(changeDescriptionButton)
updateSymbolPropertiesButton = Button()
updateSymbolPropertiesButton.Text = "Update Symbol Properties"
updateSymbolPropertiesButton.Click += self.on_update_symbol_properties_button_click
panel.AddChild(updateSymbolPropertiesButton)
updateSymbolBaseAssetButton = Button()
updateSymbolBaseAssetButton.Text = "Update Symbol Base Asset"
updateSymbolBaseAssetButton.Click += self.on_update_symbol_base_asset_button_click
panel.AddChild(updateSymbolBaseAssetButton)
updateSymbolQuoteAssetButton = Button()
updateSymbolQuoteAssetButton.Text = "Update Symbol Quote Asset"
updateSymbolQuoteAssetButton.Click += self.on_update_symbol_quote_asset_button_click
panel.AddChild(updateSymbolQuoteAssetButton)
showSymbolPropertiesButton = Button()
showSymbolPropertiesButton.Text = "Show Symbol Properties"
showSymbolPropertiesButton.Click += lambda _: self.show_symbol_properties()
panel.AddChild(showSymbolPropertiesButton)
showSymbolAssetsButton = Button()
showSymbolAssetsButton.Text = "Show Symbol Assets"
showSymbolAssetsButton.Click += lambda _: self.show_symbol_assets()
panel.AddChild(showSymbolAssetsButton)
pauseOrResumeLoadMoreButton = Button()
pauseOrResumeLoadMoreButton.Text = "Pause / Resume Load More"
pauseOrResumeLoadMoreButton.Click += self.on_pause_or_resume_load_more_button_click
panel.AddChild(pauseOrResumeLoadMoreButton)
showRegularBarsInfoButton = Button()
showRegularBarsInfoButton.Text = "Show Regular Bars Info"
showRegularBarsInfoButton.Click += self.on_show_regular_bars_info_button_click
panel.AddChild(showRegularBarsInfoButton)
addBarsButton = Button()
addBarsButton.Text = "Add Bars For Active Chart Symbol"
addBarsButton.Click += self.on_add_bars_button_click
panel.AddChild(addBarsButton)
removeBarsButton = Button()
removeBarsButton.Text = "Remove Bars For Active Chart Symbol"
removeBarsButton.Click += self.on_remove_bars_button_click
panel.AddChild(removeBarsButton)
updateLastBarButton = Button()
updateLastBarButton.Text = "Update Last Bar"
updateLastBarButton.Click += self.on_update_last_bar_button_click
panel.AddChild(updateLastBarButton)
updateSymbolSentimentButton = Button()
updateSymbolSentimentButton.Text = "Update Symbol Sentiment"
updateSymbolSentimentButton.Click += self.on_update_symbol_sentiment_button_click
panel.AddChild(updateSymbolSentimentButton)
showSymbolSentimentButton = Button()
showSymbolSentimentButton.Text = "Show Symbol Sentiment"
showSymbolSentimentButton.Click += lambda _: self.show_symbol_sentiment()
panel.AddChild(showSymbolSentimentButton)
updateSymbolDynamicLeverageButton = Button()
updateSymbolDynamicLeverageButton.Text = "Update Symbol Dynamic Leverage"
updateSymbolDynamicLeverageButton.Click += self.on_update_symbol_dynamic_leverage_button_click
panel.AddChild(updateSymbolDynamicLeverageButton)
showSymbolDynamicLeverageButton = Button()
showSymbolDynamicLeverageButton.Text = "Show Symbol Dynamic Leverage"
showSymbolDynamicLeverageButton.Click += lambda _: self.show_symbol_dynamic_leverage()
panel.AddChild(showSymbolDynamicLeverageButton)
updateSymbolHolidaysButton = Button()
updateSymbolHolidaysButton.Text = "Update Symbol Holidays"
updateSymbolHolidaysButton.Click += self.on_update_symbol_holidays_button_click
panel.AddChild(updateSymbolHolidaysButton)
showSymbolHolidaysButton = Button()
showSymbolHolidaysButton.Text = "Show Symbol Holidays"
showSymbolHolidaysButton.Click += self.on_show_symbol_holidays_button_click
panel.AddChild(showSymbolHolidaysButton)
updateSymbolSessionsButton = Button()
updateSymbolSessionsButton.Text = "Update Symbol Sessions"
updateSymbolSessionsButton.Click += self.on_update_symbol_sessions_button_click
panel.AddChild(updateSymbolSessionsButton)
showSymbolSessionsButton = Button()
showSymbolSessionsButton.Text = "Show Symbol Sessions"
showSymbolSessionsButton.Click += self.on_show_symbol_sessions_button_click
panel.AddChild(showSymbolSessionsButton)
updateSymbolFuturesSettingsButton = Button()
updateSymbolFuturesSettingsButton.Text = "Update Symbol Futures Settings"
updateSymbolFuturesSettingsButton.Click += self.on_update_symbol_futures_settings_button_click
panel.AddChild(updateSymbolFuturesSettingsButton)
showSymbolFuturesSettingsButton = Button()
showSymbolFuturesSettingsButton.Text = "Show Symbol Futures Settings"
showSymbolFuturesSettingsButton.Click += self.on_show_symbol_futures_settings_button_click
panel.AddChild(showSymbolFuturesSettingsButton)
block = api.Asp.SymbolTab.AddBlock("Custom symbol Test")
block.Child = panel
block.Height = 550
api.ChartManager.FramesAdded += self.on_chart_manager_frames_addded
api.Symbols.Added += self.on_symbol_added
api.Symbols.Removed += self.on_symbol_removed
def on_add_custom_symbol_with_base_button_click(self, args):
baseSymbol = api.Symbols.GetSymbol("EURUSD")
self.customSymbol = api.CustomSymbols.Add("Custom Symbol With Base", baseSymbol)
self.customSymbol.Symbol.Sentiment.Updated += self.on_symbol_sentiment_updated
self.customSymbol.Description = self.InitialDescription
self.customSymbol.BarsNeeded = Action[CustomSymbolBarsNeededArgs](self.on_custom_symbol_bars_needed)
baseSymbol.Tick += self.on_base_symbol_tick
self.customSymbol.UpdateQuote(baseSymbol.Bid, baseSymbol.Ask)
print(f"custom symbol '{self.customSymbol.Name}' has been added.")
def on_add_custom_symbol_without_base_button_click(self, args):
baseAsset = api.Account.Asset
quoteAsset = api.Assets.GetAsset(next((assetName for assetName in api.Assets if assetName != baseAsset.Name)))
self.customSymbol = api.CustomSymbols.Add("Custom Symbol Without Base", baseAsset, quoteAsset)
self.customSymbol.Symbol.Sentiment.Updated += self.on_symbol_sentiment_updated
self.customSymbol.Description = self.InitialDescription
self.customSymbol.BarsNeeded = Action[CustomSymbolBarsNeededArgs](self.on_custom_symbol_bars_needed)
trackingSymbol = api.Symbols.GetSymbol("EURUSD")
trackingSymbol.Tick += self.on_base_symbol_tick
self.customSymbol.UpdateQuote(trackingSymbol.Bid, trackingSymbol.Ask)
print(f"custom symbol '{self.customSymbol.Name}' has been added.")
def on_symbol_sentiment_updated(self, args):
print(f"SentimentUpdated, Buy: {args.Sentiment.BuyPercentage} | Sell: {args.Sentiment.SellPercentage}")
def on_custom_symbol_bars_needed(self, args):
print(f"Bars Needed for custom symbol, Symbol: {args.CustomBars.Symbol}, Name: {args.CustomSymbol.Name}")
self.customBars = args.CustomBars
api.MarketData.GetBarsAsync(args.CustomBars.TimeFrame, "EURUSD", Action[Bars](self.on_bars_received))
def on_bars_received(self, bars):
print(f"Bars Loaded: {bars.Count}")
api.Sleep(2000)
self.customBars.AppendBars(self.to_array([CustomBar(bar.OpenTime, bar.Open, bar.High, bar.Low, bar.Close, int(bar.TickVolume)) for bar in bars], CustomBar))
self.customBars.NeedsMore += lambda args: self.on_custom_bars_needs_more(bars, args.CustomBars)
bars.BarOpened += lambda args: self.on_bar_opened(self.customBars, args.Bars)
bars.Tick += lambda args: self.on_tick(self.customBars, args.Bars)
def on_custom_bars_needs_more(self, bars, customBars):
if self.isLoadMoreEnabled is False:
print("Needs more requested but load more is disabled")
return
print("Bars NeedsMore")
bars.LoadMoreHistoryAsync(Action[BarsHistoryLoadedEventArgs](lambda args: self.on_bars_more_history_loaded(customBars, bars, args.Count)))
def on_bars_more_history_loaded(self, customBars, bars, loadedCount):
print(f"Bars Loaded more: {loadedCount}")
if loadedCount == 0:
return
api.Sleep(2000)
loadedBars = []
for i, bar in enumerate(bars):
if i >= loadedCount:
break
loadedBars.append(CustomBar(bar.OpenTime, bar.Open, bar.High, bar.Low, bar.Close, int(bar.TickVolume)))
customBars.PrependBars(self.to_array(loadedBars, CustomBar))
def on_bar_opened(self, customBars, bars):
if self.customSymbol is None:
return
customBars.AppendBar(CustomBar(bars.LastBar.OpenTime, bars.LastBar.Open, bars.LastBar.High, bars.LastBar.Low, bars.LastBar.Close, bars.LastBar.TickVolume))
def on_tick(self, customBars, bars):
if self.customSymbol is None:
return
customBars.UpdateLastBar(bars.LastBar.Open, bars.LastBar.High, bars.LastBar.Low, bars.LastBar.Close, bars.LastBar.TickVolume)
def on_base_symbol_tick(self, args):
if self.customSymbol is None:
args.Symbol.Tick -= self.on_base_symbol_tick
return
self.customSymbol.UpdateQuote(args.Symbol.Bid, args.Symbol.Ask)
def on_remove_custom_symbol_button_click(self, args):
if api.CustomSymbols.Remove(self.customSymbol):
return
print(f"custom symbol '{self.customSymbol.Name}' has been removed.")
self.customSymbol = None
def on_display_chart_info_button_click(self, args):
for frame in api.ChartManager:
if isinstance(frame.__implementation__, ChartFrame) == False:
continue
chartFrame = ChartFrame(frame)
print(f"Chart, {chartFrame.Symbol.Name} {chartFrame.TimeFrame.Name}")
def on_change_description_button_click(self, args):
self.customSymbol.Description = self.ChangedDescription if self.customSymbol.Description == self.InitialDescription else self.InitialDescription
print(f"Changed description to: {self.customSymbol.Description}")
def on_update_symbol_properties_button_click(self, args):
self.customSymbol.PipDigits = self.customSymbol.PipDigits + 1
self.customSymbol.TickDigits = self.customSymbol.TickDigits + 1
self.customSymbol.VolumeInUnitsMin = self.customSymbol.VolumeInUnitsMin - 10
self.customSymbol.VolumeInUnitsStep = self.customSymbol.VolumeInUnitsStep + 10
self.customSymbol.VolumeInUnitsMax = self.customSymbol.VolumeInUnitsMax + 10
self.customSymbol.Commission = self.customSymbol.Commission + 1
self.customSymbol.GracePeriod = self.customSymbol.GracePeriod + 1
self.customSymbol.LotSize = self.customSymbol.LotSize * 2
self.customSymbol.PnLConversionFeeRate = self.customSymbol.PnLConversionFeeRate + 1
self.customSymbol.CommissionType = SymbolCommissionType.PercentageOfTradingVolume
self.customSymbol.MinCommission = self.customSymbol.MinCommission + 1
self.customSymbol.MinCommissionType = SymbolMinCommissionType.QuoteAsset
self.customSymbol.AdministrativeCharge3DaysRollover = DayOfWeek.Thursday
self.customSymbol.AdministrativeCharge = self.customSymbol.AdministrativeCharge + 1
self.customSymbol.SwapPeriod = self.customSymbol.SwapPeriod + 1
self.customSymbol.WeekendSwaps = not self.customSymbol.WeekendSwaps
self.customSymbol.SwapLong = self.customSymbol.SwapLong + 1
self.customSymbol.SwapShort = self.customSymbol.SwapShort + 1
self.customSymbol.SwapTimeInMinutes = self.customSymbol.SwapTimeInMinutes + 1
self.customSymbol.Swap3DaysRollover = DayOfWeek.Tuesday
self.customSymbol.SwapCalculationType = SymbolSwapCalculationType.Percentage
self.customSymbol.MinDistanceType = SymbolMinDistanceType.Pips
self.customSymbol.MinTakeProfitDistance = self.customSymbol.MinTakeProfitDistance + 1
self.customSymbol.MinStopLossDistance = self.customSymbol.MinStopLossDistance + 1
self.show_symbol_properties()
def to_array(self, iterable, itemType):
length = len(iterable)
array = Array[itemType](length)
for i in range(length):
array[i] = iterable[i]
return array
def show_symbol_properties(self):
for propertyInfo in self.customSymbol.Symbol.GetType().GetProperties():
print(f"{propertyInfo.Name}: {propertyInfo.GetValue(self.customSymbol.Symbol)}")
def on_update_symbol_base_asset_button_click(self, args):
self.customSymbol.BaseAsset = api.Assets.GetAsset(next((assetName for assetName in api.Assets if assetName != self.customSymbol.BaseAsset.Name and assetName != self.customSymbol.QuoteAsset.Name)))
self.show_symbol_assets()
def on_update_symbol_quote_asset_button_click(self, args):
self.customSymbol.QuoteAsset = api.Assets.GetAsset(next((assetName for assetName in api.Assets if assetName != self.customSymbol.BaseAsset.Name and assetName != self.customSymbol.QuoteAsset.Name)))
self.show_symbol_assets()
def show_symbol_assets(self):
print(f"Base Asset: {self.customSymbol.Symbol.BaseAsset.Name} | Quote Asset: {self.customSymbol.Symbol.QuoteAsset.Name}")
def on_pause_or_resume_load_more_button_click(self, args):
self.isLoadMoreEnabled = not self.isLoadMoreEnabled
def on_show_regular_bars_info_button_click(self, args):
print(f"Regular Bars, Count: {self.customBars.Bars.Count}, LastBar: {self.customBars.Bars.LastBar}, FirstBar: {self.customBars.Bars[0]}")
def on_add_bars_button_click(self, args):
if isinstance(api.ChartManager.ActiveFrame.__implementation__, ChartFrame) == False:
return
chart = ChartFrame(api.ChartManager.ActiveFrame).Chart
bars = self.customSymbol.AddBars(chart.TimeFrame)
print(f"Added Bars, Count: {bars.Bars.Count}, Get: {bars == self.customSymbol.GetBars(chart.TimeFrame)}")
def on_remove_bars_button_click(self, args):
if isinstance(api.ChartManager.ActiveFrame.__implementation__, ChartFrame) == False:
return
chart = ChartFrame(api.ChartManager.ActiveFrame).Chart
result = self.customSymbol.RemoveBars(chart.TimeFrame)
print(f"Removed Bars, Result: {result}, Get is None: {self.customSymbol.GetBars(chart.TimeFrame) is None}")
def on_update_last_bar_button_click(self, args):
changeAmount = (self.customBars.Bars.LastBar.High - self.customBars.Bars.LastBar.Low) * (random.random() + 0.5) * 2
print(f"Changing Last Bar: {self.customBars.Bars.LastBar} | change: {changeAmount}")
bars = self.customBars.Bars
self.customBars.UpdateLastBar(bars.LastBar.Open, bars.LastBar.Open + changeAmount, bars.LastBar.Open - changeAmount, bars.LastBar.Close + changeAmount, int(bars.LastBar.TickVolume + 1))
def on_update_symbol_sentiment_button_click(self, args):
self.customSymbol.Sentiment.BuyPercentage = self.customSymbol.Sentiment.BuyPercentage + 1 if self.customSymbol.Sentiment.BuyPercentage < 99 else self.customSymbol.Sentiment.BuyPercentage - 1
def show_symbol_sentiment(self):
print(f"Buy: {self.customSymbol.Symbol.Sentiment.BuyPercentage} | Sell: {self.customSymbol.Symbol.Sentiment.SellPercentage}")
def on_update_symbol_dynamic_leverage_button_click(self, args):
self.customSymbol.DynamicLeverage.Add(CustomSymbolLeverageTier(random.random() * 100 + 1000, 100 + random.randint(0, 100)))
def show_symbol_dynamic_leverage(self):
print(f"Dynamic Leverage Count: {self.customSymbol.Symbol.DynamicLeverage.Count}")
for leverage in self.customSymbol.Symbol.DynamicLeverage:
print(f"Volume: {leverage.Volume} | Leverage: {leverage.Leverage}")
def on_update_symbol_holidays_button_click(self, args):
startTime = api.Server.Time.AddMonths(random.randint(1, 3))
endTime = startTime.AddDays(1)
isRecurring = random.randint(0, 1) > 0
self.customSymbol.MarketHours.Holidays.Add(CustomSymbolTradingHoliday(f"Test {self.customSymbol.MarketHours.Holidays.Count}", startTime, endTime, isRecurring))
def on_show_symbol_holidays_button_click(self, args):
print(f"Holidays Count: {self.customSymbol.Symbol.MarketHours.Holidays.Count}")
for holiday in self.customSymbol.Symbol.MarketHours.Holidays:
print(f"Name: {holiday.Name} | Start: {holiday.StartTime} | End: {holiday.EndTime} | Is Recurring: {holiday.IsRecurring}")
def on_update_symbol_sessions_button_click(self, args):
startDay = DayOfWeek(random.randint(0, 6))
endDay = DayOfWeek(int(startDay) + 1)
startTime = TimeSpan.FromHours(random.randint(0, 22))
endTime = startTime.Add(TimeSpan.FromHours(1))
self.customSymbol.MarketHours.Sessions.Add(CustomSymbolTradingSession(startDay, endDay, startTime, endTime))
def on_show_symbol_sessions_button_click(self, args):
print(f"Sessions Count: {self.customSymbol.Symbol.MarketHours.Sessions.Count}")
for session in self.customSymbol.Symbol.MarketHours.Sessions:
print(f"Start Day: {session.StartDay} | End Day: {session.EndDay} | Start Time: {session.StartTime} | End Time: {session.EndTime}")
def on_update_symbol_futures_settings_button_click(self, args):
self.customSymbol.FuturesSettings = CustomSymbolFuturesSettings(api.Server.Time, api.Server.Time.AddMonths(random.randint(1, 10)), random.random() * 100 + 1000)
def on_show_symbol_futures_settings_button_click(self, args):
symbol = self.customSymbol.Symbol
print(f"Last Trade Time: {symbol.FuturesSettings.LastTradeTime} | Expiration Time: {symbol.FuturesSettings.ExpirationTime} | Maintenance Margin: {symbol.FuturesSettings.MaintenanceMargin}")
def on_chart_manager_frames_addded(self, args):
for frame in args.AddedFrames:
if isinstance(frame.__implementation__, ChartFrame) == False:
continue
chartFrame = ChartFrame(frame)
print(f"New chart frame added, {chartFrame.Symbol.Name} {chartFrame.TimeFrame.Name}")
def on_symbol_added(self, args):
print(f"Symbol Added: {args.SymbolName}")
def on_symbol_removed(self, args):
print(f"Symbol Removed: {args.SymbolName}")
|