usingcAlgo.API;usingSystem;usingSystem.Linq;namespacecAlgo.Robots{/// /// This sample shows how to close a position///[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassPositionClosingSample:Robot{[Parameter("Position Comment")]publicstringPositionComment{get;set;}[Parameter("Position Label")]publicstringPositionLabel{get;set;}protectedoverridevoidOnStart(){Positionposition=null;if(!string.IsNullOrWhiteSpace(PositionComment)&&!string.IsNullOrWhiteSpace(PositionLabel)){position=Positions.FindAll(PositionLabel).FirstOrDefault(iOrder=>string.Equals(iOrder.Comment,PositionComment,StringComparison.OrdinalIgnoreCase));}elseif(!string.IsNullOrWhiteSpace(PositionComment)){position=Positions.FirstOrDefault(iOrder=>string.Equals(iOrder.Comment,PositionComment,StringComparison.OrdinalIgnoreCase));}elseif(!string.IsNullOrWhiteSpace(PositionLabel)){position=Positions.Find(PositionLabel);}if(position==null){Print("Couldn't find the position, please check the comment and label");Stop();}ClosePosition(position);}}}
usingcAlgo.API;namespacecAlgo.Robots{/// /// This sample shows how to handle position events///[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassPositionEventsSample:Robot{protectedoverridevoidOnStart(){Positions.Opened+=Positions_Opened;Positions.Closed+=Positions_Closed;Positions.Modified+=Positions_Modified;}privatevoidPositions_Modified(PositionModifiedEventArgsobj){varmodifiedPosition=obj.Position;}privatevoidPositions_Closed(PositionClosedEventArgsobj){varclosedPosition=obj.Position;varcloseReason=obj.Reason;}privatevoidPositions_Opened(PositionOpenedEventArgsobj){varopenedPosition=obj.Position;}}}
123456
defon_start(self):forpositioninapi.Positions:print(f"Position Label {position.Label}")print(f"Position ID {position.Id}")print(f"Profit {position.GrossProfit}")print(f"Entry Price {position.EntryPrice}")
1 2 3 4 5 6 7 8 9101112131415161718192021222324
importclrclr.AddReference("cAlgo.API")# Import cAlgo API typesfromcAlgo.APIimport*# Import trading wrapper functionsfromrobot_wrapperimport*fromSystemimportActionclassPositionExecutionSample():defon_start(self):volumeInUnits=api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)distanceInPips=api.DistanceInPips*api.Symbol.PipSizestopLoss=Noneifapi.StopInPips==0elseapi.StopInPipstakeProfit=Noneifapi.TargetInPips==0elseapi.TargetInPipson_completed_action=Action[TradeResult](self.on_completed)ifapi.IsAsync:api.ExecuteMarketOrderAsync(api.Direction,api.SymbolName,volumeInUnits,api.Label,stopLoss,takeProfit,api.Comment,api.HasTrailingStop,api.StopLossTriggerMethod,on_completed_action)else:result=api.ExecuteMarketOrder(api.Direction,api.SymbolName,volumeInUnits,api.Label,stopLoss,takeProfit,api.Comment,api.HasTrailingStop,api.StopLossTriggerMethod)ifapi.IsAsync==False:self.on_completed(result)defon_completed(self,result):ifresult.IsSuccessful==False:api.Print(f"Error: {result.Error}")api.Stop()
1 2 3 4 5 6 7 8 9101112131415161718192021
importclrclr.AddReference("cAlgo.API")# Import cAlgo API typesfromcAlgo.APIimport*# Import trading wrapper functionsfromrobot_wrapperimport*classPositionClosingSample():defon_start(self):positions=[]iflen(api.PositionComment)>0andlen(api.PositionLabel)>0:positions=[positionforpositioninapi.Positions.FindAll(api.PositionLabel)ifposition.Comment==api.PositionComment]eliflen(api.PositionComment)>0:positions=[positionforpositioninapi.Positionsifposition.Comment==api.PositionComment]eliflen(api.PositionLabel)>0:positions=[positionforpositioninapi.Positions.FindAll(api.PositionLabel)]iflen(positions)==0:api.Print("Couldn't find matching postions, please check the comment and label")else:forpositioninpositions:api.ClosePosition(position)api.Stop()
varresult=ExecuteMarketOrder(TradeType.Sell,Symbol.Name,10000,"myLabel");if(result.IsSuccessful)Print("Position {0} is open",result.Position.Label);
123
result=api.ExecuteMarketOrder(TradeType.Buy,api.Symbol.Name,10000,"myLabel",10,10)ifresult.IsSuccessful:print(f"Position {result.Position.Label} is open")
Comment
Summary
Comment can be used as a note for the order.
Signature
1
publicabstractstringComment{get;}
Return Value
string
Examples
123
ExecuteMarketOrder(TradeType.Buy,Symbol.Name,5000,"myLabel",10,10,2,"this is a comment");if(result.IsSuccessful)Print("Position is open: {0}",result.Position.Comment);
123
result=api.ExecuteMarketOrder(TradeType.Buy,api.Symbol.Name,10000,"myLabel",10,10)ifresult.IsSuccessful:print(f"Position {result.Position.Comment} is open")
Quantity
Summary
Quantity of lots traded by the position.
Signature
1
publicabstractdoubleQuantity{get;}
Return Value
double
HasTrailingStop
Summary
When HasTrailingStop set to true, the server updates the Stop Loss every time the position moves in your favor.
Signature
1
publicabstractboolHasTrailingStop{get;}
Return Value
bool
Examples
12
varresult=ExecuteMarketOrder(TradeType.Buy,Symbol.Name,10000,"myLabel",10,10,2,"comment",true);Print("Position was opened, has Trailing Stop = {0}",result.Position.HasTrailingStop);
12
result=api.ExecuteMarketOrder(TradeType.Buy,api.Symbol.Name,10000,"myLabel",10,10,2,"comment",True)print(f"Position was opened, has Trailing Stop = {result.Position.HasTrailingStop}")
Gets the position current market price.If Position's TradeType is Buy it returns Symbol current Bid price.If position's TradeType is Sell it returns Symbol current Ask price.
Signature
1
publicabstractdoubleCurrentPrice{get;}
Return Value
double
Symbol
Summary
Gets the position symbol.
Signature
1
publicabstractSymbolSymbol{get;}
Return Value
Symbol
Channel
Summary
Gets the channel via which the position was placed in a string format.
Signature
1
publicabstractstringChannel{get;}
Return Value
string
Examples
1
Print(position.Channel);
1
print(position.Channel)
LastUpdateTime
Summary
Gets the time when a position was last updated.The timezone used is set in the algorithm class attribute.