I'm in learning phase and putting down some code with a basic strategy. I want to learn how to individualy and programaticaly manage evey trade.
The folowing code just make a EnterLong order at every closed bar whene the closed bar close above the EMA(9).
After the order is filled also automaticali create a StopLoss order and a TakeProfit order.
Oll orders (EnterLong, SetStopLoss and SetProfitTarget) create during an OnBarUpdate cicle are "referenced" by the same "SignalName".
protected override void OnBarUpdate() { //Add your custom strategy logic here. if (CurrentBar < BarsRequiredToTrade) return; if (Close[0] > ema_009[0]){ orderSeq++; String entrySignal = "GB1_" + orderSeq; EnterLong(1000, entrySignal); SetStopLoss(entrySignal, CalculationMode.Price, ema_015[0], false); SetProfitTarget(entrySignal, CalculationMode.Pips, 10, true); } } protected override void OnOrderUpdate( Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) { if( order.OrderState == OrderState.Submitted || order.OrderState == OrderState.Working || order.OrderState == OrderState.Filled ) { Print("Orfer: " + order); } }
Since my code can produce many Long position before the oldest (fist oppened) are closed (see script output leter down), I have the need to manage/update a specific StopLoss or TakeProfit order.
Orfer: orderId='NT-00000-357' account='Backtest' name='GB1_1' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00001-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15065 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00001-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15065 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00002-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15229 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00002-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15229 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00000-357' account='Backtest' name='GB1_1' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15129 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00004-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15072 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00004-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15072 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00005-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15228 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00005-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15228 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15128 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00007-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15077 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00007-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15077 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15109 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00010-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15082 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00010-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15082 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15114 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00013-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15086 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00013-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15086 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15119 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=1000 averageFillPrice=1.15209 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=1000 averageFillPrice=1.15214 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=1000 averageFillPrice=1.15219 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
ATM Startegyes are not taking in consideration except in case it is possible to have full programmable control over it for each open position separately.
My questions are:
1. How I can programaticaly manage/update a specific StopLoss or TakeProfit order for each current open position?
2. How I can track / manage open positions?
Sorry for my spammi post caused by huge log export.
Thank you in advance for any advice / suggestions / code snipets.
Best regards,
Valter
Comment