privateOnBalanceVolume_onBalanceVolume;[Parameter]publicDataSeriesSource{get;set;}[Output("Main")]publicIndicatorDataSeriesResult{get;set;}protectedoverridevoidInitialize(){_onBalanceVolume=Indicators.OnBalanceVolume(Source);}publicoverridevoidCalculate(intindex){// Display Result of IndicatorResult[index]=_onBalanceVolume.Result[index];}
usingcAlgo.API;usingcAlgo.API.Indicators;namespacecAlgo.Robots{// This sample cBot shows how to use the On Balance Volume indicator[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassOnBalanceVolumeSample:Robot{privatedouble_volumeInUnits;privateOnBalanceVolume_onBalanceVolume;privateSimpleMovingAverage_simpleMovingAverage;[Parameter("Volume (Lots)", DefaultValue = 0.01)]publicdoubleVolumeInLots{get;set;}[Parameter("Stop Loss (Pips)", DefaultValue = 10)]publicdoubleStopLossInPips{get;set;}[Parameter("Take Profit (Pips)", DefaultValue = 10)]publicdoubleTakeProfitInPips{get;set;}[Parameter("Label", DefaultValue = "Sample")]publicstringLabel{get;set;}publicPosition[]BotPositions{get{returnPositions.FindAll(Label);}}protectedoverridevoidOnStart(){_volumeInUnits=Symbol.QuantityToVolumeInUnits(VolumeInLots);_onBalanceVolume=Indicators.OnBalanceVolume(Bars.ClosePrices);_simpleMovingAverage=Indicators.SimpleMovingAverage(_onBalanceVolume.Result,9);}protectedoverridevoidOnBar(){if(_onBalanceVolume.Result.HasCrossedAbove(_simpleMovingAverage.Result,0)){ClosePositions(TradeType.Sell);ExecuteMarketOrder(TradeType.Buy,SymbolName,_volumeInUnits,Label,StopLossInPips,TakeProfitInPips);}elseif(_onBalanceVolume.Result.HasCrossedBelow(_simpleMovingAverage.Result,0)){ClosePositions(TradeType.Buy);ExecuteMarketOrder(TradeType.Sell,SymbolName,_volumeInUnits,Label,StopLossInPips,TakeProfitInPips);}}privatevoidClosePositions(TradeTypetradeType){foreach(varpositioninBotPositions){if(position.TradeType!=tradeType)continue;ClosePosition(position);}}}}