Price level trigger

Created at 21 Apr 2022, 18:50
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AN

AnthonyY

Joined 16.01.2022

Price level trigger
21 Apr 2022, 18:50


Hi!

Currently I am trying to make a small system where a while loop in OnStart() will keep going until becomes false by an event, like a simple change of a variable like a switch, in OnTick().

But when I place my conditions

OnTick()

while (Prc != Setup2Entry1 && test2 != 1)
            {

// Where Prc is made of by:
                AskPrc = Symbol.Ask;
                BidPrc = Symbol.Bid;
                Prc = (Symbol.Ask + Symbol.Bid) / 2;

test2 = 0;
                
            }

            if (test2 == 1)
            {
                test2 = 2;

// Check responce
                telegram.SendTelegram(ChatId, BotToken, StatusState + System.Environment.NewLine + System.Environment.NewLine + "Test22 " + Test2.ToString() + System.Environment.NewLine);

            }

Then if Prc updates right it should trigger OnStart()

while (test == 1)
            {
                test = 1;
                //telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + Prc.ToString() + System.Environment.NewLine);
            }

            telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + "test 2 =" + test.ToString() + System.Environment.NewLine);
            if (test == 2)
            {

                //telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + Prc.ToString() + System.Environment.NewLine);
                //////////////////////////////////////////////////////////////////////////////////////// Claim the levels by issuing limit orders
                // Sell Limit Order for Setup2Entry1
                PlaceLimitOrder(TradeType.Sell, Symbol.Name, RskB, Setup2Entry1, "Setup2Entry1", 0, Setup2TE1toTE2dist);

                PlaceLimitOrder(TradeType.Sell, Symbol.Name, RskB, Setup2Entry1, "Setup2Entry1", 0, Setup2TE1toTE2dist);
                // Buy Limit Order for Setup2Entry2
                PlaceLimitOrder(TradeType.Buy, Symbol.Name, RskB, Setup2Entry2, "Setup2Entry2", 0, Setup2TE2toTE3dist);

                // Sell Limit Order for Setup2Entry3
                PlaceLimitOrder(TradeType.Sell, Symbol.Name, RskB, Setup2Entry3, "Setup2Entry3", 0, Setup2CashOutdist);


                test = 3;
                test 2 = 1;
                //telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + "test =" + test.ToString() + System.Environment.NewLine);
            }

 


            //telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + Prc.ToString() + System.Environment.NewLine);


            telegram.SendTelegram(ChatId, BotToken, System.Environment.NewLine + System.Environment.NewLine + "test 3 =" + test.ToString() + System.Environment.NewLine);

            while (test == 3)
            {
                test = 3;
            }

            }

 

The goal is a way to have the while loop in OnStart break when its time to place the limitOrders based on the live price.

thank you in advance!


@AnthonyY
Replies

firemyst
25 Apr 2022, 09:15

I'm not sure your idea will work, as I believe the "OnTick" events only occur after OnStart has completed.

Obviously Spotware can confirm, but if that's the case, you'll have to redo your logic.


@firemyst

AnthonyY
25 Apr 2022, 09:23

RE:

firemyst said:

I'm not sure your idea will work, as I believe the "OnTick" events only occur after OnStart has completed.

Obviously Spotware can confirm, but if that's the case, you'll have to redo your logic.

Yeah, I was feeling that this would be the case. A confirmation is needed on this for sure.

Any idea about how this could be achieved? Or isn't possible at all to have a live price checking mechanism in OnTick() that would be combined with OnStart() to complete a continues checking cycle??

Thank you! 


@AnthonyY

firemyst
25 Apr 2022, 10:38

RE: RE:

instylereality2 said:

firemyst said:

I'm not sure your idea will work, as I believe the "OnTick" events only occur after OnStart has completed.

Obviously Spotware can confirm, but if that's the case, you'll have to redo your logic.

Yeah, I was feeling that this would be the case. A confirmation is needed on this for sure.

Any idea about how this could be achieved? Or isn't possible at all to have a live price checking mechanism in OnTick() that would be combined with OnStart() to complete a continues checking cycle??

Thank you! 

Why do you even need to wait in OnStart until something happens?

Just set a flag to false, and when it occurs in OnTick, set the flag to true.

Then check the flag in OnTick and when it's true, go through another branch of code.


@firemyst

AnthonyY
25 Apr 2022, 14:00

RE: RE: RE:

firemyst said:

instylereality2 said:

firemyst said:

I'm not sure your idea will work, as I believe the "OnTick" events only occur after OnStart has completed.

Obviously Spotware can confirm, but if that's the case, you'll have to redo your logic.

Yeah, I was feeling that this would be the case. A confirmation is needed on this for sure.

Any idea about how this could be achieved? Or isn't possible at all to have a live price checking mechanism in OnTick() that would be combined with OnStart() to complete a continues checking cycle??

Thank you! 

Why do you even need to wait in OnStart until something happens?

Just set a flag to false, and when it occurs in OnTick, set the flag to true.

Then check the flag in OnTick and when it's true, go through another branch of code.

Yeah that was kind of what I was trying to do there. Iam counting on OnStart because the OnTick will just produce infinite number of pending orders if that makes sense. Iam not a professional programmer, so I am still learning the tricks so to speak.

Thank you!


@AnthonyY

amusleh
26 Apr 2022, 09:11

Hi,

Your cBot code is executed by a single thread on an event loop, so while it's blocked inside OnStart method it can't execute OnTick or any other method/event.

If you want to keep checking for something then use the Timer, blocking the cBot thread will prevent execution of all other cBot methods, events, properties update, etc...

 


@amusleh

AnthonyY
26 Apr 2022, 10:03 ( Updated at: 26 Apr 2022, 10:07 )

RE:

amusleh said:

Hi,

Your cBot code is executed by a single thread on an event loop, so while it's blocked inside OnStart method it can't execute OnTick or any other method/event.

If you want to keep checking for something then use the Timer, blocking the cBot thread will prevent execution of all other cBot methods, events, properties update, etc...

 

Hey amusleh thank you for stepping in!

So to clarify the goal apart the code itself which was just an approach yeah, the idea is the single thread in OnStart() to handle all the limit orders, which are the only type I'll be using. So I came to the conclusion that OnTick() should be handling the price monitoring system and the two voids should be executing simultaneously somehow so the strategy could proceed to the next phases depending on how the market goes without taking certain amount of time because the bot would be on for full week for example. Now my question is,

Is that possible to be achieved with a single bot or should I just split it into different bots?If it can be in one it is a life saver if not I need to adapt everything else accordingly.

(edit: parallel invoke??) 

Thank you! 


@AnthonyY

amusleh
26 Apr 2022, 10:17

RE: RE:

instylereality2 said:

amusleh said:

Hi,

Your cBot code is executed by a single thread on an event loop, so while it's blocked inside OnStart method it can't execute OnTick or any other method/event.

If you want to keep checking for something then use the Timer, blocking the cBot thread will prevent execution of all other cBot methods, events, properties update, etc...

 

Hey amusleh thank you for stepping in!

So to clarify the goal apart the code itself which was just an approach yeah, the idea is the single thread in OnStart() to handle all the limit orders, which are the only type I'll be using. So I came to the conclusion that OnTick() should be handling the price monitoring system and the two voids should be executing simultaneously somehow so the strategy could proceed to the next phases depending on how the market goes without taking certain amount of time because the bot would be on for full week for example. Now my question is,

Is that possible to be achieved with a single bot or should I just split it into different bots?If it can be in one it is a life saver if not I need to adapt everything else accordingly.

(edit: parallel invoke??) 

Thank you! 

Hi,

I have no idea what you are trying to do, and why you need a loop inside OnStart method that will be stopped by OnTick event execution.

Use OnStart to populate or configure your bot and OnTick / OnBar / OnTimer / Events for trading.

Avoid blocking the cBot/Indicator thread for very long time as it will stop execution of all other methods and events.

What do you mean by: "the idea is the single thread in OnStart() to handle all the limit orders"

Your cBot thread is responsible for executing your cBot methods, event handlers, and updated it's properties.

 


@amusleh

AnthonyY
26 Apr 2022, 10:36

RE: RE: RE:

amusleh said:

instylereality2 said:

amusleh said:

Hi,

Your cBot code is executed by a single thread on an event loop, so while it's blocked inside OnStart method it can't execute OnTick or any other method/event.

If you want to keep checking for something then use the Timer, blocking the cBot thread will prevent execution of all other cBot methods, events, properties update, etc...

 

Hey amusleh thank you for stepping in!

So to clarify the goal apart the code itself which was just an approach yeah, the idea is the single thread in OnStart() to handle all the limit orders, which are the only type I'll be using. So I came to the conclusion that OnTick() should be handling the price monitoring system and the two voids should be executing simultaneously somehow so the strategy could proceed to the next phases depending on how the market goes without taking certain amount of time because the bot would be on for full week for example. Now my question is,

Is that possible to be achieved with a single bot or should I just split it into different bots?If it can be in one it is a life saver if not I need to adapt everything else accordingly.

(edit: parallel invoke??) 

Thank you! 

Hi,

I have no idea what you are trying to do, and why you need a loop inside OnStart method that will be stopped by OnTick event execution.

Use OnStart to populate or configure your bot and OnTick / OnBar / OnTimer / Events for trading.

Avoid blocking the cBot/Indicator thread for very long time as it will stop execution of all other methods and events.

What do you mean by: "the idea is the single thread in OnStart() to handle all the limit orders"

Your cBot thread is responsible for executing your cBot methods, event handlers, and updated it's properties.

 

Yeah I was meant to say that the main body of the code would be in OnStart. The loop was just an approach to as to how to stay in OnStart waiting for an event yes, but the event Iam looking for in order to proceed to the next trading phases placing the next set of limit orders is just the the update of Price level or Prc (live Price update). When the feedback of ask and bid / 2 matches my preset levels a certain pre calculated set of limits are placed, for that I was thinking to create a system in a system by using the OnTick () which in a sense is a loop itself, but if it's not even possible technically there's no point to bang my head over it I will just bypass it and figure something else out. The strategy is kind of big and has pretty much everything from account management based on equity to placing orders and monitoring their progress at the same time. But all depends on the main approach and how all these small systems will interrelate and function all based on preset parameters.

Thank you 


@AnthonyY